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

serverSettings.test.ts

539 lines16.5 KB

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