Open History to see the last commit
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("defaults blank binary paths to provider executables", () =>
175 Effect.gen(function* () {
176 const serverSettings = yield* ServerSettingsService;
177
178 const next = yield* serverSettings.updateSettings({
179 providers: {
180 codex: {
181 binaryPath: " ",
182 },
183 claudeAgent: {
184 binaryPath: "",
185 },
186 },
187 });
188
189 assert.equal(next.providers.codex.binaryPath, "codex");
190 assert.equal(next.providers.claudeAgent.binaryPath, "claude");
191 }).pipe(Effect.provide(makeServerSettingsLayer())),
192 );
193
194 it.effect("writes only non-default server settings to disk", () =>
195 Effect.gen(function* () {
196 const serverSettings = yield* ServerSettingsService;
197 const serverConfig = yield* ServerConfig;
198 const fileSystem = yield* FileSystem.FileSystem;
199 const next = yield* serverSettings.updateSettings({
200 providers: {
201 codex: {
202 binaryPath: "/opt/homebrew/bin/codex",
203 },
204 },
205 });
206
207 assert.equal(next.providers.codex.binaryPath, "/opt/homebrew/bin/codex");
208
209 const raw = yield* fileSystem.readFileString(serverConfig.settingsPath);
210 assert.deepEqual(JSON.parse(raw), {
211 providers: {
212 codex: {
213 binaryPath: "/opt/homebrew/bin/codex",
214 },
215 },
216 });
217 }).pipe(Effect.provide(makeServerSettingsLayer())),
218 );
219});