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

serverSettings.test.ts

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