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 useDiffPanelSessionStore,
9 useDiffPanelStore,
10} from "./diffPanelStore";
11
12describe("diffPanelStore", () => {
13 beforeEach(() => {
14 useDiffPanelStore.setState({
15 byThreadKey: {},
16 branchBaseRefByThreadKey: {},
17 });
18 useDiffPanelSessionStore.setState({ collapsedFileKeysByScopeKey: {} });
19 });
20
21 it("keeps a tool call selection independent of checkpoints and allows returning to Git changes", () => {
22 const ref = { environmentId: EnvironmentId.make("env"), threadId: ThreadId.make("thread") };
23 const target = {
24 threadId: ThreadId.make("child"),
25 itemId: TurnItemId.make("edit"),
26 revision: "1",
27 };
28 const store = useDiffPanelStore.getState();
29 store.selectToolCall(ref, target);
30 store.reconcileTurnSelection(ref, []);
31 expect(useDiffPanelStore.getState().byThreadKey[scopedThreadKey(ref)]).toEqual({
32 kind: "tool-call",
33 ...target,
34 });
35 store.selectGitScope(ref, "branch");
36 expect(useDiffPanelStore.getState().byThreadKey[scopedThreadKey(ref)]).toEqual({
37 kind: "branch",
38 baseRef: null,
39 });
40 });
41
42 it("keeps Unicode whitespace at the edges of a picked base branch", () => {
43 const ref = { environmentId: EnvironmentId.make("env"), threadId: ThreadId.make("thread") };
44 const store = useDiffPanelStore.getState();
45 store.selectBranchBaseRef(ref, "feat\u00a0");
46 expect(useDiffPanelStore.getState().byThreadKey[scopedThreadKey(ref)]).toEqual({
47 kind: "branch",
48 baseRef: "feat\u00a0",
49 });
50 store.selectBranchBaseRef(ref, " \t");
51 expect(useDiffPanelStore.getState().byThreadKey[scopedThreadKey(ref)]).toEqual({
52 kind: "branch",
53 baseRef: null,
54 });
55 });
56
57 it("refreshes a live tool-call target when its source item completes", () => {
58 const target = {
59 threadId: ThreadId.make("child"),
60 itemId: TurnItemId.make("edit"),
61 revision: "live",
62 };
63 const now = DateTime.makeUnsafe("2026-10-28T00:00:00Z");
64 const row = {
65 position: 0,
66 visibility: "local" as const,
67 sourceThreadId: target.threadId,
68 sourceItemId: target.itemId,
69 item: {
70 id: target.itemId,
71 threadId: target.threadId,
72 runId: null,
73 nodeId: null,
74 providerThreadId: null,
75 providerTurnId: null,
76 nativeItemRef: null,
77 parentItemId: null,
78 ordinal: 1,
79 status: "running" as const,
80 title: null,
81 startedAt: now,
82 completedAt: null,
83 updatedAt: now,
84 type: "file_change" as const,
85 fileName: "a.ts",
86 },
87 };
88 expect(resolveToolCallDiffTarget(target, [row])).toBe(target);
89 const completed = {
90 ...row,
91 item: { ...row.item, status: "completed" as const, completedAt: now },
92 };
93 expect(resolveToolCallDiffTarget(target, [completed])).toEqual({
94 ...target,
95 revision: DateTime.formatIso(now),
96 });
97 expect(
98 resolveToolCallDiffTarget(target, [{ ...completed, sourceThreadId: ThreadId.make("other") }]),
99 ).toBe(target);
100 expect(resolveToolCallDiffTarget(target, [])).toBe(target);
101 });
102
103 it("retains fold choices when a panel consumer returns with fresh defaults", () => {
104 const scopeKey = "environment-1:thread-1:branch";
105 const defaults = new Set(["src/app.ts", "src/index.ts"]);
106 useDiffPanelSessionStore.getState().updateCollapsedFileKeys(scopeKey, defaults, (current) => {
107 const next = new Set(current);
108 next.delete("src/app.ts");
109 return next;
110 });
111
112 expect(useDiffPanelSessionStore.getState().collapsedFileKeysByScopeKey[scopeKey]).toEqual(
113 new Set(["src/index.ts"]),
114 );
115
116 useDiffPanelSessionStore.getState().updateCollapsedFileKeys(scopeKey, new Set(), (current) => {
117 const next = new Set(current);
118 next.delete("src/index.ts");
119 return next;
120 });
121 expect(useDiffPanelSessionStore.getState().collapsedFileKeysByScopeKey[scopeKey]).toEqual(
122 new Set(),
123 );
124
125 useDiffPanelSessionStore
126 .getState()
127 .updateCollapsedFileKeys(scopeKey, defaults, (current) => current);
128 expect(useDiffPanelSessionStore.getState().collapsedFileKeysByScopeKey[scopeKey]).toEqual(
129 new Set(),
130 );
131 expect(defaults).toEqual(new Set(["src/app.ts", "src/index.ts"]));
132 });
133
134 it("keeps the repository filter across turns and drops it for a requested file", () => {
135 const ref = { environmentId: EnvironmentId.make("env"), threadId: ThreadId.make("thread") };
136 const selection = () => useDiffPanelStore.getState().byThreadKey[scopedThreadKey(ref)];
137 const store = useDiffPanelStore.getState();
138 store.selectTurnRepository(ref, RunId.make("run-1"), "/work/api");
139 store.selectTurn(ref, RunId.make("run-2"));
140 expect(selection()).toMatchObject({ turnId: "run-2", repository: "/work/api" });
141 store.selectTurn(ref, RunId.make("run-2"), "web/src/index.ts");
142 expect(selection()).toEqual({
143 kind: "turn",
144 turnId: "run-2",
145 filePath: "web/src/index.ts",
146 revealRequestId: 2,
147 });
148 store.selectTurnRepository(ref, RunId.make("run-2"), "/work/api");
149 store.selectTurnRepository(ref, RunId.make("run-2"), null);
150 expect(selection()).not.toHaveProperty("repository");
151 });
152});