Open History to see the last commit
1import { EnvironmentId, RunId, ThreadId, TurnItemId } from "@t3tools/contracts";
2import * as DateTime from "effect/DateTime";
3import { scopedThreadKey } from "@t3tools/client-runtime/environment";
4import { beforeEach, describe, expect, it } from "vite-plus/test";
5
6import {
7 resolveToolCallDiffTarget,
8 selectThreadGitRepositoryPath,
9 useDiffPanelSessionStore,
10 useDiffPanelStore,
11} from "./diffPanelStore";
12import { withNewFilesFolded } from "./lib/diffCollapse";
13
14describe("diffPanelStore", () => {
15 beforeEach(() => {
16 useDiffPanelStore.setState({
17 byThreadKey: {},
18 branchBaseRefByThreadKey: {},
19 gitRepositoryPathByThreadKey: {},
20 });
21 useDiffPanelSessionStore.setState({
22 collapsedFileKeysByScopeKey: {},
23 knownFileKeysByScopeKey: {},
24 });
25 });
26
27 it("keeps a tool call selection independent of checkpoints and allows returning to Git changes", () => {
28 const ref = { environmentId: EnvironmentId.make("env"), threadId: ThreadId.make("thread") };
29 const target = {
30 threadId: ThreadId.make("child"),
31 itemId: TurnItemId.make("edit"),
32 revision: "1",
33 };
34 const store = useDiffPanelStore.getState();
35 store.selectToolCall(ref, target);
36 store.reconcileTurnSelection(ref, []);
37 expect(useDiffPanelStore.getState().byThreadKey[scopedThreadKey(ref)]).toEqual({
38 kind: "tool-call",
39 ...target,
40 });
41 store.selectGitScope(ref, "branch");
42 expect(useDiffPanelStore.getState().byThreadKey[scopedThreadKey(ref)]).toEqual({
43 kind: "branch",
44 baseRef: null,
45 });
46 });
47
48 it("keeps Unicode whitespace at the edges of a picked base branch", () => {
49 const ref = { environmentId: EnvironmentId.make("env"), threadId: ThreadId.make("thread") };
50 const store = useDiffPanelStore.getState();
51 store.selectBranchBaseRef(ref, "feat\u00a0");
52 expect(useDiffPanelStore.getState().byThreadKey[scopedThreadKey(ref)]).toEqual({
53 kind: "branch",
54 baseRef: "feat\u00a0",
55 });
56 store.selectBranchBaseRef(ref, " \t");
57 expect(useDiffPanelStore.getState().byThreadKey[scopedThreadKey(ref)]).toEqual({
58 kind: "branch",
59 baseRef: null,
60 });
61 });
62
63 it("refreshes a live tool-call target when its source item completes", () => {
64 const target = {
65 threadId: ThreadId.make("child"),
66 itemId: TurnItemId.make("edit"),
67 revision: "live",
68 };
69 const now = DateTime.makeUnsafe("2026-10-28T00:00:00Z");
70 const row = {
71 position: 0,
72 visibility: "local" as const,
73 sourceThreadId: target.threadId,
74 sourceItemId: target.itemId,
75 item: {
76 id: target.itemId,
77 threadId: target.threadId,
78 runId: null,
79 nodeId: null,
80 providerThreadId: null,
81 providerTurnId: null,
82 nativeItemRef: null,
83 parentItemId: null,
84 ordinal: 1,
85 status: "running" as const,
86 title: null,
87 startedAt: now,
88 completedAt: null,
89 updatedAt: now,
90 type: "file_change" as const,
91 fileName: "a.ts",
92 },
93 };
94 expect(resolveToolCallDiffTarget(target, [row])).toBe(target);
95 const completed = {
96 ...row,
97 item: { ...row.item, status: "completed" as const, completedAt: now },
98 };
99 expect(resolveToolCallDiffTarget(target, [completed])).toEqual({
100 ...target,
101 revision: DateTime.formatIso(now),
102 });
103 expect(
104 resolveToolCallDiffTarget(target, [{ ...completed, sourceThreadId: ThreadId.make("other") }]),
105 ).toBe(target);
106 expect(resolveToolCallDiffTarget(target, [])).toBe(target);
107 });
108
109 it("folds only files that appear after a fold change when the default collapses", () => {
110 const scopeKey = "environment-1:thread-1:branch";
111 const { updateCollapsedFileKeys } = useDiffPanelSessionStore.getState();
112 updateCollapsedFileKeys(scopeKey, ["a.ts"], new Set(), () => new Set(["a.ts"]));
113 updateCollapsedFileKeys(scopeKey, ["a.ts"], new Set(), () => new Set());
114
115 const session = useDiffPanelSessionStore.getState();
116 expect(
117 withNewFilesFolded(
118 session.collapsedFileKeysByScopeKey[scopeKey],
119 session.knownFileKeysByScopeKey[scopeKey],
120 new Set(["a.ts", "b.ts"]),
121 ),
122 ).toEqual(new Set(["b.ts"]));
123 });
124
125 it("retains fold choices when a panel consumer returns with fresh defaults", () => {
126 const scopeKey = "environment-1:thread-1:branch";
127 const defaults = new Set(["src/app.ts", "src/index.ts"]);
128 useDiffPanelSessionStore
129 .getState()
130 .updateCollapsedFileKeys(scopeKey, [...defaults], defaults, (current) => {
131 const next = new Set(current);
132 next.delete("src/app.ts");
133 return next;
134 });
135
136 expect(useDiffPanelSessionStore.getState().collapsedFileKeysByScopeKey[scopeKey]).toEqual(
137 new Set(["src/index.ts"]),
138 );
139
140 useDiffPanelSessionStore
141 .getState()
142 .updateCollapsedFileKeys(scopeKey, [...defaults], new Set(), (current) => {
143 const next = new Set(current);
144 next.delete("src/index.ts");
145 return next;
146 });
147 expect(useDiffPanelSessionStore.getState().collapsedFileKeysByScopeKey[scopeKey]).toEqual(
148 new Set(),
149 );
150
151 useDiffPanelSessionStore
152 .getState()
153 .updateCollapsedFileKeys(scopeKey, [...defaults], defaults, (current) => current);
154 expect(useDiffPanelSessionStore.getState().collapsedFileKeysByScopeKey[scopeKey]).toEqual(
155 new Set(),
156 );
157 expect(defaults).toEqual(new Set(["src/app.ts", "src/index.ts"]));
158 });
159
160 it("keeps the repository filter across turns and drops it for a requested file", () => {
161 const ref = { environmentId: EnvironmentId.make("env"), threadId: ThreadId.make("thread") };
162 const selection = () => useDiffPanelStore.getState().byThreadKey[scopedThreadKey(ref)];
163 const store = useDiffPanelStore.getState();
164 store.selectTurnRepository(ref, RunId.make("run-1"), "/work/api");
165 store.selectTurn(ref, RunId.make("run-2"));
166 expect(selection()).toMatchObject({ turnId: "run-2", repository: "/work/api" });
167 store.selectTurn(ref, RunId.make("run-2"), "web/src/index.ts");
168 expect(selection()).toEqual({
169 kind: "turn",
170 turnId: "run-2",
171 filePath: "web/src/index.ts",
172 revealRequestId: 2,
173 });
174 store.selectTurnRepository(ref, RunId.make("run-2"), "/work/api");
175 store.selectTurnRepository(ref, RunId.make("run-2"), null);
176 expect(selection()).not.toHaveProperty("repository");
177 });
178
179 it("keeps a thread's Git repository until another checkout is chosen or the thread is removed", () => {
180 const ref = { environmentId: EnvironmentId.make("env"), threadId: ThreadId.make("thread") };
181 const other = { environmentId: EnvironmentId.make("env"), threadId: ThreadId.make("other") };
182 const repository = (threadRef: typeof ref) =>
183 selectThreadGitRepositoryPath(useDiffPanelStore.getState(), threadRef);
184 const store = useDiffPanelStore.getState();
185 store.selectGitRepository(ref, "/work/api");
186 expect(repository(ref)).toBe("/work/api");
187 expect(repository(other)).toBeNull();
188 store.selectGitRepository(ref, null);
189 expect(repository(ref)).toBeNull();
190
191 store.selectCommit(ref, "abc123", "/work/web");
192 expect(repository(ref)).toBe("/work/web");
193 store.selectCommit(ref, "def456", null);
194 expect(repository(ref)).toBeNull();
195
196 store.selectGitRepository(other, "/work/api");
197 store.removeThread(other);
198 expect(repository(other)).toBeNull();
199 });
200});