|
| 1 | +import nock from 'nock'; |
| 2 | +import 'should'; |
| 3 | +import { OsoBridgeClient } from '../../../masterBitgoExpress/clients/bridgeClient'; |
| 4 | +import { BridgeJobResponse } from '../../../masterBitgoExpress/clients/bridgeClient.types'; |
| 5 | +import { KeySource } from '../../../shared/types'; |
| 6 | + |
| 7 | +const BASE_URL = 'http://bridge.invalid'; |
| 8 | +const TIMEOUT = 60000; |
| 9 | + |
| 10 | +const mockJob: BridgeJobResponse = { |
| 11 | + jobId: 'job-123', |
| 12 | + status: 'awaiting_bitgo', |
| 13 | + version: 1, |
| 14 | + coin: 'tbtc', |
| 15 | + operationType: 'multisig_sign', |
| 16 | + createdAt: '2026-06-09T00:00:00.000Z', |
| 17 | + updatedAt: '2026-06-09T00:00:00.000Z', |
| 18 | +}; |
| 19 | + |
| 20 | +describe('OsoBridgeClient', () => { |
| 21 | + let client: OsoBridgeClient; |
| 22 | + |
| 23 | + before(() => { |
| 24 | + nock.disableNetConnect(); |
| 25 | + client = new OsoBridgeClient(BASE_URL, TIMEOUT); |
| 26 | + }); |
| 27 | + |
| 28 | + afterEach(() => nock.cleanAll()); |
| 29 | + |
| 30 | + describe('constructor', () => { |
| 31 | + it('throws when url is empty', () => { |
| 32 | + (() => new OsoBridgeClient('', TIMEOUT)).should.throw( |
| 33 | + 'OsoBridgeClient: awmAsyncUrl is required', |
| 34 | + ); |
| 35 | + }); |
| 36 | + |
| 37 | + it('strips trailing slash from url', () => { |
| 38 | + const c = new OsoBridgeClient(`${BASE_URL}/`, TIMEOUT); |
| 39 | + nock(BASE_URL).get('/health').reply(200, { status: 'ok' }); |
| 40 | + return c.health().should.be.fulfilled(); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + describe('submit()', () => { |
| 45 | + it('sets X-OSO-Source for a single source', async () => { |
| 46 | + const n = nock(BASE_URL) |
| 47 | + .post('/api/tbtc/multisig/sign') |
| 48 | + .matchHeader('x-oso-source', 'user') |
| 49 | + .reply(202, { jobId: 'job-123' }); |
| 50 | + |
| 51 | + await client.submit({ |
| 52 | + path: '/api/tbtc/multisig/sign', |
| 53 | + body: { foo: 'bar' }, |
| 54 | + sources: [KeySource.USER], |
| 55 | + operationType: 'multisig_sign', |
| 56 | + }); |
| 57 | + |
| 58 | + n.done(); |
| 59 | + }); |
| 60 | + |
| 61 | + it('sets X-OSO-Source as comma-joined for multiple sources', async () => { |
| 62 | + const n = nock(BASE_URL) |
| 63 | + .post('/api/tbtc/key/independent') |
| 64 | + .matchHeader('x-oso-source', 'user,backup') |
| 65 | + .reply(202, { jobId: 'job-456' }); |
| 66 | + |
| 67 | + await client.submit({ |
| 68 | + path: '/api/tbtc/key/independent', |
| 69 | + body: { foo: 'bar' }, |
| 70 | + sources: [KeySource.USER, KeySource.BACKUP], |
| 71 | + operationType: 'multisig_keygen', |
| 72 | + }); |
| 73 | + |
| 74 | + n.done(); |
| 75 | + }); |
| 76 | + |
| 77 | + it('sets X-OSO-Operation header', async () => { |
| 78 | + const n = nock(BASE_URL) |
| 79 | + .post('/api/tbtc/multisig/sign') |
| 80 | + .matchHeader('x-oso-operation', 'multisig_sign') |
| 81 | + .reply(202, { jobId: 'job-123' }); |
| 82 | + |
| 83 | + await client.submit({ |
| 84 | + path: '/api/tbtc/multisig/sign', |
| 85 | + body: { foo: 'bar' }, |
| 86 | + sources: [KeySource.USER], |
| 87 | + operationType: 'multisig_sign', |
| 88 | + }); |
| 89 | + |
| 90 | + n.done(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('sets X-Idempotency-Key when provided', async () => { |
| 94 | + const n = nock(BASE_URL) |
| 95 | + .post('/api/tbtc/multisig/sign') |
| 96 | + .matchHeader('x-idempotency-key', 'idem-key-abc') |
| 97 | + .reply(202, { jobId: 'job-123' }); |
| 98 | + |
| 99 | + await client.submit({ |
| 100 | + path: '/api/tbtc/multisig/sign', |
| 101 | + body: { foo: 'bar' }, |
| 102 | + sources: [KeySource.USER], |
| 103 | + operationType: 'multisig_sign', |
| 104 | + idempotencyKey: 'idem-key-abc', |
| 105 | + }); |
| 106 | + |
| 107 | + n.done(); |
| 108 | + }); |
| 109 | + |
| 110 | + it('omits X-Idempotency-Key when not provided', async () => { |
| 111 | + const n = nock(BASE_URL) |
| 112 | + .post('/api/tbtc/multisig/sign') |
| 113 | + .matchHeader('x-idempotency-key', (val) => val === undefined) |
| 114 | + .reply(202, { jobId: 'job-123' }); |
| 115 | + |
| 116 | + await client.submit({ |
| 117 | + path: '/api/tbtc/multisig/sign', |
| 118 | + body: { foo: 'bar' }, |
| 119 | + sources: [KeySource.USER], |
| 120 | + operationType: 'multisig_sign', |
| 121 | + }); |
| 122 | + |
| 123 | + n.done(); |
| 124 | + }); |
| 125 | + |
| 126 | + it('forwards body as-is', async () => { |
| 127 | + const body = { signablePayload: 'deadbeef', walletId: 'w-123' }; |
| 128 | + const n = nock(BASE_URL) |
| 129 | + .post('/api/tbtc/multisig/sign', body) |
| 130 | + .reply(202, { jobId: 'job-123' }); |
| 131 | + |
| 132 | + await client.submit({ |
| 133 | + path: '/api/tbtc/multisig/sign', |
| 134 | + body, |
| 135 | + sources: [KeySource.USER], |
| 136 | + operationType: 'multisig_sign', |
| 137 | + }); |
| 138 | + |
| 139 | + n.done(); |
| 140 | + }); |
| 141 | + |
| 142 | + it('returns jobId from response', async () => { |
| 143 | + nock(BASE_URL).post('/api/tbtc/multisig/sign').reply(202, { jobId: 'job-789' }); |
| 144 | + |
| 145 | + const result = await client.submit({ |
| 146 | + path: '/api/tbtc/multisig/sign', |
| 147 | + body: {}, |
| 148 | + sources: [KeySource.USER], |
| 149 | + operationType: 'multisig_sign', |
| 150 | + }); |
| 151 | + |
| 152 | + result.should.have.property('jobId', 'job-789'); |
| 153 | + }); |
| 154 | + |
| 155 | + it('normalizes path without leading slash', async () => { |
| 156 | + const n = nock(BASE_URL).post('/api/tbtc/multisig/sign').reply(202, { jobId: 'job-123' }); |
| 157 | + |
| 158 | + await client.submit({ |
| 159 | + path: 'api/tbtc/multisig/sign', |
| 160 | + body: {}, |
| 161 | + sources: [KeySource.USER], |
| 162 | + operationType: 'multisig_sign', |
| 163 | + }); |
| 164 | + |
| 165 | + n.done(); |
| 166 | + }); |
| 167 | + |
| 168 | + it('throws on invalid response shape', async () => { |
| 169 | + nock(BASE_URL).post('/api/tbtc/multisig/sign').reply(202, { notAJobId: true }); |
| 170 | + |
| 171 | + await client |
| 172 | + .submit({ |
| 173 | + path: '/api/tbtc/multisig/sign', |
| 174 | + body: {}, |
| 175 | + sources: [KeySource.USER], |
| 176 | + operationType: 'multisig_sign', |
| 177 | + }) |
| 178 | + .should.be.rejectedWith(/bridge returned unexpected response for submit/); |
| 179 | + }); |
| 180 | + |
| 181 | + it('throws on 400', async () => { |
| 182 | + nock(BASE_URL).post('/api/tbtc/multisig/sign').reply(400, { message: 'bad input' }); |
| 183 | + |
| 184 | + await client |
| 185 | + .submit({ |
| 186 | + path: '/api/tbtc/multisig/sign', |
| 187 | + body: {}, |
| 188 | + sources: [KeySource.USER], |
| 189 | + operationType: 'multisig_sign', |
| 190 | + }) |
| 191 | + .should.be.rejectedWith('bad input'); |
| 192 | + }); |
| 193 | + }); |
| 194 | + |
| 195 | + describe('getJob()', () => { |
| 196 | + it('GETs /job/:jobId and returns BridgeJobResponse', async () => { |
| 197 | + const n = nock(BASE_URL).get('/job/job-123').reply(200, mockJob); |
| 198 | + |
| 199 | + const result = await client.getJob('job-123'); |
| 200 | + |
| 201 | + result.should.have.property('jobId', 'job-123'); |
| 202 | + result.should.have.property('status', 'awaiting_bitgo'); |
| 203 | + n.done(); |
| 204 | + }); |
| 205 | + |
| 206 | + it('throws on 404', async () => { |
| 207 | + nock(BASE_URL).get('/job/missing').reply(404, { message: 'job not found' }); |
| 208 | + |
| 209 | + await client.getJob('missing').should.be.rejectedWith('job not found'); |
| 210 | + }); |
| 211 | + |
| 212 | + it('throws on invalid response shape', async () => { |
| 213 | + nock(BASE_URL).get('/job/job-123').reply(200, { bad: 'shape' }); |
| 214 | + |
| 215 | + await client |
| 216 | + .getJob('job-123') |
| 217 | + .should.be.rejectedWith(/bridge returned unexpected response for getJob/); |
| 218 | + }); |
| 219 | + }); |
| 220 | + |
| 221 | + describe('updateJob()', () => { |
| 222 | + it('PATCHes /job/:jobId with correct body', async () => { |
| 223 | + const n = nock(BASE_URL) |
| 224 | + .patch('/job/job-123', { version: 1, status: 'complete', result: { txid: 'abc' } }) |
| 225 | + .reply(204); |
| 226 | + |
| 227 | + await client.updateJob({ |
| 228 | + jobId: 'job-123', |
| 229 | + version: 1, |
| 230 | + status: 'complete', |
| 231 | + result: { txid: 'abc' }, |
| 232 | + }); |
| 233 | + |
| 234 | + n.done(); |
| 235 | + }); |
| 236 | + |
| 237 | + it('returns void on 204', async () => { |
| 238 | + nock(BASE_URL).patch('/job/job-123').reply(204); |
| 239 | + |
| 240 | + const result = await client.updateJob({ |
| 241 | + jobId: 'job-123', |
| 242 | + version: 1, |
| 243 | + status: 'failed', |
| 244 | + error: 'timeout', |
| 245 | + }); |
| 246 | + |
| 247 | + (result === undefined).should.be.true(); |
| 248 | + }); |
| 249 | + |
| 250 | + it('throws on 409', async () => { |
| 251 | + nock(BASE_URL).patch('/job/job-123').reply(409, { message: 'version conflict' }); |
| 252 | + |
| 253 | + await client |
| 254 | + .updateJob({ jobId: 'job-123', version: 1, status: 'complete' }) |
| 255 | + .should.be.rejectedWith('version conflict'); |
| 256 | + }); |
| 257 | + }); |
| 258 | + |
| 259 | + describe('listJobs()', () => { |
| 260 | + it('GETs /jobs without query when status omitted', async () => { |
| 261 | + const n = nock(BASE_URL) |
| 262 | + .get('/jobs') |
| 263 | + .reply(200, { jobs: [mockJob] }); |
| 264 | + |
| 265 | + const result = await client.listJobs({}); |
| 266 | + |
| 267 | + result.jobs.should.have.length(1); |
| 268 | + result.jobs[0].should.have.property('jobId', 'job-123'); |
| 269 | + n.done(); |
| 270 | + }); |
| 271 | + |
| 272 | + it('GETs /jobs?status=awaiting_bitgo when status provided', async () => { |
| 273 | + const n = nock(BASE_URL) |
| 274 | + .get('/jobs') |
| 275 | + .query({ status: 'awaiting_bitgo' }) |
| 276 | + .reply(200, { jobs: [mockJob] }); |
| 277 | + |
| 278 | + const result = await client.listJobs({ status: 'awaiting_bitgo' }); |
| 279 | + |
| 280 | + result.jobs.should.have.length(1); |
| 281 | + n.done(); |
| 282 | + }); |
| 283 | + |
| 284 | + it('returns empty array when jobs list is empty', async () => { |
| 285 | + nock(BASE_URL).get('/jobs').reply(200, { jobs: [] }); |
| 286 | + |
| 287 | + const result = await client.listJobs({}); |
| 288 | + |
| 289 | + result.jobs.should.have.length(0); |
| 290 | + }); |
| 291 | + }); |
| 292 | + |
| 293 | + describe('health()', () => { |
| 294 | + it('GETs /health and returns body', async () => { |
| 295 | + const n = nock(BASE_URL).get('/health').reply(200, { status: 'ok' }); |
| 296 | + |
| 297 | + const result = await client.health(); |
| 298 | + |
| 299 | + result.should.have.property('status', 'ok'); |
| 300 | + n.done(); |
| 301 | + }); |
| 302 | + }); |
| 303 | + |
| 304 | + describe('error handling', () => { |
| 305 | + it('throws on 401', async () => { |
| 306 | + nock(BASE_URL).get('/job/job-123').reply(401, { message: 'unauthorized' }); |
| 307 | + |
| 308 | + await client.getJob('job-123').should.be.rejectedWith('unauthorized'); |
| 309 | + }); |
| 310 | + |
| 311 | + it('throws with status code on unexpected status', async () => { |
| 312 | + nock(BASE_URL).get('/job/job-123').reply(503, { message: 'service unavailable' }); |
| 313 | + |
| 314 | + await client.getJob('job-123').should.be.rejectedWith(/503/); |
| 315 | + }); |
| 316 | + }); |
| 317 | +}); |
0 commit comments