Open History to see the last commit
1import {
2 DEFAULT_SERVER_SETTINGS,
3 ProjectId,
4 ProviderDriverKind,
5 ProviderInstanceId,
6 UsageLimitSourceId,
7 type ServerProvider,
8} from "@t3tools/contracts";
9import * as Duration from "effect/Duration";
10import { describe, expect, it } from "vite-plus/test";
11import { resolveServerBackgroundActivitySettings } from "./backgroundActivitySettings.ts";
12import { createModelSelection } from "./model.ts";
13import { resolveProjectScripts, projectScriptsInheritDefaults } from "./projectScripts.ts";
14import {
15 applyServerSettingsPatch,
16 isModelSelectionProviderEnabled,
17 parsePersistedServerObservabilitySettings,
18 resolveSourceControlWriterModelSelection,
19 resolveProjectAgentBrowserAccess,
20 resolveProjectAutoPull,
21} from "./serverSettings.ts";
22
23describe("serverSettings helpers", () => {
24 it("replaces SSH host lists when saving, editing, and removing hosts", () => {
25 const host = { id: "mini", label: "Mac mini", target: "mini" };
26 const saved = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { deviceHosts: [host] });
27 expect(saved.deviceHosts).toEqual([host]);
28 const replacement = { ...host, target: "other-mini" };
29 const edited = applyServerSettingsPatch(saved, { deviceHosts: [replacement] });
30 expect(edited.deviceHosts).toEqual([replacement]);
31 expect(applyServerSettingsPatch(edited, { deviceHosts: [] }).deviceHosts).toEqual([]);
32 });
33
34 it("inherits actions, preserves existing actions, and supports empty overrides and reset", () => {
35 const project = { id: ProjectId.make("project-actions"), scripts: [] };
36 const action = {
37 id: "check",
38 name: "Check",
39 command: "npm test",
40 icon: "play" as const,
41 runOnWorktreeCreate: false,
42 };
43 const defaults = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
44 defaultProjectScripts: [action],
45 });
46 expect(resolveProjectScripts(defaults, project)).toEqual([action]);
47 expect(projectScriptsInheritDefaults(defaults, project)).toBe(true);
48 const existing = { ...project, scripts: [{ ...action, command: "npm run lint" }] };
49 expect(resolveProjectScripts(defaults, existing)).toEqual(existing.scripts);
50 expect(projectScriptsInheritDefaults(defaults, existing)).toBe(false);
51 const disabled = applyServerSettingsPatch(defaults, {
52 projectScriptOverrides: { [project.id]: [] },
53 });
54 expect(resolveProjectScripts(disabled, project)).toEqual([]);
55 expect(projectScriptsInheritDefaults(disabled, project)).toBe(false);
56 const changedDefault = applyServerSettingsPatch(disabled, {
57 defaultProjectScripts: [{ ...action, command: "npm run build" }],
58 });
59 expect(resolveProjectScripts(changedDefault, project)).toEqual([]);
60 const reset = applyServerSettingsPatch(changedDefault, {
61 projectScriptOverrides: { [project.id]: null },
62 });
63 expect(resolveProjectScripts(reset, existing)).toEqual(changedDefault.defaultProjectScripts);
64 expect(projectScriptsInheritDefaults(reset, existing)).toBe(true);
65 expect(
66 resolveProjectScripts(
67 applyServerSettingsPatch(reset, { defaultProjectScripts: [] }),
68 existing,
69 ),
70 ).toEqual([]);
71 });
72
73 it("preserves other projects' actions when overriding, clearing, or resetting one project", () => {
74 const firstProject = { id: ProjectId.make("first-project"), scripts: [] };
75 const secondProject = { id: ProjectId.make("second-project"), scripts: [] };
76 const defaultAction = {
77 id: "check",
78 name: "Check",
79 command: "npm test",
80 icon: "play" as const,
81 runOnWorktreeCreate: false,
82 };
83 const firstAction = { ...defaultAction, command: "npm run lint" };
84 const secondAction = { ...defaultAction, command: "npm run build" };
85 const firstUpdate = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
86 defaultProjectScripts: [defaultAction],
87 projectScriptOverrides: { [firstProject.id]: [firstAction] },
88 });
89 const secondUpdate = applyServerSettingsPatch(firstUpdate, {
90 projectScriptOverrides: { [secondProject.id]: [secondAction] },
91 });
92 expect(resolveProjectScripts(secondUpdate, firstProject)).toEqual([firstAction]);
93 expect(resolveProjectScripts(secondUpdate, secondProject)).toEqual([secondAction]);
94
95 const cleared = applyServerSettingsPatch(secondUpdate, {
96 projectScriptOverrides: { [firstProject.id]: [] },
97 });
98 expect(resolveProjectScripts(cleared, firstProject)).toEqual([]);
99 expect(resolveProjectScripts(cleared, secondProject)).toEqual([secondAction]);
100
101 const reset = applyServerSettingsPatch(cleared, {
102 projectScriptOverrides: { [firstProject.id]: null },
103 });
104 expect(resolveProjectScripts(reset, { ...firstProject, scripts: [firstAction] })).toEqual([
105 defaultAction,
106 ]);
107 expect(resolveProjectScripts(reset, secondProject)).toEqual([secondAction]);
108 expect(resolveProjectScripts(secondUpdate, firstProject)).toEqual([firstAction]);
109 });
110
111 it("inherits automatic pull while preserving legacy opt-ins and explicit overrides", () => {
112 const projectId = ProjectId.make("project-pull");
113 expect(resolveProjectAutoPull(DEFAULT_SERVER_SETTINGS, projectId, false)).toBe(false);
114 expect(resolveProjectAutoPull(DEFAULT_SERVER_SETTINGS, projectId, true)).toBe(true);
115 const enabled = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { defaultAutoPull: true });
116 expect(resolveProjectAutoPull(enabled, projectId, false)).toBe(true);
117 const overridden = applyServerSettingsPatch(enabled, {
118 projectAutoPullOverrides: { [projectId]: false },
119 });
120 expect(resolveProjectAutoPull(overridden, projectId, true)).toBe(false);
121 const reset = applyServerSettingsPatch(overridden, {
122 projectAutoPullOverrides: { [projectId]: null },
123 });
124 expect(resolveProjectAutoPull(reset, projectId, false)).toBe(true);
125 const disabled = applyServerSettingsPatch(reset, {
126 defaultAutoPull: false,
127 projectAutoPullOverrides: { [projectId]: true },
128 });
129 expect(resolveProjectAutoPull(disabled, projectId, false)).toBe(true);
130 expect(resolveProjectAutoPull(disabled, ProjectId.make("other-project"), false)).toBe(false);
131 });
132
133 it("inherits browser access and restores inheritance when a project override is removed", () => {
134 const projectId = ProjectId.make("project-browser");
135 const otherProjectId = ProjectId.make("other-project");
136 const overridden = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
137 projectAgentBrowserAccessOverrides: { [projectId]: false },
138 });
139 expect(resolveProjectAgentBrowserAccess(overridden, projectId)).toBe(false);
140 expect(resolveProjectAgentBrowserAccess(overridden, otherProjectId)).toBe(true);
141 const reset = applyServerSettingsPatch(overridden, {
142 projectAgentBrowserAccessOverrides: { [projectId]: null },
143 });
144 expect(resolveProjectAgentBrowserAccess(reset, projectId)).toBe(true);
145 const enabled = applyServerSettingsPatch(reset, {
146 enableAgentBrowserAccess: false,
147 projectAgentBrowserAccessOverrides: { [projectId]: true },
148 });
149 expect(resolveProjectAgentBrowserAccess(enabled, projectId)).toBe(true);
150 expect(resolveProjectAgentBrowserAccess(enabled, otherProjectId)).toBe(false);
151 });
152
153 it("preserves other projects' boolean overrides across separate updates and resets", () => {
154 const firstProjectId = ProjectId.make("first-project");
155 const secondProjectId = ProjectId.make("second-project");
156 const firstUpdate = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
157 defaultAutoPull: true,
158 projectAutoPullOverrides: { [firstProjectId]: false },
159 projectAgentBrowserAccessOverrides: { [firstProjectId]: false },
160 });
161 const secondUpdate = applyServerSettingsPatch(firstUpdate, {
162 projectAutoPullOverrides: { [secondProjectId]: false },
163 projectAgentBrowserAccessOverrides: { [secondProjectId]: false },
164 });
165 for (const projectId of [firstProjectId, secondProjectId]) {
166 expect(resolveProjectAutoPull(secondUpdate, projectId, false)).toBe(false);
167 expect(resolveProjectAgentBrowserAccess(secondUpdate, projectId)).toBe(false);
168 }
169
170 const reset = applyServerSettingsPatch(secondUpdate, {
171 projectAutoPullOverrides: { [firstProjectId]: null },
172 projectAgentBrowserAccessOverrides: { [firstProjectId]: null },
173 });
174 expect(resolveProjectAutoPull(reset, firstProjectId, false)).toBe(true);
175 expect(resolveProjectAgentBrowserAccess(reset, firstProjectId)).toBe(true);
176 expect(resolveProjectAutoPull(reset, secondProjectId, false)).toBe(false);
177 expect(resolveProjectAgentBrowserAccess(reset, secondProjectId)).toBe(false);
178 expect(reset.projectAutoPullOverrides[firstProjectId]).toBeUndefined();
179 expect(reset.projectAgentBrowserAccessOverrides[firstProjectId]).toBeUndefined();
180 expect(resolveProjectAutoPull(secondUpdate, firstProjectId, false)).toBe(false);
181 expect(resolveProjectAgentBrowserAccess(secondUpdate, firstProjectId)).toBe(false);
182 });
183
184 it("replaces and clears conversation model defaults without retaining old options", () => {
185 const current = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
186 defaultModelSelection: createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
187 { id: "reasoningEffort", value: "high" },
188 ]),
189 });
190 const selection = createModelSelection(ProviderInstanceId.make("claudeAgent"), "sonnet");
191 const updated = applyServerSettingsPatch(current, { defaultModelSelection: selection });
192 expect(updated.defaultModelSelection).toEqual(selection);
193 expect(
194 applyServerSettingsPatch(updated, { defaultModelSelection: null }).defaultModelSelection,
195 ).toBeNull();
196 });
197
198 it("ignores missing and blank persisted observability URLs", () => {
199 expect(parsePersistedServerObservabilitySettings("{}")).toEqual({
200 otlpTracesUrl: undefined,
201 otlpMetricsUrl: undefined,
202 });
203 expect(
204 parsePersistedServerObservabilitySettings(
205 JSON.stringify({ observability: { otlpTracesUrl: " ", otlpMetricsUrl: "" } }),
206 ),
207 ).toEqual({
208 otlpTracesUrl: undefined,
209 otlpMetricsUrl: undefined,
210 });
211 });
212
213 it("parses lenient persisted settings JSON and trims observability URLs", () => {
214 expect(
215 parsePersistedServerObservabilitySettings(
216 JSON.stringify({
217 observability: {
218 otlpTracesUrl: " http://localhost:4318/v1/traces ",
219 otlpMetricsUrl: " http://localhost:4318/v1/metrics ",
220 },
221 }),
222 ),
223 ).toEqual({
224 otlpTracesUrl: "http://localhost:4318/v1/traces",
225 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
226 });
227 });
228
229 it("falls back cleanly when persisted settings are invalid", () => {
230 expect(parsePersistedServerObservabilitySettings("{")).toEqual({
231 otlpTracesUrl: undefined,
232 otlpMetricsUrl: undefined,
233 });
234 });
235
236 it("replaces text generation selection when provider/model are provided", () => {
237 const current = {
238 ...DEFAULT_SERVER_SETTINGS,
239 textGenerationModelSelection: createModelSelection(
240 ProviderInstanceId.make("codex"),
241 "gpt-5.4-mini",
242 [
243 { id: "reasoningEffort", value: "high" },
244 { id: "fastMode", value: true },
245 ],
246 ),
247 };
248
249 expect(
250 applyServerSettingsPatch(current, {
251 textGenerationModelSelection: {
252 instanceId: ProviderInstanceId.make("codex"),
253 model: "gpt-5.4-mini",
254 },
255 }).textGenerationModelSelection,
256 ).toEqual({
257 instanceId: "codex",
258 model: "gpt-5.4-mini",
259 });
260 });
261
262 it("still deep merges text generation selection when only options are provided", () => {
263 const current = {
264 ...DEFAULT_SERVER_SETTINGS,
265 textGenerationModelSelection: createModelSelection(
266 ProviderInstanceId.make("codex"),
267 "gpt-5.4-mini",
268 [
269 { id: "reasoningEffort", value: "high" },
270 { id: "fastMode", value: true },
271 ],
272 ),
273 };
274
275 expect(
276 applyServerSettingsPatch(current, {
277 textGenerationModelSelection: {
278 options: [{ id: "fastMode", value: false }],
279 },
280 }).textGenerationModelSelection,
281 ).toEqual({
282 instanceId: "codex",
283 model: "gpt-5.4-mini",
284 options: [
285 { id: "reasoningEffort", value: "high" },
286 { id: "fastMode", value: false },
287 ],
288 });
289 });
290
291 it("replaces text generation selection across providers without leaking stale options", () => {
292 const current = {
293 ...DEFAULT_SERVER_SETTINGS,
294 textGenerationModelSelection: createModelSelection(
295 ProviderInstanceId.make("codex"),
296 "gpt-5.4-mini",
297 [
298 { id: "reasoningEffort", value: "high" },
299 { id: "fastMode", value: true },
300 ],
301 ),
302 };
303
304 expect(
305 applyServerSettingsPatch(current, {
306 textGenerationModelSelection: {
307 instanceId: ProviderInstanceId.make("opencode"),
308 model: "openai/gpt-5",
309 },
310 }).textGenerationModelSelection,
311 ).toEqual({
312 instanceId: "opencode",
313 model: "openai/gpt-5",
314 });
315 });
316
317 it("accepts array-based text generation selection patches", () => {
318 expect(
319 applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
320 textGenerationModelSelection: {
321 instanceId: ProviderInstanceId.make("opencode"),
322 model: "openai/gpt-5",
323 options: [
324 { id: "variant", value: "prod" },
325 { id: "agent", value: "build" },
326 ],
327 },
328 }).textGenerationModelSelection,
329 ).toEqual({
330 instanceId: "opencode",
331 model: "openai/gpt-5",
332 options: [
333 { id: "variant", value: "prod" },
334 { id: "agent", value: "build" },
335 ],
336 });
337 });
338
339 it("replaces source control writer selection without retaining stale options", () => {
340 const current = {
341 ...DEFAULT_SERVER_SETTINGS,
342 sourceControlWriterModelSelection: createModelSelection(
343 ProviderInstanceId.make("codex"),
344 "gpt-5.4-mini",
345 [{ id: "reasoningEffort", value: "high" }],
346 ),
347 };
348
349 expect(
350 applyServerSettingsPatch(current, {
351 sourceControlWriterModelSelection: {
352 instanceId: ProviderInstanceId.make("opencode"),
353 model: "openai/gpt-5",
354 },
355 }).sourceControlWriterModelSelection,
356 ).toEqual({
357 instanceId: "opencode",
358 model: "openai/gpt-5",
359 });
360 });
361
362 it("clears source control writer selection with null", () => {
363 const current = {
364 ...DEFAULT_SERVER_SETTINGS,
365 sourceControlWriterModelSelection: createModelSelection(
366 ProviderInstanceId.make("codex"),
367 "gpt-5.4-mini",
368 ),
369 };
370
371 expect(
372 applyServerSettingsPatch(current, {
373 sourceControlWriterModelSelection: null,
374 }).sourceControlWriterModelSelection,
375 ).toBeNull();
376 });
377
378 it("falls back from a disabled source control writer provider without clearing its selection", () => {
379 const instanceId = ProviderInstanceId.make("codex_writer");
380 const sourceControlWriterModelSelection = createModelSelection(instanceId, "gpt-5.4-mini");
381 const settings = {
382 ...DEFAULT_SERVER_SETTINGS,
383 providerInstances: {
384 [instanceId]: {
385 driver: ProviderDriverKind.make("codex"),
386 enabled: false,
387 config: {},
388 },
389 },
390 sourceControlWriterModelSelection,
391 };
392
393 expect(isModelSelectionProviderEnabled(settings, sourceControlWriterModelSelection)).toBe(
394 false,
395 );
396 expect(resolveSourceControlWriterModelSelection(settings)).toBe(
397 settings.textGenerationModelSelection,
398 );
399 expect(settings.sourceControlWriterModelSelection).toBe(sourceControlWriterModelSelection);
400 });
401
402 it("falls back from an unavailable source control writer provider", () => {
403 const instanceId = ProviderInstanceId.make("missing_writer");
404 const sourceControlWriterModelSelection = createModelSelection(instanceId, "missing-model");
405 const settings = {
406 ...DEFAULT_SERVER_SETTINGS,
407 providerInstances: {
408 [instanceId]: {
409 driver: ProviderDriverKind.make("missing-driver"),
410 config: {},
411 },
412 },
413 sourceControlWriterModelSelection,
414 };
415 const unavailableProvider = {
416 instanceId,
417 driver: ProviderDriverKind.make("missing-driver"),
418 enabled: false,
419 installed: false,
420 version: null,
421 status: "disabled",
422 auth: { status: "unknown" },
423 checkedAt: "2026-07-27T00:00:00.000Z",
424 availability: "unavailable",
425 unavailableReason: "This provider driver is not available in this build.",
426 models: [],
427 slashCommands: [],
428 skills: [],
429 } satisfies ServerProvider;
430
431 expect(resolveSourceControlWriterModelSelection(settings, [unavailableProvider])).toBe(
432 settings.textGenerationModelSelection,
433 );
434 expect(settings.sourceControlWriterModelSelection).toBe(sourceControlWriterModelSelection);
435 });
436
437 it("falls back from a writer provider that cannot generate application text", () => {
438 const instanceId = ProviderInstanceId.make("acp_writer");
439 const sourceControlWriterModelSelection = createModelSelection(instanceId, "default");
440 const settings = {
441 ...DEFAULT_SERVER_SETTINGS,
442 providerInstances: {
443 [instanceId]: {
444 driver: ProviderDriverKind.make("acpRegistry"),
445 enabled: true,
446 config: {},
447 },
448 },
449 sourceControlWriterModelSelection,
450 };
451 const incapableProvider = {
452 instanceId,
453 driver: ProviderDriverKind.make("acpRegistry"),
454 supportsTextGeneration: false,
455 enabled: true,
456 installed: true,
457 version: null,
458 status: "ready",
459 auth: { status: "authenticated" },
460 checkedAt: "2026-07-27T00:00:00.000Z",
461 models: [],
462 slashCommands: [],
463 skills: [],
464 } satisfies ServerProvider;
465
466 expect(resolveSourceControlWriterModelSelection(settings, [incapableProvider])).toBe(
467 settings.textGenerationModelSelection,
468 );
469 expect(settings.sourceControlWriterModelSelection).toBe(sourceControlWriterModelSelection);
470 });
471
472 it("replaces providerInstances maps so omitted instance fields are cleared", () => {
473 const codexId = ProviderInstanceId.make("codex");
474 const current = {
475 ...DEFAULT_SERVER_SETTINGS,
476 providerInstances: {
477 [codexId]: {
478 driver: ProviderDriverKind.make("codex"),
479 displayName: "Codex Work",
480 accentColor: "#7c3aed",
481 enabled: true,
482 config: { homePath: "~/.codex" },
483 },
484 },
485 };
486
487 expect(
488 applyServerSettingsPatch(current, {
489 providerInstances: {
490 [codexId]: {
491 driver: ProviderDriverKind.make("codex"),
492 displayName: "Codex Work",
493 enabled: true,
494 config: { homePath: "~/.codex" },
495 },
496 },
497 }).providerInstances[codexId],
498 ).toEqual({
499 driver: ProviderDriverKind.make("codex"),
500 displayName: "Codex Work",
501 enabled: true,
502 config: { homePath: "~/.codex" },
503 });
504 });
505
506 it("upserts and removes usageLimitSources per entry so concurrent edits cannot clobber", () => {
507 const hubA = UsageLimitSourceId.make("cliproxy-a");
508 const hubB = UsageLimitSourceId.make("cliproxy-b");
509 const source = (url: string) => ({
510 kind: "cliproxy" as const,
511 url,
512 managementKey: "secret",
513 enabled: true,
514 });
515 const current = {
516 ...DEFAULT_SERVER_SETTINGS,
517 usageLimitSources: { [hubA]: source("http://a:8318") },
518 };
519
520 const added = applyServerSettingsPatch(current, {
521 usageLimitSources: { [hubB]: source("http://b:8318") },
522 });
523 expect(Object.keys(added.usageLimitSources)).toEqual([hubA, hubB]);
524
525 const removed = applyServerSettingsPatch(added, { usageLimitSources: { [hubA]: null } });
526 expect(Object.keys(removed.usageLimitSources)).toEqual([hubB]);
527 });
528
529 it("replaces and removes individual usage prices without clobbering other models", () => {
530 const prices = { inputCostPerMillionTokens: 2, outputCostPerMillionTokens: 8 };
531 const current = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
532 usagePriceOverrides: { "example-model": { ...prices, cacheReadCostPerMillionTokens: 0.5 } },
533 });
534 const added = applyServerSettingsPatch(current, {
535 usagePriceOverrides: { "other-model": prices },
536 });
537 const replaced = applyServerSettingsPatch(added, {
538 usagePriceOverrides: { "example-model": prices },
539 });
540 expect(replaced.usagePriceOverrides).toEqual({
541 "example-model": prices,
542 "other-model": prices,
543 });
544 const removed = applyServerSettingsPatch(replaced, {
545 usagePriceOverrides: { "example-model": null },
546 });
547 expect(removed.usagePriceOverrides).toEqual({ "other-model": prices });
548 expect(current.usagePriceOverrides["example-model"]?.cacheReadCostPerMillionTokens).toBe(0.5);
549 });
550
551 it("stores background activity profiles as a versioned object and syncs legacy aliases", () => {
552 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
553 backgroundActivity: {
554 schemaVersion: 1,
555 profile: "battery-saver",
556 overrides: {},
557 },
558 });
559
560 expect(next.backgroundActivity).toEqual({
561 schemaVersion: 1,
562 profile: "battery-saver",
563 overrides: {},
564 });
565 expect(next.backgroundActivityProfile).toBe("battery-saver");
566 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(0);
567 expect(Duration.toMillis(next.providerHealthRefreshInterval)).toBe(
568 Duration.toMillis(Duration.minutes(15)),
569 );
570 });
571
572 it("turns legacy interval patches into custom background activity overrides", () => {
573 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
574 automaticGitFetchInterval: Duration.seconds(15),
575 });
576
577 expect(next.backgroundActivity).toEqual({
578 schemaVersion: 1,
579 profile: "custom",
580 baseProfile: "balanced",
581 overrides: {
582 automaticGitFetchInterval: Duration.seconds(15),
583 },
584 });
585 expect(resolveServerBackgroundActivitySettings(next).profile).toBe("balanced");
586 expect(
587 Duration.toMillis(resolveServerBackgroundActivitySettings(next).automaticGitFetchInterval),
588 ).toBe(15_000);
589 });
590
591 it("preserves legacy background activity settings when applying an unrelated patch", () => {
592 const current = {
593 ...DEFAULT_SERVER_SETTINGS,
594 backgroundActivityProfile: "performance" as const,
595 automaticGitFetchInterval: Duration.seconds(7),
596 providerHealthRefreshInterval: Duration.minutes(4),
597 };
598
599 const next = applyServerSettingsPatch(current, {
600 sourceControlWriterModelSelection: createModelSelection(
601 ProviderInstanceId.make("codex"),
602 "gpt-5.4-mini",
603 ),
604 });
605
606 expect(next.backgroundActivity).toEqual({
607 schemaVersion: 1,
608 profile: "custom",
609 baseProfile: "performance",
610 overrides: {
611 automaticGitFetchInterval: Duration.seconds(7),
612 providerHealthRefreshInterval: Duration.minutes(4),
613 },
614 });
615 expect(next.backgroundActivityProfile).toBe("performance");
616 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(7_000);
617 expect(Duration.toMillis(next.providerHealthRefreshInterval)).toBe(240_000);
618 });
619
620 it("does not reactivate dormant overrides from a concrete profile", () => {
621 const current = {
622 ...DEFAULT_SERVER_SETTINGS,
623 backgroundActivity: {
624 schemaVersion: 1 as const,
625 profile: "battery-saver" as const,
626 overrides: {
627 providerHealthRefreshInterval: Duration.seconds(5),
628 },
629 },
630 };
631
632 const next = applyServerSettingsPatch(current, {
633 automaticGitFetchInterval: Duration.seconds(15),
634 });
635
636 expect(next.backgroundActivity).toEqual({
637 schemaVersion: 1,
638 profile: "custom",
639 baseProfile: "battery-saver",
640 overrides: {
641 automaticGitFetchInterval: Duration.seconds(15),
642 },
643 });
644 });
645
646 it("prefers structured background activity settings over legacy aliases", () => {
647 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
648 backgroundActivity: {
649 schemaVersion: 1,
650 profile: "battery-saver",
651 overrides: {},
652 },
653 automaticGitFetchInterval: Duration.seconds(5),
654 backgroundActivityProfile: "performance",
655 });
656
657 expect(next.backgroundActivity).toEqual({
658 schemaVersion: 1,
659 profile: "battery-saver",
660 overrides: {},
661 });
662 expect(next.backgroundActivityProfile).toBe("battery-saver");
663 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(0);
664 });
665
666 it("reconciles custom background activity back to a preset when overrides match the preset", () => {
667 const custom = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
668 automaticGitFetchInterval: Duration.seconds(15),
669 });
670 const next = applyServerSettingsPatch(custom, {
671 automaticGitFetchInterval: Duration.seconds(30),
672 });
673
674 expect(next.backgroundActivity).toEqual({
675 schemaVersion: 1,
676 profile: "balanced",
677 overrides: {},
678 });
679 expect(next.backgroundActivityProfile).toBe("balanced");
680 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(30_000);
681 });
682
683 it("drops custom overrides that duplicate the base profile", () => {
684 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
685 backgroundActivity: {
686 schemaVersion: 1,
687 profile: "custom",
688 baseProfile: "balanced",
689 overrides: {
690 automaticGitFetchInterval: Duration.seconds(30),
691 },
692 },
693 });
694
695 expect(next.backgroundActivity).toEqual({
696 schemaVersion: 1,
697 profile: "balanced",
698 overrides: {},
699 });
700 });
701
702 it("replaces the complete background override record", () => {
703 const current = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
704 backgroundActivity: {
705 schemaVersion: 1,
706 profile: "custom",
707 baseProfile: "balanced",
708 overrides: {
709 automaticGitFetchInterval: Duration.seconds(15),
710 providerHealthRefreshInterval: Duration.minutes(3),
711 },
712 },
713 });
714
715 const next = applyServerSettingsPatch(current, {
716 backgroundActivity: {
717 overrides: {
718 automaticGitFetchInterval: Duration.seconds(10),
719 },
720 },
721 });
722
723 expect(next.backgroundActivity).toEqual({
724 schemaVersion: 1,
725 profile: "custom",
726 baseProfile: "balanced",
727 overrides: {
728 automaticGitFetchInterval: Duration.seconds(10),
729 },
730 });
731 });
732
733 it("keeps interval overrides supplied with a profile patch", () => {
734 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
735 backgroundActivityProfile: "performance",
736 automaticGitFetchInterval: Duration.seconds(0),
737 providerHealthRefreshInterval: Duration.minutes(4),
738 });
739
740 expect(next.backgroundActivity).toEqual({
741 schemaVersion: 1,
742 profile: "custom",
743 baseProfile: "performance",
744 overrides: {
745 automaticGitFetchInterval: Duration.seconds(0),
746 providerHealthRefreshInterval: Duration.minutes(4),
747 },
748 });
749 });
750
751 it("ignores overrides attached to a concrete background profile", () => {
752 const resolved = resolveServerBackgroundActivitySettings({
753 ...DEFAULT_SERVER_SETTINGS,
754 backgroundActivity: {
755 schemaVersion: 1,
756 profile: "balanced",
757 overrides: {
758 pauseWhenOnBattery: true,
759 },
760 },
761 });
762
763 expect(resolved.pauseWhenOnBattery).toBe(false);
764 });
765});