From b88e4e182bb2de7ba8fbdd8115ae127cc156bc3f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Pierzcha=C5=82a?= Date: Tue, 17 Nov 2026 12:19:03 +0100 Subject: [PATCH] test(proxy): isolate transfer module loading mocks --- .../src/daemon-proxy-transfer-loading.test.ts | 55 +++++++++++++++++++ .../proxy/src/daemon-proxy-transfer.test.ts | 39 ------------- 2 files changed, 55 insertions(+), 39 deletions(-) create mode 100644 packages/proxy/src/daemon-proxy-transfer-loading.test.ts diff --git a/packages/proxy/src/daemon-proxy-transfer-loading.test.ts b/packages/proxy/src/daemon-proxy-transfer-loading.test.ts new file mode 100644 index 0000000000..d2df471d0f --- /dev/null +++ b/packages/proxy/src/daemon-proxy-transfer-loading.test.ts @@ -0,0 +1,55 @@ +import { expect, onTestFinished, test, vi } from 'vitest'; +import { AppError } from '@agent-device/kernel/errors'; + +const options = { + upstreamBaseUrl: 'http://daemon.internal:4310', + upstreamToken: 'daemon-secret', + clientToken: 'client-secret', +}; + +const rpcBody = JSON.stringify({ + jsonrpc: '2.0', + id: 1, + method: 'agent-device.command', + params: { token: 'client-secret', command: 'version' }, +}); + +// Keep module-registry mocks in this isolated file: existing HTTP listeners retain their module context. +test.each([false, true])( + 'non-transfer routes never load transfer mechanics (failure=%s)', + async (failure) => { + vi.resetModules(); + vi.doMock('./transfer-transport.ts', () => { + throw new Error('transfer mechanics loaded'); + }); + onTestFinished(() => { + vi.doUnmock('./transfer-transport.ts'); + vi.resetModules(); + }); + const { createDaemonProxy: create } = await import('./daemon-proxy.ts'); + const proxy = create({ + ...options, + upstreamFetch: async () => { + if (failure) throw new AppError('COMMAND_FAILED', 'original non-transfer failure'); + return Response.json({ jsonrpc: '2.0', id: 1, result: { ok: true } }); + }, + }); + const transfer = new Request('http://gateway.test/artifacts/x'); + transfer.headers.set('authorization', 'Bearer client-secret'); + await expect(proxy.handle(transfer)).rejects.toThrow(); + for (const path of ['/rpc', '/health', '/sessions/s/requests/r/diagnostics']) { + const response = await proxy.handle( + new Request(`http://gateway.test${path}`, { + method: path === '/rpc' ? 'POST' : 'GET', + headers: { authorization: 'Bearer client-secret' }, + ...(path === '/rpc' ? { body: rpcBody } : {}), + }), + ); + expect(response.status, path).toBe(failure ? 500 : 200); + const body = await response.json(); + if (failure) expect(JSON.stringify(body)).toContain('original non-transfer failure'); + expect(body).not.toHaveProperty('transferPhase'); + expect(body).not.toHaveProperty('timeoutMs'); + } + }, +); diff --git a/packages/proxy/src/daemon-proxy-transfer.test.ts b/packages/proxy/src/daemon-proxy-transfer.test.ts index 54ef6aa5ac..b6546cbb52 100644 --- a/packages/proxy/src/daemon-proxy-transfer.test.ts +++ b/packages/proxy/src/daemon-proxy-transfer.test.ts @@ -346,45 +346,6 @@ test('admitted transfers use Node while RPC and diagnostics use Fetch', async () } }); -test.each([false, true])( - 'non-transfer routes never load transfer mechanics (failure=%s)', - async (failure) => { - vi.resetModules(); - vi.doMock('./transfer-transport.ts', () => { - throw new Error('transfer mechanics loaded'); - }); - onTestFinished(() => { - vi.doUnmock('./transfer-transport.ts'); - vi.resetModules(); - }); - const { createDaemonProxy: create } = await import('./daemon-proxy.ts'); - const proxy = create({ - ...options, - upstreamFetch: async () => { - if (failure) throw new AppError('COMMAND_FAILED', 'original non-transfer failure'); - return Response.json({ jsonrpc: '2.0', id: 1, result: { ok: true } }); - }, - }); - const transfer = new Request('http://gateway.test/artifacts/x'); - transfer.headers.set('authorization', 'Bearer client-secret'); - await expect(proxy.handle(transfer)).rejects.toThrow(); - for (const path of ['/rpc', '/health', '/sessions/s/requests/r/diagnostics']) { - const response = await proxy.handle( - new Request(`http://gateway.test${path}`, { - method: path === '/rpc' ? 'POST' : 'GET', - headers: { authorization: 'Bearer client-secret' }, - ...(path === '/rpc' ? { body: rpcBody } : {}), - }), - ); - expect(response.status, path).toBe(failure ? 500 : 200); - const body = await response.json(); - if (failure) expect(JSON.stringify(body)).toContain('original non-transfer failure'); - expect(body).not.toHaveProperty('transferPhase'); - expect(body).not.toHaveProperty('timeoutMs'); - } - }, -); - test('an explicit operator cap settles an ignored custom request and reaches its bounded signal', async () => { let bounded!: Request; const origin = await listen(