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

settings.test.ts

188 lines6.7 KB

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