The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | // @effect-diagnostics nodeBuiltinImport:off - CLI integration exercises the filesystem boundary. |
| 2 | import * as NodeFS from "node:fs"; |
| 3 | import * as NodeOS from "node:os"; |
| 4 | import * as NodePath from "node:path"; |
| 5 | |
| 6 | import * as NodeCrypto from "@effect/platform-node/NodeCrypto"; |
| 7 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 8 | import * as NetService from "@t3tools/shared/Net"; |
| 9 | import { assert, describe, it } from "@effect/vitest"; |
| 10 | import * as ConfigProvider from "effect/ConfigProvider"; |
| 11 | import * as Effect from "effect/Effect"; |
| 12 | import * as Layer from "effect/Layer"; |
| 13 | import * as TestConsole from "effect/testing/TestConsole"; |
| 14 | import { Command } from "effect/cli"; |
| 15 | import { vi } from "vite-plus/test"; |
| 16 | |
| 17 | import { cli } from "../binCli.ts"; |
| 18 | import { symlinksSupported } from "@t3tools/shared/testing/symlinks"; |
| 19 | import * as HostProcess from "@t3tools/shared/HostProcess"; |
| 20 | |
| 21 | vi.mock("node:fs", { spy: true }); |
| 22 | |
| 23 | // Windows chmod cannot force these directory or file permission failures. |
| 24 | const windowsHost = HostProcess.Platform.defaultValue() === "win32"; |
| 25 | |
| 26 | const runCli = (args: ReadonlyArray<string>) => |
| 27 | Command.runWith(cli, { version: "0.0.0" })(args).pipe( |
| 28 | Effect.provide( |
| 29 | Layer.mergeAll(NodeServices.layer, NodeCrypto.layer, NetService.layer, TestConsole.layer), |
| 30 | ), |
| 31 | ); |
| 32 | |
| 33 | const makeBaseDir = () => NodeFS.mkdtempSync(NodePath.join(NodeOS.tmpdir(), "t3code-theme-cli-")); |
| 34 | |
| 35 | const settingsPathFor = (baseDir: string) => NodePath.join(baseDir, "userdata", "settings.json"); |
| 36 | |
| 37 | const NIGHTFALL_THEME_JSON = `${JSON.stringify({ |
| 38 | name: "Nightfall", |
| 39 | appearance: "dark", |
| 40 | canvas: "#1a1b26", |
| 41 | accent: "#7aa2f7", |
| 42 | })}\n`; |
| 43 | const JUNK_THEME_JSON = `${JSON.stringify({ name: "Junk" })}\n`; |
| 44 | |
| 45 | const readSettings = (baseDir: string): Record<string, unknown> => { |
| 46 | const raw = NodeFS.readFileSync(settingsPathFor(baseDir), "utf8"); |
| 47 | return JSON.parse(raw) as Record<string, unknown>; |
| 48 | }; |
| 49 | |
| 50 | const writeSettings = (baseDir: string, settings: Record<string, unknown>) => { |
| 51 | NodeFS.mkdirSync(NodePath.dirname(settingsPathFor(baseDir)), { recursive: true }); |
| 52 | NodeFS.writeFileSync(settingsPathFor(baseDir), `${JSON.stringify(settings, null, 2)}\n`); |
| 53 | }; |
| 54 | |
| 55 | describe("t3 theme", () => { |
| 56 | it.effect("preserves settings it does not recognise", () => |
| 57 | Effect.gen(function* () { |
| 58 | const baseDir = makeBaseDir(); |
| 59 | writeSettings(baseDir, { |
| 60 | enableProviderUpdateChecks: false, |
| 61 | somethingFromANewerBuild: { nested: true }, |
| 62 | }); |
| 63 | |
| 64 | yield* runCli(["theme", "set", "ocean", "--base-dir", baseDir]); |
| 65 | |
| 66 | const settings = readSettings(baseDir); |
| 67 | assert.equal(settings.defaultTheme, "ocean"); |
| 68 | assert.equal(settings.enableProviderUpdateChecks, false); |
| 69 | assert.deepEqual(settings.somethingFromANewerBuild, { nested: true }); |
| 70 | }), |
| 71 | ); |
| 72 | |
| 73 | it.effect("clears the default back to leaving fresh clients alone", () => |
| 74 | Effect.gen(function* () { |
| 75 | const baseDir = makeBaseDir(); |
| 76 | writeSettings(baseDir, { enableProviderUpdateChecks: false }); |
| 77 | |
| 78 | yield* runCli(["theme", "set", "ocean", "--base-dir", baseDir]); |
| 79 | yield* runCli(["theme", "clear", "--base-dir", baseDir]); |
| 80 | |
| 81 | const settings = readSettings(baseDir); |
| 82 | assert.equal(Object.hasOwn(settings, "defaultTheme"), false); |
| 83 | assert.equal(settings.enableProviderUpdateChecks, false); |
| 84 | }), |
| 85 | ); |
| 86 | |
| 87 | it.effect("sets the theme of a dev server running in the default home", () => |
| 88 | Effect.gen(function* () { |
| 89 | const home = makeBaseDir(); |
| 90 | const devStateDir = NodePath.join(home, ".t3", "dev"); |
| 91 | NodeFS.mkdirSync(devStateDir, { recursive: true }); |
| 92 | NodeFS.writeFileSync( |
| 93 | NodePath.join(devStateDir, "server-runtime.json"), |
| 94 | `${JSON.stringify({ |
| 95 | version: 1, |
| 96 | pid: process.pid, |
| 97 | port: 4888, |
| 98 | origin: "http://127.0.0.1:4888", |
| 99 | devUrl: "http://127.0.0.1:5999", |
| 100 | startedAt: "1970-01-01T00:00:00.000Z", |
| 101 | })}\n`, |
| 102 | ); |
| 103 | |
| 104 | const result = yield* runCli(["theme", "set", "ocean"]).pipe( |
| 105 | Effect.provideService(HostProcess.HomeDirectory, home), |
| 106 | Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env: {} }))), |
| 107 | Effect.result, |
| 108 | ); |
| 109 | |
| 110 | assert.equal( |
| 111 | NodeFS.existsSync(NodePath.join(home, ".t3", "userdata", "settings.json")), |
| 112 | false, |
| 113 | ); |
| 114 | if (result._tag === "Success") { |
| 115 | const devSettings = NodePath.join(devStateDir, "settings.json"); |
| 116 | assert.equal(JSON.parse(NodeFS.readFileSync(devSettings, "utf8")).defaultTheme, "ocean"); |
| 117 | } |
| 118 | }), |
| 119 | ); |
| 120 | |
| 121 | it.effect("publishes a theme file under its filename and sets it", () => |
| 122 | Effect.gen(function* () { |
| 123 | const baseDir = makeBaseDir(); |
| 124 | const themeFile = NodePath.join(baseDir, "nightfall.json"); |
| 125 | NodeFS.writeFileSync(themeFile, NIGHTFALL_THEME_JSON); |
| 126 | |
| 127 | yield* runCli(["theme", "set", themeFile, "--base-dir", baseDir]); |
| 128 | |
| 129 | const published = NodePath.join(baseDir, "userdata", "themes", "nightfall.json"); |
| 130 | assert.equal(NodeFS.existsSync(published), true); |
| 131 | assert.equal(readSettings(baseDir).defaultTheme, "nightfall"); |
| 132 | const residue = NodeFS.readdirSync(NodePath.dirname(published)).filter( |
| 133 | (entry) => !entry.endsWith(".json"), |
| 134 | ); |
| 135 | assert.deepEqual(residue, []); |
| 136 | }), |
| 137 | ); |
| 138 | |
| 139 | it.effect("refuses to publish over a directory with the theme's file name", () => |
| 140 | Effect.gen(function* () { |
| 141 | const baseDir = makeBaseDir(); |
| 142 | const taken = NodePath.join(baseDir, "userdata", "themes", "nightfall.json"); |
| 143 | NodeFS.mkdirSync(taken, { recursive: true }); |
| 144 | NodeFS.writeFileSync(NodePath.join(taken, "notes.txt"), "notes"); |
| 145 | const themeFile = NodePath.join(baseDir, "nightfall.json"); |
| 146 | NodeFS.writeFileSync(themeFile, NIGHTFALL_THEME_JSON); |
| 147 | |
| 148 | const failure = yield* runCli(["theme", "set", themeFile, "--base-dir", baseDir]).pipe( |
| 149 | Effect.flip, |
| 150 | ); |
| 151 | |
| 152 | assert.include(String(failure), "is a directory"); |
| 153 | assert.equal(NodeFS.readFileSync(NodePath.join(taken, "notes.txt"), "utf8"), "notes"); |
| 154 | assert.deepEqual(NodeFS.readdirSync(NodePath.dirname(taken)), ["nightfall.json"]); |
| 155 | assert.equal(NodeFS.existsSync(settingsPathFor(baseDir)), false); |
| 156 | }), |
| 157 | ); |
| 158 | |
| 159 | it.effect("refuses to publish over a directory created just before backup", () => |
| 160 | Effect.gen(function* () { |
| 161 | const baseDir = makeBaseDir(); |
| 162 | const themeFile = NodePath.join(baseDir, "nightfall.json"); |
| 163 | NodeFS.writeFileSync(themeFile, NIGHTFALL_THEME_JSON); |
| 164 | const themesDir = NodePath.join(baseDir, "userdata", "themes"); |
| 165 | const destination = NodePath.join(themesDir, "nightfall.json"); |
| 166 | const notes = NodePath.join(destination, "notes.txt"); |
| 167 | const { renameSync } = yield* Effect.promise(() => vi.importActual<typeof NodeFS>("node:fs")); |
| 168 | using _insertDirectory = vi.spyOn(NodeFS, "renameSync").mockImplementationOnce((from, to) => { |
| 169 | NodeFS.mkdirSync(destination); |
| 170 | NodeFS.writeFileSync(notes, "notes"); |
| 171 | renameSync(from, to); |
| 172 | }); |
| 173 | |
| 174 | const failure = yield* runCli(["theme", "set", themeFile, "--base-dir", baseDir]).pipe( |
| 175 | Effect.flip, |
| 176 | ); |
| 177 | |
| 178 | assert.include(String(failure), "is a directory"); |
| 179 | assert.equal(NodeFS.readFileSync(notes, "utf8"), "notes"); |
| 180 | assert.deepEqual(NodeFS.readdirSync(themesDir), ["nightfall.json"]); |
| 181 | assert.equal(NodeFS.existsSync(settingsPathFor(baseDir)), false); |
| 182 | }), |
| 183 | ); |
| 184 | |
| 185 | it.effect("publishes a theme despite a rollback file left by an earlier process", () => |
| 186 | Effect.gen(function* () { |
| 187 | const baseDir = makeBaseDir(); |
| 188 | const themeFile = NodePath.join(baseDir, "nightfall.json"); |
| 189 | NodeFS.writeFileSync(themeFile, NIGHTFALL_THEME_JSON); |
| 190 | const themesDir = NodePath.join(baseDir, "userdata", "themes"); |
| 191 | const destination = NodePath.join(themesDir, "nightfall.json"); |
| 192 | const staleBackup = `${destination}.rollback-${process.pid}`; |
| 193 | NodeFS.mkdirSync(themesDir, { recursive: true }); |
| 194 | NodeFS.writeFileSync(staleBackup, "previous theme"); |
| 195 | |
| 196 | yield* runCli(["theme", "set", themeFile, "--base-dir", baseDir]); |
| 197 | |
| 198 | assert.equal(readSettings(baseDir).defaultTheme, "nightfall"); |
| 199 | assert.equal(NodeFS.readFileSync(destination, "utf8"), NIGHTFALL_THEME_JSON); |
| 200 | assert.equal(NodeFS.readFileSync(staleBackup, "utf8"), "previous theme"); |
| 201 | assert.deepEqual(NodeFS.readdirSync(themesDir).sort(), [ |
| 202 | "nightfall.json", |
| 203 | NodePath.basename(staleBackup), |
| 204 | ]); |
| 205 | }), |
| 206 | ); |
| 207 | |
| 208 | it.effect("rejects a file that is not a theme and sets nothing", () => |
| 209 | Effect.gen(function* () { |
| 210 | const baseDir = makeBaseDir(); |
| 211 | const themeFile = NodePath.join(baseDir, "junk.json"); |
| 212 | NodeFS.writeFileSync(themeFile, JUNK_THEME_JSON); |
| 213 | |
| 214 | const failure = yield* runCli(["theme", "set", themeFile, "--base-dir", baseDir]).pipe( |
| 215 | Effect.flip, |
| 216 | ); |
| 217 | |
| 218 | assert.include(String(failure), "not a valid theme file"); |
| 219 | assert.equal(NodeFS.existsSync(NodePath.join(baseDir, "userdata", "themes")), false); |
| 220 | assert.equal(NodeFS.existsSync(settingsPathFor(baseDir)), false); |
| 221 | }), |
| 222 | ); |
| 223 | |
| 224 | it.effect.skipIf(!symlinksSupported)("never writes through a symlink at the staging path", () => |
| 225 | Effect.gen(function* () { |
| 226 | const baseDir = makeBaseDir(); |
| 227 | const themesDir = NodePath.join(baseDir, "userdata", "themes"); |
| 228 | NodeFS.mkdirSync(themesDir, { recursive: true }); |
| 229 | const victim = NodePath.join(baseDir, "victim.txt"); |
| 230 | NodeFS.writeFileSync(victim, "precious"); |
| 231 | NodeFS.symlinkSync(victim, NodePath.join(themesDir, `nightfall.json.staging-${process.pid}`)); |
| 232 | const themeFile = NodePath.join(baseDir, "nightfall.json"); |
| 233 | NodeFS.writeFileSync(themeFile, NIGHTFALL_THEME_JSON); |
| 234 | |
| 235 | yield* runCli(["theme", "set", themeFile, "--base-dir", baseDir]); |
| 236 | |
| 237 | assert.equal(NodeFS.readFileSync(victim, "utf8"), "precious"); |
| 238 | assert.equal(readSettings(baseDir).defaultTheme, "nightfall"); |
| 239 | }), |
| 240 | ); |
| 241 | |
| 242 | it.effect.skipIf(windowsHost)("restores the previous theme when a re-publish fails to set", () => |
| 243 | Effect.gen(function* () { |
| 244 | const baseDir = makeBaseDir(); |
| 245 | writeSettings(baseDir, {}); |
| 246 | const userdataDir = NodePath.dirname(settingsPathFor(baseDir)); |
| 247 | const themesDir = NodePath.join(userdataDir, "themes"); |
| 248 | NodeFS.mkdirSync(themesDir, { recursive: true }); |
| 249 | const publishedPath = NodePath.join(themesDir, "nightfall.json"); |
| 250 | const previous = |
| 251 | '{ "name": "Old Nightfall", "appearance": "dark", "canvas": "#000000", "accent": "#ffffff" }\n'; |
| 252 | NodeFS.writeFileSync(publishedPath, previous); |
| 253 | const themeFile = NodePath.join(baseDir, "nightfall.json"); |
| 254 | NodeFS.writeFileSync(themeFile, NIGHTFALL_THEME_JSON); |
| 255 | |
| 256 | NodeFS.chmodSync(userdataDir, 0o555); |
| 257 | try { |
| 258 | yield* runCli(["theme", "set", themeFile, "--base-dir", baseDir]).pipe(Effect.flip); |
| 259 | assert.equal(NodeFS.readFileSync(publishedPath, "utf8"), previous); |
| 260 | } finally { |
| 261 | NodeFS.chmodSync(userdataDir, 0o755); |
| 262 | } |
| 263 | }), |
| 264 | ); |
| 265 | |
| 266 | it.effect.skipIf(windowsHost)("refuses to write when the settings file cannot be read", () => |
| 267 | Effect.gen(function* () { |
| 268 | const baseDir = makeBaseDir(); |
| 269 | writeSettings(baseDir, { enableProviderUpdateChecks: false }); |
| 270 | NodeFS.chmodSync(settingsPathFor(baseDir), 0o000); |
| 271 | |
| 272 | const failure = yield* runCli(["theme", "set", "ocean", "--base-dir", baseDir]).pipe( |
| 273 | Effect.flip, |
| 274 | ); |
| 275 | |
| 276 | NodeFS.chmodSync(settingsPathFor(baseDir), 0o644); |
| 277 | assert.include(String(failure), "Could not read"); |
| 278 | assert.equal(readSettings(baseDir).enableProviderUpdateChecks, false); |
| 279 | assert.equal(Object.hasOwn(readSettings(baseDir), "defaultTheme"), false); |
| 280 | }), |
| 281 | ); |
| 282 | }); |