Open History to see the last commit
1import { DEFAULT_SERVER_SETTINGS, ProviderDriverKind, ProviderInstanceId } from "@v12/contracts";
2import { describe, expect, it } from "vite-plus/test";
3import { createModelSelection } from "./model.ts";
4import {
5 applyServerSettingsPatch,
6 extractPersistedServerObservabilitySettings,
7 normalizePersistedServerSettingString,
8 parsePersistedServerObservabilitySettings,
9} from "./serverSettings.ts";
10
11describe("serverSettings helpers", () => {
12 it("normalizes optional persisted strings", () => {
13 expect(normalizePersistedServerSettingString(undefined)).toBeUndefined();
14 expect(normalizePersistedServerSettingString(" ")).toBeUndefined();
15 expect(normalizePersistedServerSettingString(" http://localhost:4318/v1/traces ")).toBe(
16 "http://localhost:4318/v1/traces",
17 );
18 });
19
20 it("extracts persisted observability settings", () => {
21 expect(
22 extractPersistedServerObservabilitySettings({
23 observability: {
24 otlpTracesUrl: " http://localhost:4318/v1/traces ",
25 otlpMetricsUrl: " http://localhost:4318/v1/metrics ",
26 },
27 }),
28 ).toEqual({
29 otlpTracesUrl: "http://localhost:4318/v1/traces",
30 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
31 });
32 });
33
34 it("parses lenient persisted settings JSON", () => {
35 expect(
36 parsePersistedServerObservabilitySettings(
37 JSON.stringify({
38 observability: {
39 otlpTracesUrl: "http://localhost:4318/v1/traces",
40 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
41 },
42 }),
43 ),
44 ).toEqual({
45 otlpTracesUrl: "http://localhost:4318/v1/traces",
46 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
47 });
48 });
49
50 it("falls back cleanly when persisted settings are invalid", () => {
51 expect(parsePersistedServerObservabilitySettings("{")).toEqual({
52 otlpTracesUrl: undefined,
53 otlpMetricsUrl: undefined,
54 });
55 });
56
57 it("replaces text generation selection when provider/model are provided", () => {
58 const current = {
59 ...DEFAULT_SERVER_SETTINGS,
60 textGenerationModelSelection: createModelSelection(
61 ProviderInstanceId.make("codex"),
62 "gpt-5.4-mini",
63 [
64 { id: "reasoningEffort", value: "high" },
65 { id: "fastMode", value: true },
66 ],
67 ),
68 };
69
70 expect(
71 applyServerSettingsPatch(current, {
72 textGenerationModelSelection: {
73 instanceId: ProviderInstanceId.make("codex"),
74 model: "gpt-5.4-mini",
75 },
76 }).textGenerationModelSelection,
77 ).toEqual({
78 instanceId: "codex",
79 model: "gpt-5.4-mini",
80 });
81 });
82
83 it("still deep merges text generation selection when only options are provided", () => {
84 const current = {
85 ...DEFAULT_SERVER_SETTINGS,
86 textGenerationModelSelection: createModelSelection(
87 ProviderInstanceId.make("codex"),
88 "gpt-5.4-mini",
89 [
90 { id: "reasoningEffort", value: "high" },
91 { id: "fastMode", value: true },
92 ],
93 ),
94 };
95
96 expect(
97 applyServerSettingsPatch(current, {
98 textGenerationModelSelection: {
99 options: [{ id: "fastMode", value: false }],
100 },
101 }).textGenerationModelSelection,
102 ).toEqual({
103 instanceId: "codex",
104 model: "gpt-5.4-mini",
105 options: [
106 { id: "reasoningEffort", value: "high" },
107 { id: "fastMode", value: false },
108 ],
109 });
110 });
111
112 it("replaces text generation selection across providers without leaking stale options", () => {
113 const current = {
114 ...DEFAULT_SERVER_SETTINGS,
115 textGenerationModelSelection: createModelSelection(
116 ProviderInstanceId.make("codex"),
117 "gpt-5.4-mini",
118 [
119 { id: "reasoningEffort", value: "high" },
120 { id: "fastMode", value: true },
121 ],
122 ),
123 };
124
125 expect(
126 applyServerSettingsPatch(current, {
127 textGenerationModelSelection: {
128 instanceId: ProviderInstanceId.make("opencode"),
129 model: "openai/gpt-5",
130 },
131 }).textGenerationModelSelection,
132 ).toEqual({
133 instanceId: "opencode",
134 model: "openai/gpt-5",
135 });
136 });
137
138 it("accepts array-based text generation selection patches", () => {
139 expect(
140 applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
141 textGenerationModelSelection: {
142 instanceId: ProviderInstanceId.make("opencode"),
143 model: "openai/gpt-5",
144 options: [
145 { id: "variant", value: "prod" },
146 { id: "agent", value: "build" },
147 ],
148 },
149 }).textGenerationModelSelection,
150 ).toEqual({
151 instanceId: "opencode",
152 model: "openai/gpt-5",
153 options: [
154 { id: "variant", value: "prod" },
155 { id: "agent", value: "build" },
156 ],
157 });
158 });
159
160 it("replaces providerInstances maps so omitted instance fields are cleared", () => {
161 const codexId = ProviderInstanceId.make("codex");
162 const current = {
163 ...DEFAULT_SERVER_SETTINGS,
164 providerInstances: {
165 [codexId]: {
166 driver: ProviderDriverKind.make("codex"),
167 displayName: "Codex Work",
168 accentColor: "#7c3aed",
169 enabled: true,
170 config: { homePath: "~/.codex" },
171 },
172 },
173 };
174
175 expect(
176 applyServerSettingsPatch(current, {
177 providerInstances: {
178 [codexId]: {
179 driver: ProviderDriverKind.make("codex"),
180 displayName: "Codex Work",
181 enabled: true,
182 config: { homePath: "~/.codex" },
183 },
184 },
185 }).providerInstances[codexId],
186 ).toEqual({
187 driver: ProviderDriverKind.make("codex"),
188 displayName: "Codex Work",
189 enabled: true,
190 config: { homePath: "~/.codex" },
191 });
192 });
193});