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

SettingsPanels.logic.test.ts

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