/** @file The model, asked in code mode, writes code that drives the mower. */ import { env } from "cloudflare:workers"; import { expect, test } from "vitest"; import { codeModeMessages, extractCode } from "../src/code-mode.ts"; import { makeWorkersAiModel, MODEL } from "../src/model.ts"; import { aboutMower } from "../src/mower.ts"; test("lent a mower, the neighbor writes code to mow", async ({ annotate }) => { const model = makeWorkersAiModel(env.AI, MODEL); const messages = codeModeMessages({ say: "you can use this mower for the weekend", about: { mower: aboutMower }, }); const code = extractCode(await model.complete(messages)); await annotate("the code the model wrote", { body: code, contentType: "text/javascript" }); // `env.mower.push()`, or `mower.push()` after destructuring env const calls = [...code.matchAll(/\bmower\.(\w+)\(/g)].map(([, method]) => method); expect(calls).toContain("push"); for (const method of calls) expect(["start", "push", "turn", "stop"]).toContain(method); // calls pipeline: await only for a value, or once at the end const awaits = code.match(/\bawait\b/g) ?? []; expect(awaits.length).toBeLessThan(calls.length); });