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

serverSettings.test.ts

515 lines15.6 KB

1import {
2 DEFAULT_SERVER_SETTINGS,
3 ProviderDriverKind,
4 ProviderInstanceId,
5 type ServerProvider,
6} from "@t3tools/contracts";
7import * as Duration from "effect/Duration";
8import { describe, expect, it } from "vite-plus/test";
9import { resolveServerBackgroundActivitySettings } from "./backgroundActivitySettings.ts";
10import { createModelSelection } from "./model.ts";
11import {
12 applyServerSettingsPatch,
13 extractPersistedServerObservabilitySettings,
14 isModelSelectionProviderEnabled,
15 normalizePersistedServerSettingString,
16 parsePersistedServerObservabilitySettings,
17 resolveSourceControlWriterModelSelection,
18} from "./serverSettings.ts";
19​
20describe("serverSettings helpers", () => {
21 it("normalizes optional persisted strings", () => {
22 expect(normalizePersistedServerSettingString(undefined)).toBeUndefined();
23 expect(normalizePersistedServerSettingString(" ")).toBeUndefined();
24 expect(normalizePersistedServerSettingString(" http://localhost:4318/v1/traces ")).toBe(
25 "http://localhost:4318/v1/traces",
26 );
27 });
28​
29 it("extracts persisted observability settings", () => {
30 expect(
31 extractPersistedServerObservabilitySettings({
32 observability: {
33 otlpTracesUrl: " http://localhost:4318/v1/traces ",
34 otlpMetricsUrl: " http://localhost:4318/v1/metrics ",
35 },
36 }),
37 ).toEqual({
38 otlpTracesUrl: "http://localhost:4318/v1/traces",
39 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
40 });
41 });
42​
43 it("parses lenient persisted settings JSON", () => {
44 expect(
45 parsePersistedServerObservabilitySettings(
46 JSON.stringify({
47 observability: {
48 otlpTracesUrl: "http://localhost:4318/v1/traces",
49 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
50 },
51 }),
52 ),
53 ).toEqual({
54 otlpTracesUrl: "http://localhost:4318/v1/traces",
55 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
56 });
57 });
58​
59 it("falls back cleanly when persisted settings are invalid", () => {
60 expect(parsePersistedServerObservabilitySettings("{")).toEqual({
61 otlpTracesUrl: undefined,
62 otlpMetricsUrl: undefined,
63 });
64 });
65​
66 it("replaces text generation selection when provider/model are provided", () => {
67 const current = {
68 ...DEFAULT_SERVER_SETTINGS,
69 textGenerationModelSelection: createModelSelection(
70 ProviderInstanceId.make("codex"),
71 "gpt-5.4-mini",
72 [
73 { id: "reasoningEffort", value: "high" },
74 { id: "fastMode", value: true },
75 ],
76 ),
77 };
78​
79 expect(
80 applyServerSettingsPatch(current, {
81 textGenerationModelSelection: {
82 instanceId: ProviderInstanceId.make("codex"),
83 model: "gpt-5.4-mini",
84 },
85 }).textGenerationModelSelection,
86 ).toEqual({
87 instanceId: "codex",
88 model: "gpt-5.4-mini",
89 });
90 });
91​
92 it("still deep merges text generation selection when only options are provided", () => {
93 const current = {
94 ...DEFAULT_SERVER_SETTINGS,
95 textGenerationModelSelection: createModelSelection(
96 ProviderInstanceId.make("codex"),
97 "gpt-5.4-mini",
98 [
99 { id: "reasoningEffort", value: "high" },
100 { id: "fastMode", value: true },
101 ],
102 ),
103 };
104​
105 expect(
106 applyServerSettingsPatch(current, {
107 textGenerationModelSelection: {
108 options: [{ id: "fastMode", value: false }],
109 },
110 }).textGenerationModelSelection,
111 ).toEqual({
112 instanceId: "codex",
113 model: "gpt-5.4-mini",
114 options: [
115 { id: "reasoningEffort", value: "high" },
116 { id: "fastMode", value: false },
117 ],
118 });
119 });
120​
121 it("replaces text generation selection across providers without leaking stale options", () => {
122 const current = {
123 ...DEFAULT_SERVER_SETTINGS,
124 textGenerationModelSelection: createModelSelection(
125 ProviderInstanceId.make("codex"),
126 "gpt-5.4-mini",
127 [
128 { id: "reasoningEffort", value: "high" },
129 { id: "fastMode", value: true },
130 ],
131 ),
132 };
133​
134 expect(
135 applyServerSettingsPatch(current, {
136 textGenerationModelSelection: {
137 instanceId: ProviderInstanceId.make("opencode"),
138 model: "openai/gpt-5",
139 },
140 }).textGenerationModelSelection,
141 ).toEqual({
142 instanceId: "opencode",
143 model: "openai/gpt-5",
144 });
145 });
146​
147 it("accepts array-based text generation selection patches", () => {
148 expect(
149 applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
150 textGenerationModelSelection: {
151 instanceId: ProviderInstanceId.make("opencode"),
152 model: "openai/gpt-5",
153 options: [
154 { id: "variant", value: "prod" },
155 { id: "agent", value: "build" },
156 ],
157 },
158 }).textGenerationModelSelection,
159 ).toEqual({
160 instanceId: "opencode",
161 model: "openai/gpt-5",
162 options: [
163 { id: "variant", value: "prod" },
164 { id: "agent", value: "build" },
165 ],
166 });
167 });
168​
169 it("replaces source control writer selection without retaining stale options", () => {
170 const current = {
171 ...DEFAULT_SERVER_SETTINGS,
172 sourceControlWriterModelSelection: createModelSelection(
173 ProviderInstanceId.make("codex"),
174 "gpt-5.4-mini",
175 [{ id: "reasoningEffort", value: "high" }],
176 ),
177 };
178​
179 expect(
180 applyServerSettingsPatch(current, {
181 sourceControlWriterModelSelection: {
182 instanceId: ProviderInstanceId.make("opencode"),
183 model: "openai/gpt-5",
184 },
185 }).sourceControlWriterModelSelection,
186 ).toEqual({
187 instanceId: "opencode",
188 model: "openai/gpt-5",
189 });
190 });
191​
192 it("clears source control writer selection with null", () => {
193 const current = {
194 ...DEFAULT_SERVER_SETTINGS,
195 sourceControlWriterModelSelection: createModelSelection(
196 ProviderInstanceId.make("codex"),
197 "gpt-5.4-mini",
198 ),
199 };
200​
201 expect(
202 applyServerSettingsPatch(current, {
203 sourceControlWriterModelSelection: null,
204 }).sourceControlWriterModelSelection,
205 ).toBeNull();
206 });
207​
208 it("falls back from a disabled source control writer provider without clearing its selection", () => {
209 const instanceId = ProviderInstanceId.make("codex_writer");
210 const sourceControlWriterModelSelection = createModelSelection(instanceId, "gpt-5.4-mini");
211 const settings = {
212 ...DEFAULT_SERVER_SETTINGS,
213 providerInstances: {
214 [instanceId]: {
215 driver: ProviderDriverKind.make("codex"),
216 enabled: false,
217 config: {},
218 },
219 },
220 sourceControlWriterModelSelection,
221 };
222​
223 expect(isModelSelectionProviderEnabled(settings, sourceControlWriterModelSelection)).toBe(
224 false,
225 );
226 expect(resolveSourceControlWriterModelSelection(settings)).toBe(
227 settings.textGenerationModelSelection,
228 );
229 expect(settings.sourceControlWriterModelSelection).toBe(sourceControlWriterModelSelection);
230 });
231​
232 it("falls back from an unavailable source control writer provider", () => {
233 const instanceId = ProviderInstanceId.make("missing_writer");
234 const sourceControlWriterModelSelection = createModelSelection(instanceId, "missing-model");
235 const settings = {
236 ...DEFAULT_SERVER_SETTINGS,
237 providerInstances: {
238 [instanceId]: {
239 driver: ProviderDriverKind.make("missing-driver"),
240 config: {},
241 },
242 },
243 sourceControlWriterModelSelection,
244 };
245 const unavailableProvider = {
246 instanceId,
247 driver: ProviderDriverKind.make("missing-driver"),
248 enabled: false,
249 installed: false,
250 version: null,
251 status: "disabled",
252 auth: { status: "unknown" },
253 checkedAt: "2026-07-27T00:00:00.000Z",
254 availability: "unavailable",
255 unavailableReason: "This provider driver is not available in this build.",
256 models: [],
257 slashCommands: [],
258 skills: [],
259 } satisfies ServerProvider;
260​
261 expect(resolveSourceControlWriterModelSelection(settings, [unavailableProvider])).toBe(
262 settings.textGenerationModelSelection,
263 );
264 expect(settings.sourceControlWriterModelSelection).toBe(sourceControlWriterModelSelection);
265 });
266​
267 it("replaces providerInstances maps so omitted instance fields are cleared", () => {
268 const codexId = ProviderInstanceId.make("codex");
269 const current = {
270 ...DEFAULT_SERVER_SETTINGS,
271 providerInstances: {
272 [codexId]: {
273 driver: ProviderDriverKind.make("codex"),
274 displayName: "Codex Work",
275 accentColor: "#7c3aed",
276 enabled: true,
277 config: { homePath: "~/.codex" },
278 },
279 },
280 };
281​
282 expect(
283 applyServerSettingsPatch(current, {
284 providerInstances: {
285 [codexId]: {
286 driver: ProviderDriverKind.make("codex"),
287 displayName: "Codex Work",
288 enabled: true,
289 config: { homePath: "~/.codex" },
290 },
291 },
292 }).providerInstances[codexId],
293 ).toEqual({
294 driver: ProviderDriverKind.make("codex"),
295 displayName: "Codex Work",
296 enabled: true,
297 config: { homePath: "~/.codex" },
298 });
299 });
300​
301 it("stores background activity profiles as a versioned object and syncs legacy aliases", () => {
302 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
303 backgroundActivity: {
304 schemaVersion: 1,
305 profile: "battery-saver",
306 overrides: {},
307 },
308 });
309​
310 expect(next.backgroundActivity).toEqual({
311 schemaVersion: 1,
312 profile: "battery-saver",
313 overrides: {},
314 });
315 expect(next.backgroundActivityProfile).toBe("battery-saver");
316 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(0);
317 expect(Duration.toMillis(next.providerHealthRefreshInterval)).toBe(
318 Duration.toMillis(Duration.minutes(15)),
319 );
320 });
321​
322 it("turns legacy interval patches into custom background activity overrides", () => {
323 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
324 automaticGitFetchInterval: Duration.seconds(15),
325 });
326​
327 expect(next.backgroundActivity).toEqual({
328 schemaVersion: 1,
329 profile: "custom",
330 baseProfile: "balanced",
331 overrides: {
332 automaticGitFetchInterval: Duration.seconds(15),
333 },
334 });
335 expect(resolveServerBackgroundActivitySettings(next).profile).toBe("balanced");
336 expect(
337 Duration.toMillis(resolveServerBackgroundActivitySettings(next).automaticGitFetchInterval),
338 ).toBe(15_000);
339 });
340​
341 it("preserves legacy background activity settings when applying an unrelated patch", () => {
342 const current = {
343 ...DEFAULT_SERVER_SETTINGS,
344 backgroundActivityProfile: "performance" as const,
345 automaticGitFetchInterval: Duration.seconds(7),
346 providerHealthRefreshInterval: Duration.minutes(4),
347 };
348​
349 const next = applyServerSettingsPatch(current, {
350 sourceControlWriterModelSelection: createModelSelection(
351 ProviderInstanceId.make("codex"),
352 "gpt-5.4-mini",
353 ),
354 });
355​
356 expect(next.backgroundActivity).toEqual({
357 schemaVersion: 1,
358 profile: "custom",
359 baseProfile: "performance",
360 overrides: {
361 automaticGitFetchInterval: Duration.seconds(7),
362 providerHealthRefreshInterval: Duration.minutes(4),
363 },
364 });
365 expect(next.backgroundActivityProfile).toBe("performance");
366 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(7_000);
367 expect(Duration.toMillis(next.providerHealthRefreshInterval)).toBe(240_000);
368 });
369​
370 it("does not reactivate dormant overrides from a concrete profile", () => {
371 const current = {
372 ...DEFAULT_SERVER_SETTINGS,
373 backgroundActivity: {
374 schemaVersion: 1 as const,
375 profile: "battery-saver" as const,
376 overrides: {
377 providerHealthRefreshInterval: Duration.seconds(5),
378 },
379 },
380 };
381​
382 const next = applyServerSettingsPatch(current, {
383 automaticGitFetchInterval: Duration.seconds(15),
384 });
385​
386 expect(next.backgroundActivity).toEqual({
387 schemaVersion: 1,
388 profile: "custom",
389 baseProfile: "battery-saver",
390 overrides: {
391 automaticGitFetchInterval: Duration.seconds(15),
392 },
393 });
394 });
395​
396 it("prefers structured background activity settings over legacy aliases", () => {
397 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
398 backgroundActivity: {
399 schemaVersion: 1,
400 profile: "battery-saver",
401 overrides: {},
402 },
403 automaticGitFetchInterval: Duration.seconds(5),
404 backgroundActivityProfile: "performance",
405 });
406​
407 expect(next.backgroundActivity).toEqual({
408 schemaVersion: 1,
409 profile: "battery-saver",
410 overrides: {},
411 });
412 expect(next.backgroundActivityProfile).toBe("battery-saver");
413 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(0);
414 });
415​
416 it("reconciles custom background activity back to a preset when overrides match the preset", () => {
417 const custom = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
418 automaticGitFetchInterval: Duration.seconds(15),
419 });
420 const next = applyServerSettingsPatch(custom, {
421 automaticGitFetchInterval: Duration.seconds(30),
422 });
423​
424 expect(next.backgroundActivity).toEqual({
425 schemaVersion: 1,
426 profile: "balanced",
427 overrides: {},
428 });
429 expect(next.backgroundActivityProfile).toBe("balanced");
430 expect(Duration.toMillis(next.automaticGitFetchInterval)).toBe(30_000);
431 });
432​
433 it("drops custom overrides that duplicate the base profile", () => {
434 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
435 backgroundActivity: {
436 schemaVersion: 1,
437 profile: "custom",
438 baseProfile: "balanced",
439 overrides: {
440 automaticGitFetchInterval: Duration.seconds(30),
441 },
442 },
443 });
444​
445 expect(next.backgroundActivity).toEqual({
446 schemaVersion: 1,
447 profile: "balanced",
448 overrides: {},
449 });
450 });
451​
452 it("replaces the complete background override record", () => {
453 const current = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
454 backgroundActivity: {
455 schemaVersion: 1,
456 profile: "custom",
457 baseProfile: "balanced",
458 overrides: {
459 automaticGitFetchInterval: Duration.seconds(15),
460 providerHealthRefreshInterval: Duration.minutes(3),
461 },
462 },
463 });
464​
465 const next = applyServerSettingsPatch(current, {
466 backgroundActivity: {
467 overrides: {
468 automaticGitFetchInterval: Duration.seconds(10),
469 },
470 },
471 });
472​
473 expect(next.backgroundActivity).toEqual({
474 schemaVersion: 1,
475 profile: "custom",
476 baseProfile: "balanced",
477 overrides: {
478 automaticGitFetchInterval: Duration.seconds(10),
479 },
480 });
481 });
482​
483 it("keeps interval overrides supplied with a profile patch", () => {
484 const next = applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
485 backgroundActivityProfile: "performance",
486 automaticGitFetchInterval: Duration.seconds(0),
487 providerHealthRefreshInterval: Duration.minutes(4),
488 });
489​
490 expect(next.backgroundActivity).toEqual({
491 schemaVersion: 1,
492 profile: "custom",
493 baseProfile: "performance",
494 overrides: {
495 automaticGitFetchInterval: Duration.seconds(0),
496 providerHealthRefreshInterval: Duration.minutes(4),
497 },
498 });
499 });
500​
501 it("ignores overrides attached to a concrete background profile", () => {
502 const resolved = resolveServerBackgroundActivitySettings({
503 ...DEFAULT_SERVER_SETTINGS,
504 backgroundActivity: {
505 schemaVersion: 1,
506 profile: "balanced",
507 overrides: {
508 pauseWhenOnBattery: true,
509 },
510 },
511 });
512​
513 expect(resolved.pauseWhenOnBattery).toBe(false);
514 });
515});