Files
raea/sdk/task/test/tools.test.ts
shane d873402ac5 raea goal is the loop, raea make the asset tools (#269)
One loop, raea goal: every rule in TypeScript, held at edit, render, publish and done (goal check, tasks, judge, seen, gates, next); the brief's contract is goal: (one goal or a list, a workflow inline), and its plan is plan:. raea make is making a finished asset: character, clothes, motion, solid, wear, splat, physics, bake, sounds, component, the kit pipeline and shots. Instruments (check, judge, ask, world, scene faults, light check) stay top-level. With it: the montage pack (MK48-MK51), JavaScriptCore fixes raea goal needs (CommonJS named exports, reserved words too, dynamic import of file URLs, process.exit from a promise, stdin for -), raea-cli-goal and raea-cli-refused split out of raea-cli, and skills at raea-skills 105aede.
2026-11-29 18:43:09 -08:00

127 lines
7.2 KiB
TypeScript

// PL5: a plan's tools for an agent (src/tools.ts), answered on a plan in memory the way the app and a headless run do.
import { describe, expect, test } from "vitest"
import { answerPlanTool, find, planToolEdits, type PlanToolHost } from "../src"
import { applyEdits, readPlan, value } from "./text"
const NOW = new Date(2026, 10, 14, 16, 40)
const START = `import { plan } from "raea/task"
export default plan({
title: "Stay",
keywords: ["TODO MAKING REVIEW | DONE DROPPED"],
tasks: [
{
title: "Look",
tasks: [
{
id: "t-door",
keyword: "TODO",
title: "Light the door",
notes: ["- [ ] warm key", "- [ ] cool fill"],
},
],
},
],
})
`
/** START with another task under Light the door, or beside it */
const withTask = (task: string, under = true) => (under ? START.replace(`"- [ ] cool fill"],\n`, `"- [ ] cool fill"],\n tasks: [${task}],\n`) : START.replace(" },\n ],\n },\n ],", ` },\n ${task},\n ],\n },\n ],`))
/** A host on a plan in memory: the page's and a box's, minus `raea make tasks` (the brief run, the edit written back). */
function host(text = START): PlanToolHost & { now: () => string } {
let n = 0
let at = text
return {
who: "a-light",
plan: () => value(at),
apply: (edits) => {
const r = applyEdits(at, edits, { now: NOW, newId: () => `t-new${++n}` })
at = r.text
return r
},
now: () => at,
}
}
describe("an agent's plan tools", () => {
test("plan_read gives the open tasks by id, and the whole file with full", async () => {
const h = host()
const r = (await answerPlanTool("plan_read", {}, h)) as { file: string; open: string; plan?: unknown }
expect(r.file).toBe("plan.ts")
expect(r.open).toContain("t-door TODO Light the door")
expect(r.plan).toBeUndefined()
expect(((await answerPlanTool("plan_read", { full: true }, h)) as { plan: unknown }).plan).toEqual(value(START))
})
test("plan_add writes tasks under their stage, held by the agent that asked unless it names others", async () => {
const h = host()
const r = (await answerPlanTool("plan_add", { tasks: [{ title: "Grade the hall", under: "Look", priority: "A", deadline: "2026-11-20", tags: ["look"] }, { title: "Score it", agents: ["a-score"] }] }, h)) as { ids: string[] }
expect(r.ids).toEqual(["t-new1", "t-new2"])
const plan = readPlan(h.now())
expect(find(plan, "t-new1")).toMatchObject({ stage: "Look", level: 2, priority: "A", agents: ["a-light"], tags: ["look"], deadline: { date: "2026-11-20" } })
expect(find(plan, "t-new2")).toMatchObject({ level: 1, agents: ["a-score"] })
expect(h.now()).toContain('keyword: "TODO",\n priority: "A",\n title: "Grade the hall",')
})
test("plan_update marks, ticks, adds a checkbox, moves and sets fields by id", async () => {
const h = host()
await answerPlanTool("plan_update", { changes: [{ id: "t-door", state: "making", check: 0, add_check: "rim light", item: "i-door-2" }] }, h)
const t = find(readPlan(h.now()), "t-door")!
expect([t.keyword, t.item, t.checks.map((c) => `${c.done ? "x" : " "} ${c.text}`)]).toEqual(["MAKING", "i-door-2", ["x warm key", " cool fill", " rim light"]])
expect(t.progress).toEqual({ done: 1, total: 3 })
})
test("plan_close closes with CLOSED, the item it made and a note; dropped keeps the task", async () => {
const h = host()
await answerPlanTool("plan_close", { id: "t-door", item: "i-door-3", note: "Warm key at 3200K.", all: true }, h)
expect(find(readPlan(h.now()), "t-door")).toMatchObject({ keyword: "DONE", closed: { date: "2026-11-14", time: "16:40" }, item: "i-door-3" })
expect(h.now()).toContain("Warm key at 3200K.")
const d = host()
await answerPlanTool("plan_close", { id: "t-door", state: "dropped" }, d)
expect(find(readPlan(d.now()), "t-door")!.state).toBe("dropped")
})
test("a wrong input or a task that isn't there is said, and nothing is written", async () => {
const h = host()
expect(() => planToolEdits("plan_add", { tasks: [] }, "a")).toThrow(/at least one/)
expect(() => planToolEdits("plan_update", { changes: [{ id: "t-door" }] }, "a")).toThrow(/nothing to change/)
expect(() => planToolEdits("plan_close", { id: "t-door", state: "making" }, "a")).toThrow(/done or dropped/)
await expect(answerPlanTool("plan_close", { id: "t-door", state: "MAKING" }, h)).rejects.toThrow(/MAKING doesn't close/)
await expect(answerPlanTool("plan_close", { id: "t-gone" }, h)).rejects.toThrow(/no task t-gone/)
await expect(answerPlanTool("plan_add", { tasks: [{ title: "x", under: "t-gone" }] }, h)).rejects.toThrow(/no task t-gone/)
expect(h.now()).toBe(START)
})
test("plan_read gives a task written by hand without an id one first, so the agent never names a task by its place", async () => {
const h = host(withTask(`{ keyword: "TODO", title: "Written by hand" }`, false))
const r = (await answerPlanTool("plan_read", {}, h)) as { open: string }
expect(r.open).toContain("t-new1 TODO Written by hand")
expect(r.open).not.toMatch(/#\d/)
expect(h.now()).toContain('id: "t-new1",\n keyword: "TODO",\n title: "Written by hand",')
})
test("done with its checkboxes unticked or a subtask open is refused, naming them; all: true ticks and closes them with it", async () => {
const h = host()
await expect(answerPlanTool("plan_close", { id: "t-door" }, h)).rejects.toThrow(/t-door still has 2 unticked checkboxes \(0 "warm key", 1 "cool fill"\): tick what's done \(plan_update check\), close or move what's left, or close it with all: true/)
await expect(answerPlanTool("plan_update", { changes: [{ id: "t-door", state: "done" }] }, h)).rejects.toThrow(/or close it with plan_close and all: true/)
expect(h.now()).toBe(START)
// ticked in the same call, in either order, it closes
await answerPlanTool("plan_update", { changes: [{ id: "t-door", check: 0 }, { id: "t-door", check: 1, state: "done" }] }, h)
expect(find(readPlan(h.now()), "t-door")).toMatchObject({ state: "done", progress: { done: 2, total: 2 } })
// dropped leaves what's open as it was
const d = host()
await answerPlanTool("plan_close", { id: "t-door", state: "dropped" }, d)
expect(find(readPlan(d.now()), "t-door")!.progress).toEqual({ done: 0, total: 2 })
// an open subtask, and its own checkbox, the same
const sub = host(withTask(`{ id: "t-rim", keyword: "TODO", title: "Rim light", notes: ["- [ ] gel"] }`))
await expect(answerPlanTool("plan_close", { id: "t-door", all: false }, sub)).rejects.toThrow(/2 unticked checkboxes .* and 1 open subtask \(t-rim "Rim light"\)/)
await answerPlanTool("plan_close", { id: "t-door", all: true }, sub)
const p = readPlan(sub.now())
expect([find(p, "t-door")!.state, find(p, "t-rim")!.state, find(p, "t-rim")!.checks[0].done, find(p, "t-door")!.checks.every((c) => c.done)]).toEqual(["done", "done", true, true])
// a task already done isn't asked again (a note added to it, say)
const was = host(START.replace('keyword: "TODO"', 'keyword: "DONE"'))
await answerPlanTool("plan_close", { id: "t-door", note: "Kept." }, was)
expect(was.now()).toContain("Kept.")
})
})