Open History to see the last commit
1import { DEFAULT_UNIFIED_SETTINGS } from "@t3tools/contracts/settings";
2import { act, StrictMode, type ReactNode } from "react";
3import { create, type ReactTestRenderer } from "react-test-renderer";
4import { afterEach, beforeEach, describe, expect, it, vi } from "vite-plus/test";
5
6import type { ScopedSettingsPatch } from "./scopedSettings";
7
8type WritingStyle = typeof DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle;
9const state = vi.hoisted(() => ({
10 styles: [] as WritingStyle[],
11 updateSettings: vi.fn<(patch: ScopedSettingsPatch) => void>(),
12}));
13
14vi.mock("@tanstack/react-router", () => ({ useNavigate: () => vi.fn() }));
15vi.mock("./useScopedSettings", () => ({
16 useScopedSettings: () => ({
17 ...DEFAULT_UNIFIED_SETTINGS,
18 sourceControlWritingStyle: state.styles[0],
19 }),
20 useScopedSettingsMixed: (keys: ReadonlyArray<string>) =>
21 !keys.includes("reviewGuidesEnabled") &&
22 JSON.stringify(state.styles[0]) !== JSON.stringify(state.styles[1]),
23 useUpdateScopedSettings: () => state.updateSettings,
24}));
25vi.mock("./SettingsScopeContext", () => ({
26 useSettingsScope: () => ({
27 scope: { kind: "all", environmentIds: [] },
28 environment: null,
29 connectedEnvironments: [],
30 targets: state.styles.map((style) => ({
31 settings: { ...DEFAULT_UNIFIED_SETTINGS, sourceControlWritingStyle: style },
32 })),
33 }),
34}));
35vi.mock("./useScopedModelAvailability", () => ({
36 useScopedModelDisabledReason: () => () => null,
37}));
38vi.mock("../ui/toast", () => ({ toastManager: { add: vi.fn() } }));
39vi.mock("../../state/server", () => ({ EMPTY_SERVER_PROVIDERS: [] }));
40vi.mock("../chat/ProviderModelPicker", () => ({ ProviderModelPicker: () => null }));
41vi.mock("./settingsSearch", () => ({ searchableSetting: (id: string) => ({ id, title: id }) }));
42vi.mock("./settingsLayout", () => ({
43 SETTINGS_PICKER_TRIGGER_CLASSNAME: "",
44 SettingResetButton: ({ label, onClick }: { label: string; onClick: () => void }) => (
45 <button onClick={onClick}>{`Reset ${label}`}</button>
46 ),
47 SettingsSection: ({ children }: { children: ReactNode }) => children,
48 SettingsRow: ({
49 children,
50 control,
51 resetAction,
52 }: {
53 children: ReactNode;
54 control: ReactNode;
55 resetAction: ReactNode;
56 }) => (
57 <div>
58 {control}
59 {resetAction}
60 {children}
61 </div>
62 ),
63}));
64vi.mock("../ui/select", () => ({
65 Select: ({ children }: { children: ReactNode }) => children,
66 SelectItem: "span",
67 SelectPopup: "div",
68 SelectTrigger: "div",
69 SelectValue: "span",
70}));
71vi.mock("../ui/switch", () => ({ Switch: "input" }));
72vi.mock("../ui/textarea", () => ({ Textarea: "textarea" }));
73vi.mock("../ui/button", () => ({ Button: "button" }));
74
75import { SourceControlWritingSettingsSection } from "./SourceControlWritingSettings";
76
77let renderer: ReactTestRenderer | null;
78
79function button(label: string) {
80 return renderer!.root.findAllByType("button").find((item) => item.children.includes(label))!;
81}
82
83function openEditor() {
84 act(() => button("Write custom instructions for all").props.onClick());
85}
86
87function editInstructions(value: string) {
88 act(() => renderer!.root.findByType("textarea").props.onChange({ target: { value } }));
89}
90
91function applyInstructions() {
92 act(() => button("Apply instructions to all").props.onClick());
93}
94
95beforeEach(() => {
96 vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
97 state.styles = [
98 {
99 ...DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle,
100 mode: "custom",
101 customInstructions: "First environment instructions",
102 followChangeRequestTemplates: true,
103 },
104 {
105 ...DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle,
106 mode: "custom",
107 customInstructions: "Second environment instructions",
108 followChangeRequestTemplates: false,
109 },
110 ];
111 state.updateSettings.mockReset().mockImplementation((patch) => {
112 state.styles = state.styles.map((style) => ({ ...style, ...patch.sourceControlWritingStyle }));
113 });
114 act(() => {
115 renderer = create(
116 <StrictMode>
117 <SourceControlWritingSettingsSection />
118 </StrictMode>,
119 );
120 });
121});
122
123afterEach(async () => {
124 await act(async () => renderer?.unmount());
125 renderer = null;
126 vi.unstubAllGlobals();
127});
128
129describe("mixed source control instructions", () => {
130 it("resets mixed template preferences without replacing each environment's instructions", () => {
131 const initialInstructions = state.styles.map(({ mode, customInstructions }) => ({
132 mode,
133 customInstructions,
134 }));
135
136 act(() => button("Reset change request templates").props.onClick());
137
138 expect(state.updateSettings).toHaveBeenCalledTimes(1);
139 expect(state.styles.map((style) => style.followChangeRequestTemplates)).toEqual([
140 DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle.followChangeRequestTemplates,
141 DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle.followChangeRequestTemplates,
142 ]);
143 expect(
144 state.styles.map(({ mode, customInstructions }) => ({ mode, customInstructions })),
145 ).toEqual(initialInstructions);
146 });
147
148 it("resets every environment even when the representative already has default instructions", () => {
149 state.styles[0] = { ...DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle };
150 act(() => {
151 renderer!.update(
152 <StrictMode>
153 <SourceControlWritingSettingsSection />
154 </StrictMode>,
155 );
156 });
157
158 act(() => button("Reset source control writing style").props.onClick());
159
160 expect(state.updateSettings).toHaveBeenCalledTimes(1);
161 expect(
162 state.styles.map(({ mode, customInstructions }) => ({ mode, customInstructions })),
163 ).toEqual(
164 [0, 1].map(() => ({
165 mode: DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle.mode,
166 customInstructions: DEFAULT_UNIFIED_SETTINGS.sourceControlWritingStyle.customInstructions,
167 })),
168 );
169 expect(state.styles[1]!.followChangeRequestTemplates).toBe(false);
170 });
171
172 it("does not write an untouched bulk draft", () => {
173 const initialStyles = state.styles;
174 openEditor();
175 expect(renderer!.root.findByType("textarea").props.value).toBe("");
176 expect(button("Apply instructions to all").props.disabled).toBe(true);
177
178 applyInstructions();
179 expect(state.updateSettings).not.toHaveBeenCalled();
180 expect(state.styles).toEqual(initialStyles);
181 });
182
183 it("applies edited instructions to every selected environment", () => {
184 openEditor();
185 editInstructions(" Keep titles concise. ");
186 expect(button("Apply instructions to all").props.disabled).toBe(false);
187 applyInstructions();
188
189 expect(state.updateSettings).toHaveBeenCalledTimes(1);
190 expect(state.styles.map((style) => style.customInstructions)).toEqual([
191 "Keep titles concise.",
192 "Keep titles concise.",
193 ]);
194 expect(state.styles.map((style) => style.followChangeRequestTemplates)).toEqual([true, false]);
195 });
196
197 it("allows an intentional clear after editing", () => {
198 openEditor();
199 editInstructions("Temporary instructions");
200 editInstructions("");
201 expect(button("Apply instructions to all").props.disabled).toBe(false);
202 applyInstructions();
203
204 expect(state.updateSettings).toHaveBeenCalledTimes(1);
205 expect(state.styles.map((style) => style.customInstructions)).toEqual(["", ""]);
206 });
207
208 it("shows the plain editor once instructions agree, even while templates differ", () => {
209 openEditor();
210 editInstructions("Shared instructions");
211 applyInstructions();
212
213 expect(state.updateSettings).toHaveBeenCalledTimes(1);
214 expect(state.styles.map((style) => style.customInstructions)).toEqual([
215 "Shared instructions",
216 "Shared instructions",
217 ]);
218 expect(button("Write custom instructions for all")).toBeUndefined();
219 expect(renderer!.root.findByType("textarea").props.defaultValue).toBe("Shared instructions");
220 });
221});