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

serverSettings.test.ts

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