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

serverSettings.test.ts

730 lines26.0 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​
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("replaces providerInstances maps so omitted instance fields are cleared", () => {
438 const codexId = ProviderInstanceId.make("codex");
439 const current = {
440 ...DEFAULT_SERVER_SETTINGS,
441 providerInstances: {
442 [codexId]: {
443 driver: ProviderDriverKind.make("codex"),
444 displayName: "Codex Work",
445 accentColor: "#7c3aed",
446 enabled: true,
447 config: { homePath: "~/.codex" },
448 },
449 },
450 };
451​
452 expect(
453 applyServerSettingsPatch(current, {
454 providerInstances: {
455 [codexId]: {
456 driver: ProviderDriverKind.make("codex"),
457 displayName: "Codex Work",
458 enabled: true,
459 config: { homePath: "~/.codex" },
460 },
461 },
462 }).providerInstances[codexId],
463 ).toEqual({
464 driver: ProviderDriverKind.make("codex"),
465 displayName: "Codex Work",
466 enabled: true,
467 config: { homePath: "~/.codex" },
468 });
469 });
470​
471 it("upserts and removes usageLimitSources per entry so concurrent edits cannot clobber", () => {
472 const hubA = UsageLimitSourceId.make("cliproxy-a");
473 const hubB = UsageLimitSourceId.make("cliproxy-b");
474 const source = (url: string) => ({
475 kind: "cliproxy" as const,
476 url,
477 managementKey: "secret",
478 enabled: true,
479 });
480 const current = {
481 ...DEFAULT_SERVER_SETTINGS,
482 usageLimitSources: { [hubA]: source("http://a:8318") },
483 };
484​
485 const added = applyServerSettingsPatch(current, {
486 usageLimitSources: { [hubB]: source("http://b:8318") },
487 });
488 expect(Object.keys(added.usageLimitSources)).toEqual([hubA, hubB]);
489​
490 const removed = applyServerSettingsPatch(added, { usageLimitSources: { [hubA]: null } });
491 expect(Object.keys(removed.usageLimitSources)).toEqual([hubB]);
492 });
493​
494 it("replaces and removes individual usage prices without clobbering other models", () => {
495 const prices = { inputCostPerMillionTokens: 2, outputCostPerMillionTokens: 8 };
496 const current = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
497 usagePriceOverrides: { "example-model": { ...prices, cacheReadCostPerMillionTokens: 0.5 } },
498 });
499 const added = applyServerSettingsPatch(current, {
500 usagePriceOverrides: { "other-model": prices },
501 });
502 const replaced = applyServerSettingsPatch(added, {
503 usagePriceOverrides: { "example-model": prices },
504 });
505 expect(replaced.usagePriceOverrides).toEqual({
506 "example-model": prices,
507 "other-model": prices,
508 });
509 const removed = applyServerSettingsPatch(replaced, {
510 usagePriceOverrides: { "example-model": null },
511 });
512 expect(removed.usagePriceOverrides).toEqual({ "other-model": prices });
513 expect(current.usagePriceOverrides["example-model"]?.cacheReadCostPerMillionTokens).toBe(0.5);
514 });
515​
516 it("stores background activity profiles as a versioned object and syncs legacy aliases", () => {
517 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
518 backgroundActivity: {
519 schemaVersion: 1,
520 profile: "battery-saver",
521 overrides: {},
522 },
523 });
524​
525 expect(next.backgroundActivity).toEqual({
526 schemaVersion: 1,
527 profile: "battery-saver",
528 overrides: {},
529 });
530 expect(next.backgroundActivityProfile).toBe("battery-saver");
531 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(0);
532 expect(Duration.toMillis(next.providerHealthRefreshInterval)).toBe(
533 Duration.toMillis(Duration.minutes(15)),
534 );
535 });
536​
537 it("turns legacy interval patches into custom background activity overrides", () => {
538 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
539 automaticGitFetchInterval: Duration.seconds(15),
540 });
541​
542 expect(next.backgroundActivity).toEqual({
543 schemaVersion: 1,
544 profile: "custom",
545 baseProfile: "balanced",
546 overrides: {
547 automaticGitFetchInterval: Duration.seconds(15),
548 },
549 });
550 expect(resolveServerBackgroundActivitySettings(next).profile).toBe("balanced");
551 expect(
552 Duration.toMillis(resolveServerBackgroundActivitySettings(next).automaticGitFetchInterval),
553 ).toBe(15_000);
554 });
555​
556 it("preserves legacy background activity settings when applying an unrelated patch", () => {
557 const current = {
558 ...DEFAULT_SERVER_SETTINGS,
559 backgroundActivityProfile: "performance" as const,
560 automaticGitFetchInterval: Duration.seconds(7),
561 providerHealthRefreshInterval: Duration.minutes(4),
562 };
563​
564 const next = applyServerSettingsPatch(current, {
565 sourceControlWriterModelSelection: createModelSelection(
566 ProviderInstanceId.make("codex"),
567 "gpt-5.4-mini",
568 ),
569 });
570​
571 expect(next.backgroundActivity).toEqual({
572 schemaVersion: 1,
573 profile: "custom",
574 baseProfile: "performance",
575 overrides: {
576 automaticGitFetchInterval: Duration.seconds(7),
577 providerHealthRefreshInterval: Duration.minutes(4),
578 },
579 });
580 expect(next.backgroundActivityProfile).toBe("performance");
581 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(7_000);
582 expect(Duration.toMillis(next.providerHealthRefreshInterval)).toBe(240_000);
583 });
584​
585 it("does not reactivate dormant overrides from a concrete profile", () => {
586 const current = {
587 ...DEFAULT_SERVER_SETTINGS,
588 backgroundActivity: {
589 schemaVersion: 1 as const,
590 profile: "battery-saver" as const,
591 overrides: {
592 providerHealthRefreshInterval: Duration.seconds(5),
593 },
594 },
595 };
596​
597 const next = applyServerSettingsPatch(current, {
598 automaticGitFetchInterval: Duration.seconds(15),
599 });
600​
601 expect(next.backgroundActivity).toEqual({
602 schemaVersion: 1,
603 profile: "custom",
604 baseProfile: "battery-saver",
605 overrides: {
606 automaticGitFetchInterval: Duration.seconds(15),
607 },
608 });
609 });
610​
611 it("prefers structured background activity settings over legacy aliases", () => {
612 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
613 backgroundActivity: {
614 schemaVersion: 1,
615 profile: "battery-saver",
616 overrides: {},
617 },
618 automaticGitFetchInterval: Duration.seconds(5),
619 backgroundActivityProfile: "performance",
620 });
621​
622 expect(next.backgroundActivity).toEqual({
623 schemaVersion: 1,
624 profile: "battery-saver",
625 overrides: {},
626 });
627 expect(next.backgroundActivityProfile).toBe("battery-saver");
628 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(0);
629 });
630​
631 it("reconciles custom background activity back to a preset when overrides match the preset", () => {
632 const custom = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
633 automaticGitFetchInterval: Duration.seconds(15),
634 });
635 const next = applyServerSettingsPatch(custom, {
636 automaticGitFetchInterval: Duration.seconds(30),
637 });
638​
639 expect(next.backgroundActivity).toEqual({
640 schemaVersion: 1,
641 profile: "balanced",
642 overrides: {},
643 });
644 expect(next.backgroundActivityProfile).toBe("balanced");
645 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(30_000);
646 });
647​
648 it("drops custom overrides that duplicate the base profile", () => {
649 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
650 backgroundActivity: {
651 schemaVersion: 1,
652 profile: "custom",
653 baseProfile: "balanced",
654 overrides: {
655 automaticGitFetchInterval: Duration.seconds(30),
656 },
657 },
658 });
659​
660 expect(next.backgroundActivity).toEqual({
661 schemaVersion: 1,
662 profile: "balanced",
663 overrides: {},
664 });
665 });
666​
667 it("replaces the complete background override record", () => {
668 const current = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
669 backgroundActivity: {
670 schemaVersion: 1,
671 profile: "custom",
672 baseProfile: "balanced",
673 overrides: {
674 automaticGitFetchInterval: Duration.seconds(15),
675 providerHealthRefreshInterval: Duration.minutes(3),
676 },
677 },
678 });
679​
680 const next = applyServerSettingsPatch(current, {
681 backgroundActivity: {
682 overrides: {
683 automaticGitFetchInterval: Duration.seconds(10),
684 },
685 },
686 });
687​
688 expect(next.backgroundActivity).toEqual({
689 schemaVersion: 1,
690 profile: "custom",
691 baseProfile: "balanced",
692 overrides: {
693 automaticGitFetchInterval: Duration.seconds(10),
694 },
695 });
696 });
697​
698 it("keeps interval overrides supplied with a profile patch", () => {
699 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
700 backgroundActivityProfile: "performance",
701 automaticGitFetchInterval: Duration.seconds(0),
702 providerHealthRefreshInterval: Duration.minutes(4),
703 });
704​
705 expect(next.backgroundActivity).toEqual({
706 schemaVersion: 1,
707 profile: "custom",
708 baseProfile: "performance",
709 overrides: {
710 automaticGitFetchInterval: Duration.seconds(0),
711 providerHealthRefreshInterval: Duration.minutes(4),
712 },
713 });
714 });
715​
716 it("ignores overrides attached to a concrete background profile", () => {
717 const resolved = resolveServerBackgroundActivitySettings({
718 ...DEFAULT_SERVER_SETTINGS,
719 backgroundActivity: {
720 schemaVersion: 1,
721 profile: "balanced",
722 overrides: {
723 pauseWhenOnBattery: true,
724 },
725 },
726 });
727​
728 expect(resolved.pauseWhenOnBattery).toBe(false);
729 });
730});