Open History to see the last commit
1import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2import { EnvironmentId, ThreadId, RunId } from "@t3tools/contracts";
3import { beforeEach, describe, expect, it, vi } from "vite-plus/test";
4
5import {
6 selectThreadDiffPanelSelection,
7 useDiffPanelSessionStore,
8 useDiffPanelStore,
9} from "./diffPanelStore";
10
11const THREAD_REF = scopeThreadRef(EnvironmentId.make("environment-1"), ThreadId.make("thread-1"));
12
13describe("diffPanelStore", () => {
14 beforeEach(() => {
15 useDiffPanelStore.setState({
16 byThreadKey: {},
17 branchBaseRefByThreadKey: {},
18 });
19 useDiffPanelSessionStore.setState({ collapsedFileKeysByScopeKey: {} });
20 });
21
22 it("retains fold choices when a panel consumer returns with fresh defaults", () => {
23 const scopeKey = "environment-1:thread-1:branch";
24 const defaults = new Set(["src/app.ts", "src/index.ts"]);
25 useDiffPanelSessionStore.getState().updateCollapsedFileKeys(scopeKey, defaults, (current) => {
26 const next = new Set(current);
27 next.delete("src/app.ts");
28 return next;
29 });
30
31 expect(useDiffPanelSessionStore.getState().collapsedFileKeysByScopeKey[scopeKey]).toEqual(
32 new Set(["src/index.ts"]),
33 );
34
35 useDiffPanelSessionStore.getState().updateCollapsedFileKeys(scopeKey, new Set(), (current) => {
36 const next = new Set(current);
37 next.delete("src/index.ts");
38 return next;
39 });
40 expect(useDiffPanelSessionStore.getState().collapsedFileKeysByScopeKey[scopeKey]).toEqual(
41 new Set(),
42 );
43
44 useDiffPanelSessionStore
45 .getState()
46 .updateCollapsedFileKeys(scopeKey, defaults, (current) => current);
47 expect(useDiffPanelSessionStore.getState().collapsedFileKeysByScopeKey[scopeKey]).toEqual(
48 new Set(),
49 );
50 expect(defaults).toEqual(new Set(["src/app.ts", "src/index.ts"]));
51 });
52
53 it("persists selection while fold changes and no-op unfolds never write to storage", async () => {
54 const { storage, name } = useDiffPanelStore.persist.getOptions();
55 if (!storage || !name) throw new Error("Diff panel persistence storage is unavailable.");
56 const setItem = vi.spyOn(storage, "setItem");
57 try {
58 useDiffPanelStore.getState().selectBranchBaseRef(THREAD_REF, "origin/release");
59 expect(setItem).toHaveBeenCalledOnce();
60 const beforeFolding = await storage.getItem(name);
61 expect(beforeFolding).toMatchObject({
62 state: { byThreadKey: useDiffPanelStore.getState().byThreadKey },
63 });
64 setItem.mockClear();
65
66 const scopeKey = "environment-1:thread-1:branch";
67 useDiffPanelSessionStore
68 .getState()
69 .updateCollapsedFileKeys(scopeKey, new Set(), () => new Set(["src/app.ts"]));
70 useDiffPanelSessionStore
71 .getState()
72 .updateCollapsedFileKeys(scopeKey, new Set(), (current) => current);
73
74 expect(setItem).not.toHaveBeenCalled();
75 const persisted = await storage.getItem(name);
76 expect(persisted).toEqual(beforeFolding);
77 expect(persisted?.state).not.toHaveProperty("collapsedFileKeysByScopeKey");
78 } finally {
79 setItem.mockRestore();
80 }
81 });
82
83 it("defaults each thread to Changes without requiring git status", () => {
84 expect(
85 selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF),
86 ).toEqual({ kind: "branch", baseRef: null });
87 });
88
89 it("defaults to Changes before a thread is selected", () => {
90 expect(selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, null)).toEqual({
91 kind: "branch",
92 baseRef: null,
93 });
94 });
95
96 it("keeps a custom base when a generic open selects Changes again", () => {
97 const store = useDiffPanelStore.getState();
98 store.selectBranchBaseRef(THREAD_REF, "origin/release");
99 store.selectGitScope(THREAD_REF, "branch");
100 store.selectTurn(THREAD_REF, RunId.make("turn-1"));
101 store.selectGitScope(THREAD_REF, "branch");
102
103 expect(
104 selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF),
105 ).toEqual({ kind: "branch", baseRef: "origin/release" });
106 });
107
108 it("preserves an explicit branch selection", () => {
109 useDiffPanelStore.getState().selectGitScope(THREAD_REF, "branch");
110
111 expect(
112 selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF),
113 ).toEqual({ kind: "branch", baseRef: null });
114 });
115
116 it("clears incompatible selection fields when changing scopes", () => {
117 const store = useDiffPanelStore.getState();
118 store.selectTurn(THREAD_REF, RunId.make("turn-1"), "src/app.ts");
119 store.selectGitScope(THREAD_REF, "unstaged");
120
121 expect(
122 selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF),
123 ).toEqual({ kind: "unstaged" });
124
125 useDiffPanelStore.getState().selectBranchBaseRef(THREAD_REF, " origin/main ");
126 expect(
127 selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF),
128 ).toEqual({ kind: "branch", baseRef: "origin/main" });
129 });
130
131 it("clears a thread's turn and file when selecting working tree without changing another thread's branch base", () => {
132 const otherThreadRef = scopeThreadRef(
133 EnvironmentId.make("environment-1"),
134 ThreadId.make("thread-2"),
135 );
136 const store = useDiffPanelStore.getState();
137 store.selectBranchBaseRef(THREAD_REF, "origin/release");
138 store.selectTurn(THREAD_REF, RunId.make("turn-1"), "src/app.ts");
139 store.selectBranchBaseRef(otherThreadRef, "origin/main");
140
141 store.selectGitScope(THREAD_REF, "unstaged");
142
143 const { byThreadKey } = useDiffPanelStore.getState();
144 expect(selectThreadDiffPanelSelection(byThreadKey, THREAD_REF)).toEqual({ kind: "unstaged" });
145 expect(selectThreadDiffPanelSelection(byThreadKey, otherThreadRef)).toEqual({
146 kind: "branch",
147 baseRef: "origin/main",
148 });
149
150 store.selectGitScope(THREAD_REF, "branch");
151 expect(
152 selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF),
153 ).toEqual({ kind: "branch", baseRef: "origin/release" });
154 });
155
156 it("increments the reveal request when opening the same turn file again", () => {
157 const turnId = RunId.make("turn-1");
158 useDiffPanelStore.getState().selectTurn(THREAD_REF, turnId, "src/app.ts");
159 useDiffPanelStore.getState().selectTurn(THREAD_REF, turnId, "src/app.ts");
160
161 expect(
162 selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF),
163 ).toEqual({ kind: "turn", turnId, filePath: "src/app.ts", revealRequestId: 2 });
164 });
165
166 it("restores the selected branch base after visiting another scope", () => {
167 useDiffPanelStore.getState().selectBranchBaseRef(THREAD_REF, "origin/main");
168 useDiffPanelStore.getState().selectGitScope(THREAD_REF, "unstaged");
169 useDiffPanelStore.getState().selectGitScope(THREAD_REF, "branch");
170
171 expect(
172 selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF),
173 ).toEqual({ kind: "branch", baseRef: "origin/main" });
174 });
175
176 it("reconciles a missing turn selection to the latest available turn", () => {
177 const missingTurnId = RunId.make("turn-missing");
178 const latestTurnId = RunId.make("turn-latest");
179 useDiffPanelStore.getState().selectTurn(THREAD_REF, missingTurnId, "src/app.ts");
180 useDiffPanelStore.getState().reconcileTurnSelection(THREAD_REF, [latestTurnId]);
181
182 expect(
183 selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF),
184 ).toEqual({
185 kind: "turn",
186 turnId: latestTurnId,
187 filePath: "src/app.ts",
188 revealRequestId: 1,
189 });
190 });
191});