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