> | undefined
-afterEach(() => t?.stop())
-
-const PAGE = "\nBakery\nFresh bread.
"
-
-test("the info line: the kind whatever its case, the title the rest of the line or the page's own", () => {
- expect(parseInfo("prototype-chat Monthly sales")).toEqual({ kind: "chat", title: "Monthly sales" })
- expect(parseInfo("Prototype-Page")).toEqual({ kind: "page", title: "" })
- expect(parseInfo("prototype")).toEqual({ kind: "chat", title: "" })
- expect(parseInfo("html")).toBeNull()
- expect(titleFor(" From\n the page ", "")).toBe("From the page")
- expect(titleFor(PAGE, "Given")).toBe("Given")
- expect(titleFor("none
", "")).toBe("")
-})
-
-test("a reply split: markdown as it is, a prototype fence closed or still streaming, other code blocks left alone", () => {
- const text = "Here:\n\n```prototype-page Bakery landing\n" + PAGE + "\n```\n\nAnd code:\n\n```js\nconsole.log(1)\n```\n\n```prototype-chat Chart\nhalf"
- const segs = splitPrototypes(text)
- expect(segs.map((s) => s.kind)).toEqual(["text", "prototype", "text", "prototype"])
- expect(segs[1]).toMatchObject({ kind: "prototype", info: { kind: "page", title: "Bakery landing" }, code: PAGE, closed: true, start: 7 })
- expect(segs[2]).toEqual({ kind: "text", text: "\nAnd code:\n\n```js\nconsole.log(1)\n```\n" })
- expect(segs[3]).toMatchObject({ kind: "prototype", closed: false, code: "
half" })
- // A fence that is a longer run of backticks, or tildes, closes only with its own; `~~~` inside
- // a backtick fence is content.
- const tilde = splitPrototypes("~~~prototype\n```\nnot a fence\n```\n~~~\n")
- expect(tilde[0]).toMatchObject({ kind: "prototype", closed: true, code: "```\nnot a fence\n```" })
- expect(splitPrototypes("plain text, no fence")).toEqual([{ kind: "text", text: "plain text, no fence" }])
-})
-
-test("saved under the state directory, numbered, slugged from the title, 0600, once per content", () => {
- const id = "ses_proto_test"
- const path = savePrototype(id, PAGE, { kind: "page", title: "Bakery landing" })
- expect(path).toBe(join(prototypeDir(id), "1-bakery-landing.html"))
- expect(path.startsWith(join(paths.state, "prototypes"))).toBe(true)
- expect(readFileSync(path, "utf8")).toBe(PAGE)
- expect(statSync(path).mode & 0o777).toBe(0o600)
- // The same page again is the same file; a different one is the next number, named by its
- // own
when the fence gave none, by its kind when neither says.
- expect(savePrototype(id, PAGE, { kind: "page", title: "Bakery landing" })).toBe(path)
- expect(savePrototype(id, "Sales: Q3x
", { kind: "chat", title: "" })).toBe(join(prototypeDir(id), "2-sales-q3.html"))
- expect(savePrototype(id, "bare
", { kind: "chat", title: "" })).toBe(join(prototypeDir(id), "3-chat.html"))
- expect(listPrototypes(id).map((p) => p.name)).toEqual(["1-bakery-landing.html", "2-sales-q3.html", "3-chat.html"])
- expect(slugOf("Čaj & káva: ráno", "chat")).toBe("čaj-káva-ráno")
-})
-
-test("in the TUI a completed prototype fence is one line saying where it was saved; /prototype lists them", async () => {
- const reply = "A page for you:\n\n```prototype-page Bakery landing\n" + PAGE + "\n```\n\nThat is the landing page."
- t = await tui([{ chunks: [delta({ content: reply.slice(0, 40) }), delta({ content: reply.slice(40) })] }], { width: 120, height: 30 }, "", { store: true })
- await t.input.typeText("make a page")
- t.input.pressEnter()
- expect(await t.until("That is the landing page.")).toBe(true)
- const id = t.app.engine.sessionId!
- const saved = listPrototypes(id)
- expect(saved).toHaveLength(1)
- expect(saved[0]!.name).toBe("1-bakery-landing.html")
- expect(readFileSync(saved[0]!.path, "utf8")).toBe(PAGE)
- // Drawn as the one line, not as the code block; the markdown around it as before.
- expect(await t.until("prototype · Bakery landing · saved to")).toBe(true)
- expect(t.frame()).toContain("A page for you:")
- expect(t.frame()).not.toContain("Fresh bread.")
- await t.input.typeText("/prototype")
- t.input.pressEnter()
- expect(await t.until(`1 ${saved[0]!.path}`.slice(0, 60))).toBe(true)
- await t.input.typeText("/prototype 2")
- t.input.pressEnter()
- expect(await t.until("no prototype 2 here")).toBe(true)
- expect(existsSync(saved[0]!.path)).toBe(true)
-})
diff --git a/tests/scheduled.test.ts b/tests/scheduled.test.ts
index f3cb3bc..3ab05a0 100644
--- a/tests/scheduled.test.ts
+++ b/tests/scheduled.test.ts
@@ -12,6 +12,7 @@ import { paths } from "../src/config/paths.ts"
import { remoteState } from "../src/config/remote.ts"
import { setTrust } from "../src/project/root.ts"
import { remoteCommand } from "../src/remote.ts"
+import { TOOL_SPECS } from "../src/harness.ts"
import { delta, fakeProvider, toolCall, type Fake } from "./fake-provider.ts"
let fake: Fake | undefined
@@ -189,3 +190,86 @@ test("lembas remote scheduled on|off writes remote.scheduled, and --json says it
expect(remoteState().scheduled).toBe(false)
expect(remoteCommand(["scheduled", "maybe"], { out: () => {}, err: () => {} })).toBe(2)
})
+
+// ---- Reports (harness spec 3.8.0, reports.md) ----
+
+/** A tool result without the id every result carries (context.md). */
+const bare = (text: string) => text.replace(/^\[#\d+\]\n/, "")
+const toolNames = (i: number) => ((fake!.requests[i].tools ?? []) as { function: { name: string } }[]).map((t) => t.function.name)
+const R = (extra: Record = {}) => JSON.stringify({ title: "Disk almost full", body: "## What\n\n`/var` is at 94%.", summary: "The log partition is at 94%.", importance: "high", ...extra })
+
+test("capability reports; a run that notifies is offered report_write, and the report it writes travels in the answer", async () => {
+ fake = fakeProvider([{ chunks: [toolCall(0, "c1", "report_write", R())] }, { chunks: [delta({ content: "Filed a report: the disk is almost full." })] }])
+ config(fake.url)
+ const root = project()
+ const { c, init, asked } = await client(limitsFor(root))
+ expect(init._meta.lembas.capabilities).toContain("reports")
+ const meta = { lembas: { scheduled: sched({ notify: "when" }) } }
+ const s: any = await c.request("session/new", { cwd: root, _meta: meta })
+ const r: any = await c.request("session/prompt", { sessionId: s.sessionId, prompt: [{ type: "text", text: "check the disks" }], _meta: { lembas: { turnId: "run7", ...meta.lembas } } })
+ expect(toolNames(0)).toContain("report_write")
+ // Allowed without asking: it writes nothing here.
+ expect(asked).toEqual([])
+ expect(r._meta.lembas.scheduled).toEqual({
+ run_id: "run7",
+ status: "ok",
+ text: "Filed a report: the disk is almost full.",
+ error: null,
+ report: { title: "Disk almost full", body: "## What\n\n`/var` is at 94%.", summary: "The log partition is at 94%.", importance: "high" },
+ })
+ // The model was told the report is filed and to end with one line.
+ expect(bare(fake.requests[1].messages.at(-1).content)).toBe(SCHEDULED.notify.result.filed)
+})
+
+test("one report per run: a second call replaces the first; the next run starts with none; the sentinel is no status any more", async () => {
+ fake = fakeProvider([
+ { chunks: [toolCall(0, "c1", "report_write", R()), toolCall(1, "c2", "report_write", R({ title: "Disk fine again", importance: undefined }))] },
+ { chunks: [delta({ content: "Done." })] },
+ { chunks: [delta({ content: "Nothing needs attention.\n[SILENT]" })] },
+ ])
+ config(fake.url)
+ const root = project()
+ const { c } = await client(limitsFor(root))
+ const meta = (run: string) => ({ lembas: { turnId: run, scheduled: sched({ run_id: run, notify: "always" }) } })
+ const s: any = await c.request("session/new", { cwd: root, _meta: meta("run7") })
+ const r1: any = await c.request("session/prompt", { sessionId: s.sessionId, prompt: [{ type: "text", text: "check" }], _meta: meta("run7") })
+ expect(r1._meta.lembas.scheduled.report).toMatchObject({ title: "Disk fine again", importance: "normal" })
+ const results = (fake.requests[1].messages as { role: string; content: string }[]).filter((m) => m.role === "tool").map((m) => bare(m.content))
+ expect(results).toEqual([SCHEDULED.notify.result.filed, SCHEDULED.notify.result.replaced])
+ // The schedule's next run, in the same session: no report of its own, so none in its answer —
+ // and `silent` is not a status for a turn whose request said notify.
+ const r2: any = await c.request("session/prompt", { sessionId: s.sessionId, prompt: [{ type: "text", text: "check" }], _meta: meta("run8") })
+ expect(r2._meta.lembas.scheduled.report).toBeUndefined()
+ expect(r2._meta.lembas.scheduled.status).toBe("ok")
+})
+
+test("notify never, and a server that says no notify at all: the tool is not in the list; the person's own turn in a run's session finds it unavailable", async () => {
+ fake = fakeProvider([
+ { chunks: [delta({ content: "ok" })] },
+ { chunks: [delta({ content: "ok" })] },
+ { chunks: [delta({ content: "run" })] },
+ { chunks: [toolCall(0, "c9", "report_write", R())] },
+ { chunks: [delta({ content: "I cannot file one here." })] },
+ ])
+ config(fake.url)
+ const root = project()
+ const { c } = await client(limitsFor(root))
+ const run = async (scheduled: Record, id: string) => {
+ const s: any = await c.request("session/new", { cwd: root, _meta: { lembas: { scheduled } } })
+ const r: any = await c.request("session/prompt", { sessionId: s.sessionId, prompt: [{ type: "text", text: "check" }], _meta: { lembas: { turnId: id, scheduled } } })
+ return { s, r }
+ }
+ await run(sched({ notify: "never" }), "a")
+ expect(toolNames(0)).not.toContain("report_write")
+ // A server before 3.8.0 sends no notify: as before, and the tool is not offered.
+ await run(sched(), "b")
+ expect(toolNames(1)).not.toContain("report_write")
+ // A run that notifies, then the person writes into its session: the list is the session's,
+ // and the tool answers the spec's unavailable text.
+ const { s } = await run(sched({ notify: "always" }), "c")
+ expect(toolNames(2)).toContain("report_write")
+ const mine: any = await c.request("session/prompt", { sessionId: s.sessionId, prompt: [{ type: "text", text: "and file that for me" }] })
+ expect(toolNames(3)).toContain("report_write")
+ expect(bare(fake.requests[4].messages.at(-1).content)).toBe(TOOL_SPECS.report_write!.unavailable!)
+ expect(mine._meta.lembas.scheduled).toBeUndefined()
+})