Open History to see the last commit
1import { EnvironmentThemeFile } from "@t3tools/contracts";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import { assert, describe, it } from "@effect/vitest";
4import * as Effect from "effect/Effect";
5import * as FileSystem from "effect/FileSystem";
6import * as Layer from "effect/Layer";
7import * as Path from "effect/Path";
8import * as Option from "effect/Option";
9import * as Queue from "effect/Queue";
10import * as Schema from "effect/Schema";
11import * as Scope from "effect/Scope";
12import * as Stream from "effect/Stream";
13
14import * as ServerConfig from "./config.ts";
15import * as EnvironmentTheme from "./environmentTheme.ts";
16import { symlinksSupported } from "@t3tools/shared/testing/symlinks";
17
18const encodeThemeFile = Schema.encodeSync(Schema.fromJsonString(EnvironmentThemeFile));
19
20const NIGHTFALL_THEME: EnvironmentThemeFile = {
21 name: "Nightfall",
22 appearance: "dark",
23 canvas: "#1a1b26",
24 accent: "#7aa2f7",
25};
26
27const SHARED_THEME: EnvironmentThemeFile = {
28 version: 1,
29 name: "Shared Light",
30 appearance: "light",
31 colors: { canvas: "#eff1f5", accent: "#1e66f5" },
32};
33
34const withEnvironmentThemes = <A, E>(
35 seeds: Readonly<Record<string, string>>,
36 body: Effect.Effect<
37 A,
38 E,
39 | EnvironmentTheme.EnvironmentThemeService
40 | ServerConfig.ServerConfig
41 | FileSystem.FileSystem
42 | Path.Path
43 | Scope.Scope
44 >,
45) =>
46 Effect.gen(function* () {
47 const fs = yield* FileSystem.FileSystem;
48 const path = yield* Path.Path;
49 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-environment-theme-" });
50 const themesDir = path.join(baseDir, "userdata", "themes");
51 yield* fs.makeDirectory(themesDir, { recursive: true });
52 for (const [filename, contents] of Object.entries(seeds)) {
53 yield* fs.writeFileString(path.join(themesDir, filename), contents);
54 }
55
56 return yield* body.pipe(
57 Effect.provide(
58 EnvironmentTheme.layer.pipe(
59 Layer.provideMerge(ServerConfig.layerTest(process.cwd(), baseDir)),
60 ),
61 ),
62 );
63 }).pipe(Effect.scoped);
64
65const currentThemes = Effect.gen(function* () {
66 const environmentTheme = yield* EnvironmentTheme.EnvironmentThemeService;
67 return yield* environmentTheme.current;
68});
69
70it.layer(NodeServices.layer)("environment theme", (it) => {
71 it.effect("publishes nothing when the machine has no theme files", () =>
72 withEnvironmentThemes(
73 {},
74 Effect.gen(function* () {
75 assert.deepEqual(yield* currentThemes, []);
76 }),
77 ),
78 );
79
80 it.effect("publishes each file under its filename as the id", () =>
81 withEnvironmentThemes(
82 {
83 "nightfall.json": encodeThemeFile(NIGHTFALL_THEME),
84 "shared-light.json": encodeThemeFile(SHARED_THEME),
85 },
86 Effect.gen(function* () {
87 const themes = yield* currentThemes;
88 assert.deepEqual(
89 themes.map((theme) => theme.id),
90 ["nightfall", "shared-light"],
91 );
92 assert.deepEqual(themes[0], { id: "nightfall", ...NIGHTFALL_THEME });
93 assert.deepEqual(themes[1], { id: "shared-light", ...SHARED_THEME });
94 }),
95 ),
96 );
97
98 it.effect("follows the directory rather than the set read at start", () =>
99 withEnvironmentThemes(
100 { "nightfall.json": encodeThemeFile(NIGHTFALL_THEME) },
101 Effect.gen(function* () {
102 const { environmentThemesDir } = yield* ServerConfig.ServerConfig;
103 const fs = yield* FileSystem.FileSystem;
104 const path = yield* Path.Path;
105
106 yield* fs.writeFileString(
107 path.join(environmentThemesDir, "shared-light.json"),
108 encodeThemeFile(SHARED_THEME),
109 );
110 assert.equal((yield* currentThemes).length, 2);
111
112 yield* fs.remove(path.join(environmentThemesDir, "nightfall.json"));
113 assert.deepEqual(
114 (yield* currentThemes).map((theme) => theme.id),
115 ["shared-light"],
116 );
117 }),
118 ),
119 );
120
121 it.effect("streams the current set first", () =>
122 withEnvironmentThemes(
123 { "nightfall.json": encodeThemeFile(NIGHTFALL_THEME) },
124 Effect.gen(function* () {
125 const environmentTheme = yield* EnvironmentTheme.EnvironmentThemeService;
126 const first = yield* environmentTheme.streamChanges.pipe(Stream.runHead);
127 assert.deepEqual(Option.getOrNull(first), [{ id: "nightfall", ...NIGHTFALL_THEME }]);
128 }),
129 ),
130 );
131
132 it.effect("never replays a set older than the snapshot it started from", () =>
133 withEnvironmentThemes(
134 { "nightfall.json": encodeThemeFile(NIGHTFALL_THEME) },
135 Effect.gen(function* () {
136 const environmentTheme = yield* EnvironmentTheme.EnvironmentThemeService;
137 const { environmentThemesDir } = yield* ServerConfig.ServerConfig;
138 const fs = yield* FileSystem.FileSystem;
139 const path = yield* Path.Path;
140
141 yield* fs.writeFileString(
142 path.join(environmentThemesDir, "shared-light.json"),
143 encodeThemeFile(SHARED_THEME),
144 );
145 const seen = yield* Queue.unbounded<ReadonlyArray<{ readonly id: string }>>();
146 yield* Stream.runForEach(environmentTheme.streamChanges, (themes) =>
147 Queue.offer(seen, themes),
148 ).pipe(Effect.forkScoped);
149 assert.deepEqual(
150 (yield* Queue.take(seen)).map((theme) => theme.id),
151 ["nightfall", "shared-light"],
152 );
153 yield* fs.remove(path.join(environmentThemesDir, "nightfall.json"));
154 yield* currentThemes;
155 assert.deepEqual(
156 (yield* Queue.take(seen)).map((theme) => theme.id),
157 ["shared-light"],
158 );
159 }),
160 ),
161 );
162
163 it.effect("skips invalid files while keeping valid ones", () =>
164 withEnvironmentThemes(
165 {
166 "nightfall.json": encodeThemeFile(NIGHTFALL_THEME),
167 "unresolved.json":
168 '{ "name": "X", "appearance": "dark", "canvas": "{{ background }}", "accent": "#7aa2f7" }',
169 "malformed.json": "{ not json",
170 "no-colors.json": '{ "name": "Empty", "appearance": "dark" }',
171 "Bad Name.json": encodeThemeFile(SHARED_THEME),
172 "ocean.json": encodeThemeFile(SHARED_THEME),
173 "dark.json": encodeThemeFile(SHARED_THEME),
174 "notes.txt": "not a theme",
175 },
176 Effect.gen(function* () {
177 assert.deepEqual(
178 (yield* currentThemes).map((theme) => theme.id),
179 ["nightfall"],
180 );
181 }),
182 ),
183 );
184
185 it.effect.skipIf(!symlinksSupported)("ignores a symlinked theme file", () =>
186 withEnvironmentThemes(
187 {},
188 Effect.gen(function* () {
189 const { environmentThemesDir } = yield* ServerConfig.ServerConfig;
190 const fs = yield* FileSystem.FileSystem;
191 const path = yield* Path.Path;
192 const outside = path.join(environmentThemesDir, "..", "outside.json");
193 yield* fs.writeFileString(outside, encodeThemeFile(NIGHTFALL_THEME));
194 yield* fs.symlink(outside, path.join(environmentThemesDir, "nightfall.json"));
195 assert.deepEqual(yield* currentThemes, []);
196 }),
197 ),
198 );
199
200 it.effect("does not charge skipped files against the total size limit", () =>
201 withEnvironmentThemes(
202 {
203 ...Object.fromEntries(
204 Array.from({ length: 7 }, (_, index) => [`junk-${index}.json`, "{".repeat(30_000)]),
205 ),
206 "zz-valid.json": encodeThemeFile(NIGHTFALL_THEME),
207 },
208 Effect.gen(function* () {
209 assert.deepEqual(
210 (yield* currentThemes).map((theme) => theme.id),
211 ["zz-valid"],
212 );
213 }),
214 ),
215 );
216});
217
218// Kept outside the it.layer block above because only the top-level `it` exposes
219// `live`.
220describe("environment theme watching", () => {
221 it.live("streams a set for every change to the directory", () =>
222 Effect.gen(function* () {
223 const fs = yield* FileSystem.FileSystem;
224 const path = yield* Path.Path;
225 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-theme-watch-" });
226 const themesDir = path.join(baseDir, "userdata", "themes");
227 yield* fs.makeDirectory(themesDir, { recursive: true });
228
229 yield* Effect.gen(function* () {
230 const environmentTheme = yield* EnvironmentTheme.EnvironmentThemeService;
231 const seen = yield* Queue.unbounded<ReadonlyArray<{ readonly id: string }>>();
232 yield* Stream.runForEach(environmentTheme.streamChanges, (themes) =>
233 Queue.offer(seen, themes),
234 ).pipe(Effect.forkScoped);
235
236 assert.deepEqual(yield* Queue.take(seen), []);
237
238 const staging = path.join(baseDir, "staged.json");
239 yield* fs.writeFileString(staging, encodeThemeFile(NIGHTFALL_THEME));
240 yield* fs.rename(staging, path.join(themesDir, "nightfall.json"));
241 assert.deepEqual(
242 (yield* Queue.take(seen)).map((theme) => theme.id),
243 ["nightfall"],
244 );
245
246 yield* fs.remove(path.join(themesDir, "nightfall.json"));
247 assert.deepEqual(yield* Queue.take(seen), []);
248 }).pipe(
249 Effect.provide(
250 EnvironmentTheme.layer.pipe(
251 Layer.provideMerge(ServerConfig.layerTest(process.cwd(), baseDir)),
252 ),
253 ),
254 Effect.timeout("30 seconds"),
255 );
256 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
257 );
258});