import { describe, expect, it } from "bun:test"; import { __goalTest, GOAL_STATE_CUSTOM_TYPE } from "../index.ts"; const now = 1_700_000_000_000; function goal(overrides: Record = {}) { return { id: "goal-1", objective: "Ship the feature", criteria: ["tests pass", "docs updated"], status: "active", turnsUsed: 2, maxTurns: 5, createdAt: now, updatedAt: now, ...overrides, }; } function replay(data: unknown[]) { const entries = data.map((entry) => ({ type: "custom", customType: GOAL_STATE_CUSTOM_TYPE, data: entry, })); return __goalTest.latestGoalState({ sessionManager: { getEntries: () => entries }, } as never); } describe("goal unit helpers", () => { it("extracts text content from strings and text parts", () => { expect(__goalTest.textFromContent("plain")).toBe("plain"); expect( __goalTest.textFromContent([ { type: "text", text: "a" }, { type: "image", url: "x" }, { type: "text", text: "b" }, ]), ).toBe("a\nb"); expect(__goalTest.textFromContent({ nope: true })).toBe(""); }); it("loads settings with clamped integers and trimmed judge model", () => { expect( __goalTest.loadSettings({ settingsManager: { getSettings: () => ({ goal: { maxTurns: "3.9", tailBytes: 1_000_000, judgeModel: " test/model ", }, }), }, } as never), ).toEqual({ maxTurns: 3, judgeModel: "test/model", tailBytes: 65_536, }); expect( __goalTest.loadSettings({ settingsManager: { getSettings: () => ({ goal: { maxTurns: -1, tailBytes: "bad", judgeModel: " " }, }), }, } as never), ).toEqual({ maxTurns: 1, judgeModel: undefined, tailBytes: 4096, }); }); it("parses model refs and rejects invalid refs", () => { expect(__goalTest.parseModelRef("provider/model/name")).toEqual({ provider: "provider", model: "model/name", }); expect(() => __goalTest.parseModelRef("missing-slash")).toThrow( 'Invalid model ref "missing-slash". Use "provider/model".', ); expect(() => __goalTest.parseModelRef("provider/")).toThrow( 'Invalid model ref "provider/". Use "provider/model".', ); }); it("extracts standing goal objective blocks", () => { expect( __goalTest.extractStandingGoalObjective( "Start working toward this standing goal.\n\n\nShip it\n", ), ).toBe("Ship it"); expect( __goalTest.extractStandingGoalObjective( "Continue working toward this standing goal.\n\n\n Ship it\n now\n", ), ).toBe("Ship it\n now"); expect( __goalTest.extractStandingGoalObjective( "\nShip it\n", ), ).toBeUndefined(); expect( __goalTest.extractStandingGoalObjective("standing goal without xml"), ).toBeUndefined(); }); it("formats judge reasons as bounded one-line text", () => { expect(__goalTest.judgeDisplayReason(" a\n\t b ")).toBe("a b"); expect(__goalTest.judgeDisplayReason("\u001b[31mred\u001b[0m\u0007")).toBe( "red", ); const reason = __goalTest.judgeDisplayReason("x".repeat(200)); expect(reason).toHaveLength(160); expect(reason.endsWith("…")).toBe(true); }); it("renders compact goal widget boxes with usage-style progress", () => { expect( __goalTest.goalBar(goal({ turnsUsed: 2, maxTurns: 5 }) as never), ).toBe("━━━━──────"); expect( __goalTest.goalWidgetLines( goal({ lastReason: "needs tests" }) as never, "normal", 48, ), ).toEqual([ "╭ goal active 2/5 · 2c ────────────────────────╮", "│ objective Ship the feature │", "│ turns 2/5 ━━━━────── 40% │", "│ judge needs tests │", "╰──────────────────────────────────────────────╯", ]); expect(__goalTest.goalWidgetLines(goal() as never, "judging", 40)).toEqual([ "╭ goal judging 3/5 · 2c ───────────────╮", "│ objective Ship the feature │", "│ turn 3/5 │", "╰──────────────────────────────────────╯", ]); expect(__goalTest.insetWidgetLine("╭ goal active ╮", 30)).toBe( " ╭ goal active ╮", ); expect(__goalTest.goalJudgeMessageLine("goal judge done")).toBe( " ◇ goal judge done", ); }); it("parses judge JSON strictly", () => { expect( __goalTest.parseJudge( 'prefix {"done":true,"blocked":false,"reason":"complete"} suffix', ), ).toEqual({ done: true, blocked: false, reason: "complete" }); expect( __goalTest.parseJudge('{"done":"true","blocked":1,"reason":" loose "}'), ).toEqual({ done: false, blocked: false, reason: "loose" }); expect(__goalTest.parseJudge('{"done":false,"blocked":true}')).toEqual({ done: false, blocked: true, reason: "no reason", }); expect(() => __goalTest.parseJudge("[]")).toThrow( "judge returned non-object JSON", ); expect(() => __goalTest.parseJudge("")).toThrow( "judge returned empty response", ); expect(() => __goalTest.parseJudge("not json")).toThrow(); }); it("replays newest valid goal state, tombstones, and malformed entries", () => { expect( replay([goal({ id: "old" }), goal({ id: "new", turnsUsed: 4 })])?.id, ).toBe("new"); expect( replay([goal(), { cleared: true, updatedAt: now + 1 }]), ).toBeUndefined(); expect( replay([{ id: "bad", objective: "missing fields" }, goal({ id: "good" })]) ?.id, ).toBe("good"); expect( replay([goal({ id: "old" }), { id: "bad", objective: "missing fields" }]) ?.id, ).toBe("old"); }); it("reads latest assistant tail", () => { const entries = [ { type: "message", message: { role: "assistant", content: [{ type: "text", text: "first" }], }, }, { type: "message", message: { role: "user", content: [{ type: "text", text: "ignored" }] }, }, { type: "message", message: { role: "assistant", content: [{ type: "text", text: "abcdef" }], }, }, ]; expect( __goalTest.latestAssistantTail( { sessionManager: { getEntries: () => entries } } as never, 3, ), ).toBe("def"); }); it("autocompletes goal subcommands and criterion removal", () => { expect(__goalTest.goalArgumentCompletions("pa", undefined)).toEqual([ { value: "pause", label: "pause", description: "Pause active goal" }, ]); expect( __goalTest .goalArgumentCompletions("clear", undefined) ?.map((item) => item.value), ).toEqual(["clear", "clear-criteria"]); expect( __goalTest.goalArgumentCompletions("remove ", goal() as never), ).toEqual([ { value: "remove 1", label: "1", description: "tests pass" }, { value: "remove 2", label: "2", description: "docs updated" }, ]); expect( __goalTest.goalArgumentCompletions("remove 2", goal() as never), ).toEqual([{ value: "remove 2", label: "2", description: "docs updated" }]); expect( __goalTest.goalArgumentCompletions("add ", goal() as never), ).toBeNull(); }); it("marks goals owned by another session in status output", () => { expect( __goalTest.statusLine(goal({ sessionId: "other" }) as never, "current"), ).toContain("owned by another session"); expect( __goalTest.statusLine(goal({ sessionId: "current" }) as never, "current"), ).not.toContain("owned by another session"); }); it("hides turn budget for terminal goal statuses", () => { expect(__goalTest.statusText(goal({ status: "active" }) as never)).toBe( "goal active 2/5 · 2c", ); expect(__goalTest.statusText(goal({ status: "paused" }) as never)).toBe( "goal paused 2/5 · 2c", ); expect( __goalTest.statusText( goal({ status: "complete", turnsUsed: 0 }) as never, ), ).toBeUndefined(); expect( __goalTest.statusText(goal({ status: "blocked", turnsUsed: 0 }) as never), ).toBe("goal blocked"); expect( __goalTest.statusLine( goal({ status: "complete", turnsUsed: 0 }) as never, ), ).not.toContain("0/5"); }); it("shows useful judge progress context", () => { expect( __goalTest.judgeRunningText(goal({ turnsUsed: 2, maxTurns: 5 }) as never), ).toBe("Goal judge running: turn 3/5 · 2 criteria · Ship the feature"); expect( __goalTest.judgeRunningText( goal({ objective: "x".repeat(100), criteria: [] }) as never, ), ).toBe(`Goal judge running: turn 3/5 · ${"x".repeat(71)}…`); }); it("snapshots user-facing prompt/status output", () => { const state = goal({ lastReason: "still checking" }) as never; expect( JSON.stringify( { status: __goalTest.statusLine(state), criteria: __goalTest.criteriaText(state), active: __goalTest.activeGoalPrompt(state), continuation: __goalTest.continuationPrompt(state), kickoff: __goalTest.kickoffPrompt(state), }, null, 2, ), ).toMatchInlineSnapshot(` "{ "status": "active · 2/5 turns · 2 criteria · still checking\\nShip the feature", "criteria": "1. tests pass\\n2. docs updated", "active": "\\n\\nActive /goal:\\n\\nShip the feature\\n\\n\\nCriteria:\\n1. tests pass\\n2. docs updated\\n\\nKeep working toward this standing goal. Do not redefine success smaller. Before claiming completion, verify current files, commands, tests, or deliverables. Use update_goal only after judge review and only when complete or genuinely blocked.", "continuation": "Continue working toward this standing goal.\\n\\n\\nShip the feature\\n\\n\\nCriteria:\\n1. tests pass\\n2. docs updated\\n\\nDo not redefine success smaller. Inspect current state before claiming completion. If incomplete, make concrete progress. If complete or blocked after judge review, call update_goal.", "kickoff": "Start working toward this standing goal.\\n\\n\\nShip the feature\\n\\n\\nDo not stop just because one turn ends. Keep making concrete progress until the goal is complete, blocked, paused, cleared, or the turn budget is exhausted." }" `); }); });