binding.test.ts
This file
Path
packages/drafting/test/binding.test.ts
280 lines · 8.3 KiB · TypeScript
Last changed by
Merge build/comments-packages-rest into mainoscabrielbefore adoption38cd503
History of this fileTake it
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281import { describe, expect, expectTypeOf, it } from "@effect/vitest";import { Brand } from "@repo/brand";import { AskId, LandedSha, ProjectName, RepoPath } from "@repo/protocol/ids";import { Effect, Schema } from "effect";
import { DraftedAsk, DraftFailed } from "../src/drafted.ts";import type { DraftInput } from "../src/drafted.ts";import { Drafter } from "../src/drafter.ts";import { DrafterWorkersAi } from "../src/drafter/binding.ts";import type { AiLike, ChatInputs } from "../src/drafter/binding.ts";
const model = "@cf/test/drafting-model";
const drafted: DraftedAsk = { id: AskId.make("iso-week"), kind: "fix", rationale: "The parser has no week dates.", territory: [RepoPath.make("src/parse.ts")], test: { path: RepoPath.make("test/asks/iso-week.test.ts"), source: 'it("parses", () => expect(parse("2026-W01")).toBe(1));\n', }, want: "An ISO week date parses to the Monday of that week.",};
const encoded = Schema.encodeSync(DraftedAsk)(drafted);
const json = JSON.stringify(encoded);
const input: DraftInput = { attempt: 1, context: { at: LandedSha.make("4".repeat(40)), files: [], policy: "Ask kinds: fix (redThenGreen).", project: ProjectName.make("oscabriel/datekit"), }, want: "Parse ISO week dates.",};
interface Call { readonly inputs: ChatInputs; readonly model: string;}
type Outcome = { readonly reply: object } | { readonly thrown: Error };
const fakeAi = (outcome: Outcome) => { const calls: Call[] = [];
const ai: AiLike = { run: (name, inputs) => { calls.push({ inputs, model: name });
return "reply" in outcome ? Promise.resolve(outcome.reply) : Promise.reject(outcome.thrown); }, };
return { ai, calls };};
const draftWith = (outcome: Outcome) => Drafter.use(({ draft }) => draft(input)).pipe( Effect.provide(DrafterWorkersAi(fakeAi(outcome).ai, model)) );
const chat = (content: string): Outcome => ({ reply: { choices: [{ message: { content } }] },});
const thrown = (message: string): Outcome => ({ thrown: new Error(message) });
const failure = (reason: DraftFailed["reason"]) => new DraftFailed({ reason });
const callOf = Effect.gen(function* () { const { ai, calls } = fakeAi(chat(json));
yield* Drafter.use(({ draft }) => draft(input)).pipe( Effect.provide(DrafterWorkersAi(ai, model)) );
return yield* Effect.fromNullishOr(calls[0]);});
describe("the Workers AI drafter", () => { it("takes the generated Ai binding as its AiLike", () => { expectTypeOf<Ai>().toExtend<AiLike>(); });
it.effect( "asks the given model, once, with the system and user prompts", () => Effect.gen(function* () { const call = yield* callOf;
expect(call.model).toBe(model); expect(call.inputs.messages.map(({ role }) => role)).toStrictEqual([ "system", "user", ]); expect(call.inputs).toMatchObject({ max_completion_tokens: 12_000, temperature: 0.2, }); expect(call.inputs.seed).toBe((yield* callOf).inputs.seed); }) );
it.effect( "gives each model its own room: the fallbacks more tokens and low reasoning, anything else the defaults", () => Effect.gen(function* () { const asked = yield* Effect.forEach( [ "@cf/moonshotai/kimi-k2.7-code", "@cf/zai-org/glm-5.3", "@cf/openai/gpt-oss-120b", model, ], (name) => Effect.gen(function* () { const { ai, calls } = fakeAi(chat(json));
yield* Drafter.use(({ draft }) => draft(input)).pipe( Effect.provide(DrafterWorkersAi(ai, name)) );
const call = yield* Effect.fromNullishOr(calls[0]);
return { max: call.inputs.max_completion_tokens, model: call.model, reasoning: Object.hasOwn(call.inputs, "reasoning_effort") ? call.inputs.reasoning_effort : "none sent", }; }) );
expect(asked).toStrictEqual([ { max: 12_000, model: "@cf/moonshotai/kimi-k2.7-code", reasoning: "none sent", }, { max: 32_000, model: "@cf/zai-org/glm-5.3", reasoning: "low" }, { max: 32_000, model: "@cf/openai/gpt-oss-120b", reasoning: "low" }, { max: 12_000, model, reasoning: "none sent" }, ]); }) );
it.effect("asks for JSON Schema with no null and no Infinity in it", () => Effect.gen(function* () { const { response_format: format } = (yield* callOf).inputs; const text = JSON.stringify(format.json_schema);
expect(format.type).toBe("json_schema"); expect(text).toContain('"want"'); expect(text).not.toContain("null"); expect(text).not.toContain("Infinity"); }) );
it.effect( "decodes a valid reply, a fenced reply and a `response` reply", () => Effect.gen(function* () { expect(yield* draftWith(chat(json))).toStrictEqual(drafted); expect( yield* draftWith(chat(`\`\`\`json\n${json}\n\`\`\`\n`)) ).toStrictEqual(drafted); expect(yield* draftWith({ reply: { response: json } })).toStrictEqual( drafted ); }) );
it.effect( "gives a reply off the schema as `schema`, and no JSON as `unparseable`", () => Effect.gen(function* () { expect(yield* Effect.flip(draftWith(chat('{"id": 3}')))).toStrictEqual( failure("schema") ); expect(yield* Effect.flip(draftWith(chat("not json")))).toStrictEqual( failure("unparseable") ); expect( yield* Effect.flip(draftWith({ reply: { choices: [] } })) ).toStrictEqual(failure("unparseable")); }) );
it.effect( "takes one sentence where the schema asks for a list of them, as a list of one", () => Effect.gen(function* () { // Seen live: kimi-k2.7-code answered every attempt with `context` and // `outOfScope` as strings, whatever the JSON Schema said. const reply = JSON.stringify({ ...encoded, context: "An ordinal date is YYYY-DDD.", outOfScope: "Changing parse.", });
expect(yield* draftWith(chat(reply))).toStrictEqual({ ...drafted, context: ["An ordinal date is YYYY-DDD."], outOfScope: ["Changing parse."], }); expect( yield* Effect.flip( draftWith(chat(JSON.stringify({ ...encoded, context: "" }))) ) ).toStrictEqual(failure("schema")); }) );
it.effect( "refuses a test path in the config folder or off the tree as `schema`, so model text never lands as config", () => Effect.gen(function* () { for (const path of [ `${Brand.configDir}/project.ts`, `${Brand.configDir}/asks/other.ask.ts`, "../outside.test.ts", "/etc/passwd", "test/./asks/iso-week.test.ts", ]) { const reply = JSON.stringify({ ...encoded, test: { path, source: "export default 1;\n" }, });
expect(yield* Effect.flip(draftWith(chat(reply)))).toStrictEqual( failure("schema") ); }
const offTerritory = JSON.stringify({ ...encoded, territory: ["src/../../secrets.ts"], });
expect(yield* Effect.flip(draftWith(chat(offTerritory)))).toStrictEqual( failure("schema") ); }) );
it.effect( "names a 429 `rate-limited`, a JSON-mode miss `schema`, the rest `model-error`", () => Effect.gen(function* () { expect( yield* Effect.flip( draftWith(thrown("AiError: 429: Too Many Requests")) ) ).toStrictEqual(failure("rate-limited")); expect( yield* Effect.flip( draftWith( thrown("3040: Capacity temporarily exceeded, please try again.") ) ) ).toStrictEqual(failure("rate-limited")); expect( yield* Effect.flip( draftWith(thrown("AiError: JSON Mode couldn't be met")) ) ).toStrictEqual(failure("schema")); expect( yield* Effect.flip(draftWith(thrown("5007: No such model"))) ).toStrictEqual(failure("model-error")); }) );});