Open History to see the last commit
1import { scopeThreadRef } from "@t3tools/client-runtime/environment";
2import { EnvironmentId, ThreadId, TurnId } from "@t3tools/contracts";
3import { beforeEach, describe, expect, it } from "vite-plus/test";
4
5import { selectThreadDiffPanelSelection, useDiffPanelStore } from "./diffPanelStore";
6
7const THREAD_REF = scopeThreadRef(EnvironmentId.make("environment-1"), ThreadId.make("thread-1"));
8
9describe("diffPanelStore", () => {
10 beforeEach(() => useDiffPanelStore.setState({ byThreadKey: {}, branchBaseRefByThreadKey: {} }));
11
12 it("defaults each thread to branch changes when the working tree is clean", () => {
13 expect(
14 selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF),
15 ).toEqual({ kind: "branch", baseRef: null });
16 });
17
18 it("defaults each thread to working changes when the working tree is dirty", () => {
19 expect(
20 selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF, true),
21 ).toEqual({ kind: "unstaged" });
22 });
23
24 it("preserves an explicit scope selection when the working tree state changes", () => {
25 useDiffPanelStore.getState().selectGitScope(THREAD_REF, "branch");
26
27 expect(
28 selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF, true),
29 ).toEqual({ kind: "branch", baseRef: null });
30 });
31
32 it("clears incompatible selection fields when changing scopes", () => {
33 const store = useDiffPanelStore.getState();
34 store.selectTurn(THREAD_REF, TurnId.make("turn-1"), "src/app.ts");
35 store.selectGitScope(THREAD_REF, "unstaged");
36
37 expect(
38 selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF),
39 ).toEqual({ kind: "unstaged" });
40
41 useDiffPanelStore.getState().selectBranchBaseRef(THREAD_REF, " origin/main ");
42 expect(
43 selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF),
44 ).toEqual({ kind: "branch", baseRef: "origin/main" });
45 });
46
47 it("increments the reveal request when opening the same turn file again", () => {
48 const turnId = TurnId.make("turn-1");
49 useDiffPanelStore.getState().selectTurn(THREAD_REF, turnId, "src/app.ts");
50 useDiffPanelStore.getState().selectTurn(THREAD_REF, turnId, "src/app.ts");
51
52 expect(
53 selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF),
54 ).toEqual({ kind: "turn", turnId, filePath: "src/app.ts", revealRequestId: 2 });
55 });
56
57 it("restores the selected branch base after visiting another scope", () => {
58 useDiffPanelStore.getState().selectBranchBaseRef(THREAD_REF, "origin/main");
59 useDiffPanelStore.getState().selectGitScope(THREAD_REF, "unstaged");
60 useDiffPanelStore.getState().selectGitScope(THREAD_REF, "branch");
61
62 expect(
63 selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF),
64 ).toEqual({ kind: "branch", baseRef: "origin/main" });
65 });
66
67 it("reconciles a missing turn selection to the latest available turn", () => {
68 const missingTurnId = TurnId.make("turn-missing");
69 const latestTurnId = TurnId.make("turn-latest");
70 useDiffPanelStore.getState().selectTurn(THREAD_REF, missingTurnId, "src/app.ts");
71 useDiffPanelStore.getState().reconcileTurnSelection(THREAD_REF, [latestTurnId]);
72
73 expect(
74 selectThreadDiffPanelSelection(useDiffPanelStore.getState().byThreadKey, THREAD_REF),
75 ).toEqual({
76 kind: "turn",
77 turnId: latestTurnId,
78 filePath: "src/app.ts",
79 revealRequestId: 1,
80 });
81 });
82});