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