// The rendered units, byte for byte: host mode's two (the shape the // operator's own units on the reference machine took, with its paths replaced // by synthetic ones), a private site's, and docker mode's. // // Run with: pnpm --filter yt-dlp-transcript-common exec tsx --test lib/service/units.test.ts import { test } from "node:test"; import assert from "node:assert/strict"; import { composeArgs, defaultMemory, hostUnitPath, renderDockerUnit, renderHostUnit, renderHousekeepingService, renderHousekeepingTimer, renderSiteUnit, unitDiff, } from "./units"; // MemTotal of the reference machine (16 GB nominal), in bytes. const MEM_16G = 16283088 * 1024; const CTX = { repoRoot: "/home/user/archilyzer", home: "/home/user", nodeBin: "/usr/bin/node", bind: "0.0.0.0" }; const EDITOR = `[Unit] Description=Archilyzer editor (:3001) Documentation=file:///home/user/archilyzer/RUNNING_IN_DOCKER.md After=network-online.target Wants=network-online.target # A crash-loop stops after 5 tries in 5 minutes instead of spinning. StartLimitIntervalSec=300 StartLimitBurst=5 [Service] Type=simple WorkingDirectory=/home/user/archilyzer/editor # Next reads editor/.env itself (WORKER_TOKEN and the rest). Environment=NODE_ENV=production NEXT_TELEMETRY_DISABLED=1 UV_THREADPOOL_SIZE=16 # yt-dlp, gallery-dl, parakeet-cli and git-filter-repo live in ~/.local/bin. Environment=PATH=/home/user/.local/bin:/home/user/.local/share/pnpm:/usr/local/bin:/usr/bin:/bin ExecStartPre=/bin/sh -c 'test -s .next/BUILD_ID || { echo "archilyzer-editor: no build in /home/user/archilyzer/editor/.next - build it first (pnpm --filter editor exec next build)" >&2; exit 1; }' # node directly, not pnpm: pnpm swallows SIGTERM. ExecStart=/usr/bin/node node_modules/next/dist/bin/next start -H 0.0.0.0 -p 3001 Restart=on-failure RestartSec=5 # SIGTERM to the editor first; its shutdown reaper stops the jobs it started (downloads, # transcriptions) before the rest of the unit is killed. KillMode=mixed TimeoutStopSec=90 # The editor's jobs (parakeet ~3.4 GB, yt-dlp, ffmpeg) run in this unit and count here: # a runaway is killed inside the unit, not by a global OOM that takes the session bus. MemoryHigh=7G MemoryMax=8G [Install] WantedBy=default.target `; const UMTOOL = `[Unit] Description=Archilyzer umtool (:3050) Documentation=file:///home/user/archilyzer/RUNNING_IN_DOCKER.md After=network-online.target archilyzer-editor.service Wants=network-online.target StartLimitIntervalSec=300 StartLimitBurst=5 [Service] Type=simple WorkingDirectory=/home/user/archilyzer/umtool # Next reads umtool/.env.local itself. Environment=NODE_ENV=production NEXT_TELEMETRY_DISABLED=1 Environment=PATH=/home/user/.local/bin:/home/user/.local/share/pnpm:/usr/local/bin:/usr/bin:/bin ExecStartPre=/bin/sh -c 'test -s .next/BUILD_ID || { echo "archilyzer-umtool: no build in /home/user/archilyzer/umtool/.next - build it first (pnpm --filter umtool exec next build)" >&2; exit 1; }' ExecStart=/usr/bin/node node_modules/next/dist/bin/next start -H 0.0.0.0 -p 3050 Restart=on-failure RestartSec=5 KillMode=mixed TimeoutStopSec=60 # Renders started from umtool (ffmpeg, the chrome frames) run in this unit. MemoryHigh=5G MemoryMax=6G [Install] WantedBy=default.target `; test("host mode renders the editor unit exactly", () => { const got = renderHostUnit("editor", { ...CTX, port: 3001, memory: defaultMemory("editor", MEM_16G) }); assert.equal(got, EDITOR, unitDiff(EDITOR, got)); }); test("host mode renders the umtool unit exactly", () => { const got = renderHostUnit("umtool", { ...CTX, port: 3050, memory: defaultMemory("umtool", MEM_16G) }); assert.equal(got, UMTOOL, unitDiff(UMTOOL, got)); }); test("memory defaults scale with MemTotal and never fall under the floor", () => { assert.deepEqual(defaultMemory("editor", MEM_16G), { high: 7, max: 8 }); assert.deepEqual(defaultMemory("umtool", MEM_16G), { high: 5, max: 6 }); assert.deepEqual(defaultMemory("editor", 64 * 2 ** 30), { high: 31, max: 32 }); assert.deepEqual(defaultMemory("editor", 2 * 2 ** 30), { high: 2, max: 3 }); assert.deepEqual(defaultMemory("umtool", 2 * 2 ** 30), { high: 1, max: 2 }); }); test("the idle-boot option reaches the editor's environment only", () => { const ed = renderHostUnit("editor", { ...CTX, port: 3001, memory: { high: 7, max: 8 }, idleBoot: true }); assert.match(ed, /^Environment=NODE_ENV=production NEXT_TELEMETRY_DISABLED=1 UV_THREADPOOL_SIZE=16 ARCHILYZER_IDLE_BOOT=1$/m); const um = renderHostUnit("umtool", { ...CTX, port: 3050, memory: { high: 5, max: 6 }, idleBoot: true }); assert.doesNotMatch(um, /IDLE_BOOT/); }); test("PATH puts a version manager's node first", () => { assert.equal(hostUnitPath("/h", "/usr/bin/node"), "/h/.local/bin:/h/.local/share/pnpm:/usr/local/bin:/usr/bin:/bin"); assert.equal( hostUnitPath("/h", "/h/.nvm/versions/node/v22.1.0/bin/node"), "/h/.nvm/versions/node/v22.1.0/bin:/h/.local/bin:/h/.local/share/pnpm:/usr/local/bin:/usr/bin:/bin", ); }); test("a path with % or $ is spelled so systemd reads it back as written", () => { const got = renderHostUnit("editor", { ...CTX, repoRoot: "/srv/a%b$c", port: 3001, memory: { high: 7, max: 8 } }); // WorkingDirectory= expands specifiers but not variables; an Exec line both. assert.ok(got.includes("\nWorkingDirectory=/srv/a%%b$c/editor\n"), got); assert.ok(got.includes("no build in /srv/a%%b$$c/editor/.next"), got); }); test("a private site's unit serves its bundle on this machine only", () => { const bundle = "/home/user/archilyzer/export/.export-builds/demo-private/out"; const got = renderSiteUnit({ ...CTX, siteId: "demo-private", bundleDir: bundle, port: 8767 }); assert.ok(got.includes("\nDescription=Archilyzer site demo-private (127.0.0.1:8767)\n")); assert.ok(got.includes("\nEnvironment=HOST=127.0.0.1\n")); assert.ok(got.includes(`\nExecStart=/usr/bin/node scripts/serve-out.mjs ${bundle} 8767\n`)); assert.ok(got.includes("\nWorkingDirectory=/home/user/archilyzer/export\n")); assert.ok(got.includes("pnpm archilyzer publish build demo-private")); }); test("docker mode: one unit, compose in the foreground, profiles from the apps, down on stop", () => { const ctx = { repoRoot: "/home/user/archilyzer", home: "/home/user", dockerBin: "/usr/bin/docker", apps: ["editor", "umtool", "ollama"] as const, overlays: ["docker-compose.anthropic.yml"], }; assert.deepEqual(composeArgs(ctx), [ "compose", "-f", "docker-compose.yml", "-f", "docker-compose.anthropic.yml", "--profile", "umtool", "--profile", "ollama", ]); const got = renderDockerUnit(ctx); const compose = "/usr/bin/docker compose -f docker-compose.yml -f docker-compose.anthropic.yml --profile umtool --profile ollama"; assert.ok(got.includes("\nDescription=Archilyzer in Docker (editor, umtool, ollama)\n")); assert.ok(got.includes(`\nExecStart=${compose} up --no-build\n`)); assert.ok(got.includes(`\nExecStop=${compose} down\n`)); assert.ok(got.includes("\nWorkingDirectory=/home/user/archilyzer\n")); assert.ok(got.includes("\nRestart=on-failure\n")); // Every shell $ in an Exec line is doubled, so systemd hands sh a single one. for (const line of got.split("\n").filter((l) => l.startsWith("ExecStartPre="))) { assert.equal(line.replace(/\$\$/g, "").includes("$"), false, line); } assert.ok(got.includes(`image $$i is not built - build it first (${compose} build)`)); }); test("unitDiff marks the installed side - and the rendered side +", () => { assert.equal(unitDiff("a\nb\n", "a\nb\n"), ""); assert.equal(unitDiff("a\nb\nc", "a\nB\nc"), " a\n- b\n+ B\n c"); }); test("the housekeeping service and its weekly timer render exactly", () => { assert.equal( renderHousekeepingService({ repoRoot: "/home/user/archilyzer", home: "/home/user", nodeBin: "/usr/bin/node" }), `[Unit] Description=Archilyzer housekeeping (superseded takes, idle work files, merged worktrees) Documentation=file:///home/user/archilyzer/OPERATING.md # Started by archilyzer-housekeeping.timer; \`systemctl --user start archilyzer-housekeeping.service\` runs it now. # A run the heavy slot turns away (a render, an e2e suite) exits 75 and is tried again in an hour, for a day. StartLimitIntervalSec=1d StartLimitBurst=24 [Service] Type=oneshot WorkingDirectory=/home/user/archilyzer/common Environment=PATH=/home/user/.local/bin:/home/user/.local/share/pnpm:/usr/local/bin:/usr/bin:/bin # tsx from common's own node_modules, as \`pnpm archilyzer\` runs it. ExecStart=/usr/bin/node --import tsx bin/archilyzer.ts housekeeping --by timer RestartForceExitStatus=75 RestartSec=1h SuccessExitStatus=75 # Deleting is light: never in the way of what the machine is doing. Nice=10 IOSchedulingClass=idle MemoryMax=2G TimeoutStartSec=2h `, ); assert.equal( renderHousekeepingTimer(), `[Unit] Description=Archilyzer housekeeping, weekly [Timer] OnCalendar=weekly Persistent=true RandomizedDelaySec=30m Unit=archilyzer-housekeeping.service [Install] WantedBy=timers.target `, ); });