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

serverSettings.test.ts

298 lines9.0 KB

1import {
2 DEFAULT_SERVER_SETTINGS,
3 ProviderDriverKind,
4 ProviderInstanceId,
5 type ServerProvider,
6} from "@t3tools/contracts";
7import { describe, expect, it } from "vite-plus/test";
8import { createModelSelection } from "./model.ts";
9import {
10 applyServerSettingsPatch,
11 extractPersistedServerObservabilitySettings,
12 isModelSelectionProviderEnabled,
13 normalizePersistedServerSettingString,
14 parsePersistedServerObservabilitySettings,
15 resolveSourceControlWriterModelSelection,
16} from "./serverSettings.ts";
17​
18describe("serverSettings helpers", () => {
19 it("normalizes optional persisted strings", () => {
20 expect(normalizePersistedServerSettingString(undefined)).toBeUndefined();
21 expect(normalizePersistedServerSettingString(" ")).toBeUndefined();
22 expect(normalizePersistedServerSettingString(" http://localhost:4318/v1/traces ")).toBe(
23 "http://localhost:4318/v1/traces",
24 );
25 });
26​
27 it("extracts persisted observability settings", () => {
28 expect(
29 extractPersistedServerObservabilitySettings({
30 observability: {
31 otlpTracesUrl: " http://localhost:4318/v1/traces ",
32 otlpMetricsUrl: " http://localhost:4318/v1/metrics ",
33 },
34 }),
35 ).toEqual({
36 otlpTracesUrl: "http://localhost:4318/v1/traces",
37 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
38 });
39 });
40​
41 it("parses lenient persisted settings JSON", () => {
42 expect(
43 parsePersistedServerObservabilitySettings(
44 JSON.stringify({
45 observability: {
46 otlpTracesUrl: "http://localhost:4318/v1/traces",
47 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
48 },
49 }),
50 ),
51 ).toEqual({
52 otlpTracesUrl: "http://localhost:4318/v1/traces",
53 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
54 });
55 });
56​
57 it("falls back cleanly when persisted settings are invalid", () => {
58 expect(parsePersistedServerObservabilitySettings("{")).toEqual({
59 otlpTracesUrl: undefined,
60 otlpMetricsUrl: undefined,
61 });
62 });
63​
64 it("replaces text generation selection when provider/model are provided", () => {
65 const current = {
66 ...DEFAULT_SERVER_SETTINGS,
67 textGenerationModelSelection: createModelSelection(
68 ProviderInstanceId.make("codex"),
69 "gpt-5.4-mini",
70 [
71 { id: "reasoningEffort", value: "high" },
72 { id: "fastMode", value: true },
73 ],
74 ),
75 };
76​
77 expect(
78 applyServerSettingsPatch(current, {
79 textGenerationModelSelection: {
80 instanceId: ProviderInstanceId.make("codex"),
81 model: "gpt-5.4-mini",
82 },
83 }).textGenerationModelSelection,
84 ).toEqual({
85 instanceId: "codex",
86 model: "gpt-5.4-mini",
87 });
88 });
89​
90 it("still deep merges text generation selection when only options are provided", () => {
91 const current = {
92 ...DEFAULT_SERVER_SETTINGS,
93 textGenerationModelSelection: createModelSelection(
94 ProviderInstanceId.make("codex"),
95 "gpt-5.4-mini",
96 [
97 { id: "reasoningEffort", value: "high" },
98 { id: "fastMode", value: true },
99 ],
100 ),
101 };
102​
103 expect(
104 applyServerSettingsPatch(current, {
105 textGenerationModelSelection: {
106 options: [{ id: "fastMode", value: false }],
107 },
108 }).textGenerationModelSelection,
109 ).toEqual({
110 instanceId: "codex",
111 model: "gpt-5.4-mini",
112 options: [
113 { id: "reasoningEffort", value: "high" },
114 { id: "fastMode", value: false },
115 ],
116 });
117 });
118​
119 it("replaces text generation selection across providers without leaking stale options", () => {
120 const current = {
121 ...DEFAULT_SERVER_SETTINGS,
122 textGenerationModelSelection: createModelSelection(
123 ProviderInstanceId.make("codex"),
124 "gpt-5.4-mini",
125 [
126 { id: "reasoningEffort", value: "high" },
127 { id: "fastMode", value: true },
128 ],
129 ),
130 };
131​
132 expect(
133 applyServerSettingsPatch(current, {
134 textGenerationModelSelection: {
135 instanceId: ProviderInstanceId.make("opencode"),
136 model: "openai/gpt-5",
137 },
138 }).textGenerationModelSelection,
139 ).toEqual({
140 instanceId: "opencode",
141 model: "openai/gpt-5",
142 });
143 });
144​
145 it("accepts array-based text generation selection patches", () => {
146 expect(
147 applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
148 textGenerationModelSelection: {
149 instanceId: ProviderInstanceId.make("opencode"),
150 model: "openai/gpt-5",
151 options: [
152 { id: "variant", value: "prod" },
153 { id: "agent", value: "build" },
154 ],
155 },
156 }).textGenerationModelSelection,
157 ).toEqual({
158 instanceId: "opencode",
159 model: "openai/gpt-5",
160 options: [
161 { id: "variant", value: "prod" },
162 { id: "agent", value: "build" },
163 ],
164 });
165 });
166​
167 it("replaces source control writer selection without retaining stale options", () => {
168 const current = {
169 ...DEFAULT_SERVER_SETTINGS,
170 sourceControlWriterModelSelection: createModelSelection(
171 ProviderInstanceId.make("codex"),
172 "gpt-5.4-mini",
173 [{ id: "reasoningEffort", value: "high" }],
174 ),
175 };
176​
177 expect(
178 applyServerSettingsPatch(current, {
179 sourceControlWriterModelSelection: {
180 instanceId: ProviderInstanceId.make("opencode"),
181 model: "openai/gpt-5",
182 },
183 }).sourceControlWriterModelSelection,
184 ).toEqual({
185 instanceId: "opencode",
186 model: "openai/gpt-5",
187 });
188 });
189​
190 it("clears source control writer selection with null", () => {
191 const current = {
192 ...DEFAULT_SERVER_SETTINGS,
193 sourceControlWriterModelSelection: createModelSelection(
194 ProviderInstanceId.make("codex"),
195 "gpt-5.4-mini",
196 ),
197 };
198​
199 expect(
200 applyServerSettingsPatch(current, {
201 sourceControlWriterModelSelection: null,
202 }).sourceControlWriterModelSelection,
203 ).toBeNull();
204 });
205​
206 it("falls back from a disabled source control writer provider without clearing its selection", () => {
207 const instanceId = ProviderInstanceId.make("codex_writer");
208 const sourceControlWriterModelSelection = createModelSelection(instanceId, "gpt-5.4-mini");
209 const settings = {
210 ...DEFAULT_SERVER_SETTINGS,
211 providerInstances: {
212 [instanceId]: {
213 driver: ProviderDriverKind.make("codex"),
214 enabled: false,
215 config: {},
216 },
217 },
218 sourceControlWriterModelSelection,
219 };
220​
221 expect(isModelSelectionProviderEnabled(settings, sourceControlWriterModelSelection)).toBe(
222 false,
223 );
224 expect(resolveSourceControlWriterModelSelection(settings)).toBe(
225 settings.textGenerationModelSelection,
226 );
227 expect(settings.sourceControlWriterModelSelection).toBe(sourceControlWriterModelSelection);
228 });
229​
230 it("falls back from an unavailable source control writer provider", () => {
231 const instanceId = ProviderInstanceId.make("missing_writer");
232 const sourceControlWriterModelSelection = createModelSelection(instanceId, "missing-model");
233 const settings = {
234 ...DEFAULT_SERVER_SETTINGS,
235 providerInstances: {
236 [instanceId]: {
237 driver: ProviderDriverKind.make("missing-driver"),
238 config: {},
239 },
240 },
241 sourceControlWriterModelSelection,
242 };
243 const unavailableProvider = {
244 instanceId,
245 driver: ProviderDriverKind.make("missing-driver"),
246 enabled: false,
247 installed: false,
248 version: null,
249 status: "disabled",
250 auth: { status: "unknown" },
251 checkedAt: "2026-07-27T00:00:00.000Z",
252 availability: "unavailable",
253 unavailableReason: "This provider driver is not available in this build.",
254 models: [],
255 slashCommands: [],
256 skills: [],
257 } satisfies ServerProvider;
258​
259 expect(resolveSourceControlWriterModelSelection(settings, [unavailableProvider])).toBe(
260 settings.textGenerationModelSelection,
261 );
262 expect(settings.sourceControlWriterModelSelection).toBe(sourceControlWriterModelSelection);
263 });
264​
265 it("replaces providerInstances maps so omitted instance fields are cleared", () => {
266 const codexId = ProviderInstanceId.make("codex");
267 const current = {
268 ...DEFAULT_SERVER_SETTINGS,
269 providerInstances: {
270 [codexId]: {
271 driver: ProviderDriverKind.make("codex"),
272 displayName: "Codex Work",
273 accentColor: "#7c3aed",
274 enabled: true,
275 config: { homePath: "~/.codex" },
276 },
277 },
278 };
279​
280 expect(
281 applyServerSettingsPatch(current, {
282 providerInstances: {
283 [codexId]: {
284 driver: ProviderDriverKind.make("codex"),
285 displayName: "Codex Work",
286 enabled: true,
287 config: { homePath: "~/.codex" },
288 },
289 },
290 }).providerInstances[codexId],
291 ).toEqual({
292 driver: ProviderDriverKind.make("codex"),
293 displayName: "Codex Work",
294 enabled: true,
295 config: { homePath: "~/.codex" },
296 });
297 });
298});