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

serverSettings.test.ts

1,168 lines42.9 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(providerInstances, (instanceId) =>
35 instanceId === configured ? true : false,
36 );
37 expect(fallback).toBeNull();
38 });
39​
40 it("considers a configured non-default instance only when asked to", () => {
41 const configured = ProviderInstanceId.make("codex_work");
42 const providerInstances = {
43 [configured]: { driver: ProviderDriverKind.make("codex"), config: {} },
44 };
45 const fallback = fallbackTextGenerationModelSelection(
46 providerInstances,
47 (instanceId) => instanceId === configured,
48 { includeConfiguredInstances: true },
49 );
50 expect(fallback?.instanceId).toBe(configured);
51 });
52});
53​
54describe("serverSettings helpers", () => {
55 it("changes a cleanup rule without replacing the machine's other rules", () => {
56 const enabled = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
57 storageCleanup: { worktreeAfterDays: 8, worktreeOnMerge: true, logsAfterDays: 30 },
58 });
59 expect(
60 applyServerSettingsPatch(enabled, {
61 storageCleanup: { worktreeAfterDays: null },
62 }).storageCleanup,
63 ).toEqual({
64 worktreeAfterDays: null,
65 worktreeSettledAfterDays: null,
66 worktreeOnMerge: true,
67 worktreeOnDelete: false,
68 worktreeUnchanged: false,
69 worktreeKeepWhen: "uncommitted-changes",
70 browserArtifactsAfterDays: null,
71 logsAfterDays: 30,
72 });
73 });
74 it("replaces a source control host's patched field so a cleared account pin does not survive", () => {
75 const pinned = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
76 sourceControlHosts: {
77 github: { hosts: { "github.com": { account: "work", enabled: true } }, tokens: {} },
78 },
79 });
80 const unpinned = applyServerSettingsPatch(pinned, {
81 sourceControlHosts: { github: { hosts: { "github.com": { enabled: false } } } },
82 });
83 expect(unpinned.sourceControlHosts.github).toEqual({
84 hosts: { "github.com": { enabled: false } },
85 tokens: {},
86 });
87 });
88 it("replaces SSH host lists when saving, editing, and removing hosts", () => {
89 const host = { kind: "ssh" as const, id: "mini", label: "Mac mini", target: "mini" };
90 const saved = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { deviceHosts: [host] });
91 expect(saved.deviceHosts).toEqual([host]);
92 const replacement = { ...host, target: "other-mini" };
93 const edited = applyServerSettingsPatch(saved, { deviceHosts: [replacement] });
94 expect(edited.deviceHosts).toEqual([replacement]);
95 expect(applyServerSettingsPatch(edited, { deviceHosts: [] }).deviceHosts).toEqual([]);
96 });
97​
98 it("replaces EAS host lists the same way", () => {
99 const host = {
100 kind: "eas" as const,
101 id: "eas",
102 label: "EAS Simulator",
103 accessToken: "",
104 maxSessionMinutes: 30,
105 idleMinutes: 10,
106 maxConcurrentSessions: 2,
107 };
108 const saved = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { easDeviceHosts: [host] });
109 expect(saved.easDeviceHosts).toEqual([host]);
110 const edited = applyServerSettingsPatch(saved, {
111 easDeviceHosts: [{ ...host, maxSessionMinutes: 60 }],
112 });
113 expect(edited.easDeviceHosts).toEqual([{ ...host, maxSessionMinutes: 60 }]);
114 expect(applyServerSettingsPatch(edited, { easDeviceHosts: [] }).easDeviceHosts).toEqual([]);
115 });
116​
117 it("inherits actions, preserves existing actions, and supports empty overrides and reset", () => {
118 const project = { id: ProjectId.make("project-actions"), scripts: [] };
119 const action = {
120 id: "check",
121 name: "Check",
122 command: "npm test",
123 icon: "play" as const,
124 runOnWorktreeCreate: false,
125 };
126 const existing = { ...project, scripts: [{ ...action, command: "npm run lint" }] };
127 const unfolded = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
128 defaultProjectScripts: [action],
129 });
130 expect(resolveProjectScripts(unfolded, existing)).toEqual(existing.scripts);
131 expect(projectScriptsInheritDefaults(unfolded, existing)).toBe(false);
132 const defaults = applyServerSettingsPatch(FOLDED_SERVER_SETTINGS, {
133 defaultProjectScripts: [action],
134 });
135 expect(resolveProjectScripts(defaults, project)).toEqual([action]);
136 expect(projectScriptsInheritDefaults(defaults, project)).toBe(true);
137 expect(resolveProjectScripts(defaults, existing)).toEqual([action]);
138 const disabled = applyServerSettingsPatch(defaults, {
139 projectScriptOverrides: { [project.id]: [] },
140 });
141 expect(resolveProjectScripts(disabled, project)).toEqual([]);
142 expect(projectScriptsInheritDefaults(disabled, project)).toBe(false);
143 const changedDefault = applyServerSettingsPatch(disabled, {
144 defaultProjectScripts: [{ ...action, command: "npm run build" }],
145 });
146 expect(resolveProjectScripts(changedDefault, project)).toEqual([]);
147 const reset = applyServerSettingsPatch(changedDefault, {
148 projectScriptOverrides: { [project.id]: null },
149 });
150 expect(resolveProjectScripts(reset, existing)).toEqual(changedDefault.defaultProjectScripts);
151 expect(projectScriptsInheritDefaults(reset, existing)).toBe(true);
152 expect(
153 resolveProjectScripts(
154 applyServerSettingsPatch(reset, { defaultProjectScripts: [] }),
155 existing,
156 ),
157 ).toEqual([]);
158 });
159​
160 it("preserves other projects' actions when overriding, clearing, or resetting one project", () => {
161 const firstProject = { id: ProjectId.make("first-project"), scripts: [] };
162 const secondProject = { id: ProjectId.make("second-project"), scripts: [] };
163 const defaultAction = {
164 id: "check",
165 name: "Check",
166 command: "npm test",
167 icon: "play" as const,
168 runOnWorktreeCreate: false,
169 };
170 const firstAction = { ...defaultAction, command: "npm run lint" };
171 const secondAction = { ...defaultAction, command: "npm run build" };
172 const firstUpdate = applyServerSettingsPatch(FOLDED_SERVER_SETTINGS, {
173 defaultProjectScripts: [defaultAction],
174 projectScriptOverrides: { [firstProject.id]: [firstAction] },
175 });
176 const secondUpdate = applyServerSettingsPatch(firstUpdate, {
177 projectScriptOverrides: { [secondProject.id]: [secondAction] },
178 });
179 expect(resolveProjectScripts(secondUpdate, firstProject)).toEqual([firstAction]);
180 expect(resolveProjectScripts(secondUpdate, secondProject)).toEqual([secondAction]);
181​
182 const cleared = applyServerSettingsPatch(secondUpdate, {
183 projectScriptOverrides: { [firstProject.id]: [] },
184 });
185 expect(resolveProjectScripts(cleared, firstProject)).toEqual([]);
186 expect(resolveProjectScripts(cleared, secondProject)).toEqual([secondAction]);
187​
188 const reset = applyServerSettingsPatch(cleared, {
189 projectScriptOverrides: { [firstProject.id]: null },
190 });
191 expect(resolveProjectScripts(reset, { ...firstProject, scripts: [firstAction] })).toEqual([
192 defaultAction,
193 ]);
194 expect(resolveProjectScripts(reset, secondProject)).toEqual([secondAction]);
195 expect(resolveProjectScripts(secondUpdate, firstProject)).toEqual([firstAction]);
196 });
197​
198 it("inherits automatic pull while preserving legacy opt-ins and explicit overrides", () => {
199 const projectId = ProjectId.make("project-pull");
200 expect(resolveProjectAutoPull(DEFAULT_SERVER_SETTINGS, projectId, false)).toBe(false);
201 expect(resolveProjectAutoPull(DEFAULT_SERVER_SETTINGS, projectId, true)).toBe(true);
202 const enabled = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { defaultAutoPull: true });
203 expect(resolveProjectAutoPull(enabled, projectId, false)).toBe(true);
204 const overridden = applyServerSettingsPatch(enabled, {
205 projectAutoPullOverrides: { [projectId]: false },
206 });
207 expect(resolveProjectAutoPull(overridden, projectId, true)).toBe(false);
208 const reset = applyServerSettingsPatch(overridden, {
209 projectAutoPullOverrides: { [projectId]: null },
210 });
211 expect(resolveProjectAutoPull(reset, projectId, false)).toBe(true);
212 const disabled = applyServerSettingsPatch(reset, {
213 defaultAutoPull: false,
214 projectAutoPullOverrides: { [projectId]: true },
215 });
216 expect(resolveProjectAutoPull(disabled, projectId, false)).toBe(true);
217 expect(resolveProjectAutoPull(disabled, ProjectId.make("other-project"), false)).toBe(false);
218 });
219​
220 it("inherits browser access and restores inheritance when a project override is removed", () => {
221 const projectId = ProjectId.make("project-browser");
222 const otherProjectId = ProjectId.make("other-project");
223 const overridden = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
224 projectAgentBrowserAccessOverrides: { [projectId]: false },
225 });
226 expect(resolveProjectAgentBrowserAccess(overridden, projectId)).toBe(false);
227 expect(resolveProjectAgentBrowserAccess(overridden, otherProjectId)).toBe(true);
228 const reset = applyServerSettingsPatch(overridden, {
229 projectAgentBrowserAccessOverrides: { [projectId]: null },
230 });
231 expect(resolveProjectAgentBrowserAccess(reset, projectId)).toBe(true);
232 const enabled = applyServerSettingsPatch(reset, {
233 enableAgentBrowserAccess: false,
234 projectAgentBrowserAccessOverrides: { [projectId]: true },
235 });
236 expect(resolveProjectAgentBrowserAccess(enabled, projectId)).toBe(true);
237 expect(resolveProjectAgentBrowserAccess(enabled, otherProjectId)).toBe(false);
238 });
239​
240 it("preserves other projects' boolean overrides across separate updates and resets", () => {
241 const firstProjectId = ProjectId.make("first-project");
242 const secondProjectId = ProjectId.make("second-project");
243 const firstUpdate = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
244 defaultAutoPull: true,
245 projectAutoPullOverrides: { [firstProjectId]: false },
246 projectAgentBrowserAccessOverrides: { [firstProjectId]: false },
247 });
248 const secondUpdate = applyServerSettingsPatch(firstUpdate, {
249 projectAutoPullOverrides: { [secondProjectId]: false },
250 projectAgentBrowserAccessOverrides: { [secondProjectId]: false },
251 });
252 for (const projectId of [firstProjectId, secondProjectId]) {
253 expect(resolveProjectAutoPull(secondUpdate, projectId, false)).toBe(false);
254 expect(resolveProjectAgentBrowserAccess(secondUpdate, projectId)).toBe(false);
255 }
256​
257 const reset = applyServerSettingsPatch(secondUpdate, {
258 projectAutoPullOverrides: { [firstProjectId]: null },
259 projectAgentBrowserAccessOverrides: { [firstProjectId]: null },
260 });
261 expect(resolveProjectAutoPull(reset, firstProjectId, false)).toBe(true);
262 expect(resolveProjectAgentBrowserAccess(reset, firstProjectId)).toBe(true);
263 expect(resolveProjectAutoPull(reset, secondProjectId, false)).toBe(false);
264 expect(resolveProjectAgentBrowserAccess(reset, secondProjectId)).toBe(false);
265 expect(reset.projectAutoPullOverrides[firstProjectId]).toBeUndefined();
266 expect(reset.projectAgentBrowserAccessOverrides[firstProjectId]).toBeUndefined();
267 expect(resolveProjectAutoPull(secondUpdate, firstProjectId, false)).toBe(false);
268 expect(resolveProjectAgentBrowserAccess(secondUpdate, firstProjectId)).toBe(false);
269 });
270​
271 it("replaces and clears conversation model defaults without retaining old options", () => {
272 const current = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
273 defaultModelSelection: createModelSelection(ProviderInstanceId.make("codex"), "gpt-5.4", [
274 { id: "reasoningEffort", value: "high" },
275 ]),
276 });
277 const selection = createModelSelection(ProviderInstanceId.make("claudeAgent"), "sonnet");
278 const updated = applyServerSettingsPatch(current, { defaultModelSelection: selection });
279 expect(updated.defaultModelSelection).toEqual(selection);
280 expect(
281 applyServerSettingsPatch(updated, { defaultModelSelection: null }).defaultModelSelection,
282 ).toBeNull();
283 });
284​
285 it("ignores missing and blank persisted observability URLs", () => {
286 expect(parsePersistedServerObservabilitySettings("{}")).toEqual({
287 otlpTracesUrl: undefined,
288 otlpMetricsUrl: undefined,
289 otlpLogsUrl: undefined,
290 });
291 expect(
292 parsePersistedServerObservabilitySettings(
293 JSON.stringify({
294 observability: { otlpTracesUrl: " ", otlpMetricsUrl: "", otlpLogsUrl: " " },
295 }),
296 ),
297 ).toEqual({
298 otlpTracesUrl: undefined,
299 otlpMetricsUrl: undefined,
300 otlpLogsUrl: undefined,
301 });
302 });
303​
304 it("parses lenient persisted settings JSON and trims observability URLs", () => {
305 expect(
306 parsePersistedServerObservabilitySettings(
307 JSON.stringify({
308 observability: {
309 otlpTracesUrl: " http://localhost:4318/v1/traces ",
310 otlpMetricsUrl: " http://localhost:4318/v1/metrics ",
311 otlpLogsUrl: " http://localhost:4318/v1/logs ",
312 },
313 }),
314 ),
315 ).toEqual({
316 otlpTracesUrl: "http://localhost:4318/v1/traces",
317 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
318 otlpLogsUrl: "http://localhost:4318/v1/logs",
319 });
320 });
321​
322 it("falls back cleanly when persisted settings are invalid", () => {
323 expect(parsePersistedServerObservabilitySettings("{")).toEqual({
324 otlpTracesUrl: undefined,
325 otlpMetricsUrl: undefined,
326 otlpLogsUrl: undefined,
327 });
328 });
329​
330 it("replaces text generation selection when provider/model are provided", () => {
331 const current = {
332 ...DEFAULT_SERVER_SETTINGS,
333 textGenerationModelSelection: createModelSelection(
334 ProviderInstanceId.make("codex"),
335 "gpt-5.4-mini",
336 [
337 { id: "reasoningEffort", value: "high" },
338 { id: "fastMode", value: true },
339 ],
340 ),
341 };
342​
343 expect(
344 applyServerSettingsPatch(current, {
345 textGenerationModelSelection: {
346 instanceId: ProviderInstanceId.make("codex"),
347 model: "gpt-5.4-mini",
348 },
349 }).textGenerationModelSelection,
350 ).toEqual({
351 instanceId: "codex",
352 model: "gpt-5.4-mini",
353 });
354 });
355​
356 it("still deep merges text generation selection when only options are provided", () => {
357 const current = {
358 ...DEFAULT_SERVER_SETTINGS,
359 textGenerationModelSelection: createModelSelection(
360 ProviderInstanceId.make("codex"),
361 "gpt-5.4-mini",
362 [
363 { id: "reasoningEffort", value: "high" },
364 { id: "fastMode", value: true },
365 ],
366 ),
367 };
368​
369 expect(
370 applyServerSettingsPatch(current, {
371 textGenerationModelSelection: {
372 options: [{ id: "fastMode", value: false }],
373 },
374 }).textGenerationModelSelection,
375 ).toEqual({
376 instanceId: "codex",
377 model: "gpt-5.4-mini",
378 options: [
379 { id: "reasoningEffort", value: "high" },
380 { id: "fastMode", value: false },
381 ],
382 });
383 });
384​
385 it("replaces text generation selection across providers without leaking stale options", () => {
386 const current = {
387 ...DEFAULT_SERVER_SETTINGS,
388 textGenerationModelSelection: createModelSelection(
389 ProviderInstanceId.make("codex"),
390 "gpt-5.4-mini",
391 [
392 { id: "reasoningEffort", value: "high" },
393 { id: "fastMode", value: true },
394 ],
395 ),
396 };
397​
398 expect(
399 applyServerSettingsPatch(current, {
400 textGenerationModelSelection: {
401 instanceId: ProviderInstanceId.make("opencode"),
402 model: "openai/gpt-5",
403 },
404 }).textGenerationModelSelection,
405 ).toEqual({
406 instanceId: "opencode",
407 model: "openai/gpt-5",
408 });
409 });
410​
411 it("accepts array-based text generation selection patches", () => {
412 expect(
413 applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
414 textGenerationModelSelection: {
415 instanceId: ProviderInstanceId.make("opencode"),
416 model: "openai/gpt-5",
417 options: [
418 { id: "variant", value: "prod" },
419 { id: "agent", value: "build" },
420 ],
421 },
422 }).textGenerationModelSelection,
423 ).toEqual({
424 instanceId: "opencode",
425 model: "openai/gpt-5",
426 options: [
427 { id: "variant", value: "prod" },
428 { id: "agent", value: "build" },
429 ],
430 });
431 });
432​
433 it("replaces source control writer selection without retaining stale options", () => {
434 const current = {
435 ...DEFAULT_SERVER_SETTINGS,
436 sourceControlWriterModelSelection: createModelSelection(
437 ProviderInstanceId.make("codex"),
438 "gpt-5.4-mini",
439 [{ id: "reasoningEffort", value: "high" }],
440 ),
441 };
442​
443 expect(
444 applyServerSettingsPatch(current, {
445 sourceControlWriterModelSelection: {
446 instanceId: ProviderInstanceId.make("opencode"),
447 model: "openai/gpt-5",
448 },
449 }).sourceControlWriterModelSelection,
450 ).toEqual({
451 instanceId: "opencode",
452 model: "openai/gpt-5",
453 });
454 });
455​
456 it("clears source control writer selection with null", () => {
457 const current = {
458 ...DEFAULT_SERVER_SETTINGS,
459 sourceControlWriterModelSelection: createModelSelection(
460 ProviderInstanceId.make("codex"),
461 "gpt-5.4-mini",
462 ),
463 };
464​
465 expect(
466 applyServerSettingsPatch(current, {
467 sourceControlWriterModelSelection: null,
468 }).sourceControlWriterModelSelection,
469 ).toBeNull();
470 });
471​
472 it("falls back from a disabled source control writer provider without clearing its selection", () => {
473 const instanceId = ProviderInstanceId.make("codex_writer");
474 const sourceControlWriterModelSelection = createModelSelection(instanceId, "gpt-5.4-mini");
475 const settings = {
476 ...DEFAULT_SERVER_SETTINGS,
477 providerInstances: {
478 [instanceId]: {
479 driver: ProviderDriverKind.make("codex"),
480 enabled: false,
481 config: {},
482 },
483 },
484 sourceControlWriterModelSelection,
485 };
486​
487 expect(isModelSelectionProviderEnabled(settings, sourceControlWriterModelSelection)).toBe(
488 false,
489 );
490 expect(resolveSourceControlWriterModelSelection(settings, undefined, null)).toBe(
491 settings.textGenerationModelSelection,
492 );
493 expect(settings.sourceControlWriterModelSelection).toBe(sourceControlWriterModelSelection);
494 });
495​
496 it("falls back from an unavailable source control writer provider", () => {
497 const instanceId = ProviderInstanceId.make("missing_writer");
498 const sourceControlWriterModelSelection = createModelSelection(instanceId, "missing-model");
499 const settings = {
500 ...DEFAULT_SERVER_SETTINGS,
501 providerInstances: {
502 [instanceId]: {
503 driver: ProviderDriverKind.make("missing-driver"),
504 config: {},
505 },
506 },
507 sourceControlWriterModelSelection,
508 };
509 const unavailableProvider = {
510 instanceId,
511 driver: ProviderDriverKind.make("missing-driver"),
512 enabled: false,
513 installed: false,
514 version: null,
515 status: "disabled",
516 auth: { status: "unknown" },
517 checkedAt: "2026-07-27T00:00:00.000Z",
518 availability: "unavailable",
519 unavailableReason: "This provider driver is not available in this build.",
520 models: [],
521 slashCommands: [],
522 skills: [],
523 } satisfies ServerProvider;
524​
525 expect(resolveSourceControlWriterModelSelection(settings, [unavailableProvider], null)).toBe(
526 settings.textGenerationModelSelection,
527 );
528 expect(settings.sourceControlWriterModelSelection).toBe(sourceControlWriterModelSelection);
529 });
530​
531 it("falls back from a writer provider that cannot generate application text", () => {
532 const instanceId = ProviderInstanceId.make("acp_writer");
533 const sourceControlWriterModelSelection = createModelSelection(instanceId, "default");
534 const settings = {
535 ...DEFAULT_SERVER_SETTINGS,
536 providerInstances: {
537 [instanceId]: {
538 driver: ProviderDriverKind.make("acpRegistry"),
539 enabled: true,
540 config: {},
541 },
542 },
543 sourceControlWriterModelSelection,
544 };
545 const incapableProvider = {
546 instanceId,
547 driver: ProviderDriverKind.make("acpRegistry"),
548 supportsTextGeneration: false,
549 enabled: true,
550 installed: true,
551 version: null,
552 status: "ready",
553 auth: { status: "authenticated" },
554 checkedAt: "2026-07-27T00:00:00.000Z",
555 models: [],
556 slashCommands: [],
557 skills: [],
558 } satisfies ServerProvider;
559​
560 expect(resolveSourceControlWriterModelSelection(settings, [incapableProvider], null)).toBe(
561 settings.textGenerationModelSelection,
562 );
563 expect(settings.sourceControlWriterModelSelection).toBe(sourceControlWriterModelSelection);
564 });
565​
566 it("falls back from a writer provider a project turned off, keeping it for other projects", () => {
567 const instanceId = ProviderInstanceId.make("codex_writer");
568 const projectId = ProjectId.make("project-a");
569 const sourceControlWriterModelSelection = createModelSelection(instanceId, "gpt-5.4-mini");
570 const settings = {
571 ...DEFAULT_SERVER_SETTINGS,
572 providerInstances: {
573 [instanceId]: { driver: ProviderDriverKind.make("codex"), enabled: true, config: {} },
574 },
575 projectSettingsOverrides: {
576 [projectId]: { providerInstances: { [instanceId]: { enabled: false } } },
577 },
578 sourceControlWriterModelSelection,
579 };
580 const provider = {
581 instanceId,
582 driver: ProviderDriverKind.make("codex"),
583 enabled: true,
584 installed: true,
585 version: null,
586 status: "ready",
587 auth: { status: "authenticated" },
588 checkedAt: "2026-07-27T00:00:00.000Z",
589 availability: "available",
590 models: [],
591 slashCommands: [],
592 skills: [],
593 } satisfies ServerProvider;
594​
595 expect(resolveSourceControlWriterModelSelection(settings, [provider], projectId)).toBe(
596 settings.textGenerationModelSelection,
597 );
598 expect(resolveSourceControlWriterModelSelection(settings, [provider], null)).toBe(
599 sourceControlWriterModelSelection,
600 );
601 });
602​
603 it("keeps a writer on an instance the project turned on while the machine has it off", () => {
604 const instanceId = ProviderInstanceId.make("codex_writer");
605 const projectId = ProjectId.make("project-a");
606 const sourceControlWriterModelSelection = createModelSelection(instanceId, "gpt-5.4-mini");
607 const settings = {
608 ...DEFAULT_SERVER_SETTINGS,
609 providerInstances: {
610 [instanceId]: { driver: ProviderDriverKind.make("codex"), enabled: false, config: {} },
611 },
612 projectSettingsOverrides: {
613 [projectId]: { providerInstances: { [instanceId]: { enabled: true } } },
614 },
615 sourceControlWriterModelSelection,
616 };
617 const provider = {
618 instanceId,
619 driver: ProviderDriverKind.make("codex"),
620 enabled: true,
621 installed: true,
622 version: null,
623 status: "ready",
624 auth: { status: "authenticated" },
625 checkedAt: "2026-07-27T00:00:00.000Z",
626 availability: "available",
627 models: [],
628 slashCommands: [],
629 skills: [],
630 } satisfies ServerProvider;
631​
632 expect(resolveSourceControlWriterModelSelection(settings, [provider], projectId)).toBe(
633 sourceControlWriterModelSelection,
634 );
635 expect(resolveSourceControlWriterModelSelection(settings, [provider], null)).toBe(
636 settings.textGenerationModelSelection,
637 );
638 });
639​
640 it("replaces providerInstances maps so omitted instance fields are cleared", () => {
641 const codexId = ProviderInstanceId.make("codex");
642 const current = {
643 ...DEFAULT_SERVER_SETTINGS,
644 providerInstances: {
645 [codexId]: {
646 driver: ProviderDriverKind.make("codex"),
647 displayName: "Codex Work",
648 accentColor: "#7c3aed",
649 enabled: true,
650 config: { homePath: "~/.codex" },
651 },
652 },
653 };
654​
655 expect(
656 applyServerSettingsPatch(current, {
657 providerInstances: {
658 [codexId]: {
659 driver: ProviderDriverKind.make("codex"),
660 displayName: "Codex Work",
661 enabled: true,
662 config: { homePath: "~/.codex" },
663 },
664 },
665 }).providerInstances[codexId],
666 ).toEqual({
667 driver: ProviderDriverKind.make("codex"),
668 displayName: "Codex Work",
669 enabled: true,
670 config: { homePath: "~/.codex" },
671 });
672 });
673​
674 it("keeps a project's providerInstances override when a patch entry omits the key", () => {
675 const instanceId = ProviderInstanceId.make("codex_work");
676 const projectId = ProjectId.make("project-a");
677 const current = {
678 ...DEFAULT_SERVER_SETTINGS,
679 projectSettingsOverrides: {
680 [projectId]: {
681 newWorktreesStartFromOrigin: true,
682 providerInstances: { [instanceId]: { enabled: false } },
683 },
684 },
685 };
686​
687 const patched = applyServerSettingsPatch(current, {
688 projectSettingsOverrides: {
689 [projectId]: { newWorktreesStartFromOrigin: false },
690 },
691 });
692​
693 expect(patched.projectSettingsOverrides[projectId]).toEqual({
694 newWorktreesStartFromOrigin: false,
695 providerInstances: { [instanceId]: { enabled: false } },
696 });
697 });
698​
699 it("clears a project's providerInstances override when a patch entry sends an empty map", () => {
700 const instanceId = ProviderInstanceId.make("codex_work");
701 const projectId = ProjectId.make("project-a");
702 const current = {
703 ...DEFAULT_SERVER_SETTINGS,
704 projectSettingsOverrides: {
705 [projectId]: {
706 newWorktreesStartFromOrigin: true,
707 providerInstances: { [instanceId]: { enabled: false } },
708 },
709 },
710 };
711​
712 const patched = applyServerSettingsPatch(current, {
713 projectSettingsOverrides: {
714 [projectId]: { newWorktreesStartFromOrigin: true, providerInstances: {} },
715 },
716 });
717​
718 expect(patched.projectSettingsOverrides[projectId]).toEqual({
719 newWorktreesStartFromOrigin: true,
720 providerInstances: {},
721 });
722 });
723​
724 it("upserts and removes usageLimitSources per entry so concurrent edits cannot clobber", () => {
725 const hubA = UsageLimitSourceId.make("cliproxy-a");
726 const hubB = UsageLimitSourceId.make("cliproxy-b");
727 const source = (url: string) => ({
728 kind: "cliproxy" as const,
729 url,
730 managementKey: "secret",
731 enabled: true,
732 });
733 const current = {
734 ...DEFAULT_SERVER_SETTINGS,
735 usageLimitSources: { [hubA]: source("http://a:8318") },
736 };
737​
738 const added = applyServerSettingsPatch(current, {
739 usageLimitSources: { [hubB]: source("http://b:8318") },
740 });
741 expect(Object.keys(added.usageLimitSources)).toEqual([hubA, hubB]);
742​
743 const removed = applyServerSettingsPatch(added, { usageLimitSources: { [hubA]: null } });
744 expect(Object.keys(removed.usageLimitSources)).toEqual([hubB]);
745 });
746​
747 it("replaces and removes individual usage prices without clobbering other models", () => {
748 const prices = { inputCostPerMillionTokens: 2, outputCostPerMillionTokens: 8 };
749 const current = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
750 usagePriceOverrides: { "example-model": { ...prices, cacheReadCostPerMillionTokens: 0.5 } },
751 });
752 const added = applyServerSettingsPatch(current, {
753 usagePriceOverrides: { "other-model": prices },
754 });
755 const replaced = applyServerSettingsPatch(added, {
756 usagePriceOverrides: { "example-model": prices },
757 });
758 expect(replaced.usagePriceOverrides).toEqual({
759 "example-model": prices,
760 "other-model": prices,
761 });
762 const removed = applyServerSettingsPatch(replaced, {
763 usagePriceOverrides: { "example-model": null },
764 });
765 expect(removed.usagePriceOverrides).toEqual({ "other-model": prices });
766 expect(current.usagePriceOverrides["example-model"]?.cacheReadCostPerMillionTokens).toBe(0.5);
767 });
768​
769 it("stores background activity profiles as a versioned object and syncs legacy aliases", () => {
770 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
771 backgroundActivity: {
772 schemaVersion: 1,
773 profile: "battery-saver",
774 overrides: {},
775 },
776 });
777​
778 expect(next.backgroundActivity).toEqual({
779 schemaVersion: 1,
780 profile: "battery-saver",
781 overrides: {},
782 });
783 expect(next.backgroundActivityProfile).toBe("battery-saver");
784 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(0);
785 expect(Duration.toMillis(next.providerHealthRefreshInterval)).toBe(
786 Duration.toMillis(Duration.minutes(15)),
787 );
788 });
789​
790 it("turns legacy interval patches into custom background activity overrides", () => {
791 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
792 automaticGitFetchInterval: Duration.seconds(15),
793 });
794​
795 expect(next.backgroundActivity).toEqual({
796 schemaVersion: 1,
797 profile: "custom",
798 baseProfile: "balanced",
799 overrides: {
800 automaticGitFetchInterval: Duration.seconds(15),
801 },
802 });
803 expect(resolveServerBackgroundActivitySettings(next).profile).toBe("balanced");
804 expect(
805 Duration.toMillis(resolveServerBackgroundActivitySettings(next).automaticGitFetchInterval),
806 ).toBe(15_000);
807 });
808​
809 it("preserves legacy background activity settings when applying an unrelated patch", () => {
810 const current = {
811 ...DEFAULT_SERVER_SETTINGS,
812 backgroundActivityProfile: "performance" as const,
813 automaticGitFetchInterval: Duration.seconds(7),
814 providerHealthRefreshInterval: Duration.minutes(4),
815 };
816​
817 const next = applyServerSettingsPatch(current, {
818 sourceControlWriterModelSelection: createModelSelection(
819 ProviderInstanceId.make("codex"),
820 "gpt-5.4-mini",
821 ),
822 });
823​
824 expect(next.backgroundActivity).toEqual({
825 schemaVersion: 1,
826 profile: "custom",
827 baseProfile: "performance",
828 overrides: {
829 automaticGitFetchInterval: Duration.seconds(7),
830 providerHealthRefreshInterval: Duration.minutes(4),
831 },
832 });
833 expect(next.backgroundActivityProfile).toBe("performance");
834 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(7_000);
835 expect(Duration.toMillis(next.providerHealthRefreshInterval)).toBe(240_000);
836 });
837​
838 it("does not reactivate dormant overrides from a concrete profile", () => {
839 const current = {
840 ...DEFAULT_SERVER_SETTINGS,
841 backgroundActivity: {
842 schemaVersion: 1 as const,
843 profile: "battery-saver" as const,
844 overrides: {
845 providerHealthRefreshInterval: Duration.seconds(5),
846 },
847 },
848 };
849​
850 const next = applyServerSettingsPatch(current, {
851 automaticGitFetchInterval: Duration.seconds(15),
852 });
853​
854 expect(next.backgroundActivity).toEqual({
855 schemaVersion: 1,
856 profile: "custom",
857 baseProfile: "battery-saver",
858 overrides: {
859 automaticGitFetchInterval: Duration.seconds(15),
860 },
861 });
862 });
863​
864 it("prefers structured background activity settings over legacy aliases", () => {
865 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
866 backgroundActivity: {
867 schemaVersion: 1,
868 profile: "battery-saver",
869 overrides: {},
870 },
871 automaticGitFetchInterval: Duration.seconds(5),
872 backgroundActivityProfile: "performance",
873 });
874​
875 expect(next.backgroundActivity).toEqual({
876 schemaVersion: 1,
877 profile: "battery-saver",
878 overrides: {},
879 });
880 expect(next.backgroundActivityProfile).toBe("battery-saver");
881 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(0);
882 });
883​
884 it("reconciles custom background activity back to a preset when overrides match the preset", () => {
885 const custom = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
886 automaticGitFetchInterval: Duration.seconds(15),
887 });
888 const next = applyServerSettingsPatch(custom, {
889 automaticGitFetchInterval: Duration.seconds(30),
890 });
891​
892 expect(next.backgroundActivity).toEqual({
893 schemaVersion: 1,
894 profile: "balanced",
895 overrides: {},
896 });
897 expect(next.backgroundActivityProfile).toBe("balanced");
898 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(30_000);
899 });
900​
901 it("drops custom overrides that duplicate the base profile", () => {
902 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
903 backgroundActivity: {
904 schemaVersion: 1,
905 profile: "custom",
906 baseProfile: "balanced",
907 overrides: {
908 automaticGitFetchInterval: Duration.seconds(30),
909 },
910 },
911 });
912​
913 expect(next.backgroundActivity).toEqual({
914 schemaVersion: 1,
915 profile: "balanced",
916 overrides: {},
917 });
918 });
919​
920 it("replaces the complete background override record", () => {
921 const current = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
922 backgroundActivity: {
923 schemaVersion: 1,
924 profile: "custom",
925 baseProfile: "balanced",
926 overrides: {
927 automaticGitFetchInterval: Duration.seconds(15),
928 providerHealthRefreshInterval: Duration.minutes(3),
929 },
930 },
931 });
932​
933 const next = applyServerSettingsPatch(current, {
934 backgroundActivity: {
935 overrides: {
936 automaticGitFetchInterval: Duration.seconds(10),
937 },
938 },
939 });
940​
941 expect(next.backgroundActivity).toEqual({
942 schemaVersion: 1,
943 profile: "custom",
944 baseProfile: "balanced",
945 overrides: {
946 automaticGitFetchInterval: Duration.seconds(10),
947 },
948 });
949 });
950​
951 it("keeps interval overrides supplied with a profile patch", () => {
952 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
953 backgroundActivityProfile: "performance",
954 automaticGitFetchInterval: Duration.seconds(0),
955 providerHealthRefreshInterval: Duration.minutes(4),
956 });
957​
958 expect(next.backgroundActivity).toEqual({
959 schemaVersion: 1,
960 profile: "custom",
961 baseProfile: "performance",
962 overrides: {
963 automaticGitFetchInterval: Duration.seconds(0),
964 providerHealthRefreshInterval: Duration.minutes(4),
965 },
966 });
967 });
968​
969 it("ignores overrides attached to a concrete background profile", () => {
970 const resolved = resolveServerBackgroundActivitySettings({
971 ...DEFAULT_SERVER_SETTINGS,
972 backgroundActivity: {
973 schemaVersion: 1,
974 profile: "balanced",
975 overrides: {
976 pauseWhenOnBattery: true,
977 },
978 },
979 });
980​
981 expect(resolved.pauseWhenOnBattery).toBe(false);
982 });
983});
984​
985describe("worktreesDirectory", () => {
986 it("remembers previous custom locations so their worktrees stay managed", () => {
987 const first = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { worktreesDirectory: "/a" });
988 expect(first.previousWorktreesDirectories).toEqual([]);
989 const second = applyServerSettingsPatch(first, { worktreesDirectory: "/b" });
990 expect(second.previousWorktreesDirectories).toEqual(["/a"]);
991 const reset = applyServerSettingsPatch(second, { worktreesDirectory: "" });
992 expect(reset.previousWorktreesDirectories).toEqual(["/a", "/b"]);
993 const back = applyServerSettingsPatch(reset, { worktreesDirectory: "/a" });
994 expect(back.previousWorktreesDirectories).toEqual(["/b"]);
995 });
996});
997​
998describe("synchronized model preferences", () => {
999 const provider = ProviderInstanceId.make("codex_work");
1000 const legacy = {
1001 favorites: [{ provider, model: "sol" }],
1002 providerModelPreferences: { [provider]: { hiddenModels: ["astra"], modelOrder: ["sol"] } },
1003 };
1004​
1005 it("keeps concurrent adds and removes of the same favorite idempotent", () => {
1006 const add = { setModelFavorites: [{ provider, model: "sol", favorite: true }] };
1007 const first = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, add);
1008 expect(applyServerSettingsPatch(first, add).favorites).toEqual([{ provider, model: "sol" }]);
1009 const remove = { setModelFavorites: [{ provider, model: "sol", favorite: false }] };
1010 const removed = applyServerSettingsPatch(first, remove);
1011 expect(applyServerSettingsPatch(removed, remove).favorites).toEqual([]);
1012 });
1013​
1014 it("preserves unrelated models and providers during bulk visibility and order edits", () => {
1015 const otherProvider = ProviderInstanceId.make("claude_work");
1016 const initial = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
1017 providerModelPreferences: {
1018 ...legacy.providerModelPreferences,
1019 [otherProvider]: { hiddenModels: ["opus"], modelOrder: ["sonnet", "opus"] },
1020 },
1021 });
1022 const mobile = applyServerSettingsPatch(initial, {
1023 setModelsHidden: [{ provider, model: "luna", hidden: true }],
1024 });
1025 const web = applyServerSettingsPatch(mobile, {
1026 setModelsHidden: [
1027 { provider, model: "astra", hidden: false },
1028 { provider, model: "sol", hidden: true },
1029 ],
1030 setProviderModelOrder: { provider, modelOrder: ["sol", "luna", "astra"] },
1031 });
1032 expect(web.providerModelPreferences?.[provider]).toEqual({
1033 hiddenModels: ["luna", "sol"],
1034 modelOrder: ["sol", "luna", "astra"],
1035 });
1036 expect(web.providerModelPreferences?.[otherProvider]).toEqual(
1037 initial.providerModelPreferences?.[otherProvider],
1038 );
1039 expect(
1040 applyServerSettingsPatch(web, {
1041 setProviderModelOrder: { provider, modelOrder: [] },
1042 }).providerModelPreferences?.[provider],
1043 ).toEqual({ hiddenModels: ["luna", "sol"], modelOrder: [] });
1044 });
1045​
1046 it("migrates saved lists independently without claiming empty legacy lists", () => {
1047 expect(modelPreferencesMigrationPatch(DEFAULT_SERVER_SETTINGS, {})).toBeNull();
1048 const patch = modelPreferencesMigrationPatch(DEFAULT_SERVER_SETTINGS, legacy)!;
1049 const migrated = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, patch);
1050 expect(resolveModelPreferences(migrated)).toEqual(legacy);
1051 expect(modelPreferencesMigrationPatch(migrated, legacy)).toBeNull();
1052 const favoritesOnly = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, { favorites: [] });
1053 expect(modelPreferencesMigrationPatch(favoritesOnly, legacy)).toEqual({
1054 migrateModelPreferences: { providerModelPreferences: legacy.providerModelPreferences },
1055 });
1056 });
1057​
1058 it("keeps explicit clears when a delayed migration arrives from another device", () => {
1059 const staleMigration = modelPreferencesMigrationPatch(DEFAULT_SERVER_SETTINGS, legacy)!;
1060 const cleared = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
1061 favorites: [],
1062 providerModelPreferences: {},
1063 });
1064 expect(
1065 resolveModelPreferences(applyServerSettingsPatch(cleared, staleMigration), legacy),
1066 ).toEqual({
1067 favorites: [],
1068 providerModelPreferences: {},
1069 });
1070 });
1071​
1072 it("replaces visibility records so showing every model clears removed entries", () => {
1073 const migrated = applyServerSettingsPatch(
1074 DEFAULT_SERVER_SETTINGS,
1075 modelPreferencesMigrationPatch(DEFAULT_SERVER_SETTINGS, legacy)!,
1076 );
1077 const cleared = applyServerSettingsPatch(migrated, { providerModelPreferences: {} });
1078 expect(cleared.providerModelPreferences).toEqual({});
1079 expect(cleared.favorites).toEqual(legacy.favorites);
1080 });
1081​
1082 it("sets favorites without losing changes from other clients", () => {
1083 const first = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
1084 setModelFavorites: [{ provider, model: "sol", favorite: true }],
1085 });
1086 const second = applyServerSettingsPatch(first, {
1087 setModelFavorites: [{ provider, model: "astra", favorite: true }],
1088 migrateModelPreferences: { favorites: [] },
1089 });
1090 expect(second.favorites).toEqual([
1091 { provider, model: "sol" },
1092 { provider, model: "astra" },
1093 ]);
1094 const removed = applyServerSettingsPatch(second, {
1095 setModelFavorites: [{ provider, model: "sol", favorite: false }],
1096 });
1097 expect(removed.favorites).toEqual([{ provider, model: "astra" }]);
1098 expect(
1099 applyServerSettingsPatch(removed, {
1100 setModelFavorites: [{ provider, model: "astra", favorite: false }],
1101 }).favorites,
1102 ).toEqual([]);
1103 });
1104​
1105 it("sets visibility against the latest state without replacing other models or their order", () => {
1106 const initial = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
1107 providerModelPreferences: legacy.providerModelPreferences,
1108 });
1109 const first = applyServerSettingsPatch(initial, {
1110 setModelsHidden: [{ provider, model: "sol", hidden: true }],
1111 });
1112 const second = applyServerSettingsPatch(first, {
1113 setModelsHidden: [{ provider, model: "luna", hidden: true }],
1114 });
1115 expect(second.providerModelPreferences?.[provider]).toEqual({
1116 hiddenModels: ["astra", "sol", "luna"],
1117 modelOrder: ["sol"],
1118 });
1119 const restored = applyServerSettingsPatch(second, {
1120 setModelsHidden: [{ provider, model: "sol", hidden: false }],
1121 });
1122 expect(restored.providerModelPreferences?.[provider]).toEqual({
1123 hiddenModels: ["astra", "luna"],
1124 modelOrder: ["sol"],
1125 });
1126 expect(
1127 applyServerSettingsPatch(restored, {
1128 setModelsHidden: [{ provider, model: "sol", hidden: false }],
1129 }).providerModelPreferences,
1130 ).toEqual(restored.providerModelPreferences);
1131 expect(
1132 applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
1133 setModelsHidden: [{ provider, model: "sol", hidden: true }],
1134 }).providerModelPreferences?.[provider]?.hiddenModels,
1135 ).toEqual(["sol"]);
1136 });
1137​
1138 it("keeps the same model on different provider instances as separate favorites", () => {
1139 const otherProvider = ProviderInstanceId.make("codex_personal");
1140 const first = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
1141 setModelFavorites: [{ provider, model: "sol", favorite: true }],
1142 });
1143 const second = applyServerSettingsPatch(first, {
1144 setModelFavorites: [{ provider: otherProvider, model: "sol", favorite: true }],
1145 });
1146 expect(second.favorites).toEqual([
1147 { provider, model: "sol" },
1148 { provider: otherProvider, model: "sol" },
1149 ]);
1150 expect(
1151 applyServerSettingsPatch(second, {
1152 setModelFavorites: [{ provider, model: "sol", favorite: false }],
1153 }).favorites,
1154 ).toEqual([{ provider: otherProvider, model: "sol" }]);
1155 });
1156​
1157 it("keeps explicit edits when the same patch also carries a legacy migration", () => {
1158 const edited = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
1159 ...modelPreferencesMigrationPatch(DEFAULT_SERVER_SETTINGS, legacy),
1160 favorites: [],
1161 providerModelPreferences: {},
1162 });
1163 expect(resolveModelPreferences(edited, legacy)).toEqual({
1164 favorites: [],
1165 providerModelPreferences: {},
1166 });
1167 });
1168});