Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

SourceControlWritingSettings.test.tsx

221 lines7.7 KB

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