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

serverSettings.test.ts

245 lines7.3 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { DEFAULT_SERVER_SETTINGS, ServerSettingsPatch } from "@t3tools/contracts";
3import { assert, it } from "@effect/vitest";
4import { Effect, FileSystem, Layer, Schema } from "effect";
5import { ServerConfig } from "./config";
6import { ServerSettingsLive, ServerSettingsService } from "./serverSettings";
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 });
96 assert.deepEqual(next.textGenerationModelSelection, {
97 provider: "codex",
98 model: DEFAULT_SERVER_SETTINGS.textGenerationModelSelection.model,
99 options: {
100 reasoningEffort: "high",
101 fastMode: false,
102 },
103 });
104 }).pipe(Effect.provide(makeServerSettingsLayer())),
105 );
106​
107 it.effect("preserves model when switching providers via textGenerationModelSelection", () =>
108 Effect.gen(function* () {
109 const serverSettings = yield* ServerSettingsService;
110​
111 // Start with Claude text generation selection
112 yield* serverSettings.updateSettings({
113 textGenerationModelSelection: {
114 provider: "claudeAgent",
115 model: "claude-sonnet-4-6",
116 options: {
117 effort: "high",
118 },
119 },
120 });
121​
122 // Switch to Codex — the stale Claude "effort" in options must not
123 // cause the update to lose the selected model.
124 const next = yield* serverSettings.updateSettings({
125 textGenerationModelSelection: {
126 provider: "codex",
127 model: "gpt-5.4",
128 options: {
129 reasoningEffort: "high",
130 },
131 },
132 });
133​
134 assert.deepEqual(next.textGenerationModelSelection, {
135 provider: "codex",
136 model: "gpt-5.4",
137 options: {
138 reasoningEffort: "high",
139 },
140 });
141 }).pipe(Effect.provide(makeServerSettingsLayer())),
142 );
143​
144 it.effect("trims provider path settings when updates are applied", () =>
145 Effect.gen(function* () {
146 const serverSettings = yield* ServerSettingsService;
147​
148 const next = yield* serverSettings.updateSettings({
149 providers: {
150 codex: {
151 binaryPath: " /opt/homebrew/bin/codex ",
152 homePath: " ",
153 },
154 claudeAgent: {
155 binaryPath: " /opt/homebrew/bin/claude ",
156 },
157 },
158 });
159​
160 assert.deepEqual(next.providers.codex, {
161 enabled: true,
162 binaryPath: "/opt/homebrew/bin/codex",
163 homePath: "",
164 customModels: [],
165 });
166 assert.deepEqual(next.providers.claudeAgent, {
167 enabled: true,
168 binaryPath: "/opt/homebrew/bin/claude",
169 customModels: [],
170 });
171 }).pipe(Effect.provide(makeServerSettingsLayer())),
172 );
173​
174 it.effect("trims observability settings when updates are applied", () =>
175 Effect.gen(function* () {
176 const serverSettings = yield* ServerSettingsService;
177​
178 const next = yield* serverSettings.updateSettings({
179 observability: {
180 otlpTracesUrl: " http://localhost:4318/v1/traces ",
181 otlpMetricsUrl: " http://localhost:4318/v1/metrics ",
182 },
183 });
184​
185 assert.deepEqual(next.observability, {
186 otlpTracesUrl: "http://localhost:4318/v1/traces",
187 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
188 });
189 }).pipe(Effect.provide(makeServerSettingsLayer())),
190 );
191​
192 it.effect("defaults blank binary paths to provider executables", () =>
193 Effect.gen(function* () {
194 const serverSettings = yield* ServerSettingsService;
195​
196 const next = yield* serverSettings.updateSettings({
197 providers: {
198 codex: {
199 binaryPath: " ",
200 },
201 claudeAgent: {
202 binaryPath: "",
203 },
204 },
205 });
206​
207 assert.equal(next.providers.codex.binaryPath, "codex");
208 assert.equal(next.providers.claudeAgent.binaryPath, "claude");
209 }).pipe(Effect.provide(makeServerSettingsLayer())),
210 );
211​
212 it.effect("writes only non-default server settings to disk", () =>
213 Effect.gen(function* () {
214 const serverSettings = yield* ServerSettingsService;
215 const serverConfig = yield* ServerConfig;
216 const fileSystem = yield* FileSystem.FileSystem;
217 const next = yield* serverSettings.updateSettings({
218 observability: {
219 otlpTracesUrl: "http://localhost:4318/v1/traces",
220 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
221 },
222 providers: {
223 codex: {
224 binaryPath: "/opt/homebrew/bin/codex",
225 },
226 },
227 });
228​
229 assert.equal(next.providers.codex.binaryPath, "/opt/homebrew/bin/codex");
230​
231 const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
232 assert.deepEqual(JSON.parse(raw), {
233 observability: {
234 otlpTracesUrl: "http://localhost:4318/v1/traces",
235 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
236 },
237 providers: {
238 codex: {
239 binaryPath: "/opt/homebrew/bin/codex",
240 },
241 },
242 });
243 }).pipe(Effect.provide(makeServerSettingsLayer())),
244 );
245});