The Coline App SDK: apps, tools, file types and surfaces that run inside Coline workspaces, and public websites with Coline Sites. TypeScript, with an in-memory test workspace. MIT.
| 4 | import { describe, it, expect } from "vitest"; |
| 5 | import { fakeAi } from "./fake-ai"; |
| 6 | import { watchAiJob, type ColineAiRequest } from "../capabilities/ai"; |
| 7 | const models = [ |
| 8 | { |
| 9 | id: "model", |
| 10 | name: "Model", |
| 11 | kind: "language" as const, |
| 12 | priceMicrousd: 100, |
| 13 | supports: { streaming: true }, |
| 14 | }, |
| 15 | ]; |
| 16 | const request: ColineAiRequest = { |
| 17 | kind: "language", |
| 18 | input: { model: "model", prompt: "Hello", idempotencyKey: "same", maxCostMicrousd: 100 }, |
| 19 | }; |
| 20 | describe("durable AI contract", () => { |
| 21 | it("requires media permissions before invoking a fake provider", async () => { |
| 22 | const app = defineApp({ |
| 23 | key: "studio", |
| 24 | name: "Studio", |
| 25 | permissions: ["ai.generate"], |
| 26 | surfaces: { home: { tier: "tree" } }, |
| 27 | handlers: { renderHome: async () => ui.text("Studio") }, |
| 28 | }); |
| 29 | const workspace = createTestWorkspace(app, { |
| 30 | aiModels: [{ id: "image", name: "Image", kind: "image", priceMicrousd: 100, supports: {} }], |
| 31 | onAiJob: async () => ({ images: [] }), |
| 32 | }); |
| 33 | await expect( |
| 34 | workspace.client().ai.images.generate({ |
| 35 | model: "image", |
| 36 | prompt: "A photo", |
| 37 | driveId: "drive", |
| 38 | idempotencyKey: "image", |
| 39 | maxCostMicrousd: 100, |
| 40 | }), |
| 41 | ).rejects.toThrow("files.write"); |
| 42 | }); |
| 43 | it("deduplicates a request and rejects reuse for different input", async () => { |
| 44 | let calls = 0; |
| 45 | const ai = fakeAi({}, "owner", models, async () => { |
| 46 | calls++; |
| 47 | return { text: "Hi" }; |
| 48 | }); |
| 49 | const first = await ai.run(request); |
| 50 | const second = await ai.run(request); |
| 51 | expect(first.id).toBe(second.id); |
| 52 | expect(calls).toBe(1); |
| 53 | await expect( |
| 54 | ai.run({ ...request, input: { ...request.input, prompt: "Other" } }), |
| 55 | ).rejects.toThrow("idempotency_conflict"); |
| 56 | }); |
| 57 | it("prevents another actor from reading or cancelling a job", async () => { |
| 58 | const state = {}; |
| 59 | const owner = fakeAi(state, "owner", models, async () => ({ text: "private" })); |
| 60 | const outsider = fakeAi(state, "outsider", models); |
| 61 | const job = await owner.run(request); |
| 62 | await expect(outsider.jobs.get(job.id)).rejects.toThrow("job_not_found"); |
| 63 | await expect(outsider.jobs.cancel(job.id)).rejects.toThrow("job_not_found"); |
| 64 | expect(await outsider.jobs.list()).toEqual([]); |
| 65 | }); |
| 66 | it("does not resurrect cancelled jobs when the provider finishes", async () => { |
| 67 | let finish: (value: { text: string }) => void = () => {}; |
| 68 | const ai = fakeAi( |
| 69 | {}, |
| 70 | "owner", |
| 71 | models, |
| 72 | () => |
| 73 | new Promise((resolve) => { |
| 74 | finish = resolve; |
| 75 | }), |
| 76 | ); |
| 77 | const job = await ai.run(request); |
| 78 | await ai.jobs.cancel(job.id); |
| 79 | finish({ text: "Late" }); |
| 80 | await Promise.resolve(); |
| 81 | expect((await ai.jobs.get(job.id)).status).toBe("cancelled"); |
| 82 | expect((await ai.jobs.get(job.id)).chargeMicrousd).toBe(0); |
| 83 | }); |
| 84 | it("enforces the caller budget before invoking a provider", async () => { |
| 85 | const ai = fakeAi({}, "owner", models, async () => { |
| 86 | throw Error("must not run"); |
| 87 | }); |
| 88 | await expect( |
| 89 | ai.run({ ...request, input: { ...request.input, maxCostMicrousd: 1 } }), |
| 90 | ).rejects.toThrow("ai_budget_exceeded"); |
| 91 | }); |
| 92 | it("replays events in order after a persisted cursor", async () => { |
| 93 | const ai = fakeAi({}, "owner", models, async (_request, context) => { |
| 94 | await context.event("text-delta", { text: "One" }); |
| 95 | await context.event("text-delta", { text: "Two" }); |
| 96 | return { text: "OneTwo" }; |
| 97 | }); |
| 98 | const job = await ai.run(request); |
| 99 | const updates = []; |
| 100 | for await (const update of watchAiJob(ai, job.id, { afterSequence: 1 })) |
| 101 | if (update.type === "event") updates.push(update.event); |
| 102 | expect(updates).toEqual([{ sequence: 2, type: "text-delta", data: { text: "Two" } }]); |
| 103 | }); |
| 104 | it("can observe an already-completed job", async () => { |
| 105 | const ai = fakeAi({}, "owner", models, async () => ({ text: "Hi" })); |
| 106 | const job = await ai.run(request); |
| 107 | await Promise.resolve(); |
| 108 | const updates = []; |
| 109 | for await (const update of watchAiJob(ai, job.id)) updates.push(update); |
| 110 | expect( |
| 111 | updates.some((update) => update.type === "job" && update.job.status === "succeeded"), |
| 112 | ).toBe(true); |
| 113 | }); |
| 114 | }); |
| 115 |