Open History to see the last commit
1import { DEFAULT_SERVER_SETTINGS, type ServerSettingsPatch } from "@t3tools/contracts";
2import { AsyncResult } from "effect/reactivity";
3import type { ReactNode } from "react";
4import { act } from "react";
5import { createRoot, type Root } from "react-dom/client";
6import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
7
8const { Children, mocks } = vi.hoisted(() => ({
9 Children: ({ children }: { children?: ReactNode }) => children,
10 mocks: {
11 branchNaming: { onChange: (_patch: ServerSettingsPatch) => {} },
12 updateSettings: vi.fn(),
13 alert: vi.fn(),
14 targets: [] as ReturnType<typeof environment>[],
15 projectSelected: false,
16 },
17}));
18
19vi.mock("react-native", () => ({
20 View: Children,
21 Alert: { alert: mocks.alert },
22 Pressable: ({
23 children,
24 disabled,
25 onPress,
26 accessibilityLabel,
27 }: {
28 children?: ReactNode;
29 disabled?: boolean;
30 onPress: () => void;
31 accessibilityLabel?: string;
32 }) => (
33 <button disabled={disabled} onClick={onPress} aria-label={accessibilityLabel}>
34 {children}
35 </button>
36 ),
37}));
38vi.mock("@effect/atom-react", () => ({
39 useAtomValue: () => AsyncResult.success({}),
40 useAtomSet: () => () => {},
41}));
42vi.mock("../../state/preferences", () => ({
43 mobilePreferencesAtom: {},
44 updateMobilePreferencesAtom: {},
45}));
46vi.mock("./components/AutoSettleDaysField", () => ({ AutoSettleDaysField: () => null }));
47vi.mock("react-native-safe-area-context", () => ({ useSafeAreaInsets: () => ({ bottom: 0 }) }));
48vi.mock("@react-navigation/native", () => ({ useNavigation: () => ({}) }));
49vi.mock("../../state/session", () => ({
50 readEnvironmentScope: () => true,
51 useEnvironmentsWithScope: () => new Set(mocks.targets.map((target) => target.environmentId)),
52}));
53vi.mock("../../state/server", () => ({ serverEnvironment: { updateSettings: {} } }));
54vi.mock("../../state/use-atom-command", () => ({
55 useAtomCommand: () => mocks.updateSettings,
56}));
57vi.mock("../threads/thread-settings-options", () => ({ RUNTIME_MODE_CHOICES: [] }));
58vi.mock("../../components/ui/ScreenScrollView", () => ({ ScreenScrollView: Children }));
59vi.mock("../../components/ui/AppText", () => ({ AppText: Children }));
60vi.mock("../../components/ui/Button", () => ({
61 Button: ({
62 label,
63 disabled,
64 onPress,
65 }: {
66 label: string;
67 disabled?: boolean;
68 onPress: () => void;
69 }) => (
70 <button disabled={disabled} onClick={onPress}>
71 {label}
72 </button>
73 ),
74}));
75vi.mock("./components/SettingsScreen", () => ({ SettingsScreen: Children }));
76vi.mock("./components/SettingsEnvironmentFilterHeader", () => ({
77 AndroidSettingsEnvironmentFilter: () => null,
78 SettingsEnvironmentFilterHeader: () => null,
79}));
80vi.mock("./components/DatabaseStorageNotice", () => ({ DatabaseStorageNotice: () => null }));
81vi.mock("./components/SettingsSection", () => ({ SettingsSection: Children }));
82vi.mock("./components/SettingsChoiceRow", () => ({ SettingsChoiceRow: () => null }));
83vi.mock("./components/SettingsSwitchRow", () => ({
84 SettingsSwitchRow: ({
85 label,
86 value,
87 disabled,
88 onValueChange,
89 }: {
90 label: string;
91 value: boolean | null;
92 disabled?: boolean;
93 onValueChange: (value: boolean) => void;
94 }) => (
95 <button
96 role="switch"
97 aria-checked={value ?? "mixed"}
98 disabled={disabled}
99 onClick={() => onValueChange(!value)}
100 >
101 {label}
102 </button>
103 ),
104}));
105vi.mock("./components/SettingsRow", () => ({ SettingsRow: () => null }));
106vi.mock("./components/BranchNamingSettings", () => ({
107 BranchNamingSettings: (props: { onChange: (patch: ServerSettingsPatch) => void }) => {
108 mocks.branchNaming.onChange = props.onChange;
109 return null;
110 },
111}));
112vi.mock("./settings-environment-filter", () => ({
113 useSettingsEnvironmentFilter: () => ({
114 selectedTargets: mocks.targets,
115 projectGroups: [
116 {
117 key: "project",
118 label: "Project",
119 members: mocks.targets.map((target) => ({
120 project: { environmentId: target.environmentId, id: "project" },
121 })),
122 },
123 ],
124 selectedProjectKey: mocks.projectSelected ? "project" : null,
125 }),
126}));
127
128import { SettingsEnvironmentSourceControlRouteScreen } from "./SettingsServerControlsRouteScreen";
129
130function environment(environmentId: string, label: string) {
131 return {
132 environmentId,
133 label,
134 connection: { phase: "connected" },
135 serverConfig: {
136 settings: {
137 ...DEFAULT_SERVER_SETTINGS,
138 defaultAutoPull: false,
139 sidebarAutoSettleOnMerge: false,
140 },
141 environment: {
142 capabilities: { projectSettingsOverrides: true, threadAutoSettlement: true },
143 },
144 },
145 };
146}
147
148let root: Root;
149let container: HTMLDivElement;
150
151beforeEach(() => {
152 (globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
153 container = document.createElement("div");
154 root = createRoot(container);
155 mocks.targets = [environment("local", "Local")];
156 mocks.projectSelected = false;
157 mocks.alert.mockReset();
158 mocks.updateSettings.mockReset();
159 mocks.updateSettings.mockResolvedValue(AsyncResult.success(undefined));
160});
161
162afterEach(() => {
163 act(() => root.unmount());
164});
165
166describe("SettingsServerControlsRouteScreen", () => {
167 it("sends a draft saved during a pending write once that write settles", async () => {
168 const pending = Promise.withResolvers<AsyncResult.Success<void>>();
169 mocks.updateSettings.mockReturnValueOnce(pending.promise);
170 act(() => root.render(<SettingsEnvironmentSourceControlRouteScreen />));
171
172 act(() => mocks.branchNaming.onChange({ branchNamingMode: "custom" }));
173 act(() => mocks.branchNaming.onChange({ branchNameInstructions: "Use issue IDs." }));
174 expect(mocks.updateSettings).toHaveBeenCalledOnce();
175
176 await act(async () => pending.resolve(AsyncResult.success(undefined)));
177
178 expect(mocks.updateSettings).toHaveBeenCalledTimes(2);
179 expect(mocks.updateSettings).toHaveBeenLastCalledWith({
180 environmentId: "local",
181 input: {
182 patch: { branchNamingMode: "custom", branchNameInstructions: "Use issue IDs." },
183 },
184 });
185 });
186});