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

serverSettings.test.ts

197 lines5.7 KB

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