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

serverSettings.test.ts

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