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

serverSettings.test.ts

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