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

SettingsPanels.logic.test.ts

244 lines8.2 KB

1import {
2 DEFAULT_SERVER_SETTINGS,
3 DEFAULT_UNIFIED_SETTINGS,
4 ProviderDriverKind,
5 ProviderInstanceId,
6 type ProviderInstanceConfig,
7} from "@t3tools/contracts";
8import { getBackgroundActivityPresetSettings } from "@t3tools/shared/backgroundActivitySettings";
9import * as Duration from "effect/Duration";
10import { describe, expect, it } from "vite-plus/test";
11import {
12 backgroundActivitySharedPolicySettings,
13 buildProviderInstanceUpdatePatch,
14 formatDiagnosticsDescription,
15 getChangedTypographySettingLabels,
16 hasChangedBackgroundActivitySettings,
17 isProjectGroupingEnabled,
18 projectGroupingModeFromToggle,
19 resolveBackgroundActivityProfileOption,
20} from "./SettingsPanels.logic";
21​
22describe("typography settings restore", () => {
23 it("detects family and size changes by font row", () => {
24 expect(getChangedTypographySettingLabels(DEFAULT_UNIFIED_SETTINGS)).toEqual([]);
25 expect(
26 getChangedTypographySettingLabels({
27 ...DEFAULT_UNIFIED_SETTINGS,
28 fontSizeInterface: 18,
29 fontFamilyCode: "Fira Code",
30 }),
31 ).toEqual(["Interface font", "Code font"]);
32 });
33});
34​
35describe("background activity settings restore", () => {
36 it("detects legacy interval values even when the structured setting is at its default", () => {
37 expect(
38 hasChangedBackgroundActivitySettings({
39 backgroundActivity: DEFAULT_UNIFIED_SETTINGS.backgroundActivity,
40 backgroundActivityProfile: DEFAULT_UNIFIED_SETTINGS.backgroundActivityProfile,
41 automaticGitFetchInterval: Duration.seconds(45),
42 providerHealthRefreshInterval: DEFAULT_UNIFIED_SETTINGS.providerHealthRefreshInterval,
43 }),
44 ).toBe(true);
45 expect(
46 hasChangedBackgroundActivitySettings({
47 backgroundActivity: DEFAULT_UNIFIED_SETTINGS.backgroundActivity,
48 backgroundActivityProfile: DEFAULT_UNIFIED_SETTINGS.backgroundActivityProfile,
49 automaticGitFetchInterval: DEFAULT_UNIFIED_SETTINGS.automaticGitFetchInterval,
50 providerHealthRefreshInterval: Duration.minutes(7),
51 }),
52 ).toBe(true);
53 expect(hasChangedBackgroundActivitySettings(DEFAULT_UNIFIED_SETTINGS)).toBe(false);
54 });
55​
56 it("detects a legacy profile override so restoring defaults clears it", () => {
57 expect(
58 hasChangedBackgroundActivitySettings({
59 ...DEFAULT_UNIFIED_SETTINGS,
60 backgroundActivityProfile: "performance",
61 }),
62 ).toBe(true);
63 });
64​
65 it("shows the effective legacy preset and marks custom legacy intervals as advanced", () => {
66 const performance = getBackgroundActivityPresetSettings("performance");
67 expect(
68 resolveBackgroundActivityProfileOption({
69 ...DEFAULT_UNIFIED_SETTINGS,
70 backgroundActivity: DEFAULT_UNIFIED_SETTINGS.backgroundActivity,
71 backgroundActivityProfile: "performance",
72 automaticGitFetchInterval: performance.automaticGitFetchInterval,
73 providerHealthRefreshInterval: performance.providerHealthRefreshInterval,
74 }),
75 ).toBe("performance");
76​
77 expect(
78 resolveBackgroundActivityProfileOption({
79 ...DEFAULT_UNIFIED_SETTINGS,
80 backgroundActivity: DEFAULT_UNIFIED_SETTINGS.backgroundActivity,
81 backgroundActivityProfile: "performance",
82 automaticGitFetchInterval: Duration.seconds(45),
83 providerHealthRefreshInterval: Duration.minutes(7),
84 }),
85 ).toBe("advanced");
86 });
87​
88 it("preserves advanced overrides when the shared policy changes", () => {
89 const automaticGitFetchInterval = Duration.seconds(42);
90 expect(
91 backgroundActivitySharedPolicySettings(
92 {
93 ...DEFAULT_UNIFIED_SETTINGS,
94 backgroundActivity: {
95 schemaVersion: 1,
96 profile: "custom",
97 baseProfile: "balanced",
98 overrides: {
99 automaticGitFetchInterval,
100 pauseWhenOnBattery: true,
101 },
102 },
103 },
104 "performance",
105 ),
106 ).toEqual({
107 schemaVersion: 1,
108 profile: "custom",
109 baseProfile: "performance",
110 overrides: {
111 automaticGitFetchInterval,
112 pauseWhenOnBattery: true,
113 },
114 });
115 });
116​
117 it("materializes legacy advanced overrides before changing the shared policy", () => {
118 const automaticGitFetchInterval = Duration.seconds(42);
119 expect(
120 backgroundActivitySharedPolicySettings(
121 {
122 ...DEFAULT_UNIFIED_SETTINGS,
123 automaticGitFetchInterval,
124 },
125 "battery-saver",
126 ),
127 ).toEqual({
128 schemaVersion: 1,
129 profile: "custom",
130 baseProfile: "battery-saver",
131 overrides: {
132 automaticGitFetchInterval,
133 },
134 });
135 });
136});
137​
138describe("project grouping toggle", () => {
139 it("enables repository grouping and disables into separate projects", () => {
140 expect(isProjectGroupingEnabled("repository")).toBe(true);
141 expect(isProjectGroupingEnabled("repository_path")).toBe(true);
142 expect(isProjectGroupingEnabled("separate")).toBe(false);
143 expect(projectGroupingModeFromToggle(true)).toBe("repository");
144 expect(projectGroupingModeFromToggle(false)).toBe("separate");
145 });
146​
147 it("restores repository path grouping when the toggle is cycled", () => {
148 expect(projectGroupingModeFromToggle(false, "repository_path")).toBe("separate");
149 expect(projectGroupingModeFromToggle(true, "repository_path")).toBe("repository_path");
150 });
151});
152​
153describe("formatDiagnosticsDescription", () => {
154 it("collapses trace and metric URLs that share the same OTEL base path", () => {
155 expect(
156 formatDiagnosticsDescription({
157 localTracingEnabled: true,
158 otlpTracesEnabled: true,
159 otlpTracesUrl: "http://localhost:4318/v1/traces",
160 otlpMetricsEnabled: true,
161 otlpMetricsUrl: "http://localhost:4318/v1/metrics",
162 }),
163 ).toBe("Local trace file. Exporting OTEL to http://localhost:4318/v1/{traces,metrics}.");
164 });
165​
166 it("keeps separate trace and metric URLs when their base paths differ", () => {
167 expect(
168 formatDiagnosticsDescription({
169 localTracingEnabled: true,
170 otlpTracesEnabled: true,
171 otlpTracesUrl: "http://localhost:4318/v1/traces",
172 otlpMetricsEnabled: true,
173 otlpMetricsUrl: "http://localhost:9000/v1/metrics",
174 }),
175 ).toBe(
176 "Local trace file. Exporting OTEL traces to http://localhost:4318/v1/traces and metrics to http://localhost:9000/v1/metrics.",
177 );
178 });
179​
180 it("omits OTEL text when no exporter is enabled", () => {
181 expect(
182 formatDiagnosticsDescription({
183 localTracingEnabled: true,
184 otlpTracesEnabled: false,
185 otlpMetricsEnabled: false,
186 }),
187 ).toBe("Local trace file.");
188 });
189});
190​
191describe("buildProviderInstanceUpdatePatch", () => {
192 it("promotes an edited default provider into providerInstances and resets the legacy provider", () => {
193 const instanceId = ProviderInstanceId.make("codex");
194 const nextInstance = {
195 driver: ProviderDriverKind.make("codex"),
196 enabled: true,
197 config: {
198 binaryPath: "/opt/t3/codex",
199 },
200 } satisfies ProviderInstanceConfig;
201​
202 const patch = buildProviderInstanceUpdatePatch({
203 settings: {
204 ...DEFAULT_SERVER_SETTINGS,
205 providers: {
206 ...DEFAULT_SERVER_SETTINGS.providers,
207 codex: {
208 ...DEFAULT_SERVER_SETTINGS.providers.codex,
209 binaryPath: "/legacy/codex",
210 },
211 },
212 },
213 instanceId,
214 instance: nextInstance,
215 driver: ProviderDriverKind.make("codex"),
216 isDefault: true,
217 });
218​
219 expect(patch.providerInstances?.[instanceId]).toEqual(nextInstance);
220 expect(patch.providers?.codex).toEqual(DEFAULT_SERVER_SETTINGS.providers.codex);
221 });
222​
223 it("updates custom instances without touching legacy provider settings", () => {
224 const instanceId = ProviderInstanceId.make("codex_personal");
225 const nextInstance = {
226 driver: ProviderDriverKind.make("codex"),
227 enabled: true,
228 config: {
229 homePath: "/Users/example/.codex-personal",
230 },
231 } satisfies ProviderInstanceConfig;
232​
233 const patch = buildProviderInstanceUpdatePatch({
234 settings: DEFAULT_SERVER_SETTINGS,
235 instanceId,
236 instance: nextInstance,
237 driver: ProviderDriverKind.make("codex"),
238 isDefault: false,
239 });
240​
241 expect(patch.providerInstances?.[instanceId]).toEqual(nextInstance);
242 expect(patch.providers).toBeUndefined();
243 });
244});