diff --git a/packages/plugin/tests/workspace-upload.test.ts b/packages/plugin/tests/workspace-upload.test.ts index f1c801d..4034653 100644 --- a/packages/plugin/tests/workspace-upload.test.ts +++ b/packages/plugin/tests/workspace-upload.test.ts @@ -253,3 +253,77 @@ describe('workspace-root upload authority and lifecycle', () => { expect(await router.handle(createRpcRequest('workspace.upload.open', request()))).toMatchObject({ type: 'rpc.error', payload: { code: 'UPLOAD_CLOSED' } }) }) }) + +it('negotiates binary chunks explicitly and preserves legacy open responses', async () => { + const { host, root } = await setup() + const legacy = request('legacy.bin', 3) + expect(await host.call('workspace.upload.open', legacy)).toEqual({ uploadId: legacy.uploadId, chunkBytes: WORKSPACE_UPLOAD_CHUNK }) + await host.call('workspace.upload.chunk', { ...scope(legacy), offset: 0, data: 'AP8M' }) + await host.call('workspace.upload.commit', scope(legacy)) + const bytes = new Uint8Array(256 * 1024).map((_, i) => i % 251) + const binary = { ...request('binary.bin', bytes.length), encoding: 'binary-v1' } + expect(await host.call('workspace.upload.open', binary)).toEqual({ uploadId: binary.uploadId, chunkBytes: bytes.length, encoding: 'binary-v1' }) + await host.call('workspace.upload.chunk.binary', { ...scope(binary), offset: 0, data: bytes }) + await host.call('workspace.upload.commit', scope(binary)) + expect(await readFile(join(root, 'binary.bin'))).toEqual(Buffer.from(bytes)) + expect(await readFile(join(root, 'legacy.bin'))).toEqual(Buffer.from([0, 255, 12])) +}) + +it('rejects mixed encodings, wrong offsets and oversized binary chunks and discards partial data', async () => { + const { host } = await setup() + for (const [encoding, method, offset, data] of [ + [undefined, 'workspace.upload.chunk.binary', 0, new Uint8Array([1])], + ['binary-v1', 'workspace.upload.chunk', 0, 'AQ=='], + ['binary-v1', 'workspace.upload.chunk.binary', 1, new Uint8Array([1])], + ['binary-v1', 'workspace.upload.chunk.binary', 0, new Uint8Array(256 * 1024 + 1)], + ] as const) { + const p = { ...request('bad.bin', 300000), ...(encoding ? { encoding } : {}) } + await host.call('workspace.upload.open', p) + await expect(host.call(method, { ...scope(p), offset, data })).rejects.toThrow() + await expect(host.call('workspace.upload.commit', scope(p))).rejects.toThrow() + } +}) + +it('advertises binary upload only with the base capability and a working upload service', async () => { + const { host } = await setup() + const describe = createRpcRequest('harness.transport.describe', {}) + for (const [base, service, expected] of [[true, true, true], [true, false, false], [false, true, false]] as const) { + const router = new RpcRouter(128, undefined, undefined, undefined, () => [...(base ? ['workspace.upload.v1'] : []), 'workspace.upload.binary.v1'], undefined, undefined, undefined, undefined, undefined, service ? host : undefined) + const response = await router.handle(describe) + expect((response.payload as { result: { capabilities: string[] } }).result.capabilities.includes('workspace.upload.binary.v1')).toBe(expected) + } +}) +it('keeps binary uploads connection/session scoped after a foreign session attempts a chunk', async () => { + const { host, root } = await setup() + const p = { ...request('scoped.bin', 1), encoding: 'binary-v1' } + await host.call('workspace.upload.open', p) + await expect(host.call('workspace.upload.chunk.binary', { ...scope(p), sessionId: 'foreign', offset: 0, data: new Uint8Array([42]) })).rejects.toMatchObject({ code: 'UPLOAD_NOT_FOUND' }) + await host.call('workspace.upload.chunk.binary', { ...scope(p), offset: 0, data: new Uint8Array([42]) }) + await host.call('workspace.upload.commit', scope(p)) + expect(await readFile(join(root, 'scoped.bin'))).toEqual(Buffer.from([42])) +}) +it('carries binary chunks through the real client codec, RPC router and ordinary JSON responses', async () => { + const { RemoteClientCore } = await import('@dsh-remote/client-core') + const { encodeMessage, decodeMessage } = await import('@dsh-remote/protocol') + const { host, root } = await setup() + const router = new RpcRouter(128, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, host) + let receive: ((data: Uint8Array) => void) | undefined + const frames: Uint8Array[] = [] + const client = new RemoteClientCore({ + async connect() {}, + async send(data) { frames.push(data); receive?.(encodeMessage(await router.handle(decodeMessage(data)))) }, + onMessage(handler) { receive = handler; return () => { receive = undefined } }, + async close() {}, + getStats: () => ({ mode: 'LAN', connected: true }), + }) + try { + await client.connect() + const data = new Uint8Array(256 * 1024).map((_, i) => i % 251) + const p = { ...request('wire.bin', data.length), encoding: 'binary-v1' } + await client.rpc('workspace.upload.open', p) + await expect(client.rpc('workspace.upload.chunk.binary', { ...scope(p), offset: 0, data })).resolves.toEqual({ uploadId: p.uploadId, receivedBytes: data.length }) + await expect(client.rpc('workspace.upload.commit', scope(p))).resolves.toEqual({ name: 'wire.bin', path: './wire.bin', bytes: data.length }) + expect(frames[1]!.byteLength).toBeLessThan(data.length + 4096) + expect(await readFile(join(root, 'wire.bin'))).toEqual(Buffer.from(data)) + } finally { await client.close() } +})