|
| 1 | +import { expect, test } from "bun:test"; |
| 2 | +import type { ToolCall } from "@intx/types/runtime"; |
| 3 | +import type { CredentialCapability, MediatedCredential } from "@intx/types"; |
| 4 | + |
| 5 | +import { GOTENBERG_RENDER_PDF_TOOL, gotenbergTools } from "./tool"; |
| 6 | +import type { GotenbergEnv } from "./tool"; |
| 7 | + |
| 8 | +const CALL: ToolCall = { |
| 9 | + id: "call_1", |
| 10 | + name: GOTENBERG_RENDER_PDF_TOOL, |
| 11 | + arguments: { title: "Acme Brief", markdown: "# Hello" }, |
| 12 | +}; |
| 13 | + |
| 14 | +/** |
| 15 | + * A fake `credentials` capability mirroring the platform's own |
| 16 | + * `createCredentialCapability`/`createHttpCredentialProvider` shape: a |
| 17 | + * bound handle resolves to a mediated `fetch` pinned to the operator's |
| 18 | + * Gotenberg origin; an unbound handle throws. |
| 19 | + */ |
| 20 | +type FetchLike = (input: string | URL | Request, init?: RequestInit) => Promise<Response>; |
| 21 | + |
| 22 | +function fakeCredentials(fetchImpl: FetchLike | undefined): CredentialCapability { |
| 23 | + return { |
| 24 | + resolve(handle: string): Promise<MediatedCredential> { |
| 25 | + if (fetchImpl === undefined) { |
| 26 | + return Promise.reject(new Error(`no credential is bound to handle "${handle}"`)); |
| 27 | + } |
| 28 | + return Promise.resolve({ |
| 29 | + kind: "http", |
| 30 | + fetch: fetchImpl, |
| 31 | + dispose: () => {}, |
| 32 | + }); |
| 33 | + }, |
| 34 | + }; |
| 35 | +} |
| 36 | + |
| 37 | +function fakeEnv(credentials: CredentialCapability | undefined): GotenbergEnv { |
| 38 | + return { credentials } as unknown as GotenbergEnv; |
| 39 | +} |
| 40 | + |
| 41 | +test("declares the gotenberg_render_pdf tool", () => { |
| 42 | + const bundle = gotenbergTools(fakeEnv(fakeCredentials(async () => new Response()))); |
| 43 | + expect(bundle.definitions.map((d) => d.name)).toEqual([GOTENBERG_RENDER_PDF_TOOL]); |
| 44 | +}); |
| 45 | + |
| 46 | +test("degrades to a non-throwing 'not connected' error when no credential is bound", async () => { |
| 47 | + const bundle = gotenbergTools(fakeEnv(fakeCredentials(undefined))); |
| 48 | + const result = await bundle.run(CALL, new AbortController().signal); |
| 49 | + expect(result.isError).toBe(true); |
| 50 | + expect(result.content).toMatch(/not connected/i); |
| 51 | +}); |
| 52 | + |
| 53 | +test("degrades the same way when the step carries no credentials capability at all", async () => { |
| 54 | + const bundle = gotenbergTools(fakeEnv(undefined)); |
| 55 | + const result = await bundle.run(CALL, new AbortController().signal); |
| 56 | + expect(result.isError).toBe(true); |
| 57 | + expect(result.content).toMatch(/not connected/i); |
| 58 | +}); |
| 59 | + |
| 60 | +test("rejects a missing markdown argument without calling the network", async () => { |
| 61 | + let called = false; |
| 62 | + const bundle = gotenbergTools( |
| 63 | + fakeEnv( |
| 64 | + fakeCredentials(async () => { |
| 65 | + called = true; |
| 66 | + return new Response(); |
| 67 | + }), |
| 68 | + ), |
| 69 | + ); |
| 70 | + const result = await bundle.run( |
| 71 | + { id: "call_2", name: GOTENBERG_RENDER_PDF_TOOL, arguments: { title: "t" } }, |
| 72 | + new AbortController().signal, |
| 73 | + ); |
| 74 | + expect(called).toBe(false); |
| 75 | + expect(result.isError).toBe(true); |
| 76 | + expect(result.content).toContain("markdown"); |
| 77 | +}); |
| 78 | + |
| 79 | +test("returns the rendered PDF as base64 content on a successful call", async () => { |
| 80 | + const pdfBytes = new Uint8Array([0x25, 0x50, 0x44, 0x46]); |
| 81 | + const bundle = gotenbergTools( |
| 82 | + fakeEnv(fakeCredentials(async () => new Response(pdfBytes, { status: 200 }))), |
| 83 | + ); |
| 84 | + const result = await bundle.run(CALL, new AbortController().signal); |
| 85 | + expect(result.isError).toBeUndefined(); |
| 86 | + const parsed = JSON.parse(result.content as string) as { |
| 87 | + filename: string; |
| 88 | + mimeType: string; |
| 89 | + pdfBase64: string; |
| 90 | + }; |
| 91 | + expect(parsed.filename).toBe("Acme Brief.pdf"); |
| 92 | + expect(parsed.mimeType).toBe("application/pdf"); |
| 93 | + expect(Buffer.from(parsed.pdfBase64, "base64")).toEqual(Buffer.from(pdfBytes)); |
| 94 | +}); |
| 95 | + |
| 96 | +test("degrades to an error result (never throws) when the underlying call fails", async () => { |
| 97 | + const bundle = gotenbergTools( |
| 98 | + fakeEnv(fakeCredentials(async () => new Response("nope", { status: 500 }))), |
| 99 | + ); |
| 100 | + const result = await bundle.run(CALL, new AbortController().signal); |
| 101 | + expect(result.isError).toBe(true); |
| 102 | +}); |
0 commit comments