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

serverSettings.test.ts

773 lines27.6 KB

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