|
| 1 | +import { authMockFns } from '@sim/testing' |
| 2 | +import { NextRequest } from 'next/server' |
| 3 | +import { beforeEach, describe, expect, it, vi } from 'vitest' |
| 4 | +import { DesktopDeviceUnrecognizedError } from '@/lib/desktop/executor/errors' |
| 5 | + |
| 6 | +const { admitted, stored, admitError } = vi.hoisted(() => ({ |
| 7 | + /** Entries the route admitted, in order. */ |
| 8 | + admitted: [] as Array<Record<string, unknown>>, |
| 9 | + /** Entries the route stored, with how many bytes each file carried. */ |
| 10 | + stored: [] as Array<{ executionToken: unknown; bytes: number | null }>, |
| 11 | + admitError: { next: null as Error | null }, |
| 12 | +})) |
| 13 | + |
| 14 | +vi.mock('@/lib/desktop/application/import', () => ({ |
| 15 | + admitDesktopImportEntry: async (_principal: unknown, input: Record<string, unknown>) => { |
| 16 | + const error = admitError.next |
| 17 | + admitError.next = null |
| 18 | + if (error) throw error |
| 19 | + admitted.push(input) |
| 20 | + }, |
| 21 | + importDesktopEntry: { |
| 22 | + execute: async ({ input }: { input: { executionToken: unknown; content?: Buffer } }) => { |
| 23 | + stored.push({ |
| 24 | + executionToken: input.executionToken, |
| 25 | + bytes: input.content ? input.content.length : null, |
| 26 | + }) |
| 27 | + return { id: 'file-1', name: 'notes.txt' } |
| 28 | + }, |
| 29 | + }, |
| 30 | +})) |
| 31 | + |
| 32 | +import { PUT } from '@/app/api/desktop/tool/import/route' |
| 33 | + |
| 34 | +const DEVICE = '00000000-0000-4000-8000-000000000001' |
| 35 | +const QUERY = `deviceId=${DEVICE}&toolCallId=call-1&kind=file&sourceName=Reports&relativePath=notes.txt` |
| 36 | + |
| 37 | +/** A request whose body counts how much of it the route read. */ |
| 38 | +function put(options: { |
| 39 | + body?: Uint8Array |
| 40 | + length?: string | null |
| 41 | + token?: string | null |
| 42 | + host?: string |
| 43 | + query?: string |
| 44 | +}) { |
| 45 | + const body = options.body ?? new TextEncoder().encode('hello') |
| 46 | + let pulled = 0 |
| 47 | + const stream = new ReadableStream<Uint8Array>( |
| 48 | + { |
| 49 | + pull(controller) { |
| 50 | + pulled += 1 |
| 51 | + controller.enqueue(body) |
| 52 | + controller.close() |
| 53 | + }, |
| 54 | + }, |
| 55 | + // Pulled only when the route reads it, never ahead of time. |
| 56 | + { highWaterMark: 0 } |
| 57 | + ) |
| 58 | + const headers: Record<string, string> = {} |
| 59 | + const length = options.length === undefined ? String(body.byteLength) : options.length |
| 60 | + if (length !== null) headers['content-length'] = length |
| 61 | + if (options.token !== null) headers['x-sim-execution-token'] = options.token ?? 'token-1' |
| 62 | + if (options.host) headers.host = options.host |
| 63 | + const request = new NextRequest( |
| 64 | + `http://${options.host ?? 'localhost'}/api/desktop/tool/import?${options.query ?? QUERY}`, |
| 65 | + { method: 'PUT', headers, body: stream, duplex: 'half' } |
| 66 | + ) |
| 67 | + return { request, pulled: () => pulled } |
| 68 | +} |
| 69 | + |
| 70 | +describe('PUT /api/desktop/tool/import', () => { |
| 71 | + beforeEach(() => { |
| 72 | + authMockFns.mockGetSession.mockResolvedValue({ |
| 73 | + user: { id: 'u1' }, |
| 74 | + session: { id: 's1' }, |
| 75 | + }) |
| 76 | + admitted.length = 0 |
| 77 | + stored.length = 0 |
| 78 | + admitError.next = null |
| 79 | + }) |
| 80 | + |
| 81 | + it('stores a whole file under the claim the token header names', async () => { |
| 82 | + const { request } = put({}) |
| 83 | + |
| 84 | + const response = await PUT(request, {}) |
| 85 | + |
| 86 | + expect(response.status).toBe(200) |
| 87 | + expect(stored).toEqual([{ executionToken: 'token-1', bytes: 5 }]) |
| 88 | + }) |
| 89 | + |
| 90 | + it('refuses a body that does not match its declared length, storing nothing', async () => { |
| 91 | + const { request } = put({ length: '10' }) |
| 92 | + |
| 93 | + const response = await PUT(request, {}) |
| 94 | + |
| 95 | + expect(response.status).toBe(400) |
| 96 | + expect(stored).toEqual([]) |
| 97 | + }) |
| 98 | + |
| 99 | + it('refuses a file that does not declare its length', async () => { |
| 100 | + const { request } = put({ length: null }) |
| 101 | + |
| 102 | + expect((await PUT(request, {})).status).toBe(411) |
| 103 | + expect(stored).toEqual([]) |
| 104 | + }) |
| 105 | + |
| 106 | + it('refuses a file over the import limit before admitting it', async () => { |
| 107 | + const { request, pulled } = put({ length: String(65 * 1024 * 1024) }) |
| 108 | + |
| 109 | + expect((await PUT(request, {})).status).toBe(413) |
| 110 | + expect(admitted).toEqual([]) |
| 111 | + expect(pulled()).toBe(0) |
| 112 | + }) |
| 113 | + |
| 114 | + it('reads no body for an entry it does not admit', async () => { |
| 115 | + admitError.next = new DesktopDeviceUnrecognizedError() |
| 116 | + const { request, pulled } = put({}) |
| 117 | + |
| 118 | + const response = await PUT(request, {}) |
| 119 | + |
| 120 | + expect(response.status).toBe(401) |
| 121 | + expect(pulled()).toBe(0) |
| 122 | + expect(stored).toEqual([]) |
| 123 | + }) |
| 124 | + |
| 125 | + it('answers an unauthenticated request with 401', async () => { |
| 126 | + authMockFns.mockGetSession.mockResolvedValueOnce(null) |
| 127 | + const { request } = put({}) |
| 128 | + |
| 129 | + expect((await PUT(request, {})).status).toBe(401) |
| 130 | + expect(admitted).toEqual([]) |
| 131 | + }) |
| 132 | + |
| 133 | + it('refuses a request without the execution token header', async () => { |
| 134 | + const { request } = put({ token: null }) |
| 135 | + |
| 136 | + expect((await PUT(request, {})).status).toBe(400) |
| 137 | + expect(admitted).toEqual([]) |
| 138 | + }) |
| 139 | + |
| 140 | + it('refuses a name a workspace cannot store before admitting it', async () => { |
| 141 | + const { request } = put({ |
| 142 | + query: `deviceId=${DEVICE}&toolCallId=call-1&kind=file&sourceName=Reports&relativePath=${encodeURIComponent('.. /notes.txt')}`, |
| 143 | + }) |
| 144 | + |
| 145 | + expect((await PUT(request, {})).status).toBe(400) |
| 146 | + expect(admitted).toEqual([]) |
| 147 | + }) |
| 148 | +}) |
0 commit comments