|
| 1 | +import { describe, expect, it } from "vitest"; |
| 2 | +import { CoderChatClient } from "../../src/coder/client.js"; |
| 3 | +import { CoderAgentError, CoderApiError } from "../../src/errors.js"; |
| 4 | + |
| 5 | +type Init = RequestInit & { headers: Record<string, string> }; |
| 6 | + |
| 7 | +/** A fake `fetch` that records calls and returns a scripted `Response`. */ |
| 8 | +function fakeFetch(handler: () => Response) { |
| 9 | + const calls: { url: string; init: Init }[] = []; |
| 10 | + const fn = ((url: string, init: Init) => { |
| 11 | + calls.push({ url, init }); |
| 12 | + return Promise.resolve(handler()); |
| 13 | + }) as unknown as typeof globalThis.fetch; |
| 14 | + return { fn, calls }; |
| 15 | +} |
| 16 | + |
| 17 | +function client(fetchFn: typeof globalThis.fetch) { |
| 18 | + return new CoderChatClient({ baseUrl: "https://x", token: "t", fetch: fetchFn }); |
| 19 | +} |
| 20 | + |
| 21 | +describe("CoderChatClient.uploadChatFile", () => { |
| 22 | + it("uploads bytes to the org-scoped endpoint and returns the id", async () => { |
| 23 | + const { fn, calls } = fakeFetch( |
| 24 | + () => new Response(JSON.stringify({ id: "file-1" }), { status: 201 }), |
| 25 | + ); |
| 26 | + const r = await client(fn).uploadChatFile("org-1", { |
| 27 | + content: new Uint8Array([1, 2, 3]), |
| 28 | + mediaType: "application/pdf", |
| 29 | + name: "report.pdf", |
| 30 | + }); |
| 31 | + |
| 32 | + expect(r).toEqual({ id: "file-1", mediaType: "application/pdf", name: "report.pdf" }); |
| 33 | + expect(calls[0]?.url).toBe("https://x/api/experimental/chats/files?organization=org-1"); |
| 34 | + expect(calls[0]?.init.headers["Content-Type"]).toBe("application/pdf"); |
| 35 | + expect(calls[0]?.init.headers["Content-Disposition"]).toBe( |
| 36 | + "attachment; filename=\"report.pdf\"; filename*=UTF-8''report.pdf", |
| 37 | + ); |
| 38 | + }); |
| 39 | + |
| 40 | + it("normalizes a parameterized media type for the allowlist check and the header", async () => { |
| 41 | + const { fn, calls } = fakeFetch( |
| 42 | + () => new Response(JSON.stringify({ id: "f" }), { status: 201 }), |
| 43 | + ); |
| 44 | + const r = await client(fn).uploadChatFile("o", { |
| 45 | + content: new Uint8Array([1]), |
| 46 | + mediaType: "text/plain; charset=utf-8", |
| 47 | + }); |
| 48 | + |
| 49 | + expect(r.mediaType).toBe("text/plain"); |
| 50 | + expect(calls[0]?.init.headers["Content-Type"]).toBe("text/plain"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("sanitizes the ASCII filename (escapes quotes, drops CR/LF) and adds filename*", async () => { |
| 54 | + const { fn, calls } = fakeFetch( |
| 55 | + () => new Response(JSON.stringify({ id: "f" }), { status: 201 }), |
| 56 | + ); |
| 57 | + await client(fn).uploadChatFile("o", { |
| 58 | + content: new Uint8Array([1]), |
| 59 | + mediaType: "text/plain", |
| 60 | + name: 'he"llo\r\nworld', |
| 61 | + }); |
| 62 | + |
| 63 | + const cd = calls[0]?.init.headers["Content-Disposition"] ?? ""; |
| 64 | + // CR/LF → "_" in the ASCII fallback, quote escaped; exact name preserved in filename*. |
| 65 | + expect(cd).toBe( |
| 66 | + 'attachment; filename="he\\"llo__world"; filename*=UTF-8\'\'he%22llo%0D%0Aworld', |
| 67 | + ); |
| 68 | + expect(cd).not.toMatch(/[\r\n]/); |
| 69 | + }); |
| 70 | + |
| 71 | + it("encodes a non-ASCII filename instead of throwing a ByteString error", async () => { |
| 72 | + const { fn, calls } = fakeFetch( |
| 73 | + () => new Response(JSON.stringify({ id: "f" }), { status: 201 }), |
| 74 | + ); |
| 75 | + await client(fn).uploadChatFile("o", { |
| 76 | + content: new Uint8Array([1]), |
| 77 | + mediaType: "application/pdf", |
| 78 | + name: "報告書.pdf", |
| 79 | + }); |
| 80 | + |
| 81 | + const cd = calls[0]?.init.headers["Content-Disposition"] ?? ""; |
| 82 | + // Pure ASCII header value (no code point > 0x7f) so fetch can't reject it. |
| 83 | + expect([...cd].every((ch) => ch.charCodeAt(0) <= 0x7f)).toBe(true); |
| 84 | + expect(cd).toContain("filename*=UTF-8''%E5%A0%B1%E5%91%8A%E6%9B%B8.pdf"); |
| 85 | + }); |
| 86 | + |
| 87 | + it("throws when a 2xx response carries no file id (instead of returning an empty id)", async () => { |
| 88 | + const { fn } = fakeFetch(() => new Response("", { status: 201 })); |
| 89 | + await expect( |
| 90 | + client(fn).uploadChatFile("o", { content: new Uint8Array([1]), mediaType: "text/plain" }), |
| 91 | + ).rejects.toThrow(/no file id/); |
| 92 | + }); |
| 93 | + |
| 94 | + it("rejects a non-allowlisted media type before issuing any request", async () => { |
| 95 | + let called = false; |
| 96 | + const { fn } = fakeFetch(() => { |
| 97 | + called = true; |
| 98 | + return new Response("{}", { status: 201 }); |
| 99 | + }); |
| 100 | + await expect( |
| 101 | + client(fn).uploadChatFile("o", { |
| 102 | + content: new Uint8Array([1]), |
| 103 | + mediaType: "application/zip", |
| 104 | + }), |
| 105 | + ).rejects.toThrow(CoderAgentError); |
| 106 | + expect(called).toBe(false); |
| 107 | + }); |
| 108 | + |
| 109 | + it("surfaces a non-2xx upload as a CoderApiError", async () => { |
| 110 | + const { fn } = fakeFetch( |
| 111 | + () => new Response(JSON.stringify({ message: "too big" }), { status: 413 }), |
| 112 | + ); |
| 113 | + await expect( |
| 114 | + client(fn).uploadChatFile("o", { content: new Uint8Array([1]), mediaType: "text/plain" }), |
| 115 | + ).rejects.toThrow(CoderApiError); |
| 116 | + }); |
| 117 | +}); |
0 commit comments