|
| 1 | +import { EnvironmentThemeFile } from "@t3tools/contracts"; |
| 2 | +import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 3 | +import { assert, describe, it } from "@effect/vitest"; |
| 4 | +import * as Effect from "effect/Effect"; |
| 5 | +import * as FileSystem from "effect/FileSystem"; |
| 6 | +import * as Layer from "effect/Layer"; |
| 7 | +import * as Path from "effect/Path"; |
| 8 | +import * as Option from "effect/Option"; |
| 9 | +import * as Queue from "effect/Queue"; |
| 10 | +import * as Schema from "effect/Schema"; |
| 11 | +import * as Scope from "effect/Scope"; |
| 12 | +import * as Stream from "effect/Stream"; |
| 13 | + |
| 14 | +import * as ServerConfig from "./config.ts"; |
| 15 | +import * as EnvironmentTheme from "./environmentTheme.ts"; |
| 16 | + |
| 17 | +const encodeThemeFile = Schema.encodeSync(Schema.fromJsonString(EnvironmentThemeFile)); |
| 18 | + |
| 19 | +const NIGHTFALL_THEME: EnvironmentThemeFile = { |
| 20 | + name: "Nightfall", |
| 21 | + appearance: "dark", |
| 22 | + canvas: "#1a1b26", |
| 23 | + accent: "#7aa2f7", |
| 24 | +}; |
| 25 | + |
| 26 | +/** The standard exported form: a full palette, no seeds. */ |
| 27 | +const SHARED_THEME: EnvironmentThemeFile = { |
| 28 | + version: 1, |
| 29 | + name: "Shared Light", |
| 30 | + appearance: "light", |
| 31 | + colors: { canvas: "#eff1f5", accent: "#1e66f5" }, |
| 32 | +}; |
| 33 | + |
| 34 | +/** Seeds theme files before the service starts, as a real machine would. */ |
| 35 | +const withEnvironmentThemes = <A, E>( |
| 36 | + seeds: Readonly<Record<string, string>>, |
| 37 | + body: Effect.Effect< |
| 38 | + A, |
| 39 | + E, |
| 40 | + | EnvironmentTheme.EnvironmentThemeService |
| 41 | + | ServerConfig.ServerConfig |
| 42 | + | FileSystem.FileSystem |
| 43 | + | Path.Path |
| 44 | + | Scope.Scope |
| 45 | + >, |
| 46 | +) => |
| 47 | + Effect.gen(function* () { |
| 48 | + const fs = yield* FileSystem.FileSystem; |
| 49 | + const path = yield* Path.Path; |
| 50 | + const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-environment-theme-" }); |
| 51 | + const themesDir = path.join(baseDir, "userdata", "themes"); |
| 52 | + yield* fs.makeDirectory(themesDir, { recursive: true }); |
| 53 | + for (const [filename, contents] of Object.entries(seeds)) { |
| 54 | + yield* fs.writeFileString(path.join(themesDir, filename), contents); |
| 55 | + } |
| 56 | + |
| 57 | + return yield* body.pipe( |
| 58 | + Effect.provide( |
| 59 | + EnvironmentTheme.layer.pipe( |
| 60 | + Layer.provideMerge(ServerConfig.layerTest(process.cwd(), baseDir)), |
| 61 | + ), |
| 62 | + ), |
| 63 | + ); |
| 64 | + }).pipe(Effect.scoped); |
| 65 | + |
| 66 | +const currentThemes = Effect.gen(function* () { |
| 67 | + const environmentTheme = yield* EnvironmentTheme.EnvironmentThemeService; |
| 68 | + return yield* environmentTheme.current; |
| 69 | +}); |
| 70 | + |
| 71 | +it.layer(NodeServices.layer)("environment theme", (it) => { |
| 72 | + it.effect("publishes nothing when the machine has no theme files", () => |
| 73 | + withEnvironmentThemes( |
| 74 | + {}, |
| 75 | + Effect.gen(function* () { |
| 76 | + assert.deepEqual(yield* currentThemes, []); |
| 77 | + }), |
| 78 | + ), |
| 79 | + ); |
| 80 | + |
| 81 | + it.effect("publishes each file under its filename as the id", () => |
| 82 | + withEnvironmentThemes( |
| 83 | + { |
| 84 | + "nightfall.json": encodeThemeFile(NIGHTFALL_THEME), |
| 85 | + "shared-light.json": encodeThemeFile(SHARED_THEME), |
| 86 | + }, |
| 87 | + Effect.gen(function* () { |
| 88 | + const themes = yield* currentThemes; |
| 89 | + assert.deepEqual( |
| 90 | + themes.map((theme) => theme.id), |
| 91 | + ["nightfall", "shared-light"], |
| 92 | + ); |
| 93 | + assert.deepEqual(themes[0], { id: "nightfall", ...NIGHTFALL_THEME }); |
| 94 | + assert.deepEqual(themes[1], { id: "shared-light", ...SHARED_THEME }); |
| 95 | + }), |
| 96 | + ), |
| 97 | + ); |
| 98 | + |
| 99 | + // Read from disk rather than from the watcher's last observation, so a |
| 100 | + // client connecting after a missed filesystem event still sees the truth. |
| 101 | + it.effect("follows the directory rather than the set read at start", () => |
| 102 | + withEnvironmentThemes( |
| 103 | + { "nightfall.json": encodeThemeFile(NIGHTFALL_THEME) }, |
| 104 | + Effect.gen(function* () { |
| 105 | + const { environmentThemesDir } = yield* ServerConfig.ServerConfig; |
| 106 | + const fs = yield* FileSystem.FileSystem; |
| 107 | + const path = yield* Path.Path; |
| 108 | + |
| 109 | + yield* fs.writeFileString( |
| 110 | + path.join(environmentThemesDir, "shared-light.json"), |
| 111 | + encodeThemeFile(SHARED_THEME), |
| 112 | + ); |
| 113 | + assert.equal((yield* currentThemes).length, 2); |
| 114 | + |
| 115 | + yield* fs.remove(path.join(environmentThemesDir, "nightfall.json")); |
| 116 | + assert.deepEqual( |
| 117 | + (yield* currentThemes).map((theme) => theme.id), |
| 118 | + ["shared-light"], |
| 119 | + ); |
| 120 | + }), |
| 121 | + ), |
| 122 | + ); |
| 123 | + |
| 124 | + // One bad file must not take down the machine's other themes: a theme |
| 125 | + // script that leaves a template placeholder unresolved, a half-written |
| 126 | + // file, or a stray name are each that file's problem alone. |
| 127 | + // The subscription is acquired before the current set is read, so nothing |
| 128 | + // published while a client connects can fall between snapshot and stream. |
| 129 | + it.effect("streams the current set first", () => |
| 130 | + withEnvironmentThemes( |
| 131 | + { "nightfall.json": encodeThemeFile(NIGHTFALL_THEME) }, |
| 132 | + Effect.gen(function* () { |
| 133 | + const environmentTheme = yield* EnvironmentTheme.EnvironmentThemeService; |
| 134 | + const first = yield* environmentTheme.streamChanges.pipe(Stream.runHead); |
| 135 | + assert.deepEqual(Option.getOrNull(first), [{ id: "nightfall", ...NIGHTFALL_THEME }]); |
| 136 | + }), |
| 137 | + ), |
| 138 | + ); |
| 139 | + |
| 140 | + // Subscribing happens before the snapshot read, so a publish landing in |
| 141 | + // between is queued. It must not replay after the newer snapshot and walk |
| 142 | + // clients back onto colors the machine has already moved past. |
| 143 | + it.effect("never replays a set older than the snapshot it started from", () => |
| 144 | + withEnvironmentThemes( |
| 145 | + { "nightfall.json": encodeThemeFile(NIGHTFALL_THEME) }, |
| 146 | + Effect.gen(function* () { |
| 147 | + const environmentTheme = yield* EnvironmentTheme.EnvironmentThemeService; |
| 148 | + const { environmentThemesDir } = yield* ServerConfig.ServerConfig; |
| 149 | + const fs = yield* FileSystem.FileSystem; |
| 150 | + const path = yield* Path.Path; |
| 151 | + |
| 152 | + // Advance the directory twice without the watcher running, so the |
| 153 | + // second read is strictly newer than anything already observed. |
| 154 | + yield* fs.writeFileString( |
| 155 | + path.join(environmentThemesDir, "shared-light.json"), |
| 156 | + encodeThemeFile(SHARED_THEME), |
| 157 | + ); |
| 158 | + const first = yield* environmentTheme.streamChanges.pipe(Stream.runHead); |
| 159 | + assert.deepEqual( |
| 160 | + Option.getOrNull(first)?.map((theme) => theme.id), |
| 161 | + ["nightfall", "shared-light"], |
| 162 | + ); |
| 163 | + }), |
| 164 | + ), |
| 165 | + ); |
| 166 | + |
| 167 | + it.effect("skips invalid files while keeping valid ones", () => |
| 168 | + withEnvironmentThemes( |
| 169 | + { |
| 170 | + "nightfall.json": encodeThemeFile(NIGHTFALL_THEME), |
| 171 | + "unresolved.json": |
| 172 | + '{ "name": "X", "appearance": "dark", "canvas": "{{ background }}", "accent": "#7aa2f7" }', |
| 173 | + "malformed.json": "{ not json", |
| 174 | + "no-colors.json": '{ "name": "Empty", "appearance": "dark" }', |
| 175 | + "Bad Name.json": encodeThemeFile(SHARED_THEME), |
| 176 | + "ocean.json": encodeThemeFile(SHARED_THEME), |
| 177 | + "dark.json": encodeThemeFile(SHARED_THEME), |
| 178 | + "notes.txt": "not a theme", |
| 179 | + }, |
| 180 | + Effect.gen(function* () { |
| 181 | + assert.deepEqual( |
| 182 | + (yield* currentThemes).map((theme) => theme.id), |
| 183 | + ["nightfall"], |
| 184 | + ); |
| 185 | + }), |
| 186 | + ), |
| 187 | + ); |
| 188 | + |
| 189 | + // A symlinked themes directory stays usable, but a symlinked file inside it |
| 190 | + // must not publish whatever it points at. |
| 191 | + it.effect("ignores a symlinked theme file", () => |
| 192 | + withEnvironmentThemes( |
| 193 | + {}, |
| 194 | + Effect.gen(function* () { |
| 195 | + const { environmentThemesDir } = yield* ServerConfig.ServerConfig; |
| 196 | + const fs = yield* FileSystem.FileSystem; |
| 197 | + const path = yield* Path.Path; |
| 198 | + const outside = path.join(environmentThemesDir, "..", "outside.json"); |
| 199 | + yield* fs.writeFileString(outside, encodeThemeFile(NIGHTFALL_THEME)); |
| 200 | + yield* fs.symlink(outside, path.join(environmentThemesDir, "nightfall.json")); |
| 201 | + assert.deepEqual(yield* currentThemes, []); |
| 202 | + }), |
| 203 | + ), |
| 204 | + ); |
| 205 | + |
| 206 | + // The aggregate size cap charges only accepted themes, so a pile of |
| 207 | + // malformed files cannot spend the budget and hide a valid theme sorted |
| 208 | + // after them. |
| 209 | + it.effect("does not charge skipped files against the total size limit", () => |
| 210 | + withEnvironmentThemes( |
| 211 | + { |
| 212 | + ...Object.fromEntries( |
| 213 | + Array.from({ length: 7 }, (_, index) => [`junk-${index}.json`, "{".repeat(30_000)]), |
| 214 | + ), |
| 215 | + "zz-valid.json": encodeThemeFile(NIGHTFALL_THEME), |
| 216 | + }, |
| 217 | + Effect.gen(function* () { |
| 218 | + assert.deepEqual( |
| 219 | + (yield* currentThemes).map((theme) => theme.id), |
| 220 | + ["zz-valid"], |
| 221 | + ); |
| 222 | + }), |
| 223 | + ), |
| 224 | + ); |
| 225 | +}); |
| 226 | + |
| 227 | +// The feature's headline claim: rewrite a file and connected clients retint |
| 228 | +// without a restart. Live clock and a real filesystem event, so this proves |
| 229 | +// the watcher rather than a direct read. Kept outside the it.layer block above |
| 230 | +// because only the top-level `it` exposes `live`. |
| 231 | +describe("environment theme watching", () => { |
| 232 | + it.live("streams a set for every change to the directory", () => |
| 233 | + Effect.gen(function* () { |
| 234 | + const fs = yield* FileSystem.FileSystem; |
| 235 | + const path = yield* Path.Path; |
| 236 | + const baseDir = yield* fs.makeTempDirectoryScoped({ prefix: "t3code-theme-watch-" }); |
| 237 | + const themesDir = path.join(baseDir, "userdata", "themes"); |
| 238 | + yield* fs.makeDirectory(themesDir, { recursive: true }); |
| 239 | + |
| 240 | + yield* Effect.gen(function* () { |
| 241 | + const environmentTheme = yield* EnvironmentTheme.EnvironmentThemeService; |
| 242 | + const seen = yield* Queue.unbounded<ReadonlyArray<{ readonly id: string }>>(); |
| 243 | + yield* Stream.runForEach(environmentTheme.streamChanges, (themes) => |
| 244 | + Queue.offer(seen, themes), |
| 245 | + ).pipe(Effect.forkScoped); |
| 246 | + |
| 247 | + // Empty to start. |
| 248 | + assert.deepEqual(yield* Queue.take(seen), []); |
| 249 | + |
| 250 | + // Published atomically, the way a theme hook writes it. |
| 251 | + const staging = path.join(baseDir, "staged.json"); |
| 252 | + yield* fs.writeFileString(staging, encodeThemeFile(NIGHTFALL_THEME)); |
| 253 | + yield* fs.rename(staging, path.join(themesDir, "nightfall.json")); |
| 254 | + assert.deepEqual( |
| 255 | + (yield* Queue.take(seen)).map((theme) => theme.id), |
| 256 | + ["nightfall"], |
| 257 | + ); |
| 258 | + |
| 259 | + // Removed again, and the set empties without a restart. |
| 260 | + yield* fs.remove(path.join(themesDir, "nightfall.json")); |
| 261 | + assert.deepEqual(yield* Queue.take(seen), []); |
| 262 | + }).pipe( |
| 263 | + Effect.provide( |
| 264 | + EnvironmentTheme.layer.pipe( |
| 265 | + Layer.provideMerge(ServerConfig.layerTest(process.cwd(), baseDir)), |
| 266 | + ), |
| 267 | + ), |
| 268 | + Effect.timeout("30 seconds"), |
| 269 | + ); |
| 270 | + }).pipe(Effect.scoped, Effect.provide(NodeServices.layer)), |
| 271 | + ); |
| 272 | +}); |
0 commit comments