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

settings.test.ts

160 lines5.9 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);
9const encodeServerSettings = Schema.encodeSync(ServerSettings);
10​
11describe("ServerSettings.providerInstances (slice-2 invariant)", () => {
12 it("defaults to an empty record so legacy configs without the key still decode", () => {
13 expect(DEFAULT_SERVER_SETTINGS.providerInstances).toEqual({});
14 });
15​
16 it("decodes a fully empty config (legacy on-disk shape) without complaint", () => {
17 const decoded = decodeServerSettings({});
18 expect(decoded.providerInstances).toEqual({});
19 // Legacy `providers` struct is still hydrated with its per-driver defaults
20 // so existing call sites keep working through the migration.
21 expect(decoded.providers.codex.enabled).toBe(true);
22 });
23​
24 it("decodes a multi-instance map mixing first-party and fork drivers", () => {
25 const decoded = decodeServerSettings({
26 providerInstances: {
27 codex_personal: {
28 driver: "codex",
29 displayName: "Codex (personal)",
30 config: { homePath: "~/.codex_personal" },
31 },
32 codex_work: {
33 driver: "codex",
34 config: { homePath: "~/.codex_work" },
35 },
36 ollama_local: {
37 driver: "ollama",
38 displayName: "Ollama (local)",
39 config: { endpoint: "http://localhost:11434" },
40 },
41 },
42 });
43 const personalId = ProviderInstanceId.make("codex_personal");
44 const workId = ProviderInstanceId.make("codex_work");
45 const ollamaId = ProviderInstanceId.make("ollama_local");
46​
47 expect(decoded.providerInstances[personalId]?.driver).toBe("codex");
48 expect(decoded.providerInstances[workId]?.config).toEqual({ homePath: "~/.codex_work" });
49 // Critical: a config naming a driver this build does not know about
50 // (`ollama` is not in `ProviderDriverKind`) must round-trip without loss.
51 // The runtime handles "driver not installed" — the schema must not.
52 expect(decoded.providerInstances[ollamaId]?.driver).toBe("ollama");
53 expect(decoded.providerInstances[ollamaId]?.config).toEqual({
54 endpoint: "http://localhost:11434",
55 });
56 });
57​
58 it("rejects instance keys that violate the slug pattern", () => {
59 expect(() =>
60 decodeServerSettings({
61 providerInstances: { "1bad": { driver: "codex" } },
62 }),
63 ).toThrow();
64 });
65});
66​
67describe("ServerSettingsPatch.providerInstances", () => {
68 it("treats providerInstances as an optional whole-map replacement", () => {
69 const patch = decodeServerSettingsPatch({});
70 expect(patch.providerInstances).toBeUndefined();
71​
72 const replacement = decodeServerSettingsPatch({
73 providerInstances: {
74 codex_personal: { driver: "codex", config: { homePath: "~/.codex" } },
75 },
76 });
77 expect(replacement.providerInstances).toBeDefined();
78 expect(replacement.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.driver).toBe(
79 "codex",
80 );
81 });
82​
83 it("preserves a fork-defined driver entry through patch decoding", () => {
84 const patch = decodeServerSettingsPatch({
85 providerInstances: {
86 ollama_local: {
87 driver: "ollama",
88 config: { endpoint: "http://localhost:11434" },
89 },
90 },
91 });
92 const ollamaId = ProviderInstanceId.make("ollama_local");
93 expect(patch.providerInstances?.[ollamaId]?.driver).toBe("ollama");
94 });
95});
96​
97describe("ServerSettingsPatch string normalization", () => {
98 it("trims string settings while decoding patches", () => {
99 const patch = decodeServerSettingsPatch({
100 addProjectBaseDirectory: " ~/Development ",
101 textGenerationModelSelection: { model: " gpt-5.4-mini " },
102 observability: {
103 otlpTracesUrl: " http://localhost:4318/v1/traces ",
104 },
105 providers: {
106 codex: {
107 binaryPath: " /opt/homebrew/bin/codex ",
108 homePath: " ~/.codex ",
109 },
110 piAgent: {
111 enabled: true,
112 binaryPath: "",
113 homePath: "",
114 customModels: [],
115 launchArgs: "",
116 },
117 },
118 providerInstances: {
119 codex_personal: {
120 driver: " codex ",
121 displayName: " Codex Personal ",
122 config: { homePath: " ~/.codex-personal " },
123 },
124 },
125 });
126​
127 expect(patch.addProjectBaseDirectory).toBe("~/Development");
128 expect(patch.textGenerationModelSelection?.model).toBe("gpt-5.4-mini");
129 expect(patch.observability?.otlpTracesUrl).toBe("http://localhost:4318/v1/traces");
130 expect(patch.providers?.codex?.binaryPath).toBe("/opt/homebrew/bin/codex");
131 expect(patch.providers?.codex?.homePath).toBe("~/.codex");
132 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.driver).toBe(
133 "codex",
134 );
135 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.displayName).toBe(
136 "Codex Personal",
137 );
138 expect(patch.providerInstances?.[ProviderInstanceId.make("codex_personal")]?.config).toEqual({
139 homePath: " ~/.codex-personal ",
140 });
141 });
142​
143 it("trims encoded server settings values before validation", () => {
144 const defaultSettings = decodeServerSettings({});
145 const encoded = encodeServerSettings({
146 ...defaultSettings,
147 addProjectBaseDirectory: " ~/Development ",
148 providers: {
149 ...defaultSettings.providers,
150 codex: {
151 ...defaultSettings.providers.codex,
152 binaryPath: " /opt/homebrew/bin/codex ",
153 },
154 },
155 });
156​
157 expect(encoded.addProjectBaseDirectory).toBe("~/Development");
158 expect(encoded.providers?.codex?.binaryPath).toBe("/opt/homebrew/bin/codex");
159 });
160});