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

settings.test.ts

94 lines3.6 KB

1import { describe, expect, it } from "vitest";
2import * as Schema from "effect/Schema";
3​
4import { ProviderInstanceId } from "./providerInstance.ts";
5import { DEFAULT_SERVER_SETTINGS, ServerSettings, ServerSettingsPatch } from "./settings.ts";
6​
7const decodeServerSettings = Schema.decodeUnknownSync(ServerSettings);
8const decodeServerSettingsPatch = Schema.decodeUnknownSync(ServerSettingsPatch);
9​
10describe("ServerSettings.providerInstances (slice-2 invariant)", () => {
11 it("defaults to an empty record so legacy configs without the key still decode", () => {
12 expect(DEFAULT_SERVER_SETTINGS.providerInstances).toEqual({});
13 });
14​
15 it("decodes a fully empty config (legacy on-disk shape) without complaint", () => {
16 const decoded = decodeServerSettings({});
17 expect(decoded.providerInstances).toEqual({});
18 // Legacy `providers` struct is still hydrated with its per-driver defaults
19 // so existing call sites keep working through the migration.
20 expect(decoded.providers.codex.enabled).toBe(true);
21 });
22​
23 it("decodes a multi-instance map mixing first-party and fork drivers", () => {
24 const decoded = decodeServerSettings({
25 providerInstances: {
26 codex_personal: {
27 driver: "codex",
28 displayName: "Codex (personal)",
29 config: { homePath: "~/.codex_personal" },
30 },
31 codex_work: {
32 driver: "codex",
33 config: { homePath: "~/.codex_work" },
34 },
35 ollama_local: {
36 driver: "ollama",
37 displayName: "Ollama (local)",
38 config: { endpoint: "http://localhost:11434" },
39 },
40 },
41 });
42 const personalId = ProviderInstanceId.make("codex_personal");
43 const workId = ProviderInstanceId.make("codex_work");
44 const ollamaId = ProviderInstanceId.make("ollama_local");
45​
46 expect(decoded.providerInstances[personalId]?.driver).toBe("codex");
47 expect(decoded.providerInstances[workId]?.config).toEqual({ homePath: "~/.codex_work" });
48 // Critical: a config naming a driver this build does not know about
49 // (`ollama` is not in `ProviderDriverKind`) must round-trip without loss.
50 // The runtime handles "driver not installed" — the schema must not.
51 expect(decoded.providerInstances[ollamaId]?.driver).toBe("ollama");
52 expect(decoded.providerInstances[ollamaId]?.config).toEqual({
53 endpoint: "http://localhost:11434",
54 });
55 });
56​
57 it("rejects instance keys that violate the slug pattern", () => {
58 expect(() =>
59 decodeServerSettings({
60 providerInstances: { "1bad": { driver: "codex" } },
61 }),
62 ).toThrow();
63 });
64});
65​
66describe("ServerSettingsPatch.providerInstances", () => {
67 it("treats providerInstances as an optional whole-map replacement", () => {
68 const patch = decodeServerSettingsPatch({});
69 expect(patch.providerInstances).toBeUndefined();
70​
71 const replacement = decodeServerSettingsPatch({
72 providerInstances: {
73 codex_personal: { driver: "codex", config: { homePath: "~/.codex" } },
74 },
75 });
76 expect(replacement.providerInstances).toBeDefined();
77 expect(replacement.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.driver).toBe(
78 "codex",
79 );
80 });
81​
82 it("preserves a fork-defined driver entry through patch decoding", () => {
83 const patch = decodeServerSettingsPatch({
84 providerInstances: {
85 ollama_local: {
86 driver: "ollama",
87 config: { endpoint: "http://localhost:11434" },
88 },
89 },
90 });
91 const ollamaId = ProviderInstanceId.make("ollama_local");
92 expect(patch.providerInstances?.[ollamaId]?.driver).toBe("ollama");
93 });
94});