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

serverSettings.test.ts

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