Open History to see the last commit
1import type { VcsStatusResult } from "@t3tools/contracts";
2import { act } from "react";
3import { createRoot, type Root } from "react-dom/client";
4import { afterEach, describe, expect, it, vi } from "vite-plus/test";
5
6// Native header item factories are only read when options are applied, so the fake
7// navigation snapshots the right items at each setOptions, as native-stack does.
8const harness = vi.hoisted(() => ({
9 setOptionsCalls: 0,
10 renderedRightItems: [] as Array<ReadonlyArray<Record<string, unknown>>>,
11 navigatedRoutes: [] as string[],
12}));
13
14vi.mock("@react-navigation/native", () => {
15 const navigation = {
16 canGoBack: () => true,
17 dispatch: () => {},
18 goBack: () => {},
19 navigate: (route: string) => {
20 harness.navigatedRoutes.push(route);
21 },
22 addListener: () => () => {},
23 setOptions: (options: { unstable_headerRightItems?: () => Array<Record<string, unknown>> }) => {
24 harness.setOptionsCalls += 1;
25 harness.renderedRightItems.push(options.unstable_headerRightItems?.() ?? []);
26 },
27 };
28 return {
29 StackActions: { replace: () => ({}) },
30 useNavigation: () => navigation,
31 };
32});
33vi.mock("../../native/useOpenPullRequest", () => ({ useOpenPullRequest: () => () => {} }));
34
35vi.mock("../../state/session", () => ({ useEnvironmentScope: () => true }));
36vi.mock("../../components/ui/AppBar", async () => {
37 const { NativeStackScreenOptions } = await import("../../native/StackHeader");
38 return {
39 AppBar: (props: import("../../components/ui/AppBar.types").AppBarProps) => (
40 <NativeStackScreenOptions options={props.options} optionsVersion={props.optionsVersion} />
41 ),
42 };
43});
44vi.mock("react-native", () => ({ Alert: { alert: () => {} }, Linking: {}, Platform: {} }));
45vi.mock("../layout/AdaptiveWorkspaceLayout", () => ({
46 useAdaptiveWorkspaceLayout: () => ({
47 layout: { usesSplitView: false },
48 panes: { primarySidebarVisible: false, auxiliaryPaneVisible: false },
49 togglePrimarySidebar: () => {},
50 toggleAuxiliaryPane: () => {},
51 }),
52}));
53vi.mock("../layout/native-mail-search-toolbar", () => ({
54 NATIVE_MAIL_SEARCH_TOOLBAR_SUPPORTED: true,
55 createNativeMailSearchToolbarItem: () => ({}),
56}));
57vi.mock("../settings/appearance/AppearancePreferencesProvider", () => ({
58 useAppearancePreferences: () => ({ themeVariables: {} }),
59}));
60
61import { ThreadHeader } from "./ThreadHeader";
62
63const projectScripts: never[] = [];
64
65function status(
66 files: ReadonlyArray<string>,
67 overrides: Partial<VcsStatusResult> = {},
68): VcsStatusResult {
69 return {
70 isRepo: true,
71 hasPrimaryRemote: false,
72 isDefaultRef: true,
73 refName: "master",
74 hasWorkingTreeChanges: files.length > 0,
75 workingTree: {
76 files: files.map((path) => ({ path, insertions: 0, deletions: 0 })),
77 insertions: 0,
78 deletions: 0,
79 },
80 hasUpstream: false,
81 aheadCount: 0,
82 behindCount: 0,
83 pr: null,
84 ...overrides,
85 } as unknown as VcsStatusResult;
86}
87
88function Header(props: { readonly gitStatus: VcsStatusResult | null; readonly title?: string }) {
89 return (
90 <ThreadHeader
91 title={props.title ?? "Thread"}
92 subtitle=""
93 headerColor="#000"
94 usesNativeHeaderGlass
95 gitControls={{
96 environmentId: "environment-1",
97 threadId: "thread-1",
98 currentBranch: "master",
99 gitStatus: props.gitStatus,
100 gitOperationLabel: null,
101 onPull: async () => {},
102 onRunAction: async () => null,
103 canOpenTerminal: true,
104 canOperateTerminal: true,
105 canOpenFiles: true,
106 projectScripts,
107 terminalSessions: [],
108 onOpenTerminal: () => {},
109 onOpenNewTerminal: () => {},
110 onRunProjectScript: async () => {},
111 }}
112 hasThreadCwd
113 hasWorkspaceRoot
114 fileInspectorSupported={false}
115 inspectorMode={null}
116 onToggleInspector={() => {}}
117 onOpenGitInspector={() => {}}
118 onOpenFilesInspector={() => {}}
119 />
120 );
121}
122
123function renderedGitMenuItems(): Array<{
124 description?: unknown;
125 label?: unknown;
126 onPress?: () => void;
127}> {
128 const items = harness.renderedRightItems.at(-1) ?? [];
129 const git = items.find((item) => item.identifier === "thread-right-git") as
130 | { menu: { items: Array<{ description?: unknown; label?: unknown; onPress?: () => void }> } }
131 | undefined;
132 return git?.menu.items ?? [];
133}
134
135function renderedGitStatusDescription(): unknown {
136 return renderedGitMenuItems()[0]?.description;
137}
138
139let root: Root | null = null;
140
141function render(element: React.ReactElement) {
142 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
143 root ??= createRoot(document.createElement("div"));
144 act(() => root!.render(element));
145}
146
147afterEach(() => {
148 act(() => root?.unmount());
149 root = null;
150 harness.setOptionsCalls = 0;
151 harness.renderedRightItems = [];
152 harness.navigatedRoutes = [];
153});
154
155describe("ThreadHeader", () => {
156 it("re-applies native header items when Git status changes", () => {
157 render(<Header gitStatus={status([])} />);
158 expect(renderedGitStatusDescription()).toBe("Clean");
159
160 render(<Header gitStatus={status(["audit.txt"])} />);
161 expect(renderedGitStatusDescription()).toBe("1 changed");
162
163 render(<Header gitStatus={status([])} />);
164 expect(renderedGitStatusDescription()).toBe("Clean");
165 });
166
167 it("does not re-apply options for re-renders with equivalent header content", () => {
168 render(<Header gitStatus={status([])} />);
169 const applied = harness.setOptionsCalls;
170
171 for (let index = 0; index < 20; index += 1) {
172 render(<Header gitStatus={status([])} />);
173 }
174 expect(harness.setOptionsCalls).toBe(applied);
175 });
176
177 it("re-applies menu actions when the default ref changes behind the same label", async () => {
178 const pushable = (isDefaultRef: boolean) =>
179 status([], {
180 isDefaultRef,
181 aheadCount: 1,
182 hasPrimaryRemote: true,
183 pr: { state: "open", url: "https://example.com/pr/1" },
184 } as Partial<VcsStatusResult>);
185 render(<Header gitStatus={pushable(false)} />);
186 expect(renderedGitMenuItems()[1]?.label).toBe("Push");
187
188 render(<Header gitStatus={pushable(true)} />);
189 expect(renderedGitMenuItems()[1]?.label).toBe("Push");
190 await act(async () => renderedGitMenuItems()[1]?.onPress?.());
191 expect(harness.navigatedRoutes).toEqual(["GitConfirm"]);
192 });
193});