Open History to see the last commit
1import { scopeThreadRef } from "@v12/client-runtime/environment";
2import { EnvironmentId, MessageId, ThreadId, TurnId } from "@v12/contracts";
3import { beforeEach, describe, expect, it } from "vite-plus/test";
4
5import {
6 appendContextTasksToPrompt,
7 applyTaskHudOverrides,
8 buildForkMessageIdMap,
9 identifyTaskHudSteps,
10 taskHudPlanKey,
11 useTaskHudStore,
12} from "./taskHudState";
13
14const steps = [
15 { step: "Inspect", status: "completed" as const },
16 { step: "Implement", status: "inProgress" as const },
17 { step: "Verify", status: "pending" as const },
18];
19
20beforeEach(() => {
21 useTaskHudStore.setState({ overridesByPlanKey: {}, contextTasksByThreadKey: {} });
22});
23
24describe("task HUD state", () => {
25 it("turns pending tasks into a sendable prompt without requiring comment text", () => {
26 expect(
27 appendContextTasksToPrompt("", [
28 { instruction: "No longer animates width", quote: "No longer animates width" },
29 ]),
30 ).toBe("1. No longer animates width");
31 expect(
32 appendContextTasksToPrompt("Please verify", [
33 { instruction: "Check this claim", quote: "The selected claim" },
34 ]),
35 ).toContain("Please verify\n\n1. Check this claim");
36 });
37
38 it("keeps sent tasks in the HUD but clears their pending-send state", () => {
39 const store = useTaskHudStore.getState();
40 const task = {
41 id: "pending-task",
42 instruction: "Send this task",
43 quote: "Send this task",
44 sourceThreadId: ThreadId.make("thread-1"),
45 sourceMessageId: MessageId.make("message-1"),
46 sourceAuthor: "assistant" as const,
47 sourceCreatedAt: "2026-07-15T00:00:00.000Z",
48 completed: false,
49 pendingSend: true,
50 };
51 store.addContextTask("env-1:thread-1", task);
52 store.markContextTasksSent("env-1:thread-1", [task.id]);
53
54 expect(useTaskHudStore.getState().contextTasksByThreadKey["env-1:thread-1"]).toEqual([
55 { ...task, pendingSend: false },
56 ]);
57 });
58
59 it("builds a thread and turn scoped persistence key", () => {
60 expect(
61 taskHudPlanKey(scopeThreadRef(EnvironmentId.make("env-1"), ThreadId.make("thread-1")), {
62 turnId: TurnId.make("turn-1"),
63 createdAt: "2026-07-15T00:00:00.000Z",
64 }),
65 ).toBe("env-1:thread-1:turn-1");
66 });
67
68 it("gives duplicate task text stable occurrence ids", () => {
69 expect(
70 identifyTaskHudSteps([
71 { step: "Test", status: "pending" },
72 { step: "Test", status: "completed" },
73 ]).map((step) => step.id),
74 ).toEqual(["Test\u00000", "Test\u00001"]);
75 });
76
77 it("applies completion, removal, and ordering without losing streamed tasks", () => {
78 const identified = identifyTaskHudSteps(steps);
79 const overrides = {
80 order: [identified[2]!.id, identified[0]!.id],
81 removed: [identified[1]!.id],
82 completedByTaskId: { [identified[2]!.id]: true },
83 };
84
85 expect(applyTaskHudOverrides(steps, overrides)).toEqual([
86 { ...identified[2], status: "completed" },
87 identified[0],
88 ]);
89
90 expect(
91 applyTaskHudOverrides([...steps, { step: "Ship", status: "pending" }], overrides).map(
92 (step) => step.step,
93 ),
94 ).toEqual(["Verify", "Inspect", "Ship"]);
95 });
96
97 it("persists user task changes in the store", () => {
98 const planKey = "env-1:thread-1:turn-1";
99 const taskIds = identifyTaskHudSteps(steps).map((step) => step.id);
100 const store = useTaskHudStore.getState();
101 store.setTaskCompleted(planKey, taskIds[1]!, true);
102 store.removeTask(planKey, taskIds[0]!);
103 store.setTaskOrder(planKey, [taskIds[2]!, taskIds[1]!]);
104
105 expect(useTaskHudStore.getState().overridesByPlanKey[planKey]).toEqual({
106 order: [taskIds[2], taskIds[1]],
107 removed: [taskIds[0]],
108 completedByTaskId: { [taskIds[1]!]: true },
109 });
110 });
111
112 it("copies contextual tasks to a fork with remapped message sources", () => {
113 const task = {
114 id: "task-1",
115 instruction: "Verify this claim",
116 quote: "the selected claim",
117 sourceThreadId: ThreadId.make("thread-1"),
118 sourceMessageId: MessageId.make("message-1"),
119 sourceAuthor: "assistant" as const,
120 sourceCreatedAt: "2026-07-15T00:00:00.000Z",
121 completed: false,
122 };
123 const store = useTaskHudStore.getState();
124 store.addContextTask("env-1:thread-1", task);
125 const targetMessageId = MessageId.make("message-2");
126 store.copyContextTasksToFork({
127 sourceThreadKey: "env-1:thread-1",
128 targetThreadKey: "env-1:thread-2",
129 sourceThreadId: ThreadId.make("thread-1"),
130 targetThreadId: ThreadId.make("thread-2"),
131 messageIdBySourceId: { "message-1": targetMessageId },
132 });
133
134 expect(useTaskHudStore.getState().contextTasksByThreadKey["env-1:thread-2"]).toEqual([
135 { ...task, sourceThreadId: ThreadId.make("thread-2"), sourceMessageId: targetMessageId },
136 ]);
137 });
138
139 it("drops forked tasks whose source message is outside the copied prefix", () => {
140 const store = useTaskHudStore.getState();
141 store.addContextTask("env-1:thread-1", {
142 id: "task-1",
143 instruction: "Check this",
144 quote: "source",
145 sourceThreadId: ThreadId.make("thread-1"),
146 sourceMessageId: MessageId.make("message-after-fork"),
147 sourceAuthor: "assistant",
148 sourceCreatedAt: "2026-01-01T00:00:00.000Z",
149 completed: false,
150 });
151
152 store.copyContextTasksToFork({
153 sourceThreadKey: "env-1:thread-1",
154 targetThreadKey: "env-1:thread-2",
155 sourceThreadId: ThreadId.make("thread-1"),
156 targetThreadId: ThreadId.make("thread-2"),
157 messageIdBySourceId: {},
158 });
159
160 expect(useTaskHudStore.getState().contextTasksByThreadKey["env-1:thread-2"]).toEqual([]);
161 });
162
163 it("maps forked message ids by copied history order", () => {
164 expect(
165 buildForkMessageIdMap(
166 [{ id: MessageId.make("source-1") }, { id: MessageId.make("source-2") }],
167 [{ id: MessageId.make("target-1") }, { id: MessageId.make("target-2") }],
168 ),
169 ).toEqual({ "source-1": "target-1", "source-2": "target-2" });
170 });
171
172 it("refuses to remap tasks from a partial fork projection", () => {
173 expect(() =>
174 buildForkMessageIdMap(
175 [{ id: MessageId.make("source-1") }, { id: MessageId.make("source-2") }],
176 [{ id: MessageId.make("target-1") }],
177 ),
178 ).toThrow("Fork history is incomplete");
179 });
180});