Open History to see the last commit
1import {
2 DEFAULT_SERVER_SETTINGS,
3 DEFAULT_UNIFIED_SETTINGS,
4 ProviderDriverKind,
5 ProviderInstanceId,
6 type ProviderInstanceConfig,
7} from "@t3tools/contracts";
8import { getBackgroundActivityPresetSettings } from "@t3tools/shared/backgroundActivitySettings";
9import * as Duration from "effect/Duration";
10import { describe, expect, it } from "vite-plus/test";
11import {
12 backgroundActivitySharedPolicySettings,
13 buildProviderInstanceUpdatePatch,
14 formatDiagnosticsDescription,
15 getChangedBrowserSettingLabels,
16 getChangedTypographySettingLabels,
17 isSamePreviewViewport,
18 hasChangedBackgroundActivitySettings,
19 isProjectGroupingEnabled,
20 projectGroupingModeFromToggle,
21 resolveBackgroundActivityProfileOption,
22} from "./SettingsPanels.logic";
23
24describe("typography settings restore", () => {
25 it("detects family and size changes by font row", () => {
26 expect(getChangedTypographySettingLabels(DEFAULT_UNIFIED_SETTINGS)).toEqual([]);
27 expect(
28 getChangedTypographySettingLabels({
29 ...DEFAULT_UNIFIED_SETTINGS,
30 fontSizeInterface: 18,
31 fontFamilyCode: "Fira Code",
32 }),
33 ).toEqual(["Interface font", "Code font"]);
34 });
35});
36
37describe("background activity settings restore", () => {
38 it("detects legacy interval values even when the structured setting is at its default", () => {
39 expect(
40 hasChangedBackgroundActivitySettings({
41 backgroundActivity: DEFAULT_UNIFIED_SETTINGS.backgroundActivity,
42 backgroundActivityProfile: DEFAULT_UNIFIED_SETTINGS.backgroundActivityProfile,
43 automaticGitFetchInterval: Duration.seconds(45),
44 providerHealthRefreshInterval: DEFAULT_UNIFIED_SETTINGS.providerHealthRefreshInterval,
45 }),
46 ).toBe(true);
47 expect(
48 hasChangedBackgroundActivitySettings({
49 backgroundActivity: DEFAULT_UNIFIED_SETTINGS.backgroundActivity,
50 backgroundActivityProfile: DEFAULT_UNIFIED_SETTINGS.backgroundActivityProfile,
51 automaticGitFetchInterval: DEFAULT_UNIFIED_SETTINGS.automaticGitFetchInterval,
52 providerHealthRefreshInterval: Duration.minutes(7),
53 }),
54 ).toBe(true);
55 expect(hasChangedBackgroundActivitySettings(DEFAULT_UNIFIED_SETTINGS)).toBe(false);
56 });
57
58 it("detects a legacy profile override so restoring defaults clears it", () => {
59 expect(
60 hasChangedBackgroundActivitySettings({
61 ...DEFAULT_UNIFIED_SETTINGS,
62 backgroundActivityProfile: "performance",
63 }),
64 ).toBe(true);
65 });
66
67 it("shows the effective legacy preset and marks custom legacy intervals as advanced", () => {
68 const performance = getBackgroundActivityPresetSettings("performance");
69 expect(
70 resolveBackgroundActivityProfileOption({
71 ...DEFAULT_UNIFIED_SETTINGS,
72 backgroundActivity: DEFAULT_UNIFIED_SETTINGS.backgroundActivity,
73 backgroundActivityProfile: "performance",
74 automaticGitFetchInterval: performance.automaticGitFetchInterval,
75 providerHealthRefreshInterval: performance.providerHealthRefreshInterval,
76 }),
77 ).toBe("performance");
78
79 expect(
80 resolveBackgroundActivityProfileOption({
81 ...DEFAULT_UNIFIED_SETTINGS,
82 backgroundActivity: DEFAULT_UNIFIED_SETTINGS.backgroundActivity,
83 backgroundActivityProfile: "performance",
84 automaticGitFetchInterval: Duration.seconds(45),
85 providerHealthRefreshInterval: Duration.minutes(7),
86 }),
87 ).toBe("advanced");
88 });
89
90 it("preserves advanced overrides when the shared policy changes", () => {
91 const automaticGitFetchInterval = Duration.seconds(42);
92 expect(
93 backgroundActivitySharedPolicySettings(
94 {
95 ...DEFAULT_UNIFIED_SETTINGS,
96 backgroundActivity: {
97 schemaVersion: 1,
98 profile: "custom",
99 baseProfile: "balanced",
100 overrides: {
101 automaticGitFetchInterval,
102 pauseWhenOnBattery: true,
103 },
104 },
105 },
106 "performance",
107 ),
108 ).toEqual({
109 schemaVersion: 1,
110 profile: "custom",
111 baseProfile: "performance",
112 overrides: {
113 automaticGitFetchInterval,
114 pauseWhenOnBattery: true,
115 },
116 });
117 });
118
119 it("materializes legacy advanced overrides before changing the shared policy", () => {
120 const automaticGitFetchInterval = Duration.seconds(42);
121 expect(
122 backgroundActivitySharedPolicySettings(
123 {
124 ...DEFAULT_UNIFIED_SETTINGS,
125 automaticGitFetchInterval,
126 },
127 "battery-saver",
128 ),
129 ).toEqual({
130 schemaVersion: 1,
131 profile: "custom",
132 baseProfile: "battery-saver",
133 overrides: {
134 automaticGitFetchInterval,
135 },
136 });
137 });
138});
139
140describe("project grouping toggle", () => {
141 it("enables repository grouping and disables into separate projects", () => {
142 expect(isProjectGroupingEnabled("repository")).toBe(true);
143 expect(isProjectGroupingEnabled("repository_path")).toBe(true);
144 expect(isProjectGroupingEnabled("separate")).toBe(false);
145 expect(projectGroupingModeFromToggle(true)).toBe("repository");
146 expect(projectGroupingModeFromToggle(false)).toBe("separate");
147 });
148
149 it("restores repository path grouping when the toggle is cycled", () => {
150 expect(projectGroupingModeFromToggle(false, "repository_path")).toBe("separate");
151 expect(projectGroupingModeFromToggle(true, "repository_path")).toBe("repository_path");
152 });
153});
154
155describe("formatDiagnosticsDescription", () => {
156 it("collapses trace and metric URLs that share the same OTEL base path", () => {
157 expect(
158 formatDiagnosticsDescription({
159 localTracingEnabled: true,
160 otlpTracesEnabled: true,
161 otlpTracesUrl: "http://localhost:4318/v1/traces",
162 otlpMetricsEnabled: true,
163 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
164 }),
165 ).toBe("Local trace file. Exporting OTEL to http://localhost:4318/v1/{traces,metrics}.");
166 });
167
168 it("keeps separate trace and metric URLs when their base paths differ", () => {
169 expect(
170 formatDiagnosticsDescription({
171 localTracingEnabled: true,
172 otlpTracesEnabled: true,
173 otlpTracesUrl: "http://localhost:4318/v1/traces",
174 otlpMetricsEnabled: true,
175 otlpMetricsUrl: "http://localhost:9000/v1/metrics",
176 }),
177 ).toBe(
178 "Local trace file. Exporting OTEL traces to http://localhost:4318/v1/traces and metrics to http://localhost:9000/v1/metrics.",
179 );
180 });
181
182 it("omits OTEL text when no exporter is enabled", () => {
183 expect(
184 formatDiagnosticsDescription({
185 localTracingEnabled: true,
186 otlpTracesEnabled: false,
187 otlpMetricsEnabled: false,
188 }),
189 ).toBe("Local trace file.");
190 });
191});
192
193describe("buildProviderInstanceUpdatePatch", () => {
194 it("promotes an edited default provider into providerInstances and resets the legacy provider", () => {
195 const instanceId = ProviderInstanceId.make("codex");
196 const nextInstance = {
197 driver: ProviderDriverKind.make("codex"),
198 enabled: true,
199 config: {
200 binaryPath: "/opt/t3/codex",
201 },
202 } satisfies ProviderInstanceConfig;
203
204 const patch = buildProviderInstanceUpdatePatch({
205 settings: {
206 ...DEFAULT_SERVER_SETTINGS,
207 providers: {
208 ...DEFAULT_SERVER_SETTINGS.providers,
209 codex: {
210 ...DEFAULT_SERVER_SETTINGS.providers.codex,
211 binaryPath: "/legacy/codex",
212 },
213 },
214 },
215 instanceId,
216 instance: nextInstance,
217 driver: ProviderDriverKind.make("codex"),
218 isDefault: true,
219 });
220
221 expect(patch.providerInstances?.[instanceId]).toEqual(nextInstance);
222 expect(patch.providers?.codex).toEqual(DEFAULT_SERVER_SETTINGS.providers.codex);
223 });
224
225 it("updates custom instances without touching legacy provider settings", () => {
226 const instanceId = ProviderInstanceId.make("codex_personal");
227 const nextInstance = {
228 driver: ProviderDriverKind.make("codex"),
229 enabled: true,
230 config: {
231 homePath: "/Users/example/.codex-personal",
232 },
233 } satisfies ProviderInstanceConfig;
234
235 const patch = buildProviderInstanceUpdatePatch({
236 settings: DEFAULT_SERVER_SETTINGS,
237 instanceId,
238 instance: nextInstance,
239 driver: ProviderDriverKind.make("codex"),
240 isDefault: false,
241 });
242
243 expect(patch.providerInstances?.[instanceId]).toEqual(nextInstance);
244 expect(patch.providers).toBeUndefined();
245 });
246});
247
248describe("getChangedBrowserSettingLabels", () => {
249 it("reports nothing for the defaults", () => {
250 expect(getChangedBrowserSettingLabels(DEFAULT_UNIFIED_SETTINGS)).toEqual([]);
251 });
252
253 it("treats a structurally equal viewport as unchanged", () => {
254 // The viewport is a tagged union, so identity comparison would report a
255 // freshly decoded copy of the default as dirty and offer to "restore" it.
256 expect(
257 getChangedBrowserSettingLabels({
258 ...DEFAULT_UNIFIED_SETTINGS,
259 browserDefaultViewport: { ...DEFAULT_UNIFIED_SETTINGS.browserDefaultViewport },
260 }),
261 ).toEqual([]);
262 });
263
264 it("labels each browser default that differs", () => {
265 expect(
266 getChangedBrowserSettingLabels({
267 ...DEFAULT_UNIFIED_SETTINGS,
268 browserDefaultViewport: { _tag: "freeform", width: 900, height: 600 },
269 browserDefaultZoomFactor: 1.5,
270 browserDefaultAppearance: "dark",
271 browserAutoShowFloatingPreview: !DEFAULT_UNIFIED_SETTINGS.browserAutoShowFloatingPreview,
272 }),
273 ).toEqual(["Browser viewport", "Browser zoom", "Browser appearance", "Floating preview"]);
274 });
275});
276
277describe("isSamePreviewViewport", () => {
278 it("separates presets that share a size", () => {
279 // Two presets can agree on width and height and still be different
280 // entries in the picker, so the id has to take part in the comparison.
281 expect(
282 isSamePreviewViewport(
283 { _tag: "preset", width: 390, height: 844, presetId: "iphone-12-pro" },
284 { _tag: "preset", width: 390, height: 844, presetId: "ipad-mini" },
285 ),
286 ).toBe(false);
287 });
288
289 it("separates a freeform viewport from a preset of the same size", () => {
290 expect(
291 isSamePreviewViewport(
292 { _tag: "freeform", width: 390, height: 844 },
293 { _tag: "preset", width: 390, height: 844, presetId: "iphone-12-pro" },
294 ),
295 ).toBe(false);
296 });
297});