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 first = yield* environmentTheme.streamChanges.pipe(Stream.runHead);
146 assert.deepEqual(
147 Option.getOrNull(first)?.map((theme) => theme.id),
148 ["nightfall", "shared-light"],
149 );
150 }),
151 ),
152 );
153
154 it.effect("skips invalid files while keeping valid ones", () =>
155 withEnvironmentThemes(
156 {
157 "nightfall.json": encodeThemeFile(NIGHTFALL_THEME),
158 "unresolved.json":
159 '{ "name": "X", "appearance": "dark", "canvas": "{{ background }}", "accent": "#7aa2f7" }',
160 "malformed.json": "{ not json",
161 "no-colors.json": '{ "name": "Empty", "appearance": "dark" }',
162 "Bad Name.json": encodeThemeFile(SHARED_THEME),
163 "ocean.json": encodeThemeFile(SHARED_THEME),
164 "dark.json": encodeThemeFile(SHARED_THEME),
165 "notes.txt": "not a theme",
166 },
167 Effect.gen(function* () {
168 assert.deepEqual(
169 (yield* currentThemes).map((theme) => theme.id),
170 ["nightfall"],
171 );
172 }),
173 ),
174 );
175
176 it.effect.skipIf(!symlinksSupported)("ignores a symlinked theme file", () =>
177 withEnvironmentThemes(
178 {},
179 Effect.gen(function* () {
180 const { environmentThemesDir } = yield* ServerConfig.ServerConfig;
181 const fs = yield* FileSystem.FileSystem;
182 const path = yield* Path.Path;
183 const outside = path.join(environmentThemesDir, "..", "outside.json");
184 yield* fs.writeFileString(outside, encodeThemeFile(NIGHTFALL_THEME));
185 yield* fs.symlink(outside, path.join(environmentThemesDir, "nightfall.json"));
186 assert.deepEqual(yield* currentThemes, []);
187 }),
188 ),
189 );
190
191 it.effect("does not charge skipped files against the total size limit", () =>
192 withEnvironmentThemes(
193 {
194 ...Object.fromEntries(
195 Array.from({ length: 7 }, (_, index) => [`junk-${index}.json`, "{".repeat(30_000)]),
196 ),
197 "zz-valid.json": encodeThemeFile(NIGHTFALL_THEME),
198 },
199 Effect.gen(function* () {
200 assert.deepEqual(
201 (yield* currentThemes).map((theme) => theme.id),
202 ["zz-valid"],
203 );
204 }),
205 ),
206 );
207});
208
209// Kept outside the it.layer block above because only the top-level `it` exposes
210// `live`.
211describe("environment theme watching", () => {
212 it.live("streams a set for every change to the directory", () =>
213 Effect.gen(function* () {
214 const fs = yield* FileSystem.FileSystem;
215 const path = yield* Path.Path;
216 const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-theme-watch-" });
217 const themesDir = path.join(baseDir, "userdata", "themes");
218 yield* fs.makeDirectory(themesDir, { recursive: true });
219
220 yield* Effect.gen(function* () {
221 const environmentTheme = yield* EnvironmentTheme.EnvironmentThemeService;
222 const seen = yield* Queue.unbounded<ReadonlyArray<{ readonly id: string }>>();
223 yield* Stream.runForEach(environmentTheme.streamChanges, (themes) =>
224 Queue.offer(seen, themes),
225 ).pipe(Effect.forkScoped);
226
227 assert.deepEqual(yield* Queue.take(seen), []);
228
229 const staging = path.join(baseDir, "staged.json");
230 yield* fs.writeFileString(staging, encodeThemeFile(NIGHTFALL_THEME));
231 yield* fs.rename(staging, path.join(themesDir, "nightfall.json"));
232 assert.deepEqual(
233 (yield* Queue.take(seen)).map((theme) => theme.id),
234 ["nightfall"],
235 );
236
237 yield* fs.remove(path.join(themesDir, "nightfall.json"));
238 assert.deepEqual(yield* Queue.take(seen), []);
239 }).pipe(
240 Effect.provide(
241 EnvironmentTheme.layer.pipe(
242 Layer.provideMerge(ServerConfig.layerTest(process.cwd(), baseDir)),
243 ),
244 ),
245 Effect.timeout("30 seconds"),
246 );
247 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)),
248 );
249});