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

serverSettings.test.ts

300 lines9.3 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { DEFAULT_SERVER_SETTINGS, ServerSettingsPatch } from "@workbench/contracts";
3import { assert, it } from "@effect/vitest";
4import { Effect, FileSystem, Layer, Schema } from "effect";
5import { ServerConfig } from "./config.ts";
6import { ServerSettingsLive, ServerSettingsService } from "./serverSettings.ts";
7​
8const makeServerSettingsLayer = () =>
9 ServerSettingsLive.pipe(
10 Layer.provideMerge(
11 Layer.fresh(
12 ServerConfig.layerTest(process.cwd(), {
13 prefix: "t3code-server-settings-test-",
14 }),
15 ),
16 ),
17 );
18​
19it.layer(NodeServices.layer)("server settings", (it) => {
20 it.effect("decodes nested settings patches", () =>
21 Effect.sync(() => {
22 const decodePatch = Schema.decodeUnknownSync(ServerSettingsPatch);
23​
24 assert.deepEqual(decodePatch({ providers: { codex: { binaryPath: "/tmp/codex" } } }), {
25 providers: { codex: { binaryPath: "/tmp/codex" } },
26 });
27​
28 assert.deepEqual(
29 decodePatch({
30 textGenerationModelSelection: {
31 options: {
32 fastMode: false,
33 },
34 },
35 }),
36 {
37 textGenerationModelSelection: {
38 options: {
39 fastMode: false,
40 },
41 },
42 },
43 );
44 }),
45 );
46​
47 it.effect("deep merges nested settings updates without dropping siblings", () =>
48 Effect.gen(function* () {
49 const serverSettings = yield* ServerSettingsService;
50​
51 yield* serverSettings.updateSettings({
52 providers: {
53 codex: {
54 binaryPath: "/usr/local/bin/codex",
55 homePath: "/Users/julius/.codex",
56 },
57 claudeAgent: {
58 binaryPath: "/usr/local/bin/claude",
59 customModels: ["claude-custom"],
60 },
61 },
62 textGenerationModelSelection: {
63 provider: "codex",
64 model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
65 options: {
66 reasoningEffort: "high",
67 fastMode: true,
68 },
69 },
70 });
71​
72 const next = yield* serverSettings.updateSettings({
73 providers: {
74 codex: {
75 binaryPath: "/opt/homebrew/bin/codex",
76 },
77 },
78 textGenerationModelSelection: {
79 options: {
80 fastMode: false,
81 },
82 },
83 });
84​
85 assert.deepEqual(next.providers.codex, {
86 enabled: true,
87 binaryPath: "/opt/homebrew/bin/codex",
88 homePath: "/Users/julius/.codex",
89 customModels: [],
90 });
91 assert.deepEqual(next.providers.claudeAgent, {
92 enabled: true,
93 binaryPath: "/usr/local/bin/claude",
94 customModels: ["claude-custom"],
95 launchArgs: "",
96 });
97 assert.deepEqual(next.textGenerationModelSelection, {
98 provider: "codex",
99 model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
100 options: {
101 reasoningEffort: "high",
102 fastMode: false,
103 },
104 });
105 }).pipe(Effect.provide(makeServerSettingsLayer())),
106 );
107​
108 it.effect("preserves model when switching providers via textGenerationModelSelection", () =>
109 Effect.gen(function* () {
110 const serverSettings = yield* ServerSettingsService;
111​
112 // Start with Claude text generation selection
113 yield* serverSettings.updateSettings({
114 textGenerationModelSelection: {
115 provider: "claudeAgent",
116 model: "claude-sonnet-4-6",
117 options: {
118 effort: "high",
119 },
120 },
121 });
122​
123 // Switch to Codex — the stale Claude "effort" in options must not
124 // cause the update to lose the selected model.
125 const next = yield* serverSettings.updateSettings({
126 textGenerationModelSelection: {
127 provider: "codex",
128 model: "gpt-5.4",
129 options: {
130 reasoningEffort: "high",
131 },
132 },
133 });
134​
135 assert.deepEqual(next.textGenerationModelSelection, {
136 provider: "codex",
137 model: "gpt-5.4",
138 options: {
139 reasoningEffort: "high",
140 },
141 });
142 }).pipe(Effect.provide(makeServerSettingsLayer())),
143 );
144​
145 it.effect("drops stale text generation options when resetting model selection", () =>
146 Effect.gen(function* () {
147 const serverSettings = yield* ServerSettingsService;
148​
149 yield* serverSettings.updateSettings({
150 textGenerationModelSelection: {
151 provider: "codex",
152 model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
153 options: {
154 reasoningEffort: "high",
155 fastMode: true,
156 },
157 },
158 });
159​
160 const next = yield* serverSettings.updateSettings({
161 textGenerationModelSelection: {
162 provider: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.provider,
163 model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
164 },
165 });
166​
167 assert.deepEqual(next.textGenerationModelSelection, {
168 provider: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.provider,
169 model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
170 });
171 }).pipe(Effect.provide(makeServerSettingsLayer())),
172 );
173​
174 it.effect("trims provider path settings when updates are applied", () =>
175 Effect.gen(function* () {
176 const serverSettings = yield* ServerSettingsService;
177​
178 const next = yield* serverSettings.updateSettings({
179 providers: {
180 codex: {
181 binaryPath: " /opt/homebrew/bin/codex ",
182 homePath: " ",
183 },
184 claudeAgent: {
185 binaryPath: " /opt/homebrew/bin/claude ",
186 },
187 opencode: {
188 binaryPath: " /opt/homebrew/bin/opencode ",
189 serverUrl: " http://127.0.0.1:4096 ",
190 serverPassword: " secret-password ",
191 },
192 },
193 });
194​
195 assert.deepEqual(next.providers.codex, {
196 enabled: true,
197 binaryPath: "/opt/homebrew/bin/codex",
198 homePath: "",
199 customModels: [],
200 });
201 assert.deepEqual(next.providers.claudeAgent, {
202 enabled: true,
203 binaryPath: "/opt/homebrew/bin/claude",
204 customModels: [],
205 launchArgs: "",
206 });
207 assert.deepEqual(next.providers.opencode, {
208 enabled: true,
209 binaryPath: "/opt/homebrew/bin/opencode",
210 serverUrl: "http://127.0.0.1:4096",
211 serverPassword: "secret-password",
212 customModels: [],
213 });
214 }).pipe(Effect.provide(makeServerSettingsLayer())),
215 );
216​
217 it.effect("trims observability settings when updates are applied", () =>
218 Effect.gen(function* () {
219 const serverSettings = yield* ServerSettingsService;
220​
221 const next = yield* serverSettings.updateSettings({
222 addProjectBaseDirectory: " ~/Development ",
223 observability: {
224 otlpTracesUrl: " http://localhost:4318/v1/traces ",
225 otlpMetricsUrl: " http://localhost:4318/v1/metrics ",
226 },
227 });
228​
229 assert.equal(next.addProjectBaseDirectory, "~/Development");
230 assert.deepEqual(next.observability, {
231 otlpTracesUrl: "http://localhost:4318/v1/traces",
232 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
233 });
234 }).pipe(Effect.provide(makeServerSettingsLayer())),
235 );
236​
237 it.effect("defaults blank binary paths to provider executables", () =>
238 Effect.gen(function* () {
239 const serverSettings = yield* ServerSettingsService;
240​
241 const next = yield* serverSettings.updateSettings({
242 providers: {
243 codex: {
244 binaryPath: " ",
245 },
246 claudeAgent: {
247 binaryPath: "",
248 },
249 },
250 });
251​
252 assert.equal(next.providers.codex.binaryPath, "codex");
253 assert.equal(next.providers.claudeAgent.binaryPath, "claude");
254 }).pipe(Effect.provide(makeServerSettingsLayer())),
255 );
256​
257 it.effect("writes only non-default server settings to disk", () =>
258 Effect.gen(function* () {
259 const serverSettings = yield* ServerSettingsService;
260 const serverConfig = yield* ServerConfig;
261 const fileSystem = yield* FileSystem.FileSystem;
262 const next = yield* serverSettings.updateSettings({
263 addProjectBaseDirectory: "~/Development",
264 observability: {
265 otlpTracesUrl: "http://localhost:4318/v1/traces",
266 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
267 },
268 providers: {
269 codex: {
270 binaryPath: "/opt/homebrew/bin/codex",
271 },
272 opencode: {
273 serverUrl: "http://127.0.0.1:4096",
274 serverPassword: "secret-password",
275 },
276 },
277 });
278​
279 assert.equal(next.providers.codex.binaryPath, "/opt/homebrew/bin/codex");
280​
281 const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
282 assert.deepEqual(JSON.parse(raw), {
283 addProjectBaseDirectory: "~/Development",
284 observability: {
285 otlpTracesUrl: "http://localhost:4318/v1/traces",
286 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
287 },
288 providers: {
289 codex: {
290 binaryPath: "/opt/homebrew/bin/codex",
291 },
292 opencode: {
293 serverUrl: "http://127.0.0.1:4096",
294 serverPassword: "secret-password",
295 },
296 },
297 });
298 }).pipe(Effect.provide(makeServerSettingsLayer())),
299 );
300});