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

theme.test.ts

478 lines19.0 KB

1// @effect-diagnostics nodeBuiltinImport:off - CLI integration exercises the filesystem boundary.
2import * as NodeFS from "node:fs";
3import * as NodeOS from "node:os";
4import * as NodePath from "node:path";
5​
6import * as NodeServices from "@effect/platform-node/NodeServices";
7import * as ConfigProvider from "effect/ConfigProvider";
8import * as NetService from "@t3tools/shared/Net";
9import { assert, describe, it } from "@effect/vitest";
10import * as Effect from "effect/Effect";
11import * as Layer from "effect/Layer";
12import * as TestConsole from "effect/testing/TestConsole";
13import { Command } from "effect/unstable/cli";
14​
15import { cli } from "../bin.ts";
16​
17const runCli = (args: ReadonlyArray<string>) =>
18 Command.runWith(cli, { version: "0.0.0" })(args).pipe(
19 Effect.provide(Layer.mergeAll(NodeServices.layer, NetService.layer, TestConsole.layer)),
20 );
21​
22const makeBaseDir = () => NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3code-theme-cli-"));
23​
24const settingsPathFor = (baseDir: string) => NodePath.join(baseDir, "userdata", "settings.json");
25​
26const NIGHTFALL_THEME_JSON = `${JSON.stringify({
27 name: "Nightfall",
28 appearance: "dark",
29 canvas: "#1a1b26",
30 accent: "#7aa2f7",
31})}\n`;
32const JUNK_THEME_JSON = `${JSON.stringify({ name: "Junk" })}\n`;
33​
34const readSettings = (baseDir: string): Record<string, unknown> => {
35 const raw = NodeFS.readFileSync(settingsPathFor(baseDir), "utf8");
36 return JSON.parse(raw) as Record<string, unknown>;
37};
38​
39const writeSettings = (baseDir: string, settings: Record<string, unknown>) => {
40 NodeFS.mkdirSync(NodePath.dirname(settingsPathFor(baseDir)), { recursive: true });
41 NodeFS.writeFileSync(settingsPathFor(baseDir), `${JSON.stringify(settings, null, 2)}\n`);
42};
43​
44describe("t3 theme", () => {
45 it.effect("writes a default theme when no settings file exists yet", () =>
46 Effect.gen(function* () {
47 const baseDir = makeBaseDir();
48 yield* runCli(["theme", "set", "ocean", "--base-dir", baseDir]);
49 assert.equal(readSettings(baseDir).defaultTheme, "ocean");
50 }),
51 );
52​
53 // A provisioning command runs against settings written by whatever version
54 // happens to be installed, so it must not drop what it cannot interpret.
55 it.effect("preserves settings it does not recognise", () =>
56 Effect.gen(function* () {
57 const baseDir = makeBaseDir();
58 writeSettings(baseDir, {
59 enableProviderUpdateChecks: false,
60 somethingFromANewerBuild: { nested: true },
61 });
62​
63 yield* runCli(["theme", "set", "ocean", "--base-dir", baseDir]);
64​
65 const settings = readSettings(baseDir);
66 assert.equal(settings.defaultTheme, "ocean");
67 assert.equal(settings.enableProviderUpdateChecks, false);
68 assert.deepEqual(settings.somethingFromANewerBuild, { nested: true });
69 }),
70 );
71​
72 it.effect("clears the default back to leaving fresh clients alone", () =>
73 Effect.gen(function* () {
74 const baseDir = makeBaseDir();
75 writeSettings(baseDir, { enableProviderUpdateChecks: false });
76​
77 yield* runCli(["theme", "set", "ocean", "--base-dir", baseDir]);
78 yield* runCli(["theme", "clear", "--base-dir", baseDir]);
79​
80 const settings = readSettings(baseDir);
81 assert.equal(Object.hasOwn(settings, "defaultTheme"), false);
82 assert.equal(settings.enableProviderUpdateChecks, false);
83 }),
84 );
85​
86 // Publishing a file and pointing at it are one step, so an integration
87 // (a desktop's theme hook) needs no knowledge of the themes directory.
88 it.effect("publishes a theme file under its filename and sets it", () =>
89 Effect.gen(function* () {
90 const baseDir = makeBaseDir();
91 const themeFile = NodePath.join(baseDir, "nightfall.json");
92 NodeFS.writeFileSync(themeFile, NIGHTFALL_THEME_JSON);
93​
94 yield* runCli(["theme", "set", themeFile, "--base-dir", baseDir]);
95​
96 const published = NodePath.join(baseDir, "userdata", "themes", "nightfall.json");
97 assert.equal(NodeFS.existsSync(published), true);
98 assert.equal(readSettings(baseDir).defaultTheme, "nightfall");
99 // No rollback or staging residue after a successful set.
100 const residue = NodeFS.readdirSync(NodePath.dirname(published)).filter(
101 (entry) => !entry.endsWith(".json"),
102 );
103 assert.deepEqual(residue, []);
104 }),
105 );
106​
107 it.effect("publishes a theme file under an explicit id", () =>
108 Effect.gen(function* () {
109 const baseDir = makeBaseDir();
110 const themeFile = NodePath.join(baseDir, "t3code.json");
111 NodeFS.writeFileSync(themeFile, NIGHTFALL_THEME_JSON);
112​
113 yield* runCli(["theme", "set", "--id", "nightfall", themeFile, "--base-dir", baseDir]);
114​
115 assert.equal(
116 NodeFS.existsSync(NodePath.join(baseDir, "userdata", "themes", "nightfall.json")),
117 true,
118 );
119 assert.equal(readSettings(baseDir).defaultTheme, "nightfall");
120 }),
121 );
122​
123 it.effect("rejects a file that is not a theme and sets nothing", () =>
124 Effect.gen(function* () {
125 const baseDir = makeBaseDir();
126 const themeFile = NodePath.join(baseDir, "junk.json");
127 NodeFS.writeFileSync(themeFile, JUNK_THEME_JSON);
128​
129 const failure = yield* runCli(["theme", "set", themeFile, "--base-dir", baseDir]).pipe(
130 Effect.flip,
131 );
132​
133 assert.include(String(failure), "not a valid theme file");
134 assert.equal(NodeFS.existsSync(NodePath.join(baseDir, "userdata", "themes")), false);
135 assert.equal(NodeFS.existsSync(settingsPathFor(baseDir)), false);
136 }),
137 );
138​
139 // Publish and set are one command, so a settings file the set step cannot
140 // use must fail it before the themes directory is mutated -- not after,
141 // with a half-applied publish left behind.
142 it.effect("publishes nothing when the settings file cannot be used", () =>
143 Effect.gen(function* () {
144 const baseDir = makeBaseDir();
145 NodeFS.mkdirSync(NodePath.dirname(settingsPathFor(baseDir)), { recursive: true });
146 NodeFS.writeFileSync(settingsPathFor(baseDir), "{ not json");
147 const themeFile = NodePath.join(baseDir, "nightfall.json");
148 NodeFS.writeFileSync(themeFile, NIGHTFALL_THEME_JSON);
149​
150 const failure = yield* runCli(["theme", "set", themeFile, "--base-dir", baseDir]).pipe(
151 Effect.flip,
152 );
153​
154 assert.include(String(failure), "not a JSON object");
155 assert.equal(NodeFS.existsSync(NodePath.join(baseDir, "userdata", "themes")), false);
156 }),
157 );
158​
159 // set means set: a publish that rode along with a failed default write is
160 // rolled back rather than left mutating the environment's theme set. The
161 // userdata directory is made read-only while themes stays writable, so the
162 // failure lands after the publish -- the case the rollback exists for.
163 it.effect("rolls back a publish when the default cannot be written", () =>
164 Effect.gen(function* () {
165 const baseDir = makeBaseDir();
166 writeSettings(baseDir, {});
167 const userdataDir = NodePath.dirname(settingsPathFor(baseDir));
168 const themesDir = NodePath.join(userdataDir, "themes");
169 NodeFS.mkdirSync(themesDir, { recursive: true });
170 const themeFile = NodePath.join(baseDir, "nightfall.json");
171 NodeFS.writeFileSync(themeFile, NIGHTFALL_THEME_JSON);
172​
173 NodeFS.chmodSync(userdataDir, 0o555);
174 try {
175 const failure = yield* runCli(["theme", "set", themeFile, "--base-dir", baseDir]).pipe(
176 Effect.flip,
177 );
178 assert.include(String(failure), "Could not write");
179 assert.equal(NodeFS.existsSync(NodePath.join(themesDir, "nightfall.json")), false);
180 } finally {
181 NodeFS.chmodSync(userdataDir, 0o755);
182 }
183 }),
184 );
185​
186 // A symlink is a normal way to hand this command a theme -- desktop hooks
187 // symlink the current palette -- so the source is resolved, not refused.
188 it.effect("publishes a theme file through a symlinked source path", () =>
189 Effect.gen(function* () {
190 const baseDir = makeBaseDir();
191 const realFile = NodePath.join(baseDir, "real-nightfall.json");
192 NodeFS.writeFileSync(realFile, NIGHTFALL_THEME_JSON);
193 const linkPath = NodePath.join(baseDir, "nightfall.json");
194 NodeFS.symlinkSync(realFile, linkPath);
195​
196 yield* runCli(["theme", "set", linkPath, "--base-dir", baseDir]);
197​
198 assert.equal(
199 NodeFS.existsSync(NodePath.join(baseDir, "userdata", "themes", "nightfall.json")),
200 true,
201 );
202 assert.equal(readSettings(baseDir).defaultTheme, "nightfall");
203 }),
204 );
205​
206 // The staging entry is created fresh with O_EXCL, so a symlink planted at
207 // its predictable name is cleared, never followed and written through.
208 it.effect("never writes through a symlink at the staging path", () =>
209 Effect.gen(function* () {
210 const baseDir = makeBaseDir();
211 const themesDir = NodePath.join(baseDir, "userdata", "themes");
212 NodeFS.mkdirSync(themesDir, { recursive: true });
213 const victim = NodePath.join(baseDir, "victim.txt");
214 NodeFS.writeFileSync(victim, "precious");
215 NodeFS.symlinkSync(victim, NodePath.join(themesDir, `nightfall.json.staging-${process.pid}`));
216 const themeFile = NodePath.join(baseDir, "nightfall.json");
217 NodeFS.writeFileSync(themeFile, NIGHTFALL_THEME_JSON);
218​
219 yield* runCli(["theme", "set", themeFile, "--base-dir", baseDir]);
220​
221 assert.equal(NodeFS.readFileSync(victim, "utf8"), "precious");
222 assert.equal(readSettings(baseDir).defaultTheme, "nightfall");
223 }),
224 );
225​
226 // Rollback moves the previous directory entry aside and back, so even an
227 // entry the watcher would never publish -- here a symlink -- comes back
228 // exactly as it was when the set fails.
229 it.effect("restores a non-theme destination entry when the set fails", () =>
230 Effect.gen(function* () {
231 const baseDir = makeBaseDir();
232 writeSettings(baseDir, {});
233 const userdataDir = NodePath.dirname(settingsPathFor(baseDir));
234 const themesDir = NodePath.join(userdataDir, "themes");
235 NodeFS.mkdirSync(themesDir, { recursive: true });
236 const outside = NodePath.join(baseDir, "outside.json");
237 NodeFS.writeFileSync(outside, NIGHTFALL_THEME_JSON);
238 const destination = NodePath.join(themesDir, "nightfall.json");
239 NodeFS.symlinkSync(outside, destination);
240 const themeFile = NodePath.join(baseDir, "nightfall.json");
241 NodeFS.writeFileSync(themeFile, NIGHTFALL_THEME_JSON);
242​
243 NodeFS.chmodSync(userdataDir, 0o555);
244 try {
245 yield* runCli(["theme", "set", themeFile, "--base-dir", baseDir]).pipe(Effect.flip);
246 assert.equal(NodeFS.lstatSync(destination).isSymbolicLink(), true);
247 } finally {
248 NodeFS.chmodSync(userdataDir, 0o755);
249 }
250 }),
251 );
252​
253 it.effect("restores the previous theme when a re-publish fails to set", () =>
254 Effect.gen(function* () {
255 const baseDir = makeBaseDir();
256 writeSettings(baseDir, {});
257 const userdataDir = NodePath.dirname(settingsPathFor(baseDir));
258 const themesDir = NodePath.join(userdataDir, "themes");
259 NodeFS.mkdirSync(themesDir, { recursive: true });
260 const publishedPath = NodePath.join(themesDir, "nightfall.json");
261 const previous =
262 '{ "name": "Old Nightfall", "appearance": "dark", "canvas": "#000000", "accent": "#ffffff" }\n';
263 NodeFS.writeFileSync(publishedPath, previous);
264 const themeFile = NodePath.join(baseDir, "nightfall.json");
265 NodeFS.writeFileSync(themeFile, NIGHTFALL_THEME_JSON);
266​
267 NodeFS.chmodSync(userdataDir, 0o555);
268 try {
269 yield* runCli(["theme", "set", themeFile, "--base-dir", baseDir]).pipe(Effect.flip);
270 assert.equal(NodeFS.readFileSync(publishedPath, "utf8"), previous);
271 } finally {
272 NodeFS.chmodSync(userdataDir, 0o755);
273 }
274 }),
275 );
276​
277 // A typo'd id written as the theme would silently never resolve anywhere;
278 // the id branch is as strict as the filename rule.
279 it.effect("rejects an id no client could resolve", () =>
280 Effect.gen(function* () {
281 const baseDir = makeBaseDir();
282 const failure = yield* runCli(["theme", "set", "Nightfall", "--base-dir", baseDir]).pipe(
283 Effect.flip,
284 );
285 assert.include(String(failure), "not a valid theme id");
286 assert.equal(NodeFS.existsSync(settingsPathFor(baseDir)), false);
287 }),
288 );
289​
290 it.effect("rejects a path that does not exist instead of storing it as an id", () =>
291 Effect.gen(function* () {
292 const baseDir = makeBaseDir();
293 const failure = yield* runCli([
294 "theme",
295 "set",
296 `${baseDir}/missing.json`,
297 "--base-dir",
298 baseDir,
299 ]).pipe(Effect.flip);
300 assert.include(String(failure), "Could not read");
301 }),
302 );
303​
304 // File-ness is decided by existence, not extension, so a generated file
305 // named for its target app still publishes.
306 it.effect("publishes an extensionless file", () =>
307 Effect.gen(function* () {
308 const baseDir = makeBaseDir();
309 const themeFile = NodePath.join(baseDir, "brand");
310 NodeFS.writeFileSync(themeFile, NIGHTFALL_THEME_JSON);
311​
312 yield* runCli(["theme", "set", themeFile, "--base-dir", baseDir]);
313​
314 assert.equal(
315 NodeFS.existsSync(NodePath.join(baseDir, "userdata", "themes", "brand.json")),
316 true,
317 );
318 assert.equal(readSettings(baseDir).defaultTheme, "brand");
319 }),
320 );
321​
322 it.effect("records a set generation and clears it with the theme", () =>
323 Effect.gen(function* () {
324 const baseDir = makeBaseDir();
325 yield* runCli(["theme", "set", "ocean", "--base-dir", baseDir]);
326 const setAt = readSettings(baseDir).defaultThemeSetAt;
327 assert.equal(typeof setAt, "string");
328​
329 yield* runCli(["theme", "clear", "--base-dir", baseDir]);
330 const cleared = readSettings(baseDir);
331 assert.equal(Object.hasOwn(cleared, "defaultTheme"), false);
332 assert.equal(Object.hasOwn(cleared, "defaultThemeSetAt"), false);
333 }),
334 );
335​
336 it.effect("honors T3CODE_HOME like the rest of the CLI", () =>
337 Effect.gen(function* () {
338 const baseDir = makeBaseDir();
339 yield* runCli(["theme", "set", "ocean"]).pipe(
340 Effect.provide(
341 ConfigProvider.layer(ConfigProvider.fromEnv({ env: { T3CODE_HOME: baseDir } })),
342 ),
343 );
344 assert.equal(readSettings(baseDir).defaultTheme, "ocean");
345 }),
346 );
347​
348 // An unreadable settings file must never read as "no settings": writing a
349 // fresh sparse file over it would discard every key the user had.
350 it.effect("refuses to write when the settings file cannot be read", () =>
351 Effect.gen(function* () {
352 const baseDir = makeBaseDir();
353 writeSettings(baseDir, { enableProviderUpdateChecks: false });
354 NodeFS.chmodSync(settingsPathFor(baseDir), 0o000);
355​
356 const failure = yield* runCli(["theme", "set", "ocean", "--base-dir", baseDir]).pipe(
357 Effect.flip,
358 );
359​
360 NodeFS.chmodSync(settingsPathFor(baseDir), 0o644);
361 assert.include(String(failure), "Could not read");
362 assert.equal(readSettings(baseDir).enableProviderUpdateChecks, false);
363 assert.equal(Object.hasOwn(readSettings(baseDir), "defaultTheme"), false);
364 }),
365 );
366​
367 // A typo is syntactically a valid id, so shape validation alone would write
368 // a theme no client can resolve and report success.
369 it.effect("rejects an id that names no theme", () =>
370 Effect.gen(function* () {
371 const baseDir = makeBaseDir();
372 const failure = yield* runCli(["theme", "set", "ocian", "--base-dir", baseDir]).pipe(
373 Effect.flip,
374 );
375 assert.include(String(failure), "No theme named");
376 assert.equal(NodeFS.existsSync(settingsPathFor(baseDir)), false);
377 }),
378 );
379​
380 it.effect("accepts an id a published file provides", () =>
381 Effect.gen(function* () {
382 const baseDir = makeBaseDir();
383 const themeFile = NodePath.join(baseDir, "nightfall.json");
384 NodeFS.writeFileSync(themeFile, NIGHTFALL_THEME_JSON);
385 yield* runCli(["theme", "set", themeFile, "--base-dir", baseDir]);
386​
387 // Now resolvable by bare id, because the file published it.
388 yield* runCli(["theme", "clear", "--base-dir", baseDir]);
389 yield* runCli(["theme", "set", "nightfall", "--base-dir", baseDir]);
390 assert.equal(readSettings(baseDir).defaultTheme, "nightfall");
391 }),
392 );
393​
394 // The watcher skips files it cannot use, so accepting their filename would
395 // set a theme no client ever receives.
396 it.effect("rejects an id whose published file the watcher would skip", () =>
397 Effect.gen(function* () {
398 const baseDir = makeBaseDir();
399 const themesDir = NodePath.join(baseDir, "userdata", "themes");
400 NodeFS.mkdirSync(themesDir, { recursive: true });
401 NodeFS.writeFileSync(NodePath.join(themesDir, "broken.json"), "{ not json\n");
402​
403 const failure = yield* runCli(["theme", "set", "broken", "--base-dir", baseDir]).pipe(
404 Effect.flip,
405 );
406 assert.include(String(failure), "No theme named");
407 }),
408 );
409​
410 // Web and desktop cannot resolve the mobile default, and mobile does not
411 // follow this setting, so naming it would be a silent no-op.
412 it.effect("rejects the mobile default theme id", () =>
413 Effect.gen(function* () {
414 const baseDir = makeBaseDir();
415 const failure = yield* runCli(["theme", "set", "t3-code", "--base-dir", baseDir]).pipe(
416 Effect.flip,
417 );
418 assert.include(String(failure), "No theme named");
419 }),
420 );
421​
422 // Deciding on existence alone would publish ./ocean instead of selecting the
423 // built-in, purely because of what happens to be in the working directory.
424 it.effect("treats a bare id as an id even when a file shares its name", () =>
425 Effect.gen(function* () {
426 const baseDir = makeBaseDir();
427 const cwdFile = NodePath.join(baseDir, "ocean");
428 NodeFS.writeFileSync(cwdFile, NIGHTFALL_THEME_JSON);
429​
430 const previous = process.cwd();
431 process.chdir(baseDir);
432 try {
433 yield* runCli(["theme", "set", "ocean", "--base-dir", baseDir]);
434 } finally {
435 process.chdir(previous);
436 }
437​
438 assert.equal(readSettings(baseDir).defaultTheme, "ocean");
439 assert.equal(
440 NodeFS.existsSync(NodePath.join(baseDir, "userdata", "themes", "ocean.json")),
441 false,
442 );
443 }),
444 );
445​
446 // The watcher would skip an oversized file, so publishing one must not
447 // report success for a theme no client receives.
448 it.effect("rejects a theme file larger than the watcher will read", () =>
449 Effect.gen(function* () {
450 const baseDir = makeBaseDir();
451 const themeFile = NodePath.join(baseDir, "huge.json");
452 const padding = "x".repeat(40 * 1024);
453 NodeFS.writeFileSync(
454 themeFile,
455 `{ "name": "Huge", "appearance": "dark", "canvas": "#1a1b26", "accent": "#7aa2f7", "note": "${padding}" }\n`,
456 );
457​
458 const failure = yield* runCli(["theme", "set", themeFile, "--base-dir", baseDir]).pipe(
459 Effect.flip,
460 );
461 assert.include(String(failure), "larger than");
462 }),
463 );
464​
465 it.effect("refuses a settings file that is not a JSON object", () =>
466 Effect.gen(function* () {
467 const baseDir = makeBaseDir();
468 NodeFS.mkdirSync(NodePath.dirname(settingsPathFor(baseDir)), { recursive: true });
469 NodeFS.writeFileSync(settingsPathFor(baseDir), "[1, 2, 3]\n");
470​
471 const failure = yield* runCli(["theme", "set", "ocean", "--base-dir", baseDir]).pipe(
472 Effect.flip,
473 );
474​
475 assert.include(String(failure), "not a JSON object");
476 }),
477 );
478});