|
| 1 | +import { mkdtemp, mkdir, readFile, writeFile } from "node:fs/promises"; |
| 2 | +import os from "node:os"; |
| 3 | +import { join } from "node:path"; |
| 4 | +import { afterEach, describe, expect, it } from "vitest"; |
| 5 | +import { doctorSrdFramework } from "../src/lib/doctor.js"; |
| 6 | +import { installSrdFramework } from "../src/lib/install.js"; |
| 7 | +import { resolvePackageRoot } from "../src/lib/paths.js"; |
| 8 | +import { uninstallSrdFramework } from "../src/lib/uninstall.js"; |
| 9 | +import { updateSrdFramework } from "../src/lib/update.js"; |
| 10 | + |
| 11 | +const tempRoots: string[] = []; |
| 12 | + |
| 13 | +async function makeTempDir(prefix: string): Promise<string> { |
| 14 | + const root = await mkdtemp(join(os.tmpdir(), prefix)); |
| 15 | + tempRoots.push(root); |
| 16 | + return root; |
| 17 | +} |
| 18 | + |
| 19 | +afterEach(async () => { |
| 20 | + const { rm } = await import("node:fs/promises"); |
| 21 | + await Promise.all(tempRoots.splice(0).map((path) => rm(path, { recursive: true, force: true }))); |
| 22 | +}); |
| 23 | + |
| 24 | +describe("install/update/uninstall/doctor", () => { |
| 25 | + it("installs into a temp config directory and is idempotent", async () => { |
| 26 | + const packageRoot = resolvePackageRoot(import.meta.url); |
| 27 | + const configDir = await makeTempDir("opencode-srd-install-"); |
| 28 | + |
| 29 | + const first = await installSrdFramework({ configDir, packageRoot }); |
| 30 | + const second = await installSrdFramework({ configDir, packageRoot }); |
| 31 | + |
| 32 | + expect(first.copied.length).toBeGreaterThan(0); |
| 33 | + expect(second.copied).toHaveLength(0); |
| 34 | + expect(second.updated).toHaveLength(0); |
| 35 | + expect(join(configDir, "commands", "srd-assess.md")).toBeTruthy(); |
| 36 | + expect(await readFile(join(configDir, "commands", "srd-assess.md"), "utf8")).toContain("SRD Assess"); |
| 37 | + }); |
| 38 | + |
| 39 | + it("update refreshes managed files", async () => { |
| 40 | + const packageRoot = resolvePackageRoot(import.meta.url); |
| 41 | + const configDir = await makeTempDir("opencode-srd-update-"); |
| 42 | + |
| 43 | + await installSrdFramework({ configDir, packageRoot }); |
| 44 | + await writeFile(join(configDir, "commands", "srd-quick.md"), "mutated", "utf8"); |
| 45 | + |
| 46 | + const result = await updateSrdFramework({ configDir, packageRoot }); |
| 47 | + const content = await readFile(join(configDir, "commands", "srd-quick.md"), "utf8"); |
| 48 | + |
| 49 | + expect(result.updated).toContain("commands/srd-quick.md"); |
| 50 | + expect(content).toContain("SRD Quick"); |
| 51 | + }); |
| 52 | + |
| 53 | + it("uninstall removes only managed files", async () => { |
| 54 | + const packageRoot = resolvePackageRoot(import.meta.url); |
| 55 | + const configDir = await makeTempDir("opencode-srd-uninstall-"); |
| 56 | + await installSrdFramework({ configDir, packageRoot }); |
| 57 | + |
| 58 | + await mkdir(join(configDir, "commands"), { recursive: true }); |
| 59 | + await writeFile(join(configDir, "commands", "custom.md"), "keep me", "utf8"); |
| 60 | + |
| 61 | + await uninstallSrdFramework({ configDir }); |
| 62 | + |
| 63 | + await expect(readFile(join(configDir, "commands", "custom.md"), "utf8")).resolves.toBe("keep me"); |
| 64 | + await expect(readFile(join(configDir, "commands", "srd-assess.md"), "utf8")).rejects.toThrow(); |
| 65 | + }); |
| 66 | + |
| 67 | + it("doctor reports healthy and unhealthy states", async () => { |
| 68 | + const packageRoot = resolvePackageRoot(import.meta.url); |
| 69 | + const configDir = await makeTempDir("opencode-srd-doctor-"); |
| 70 | + |
| 71 | + await installSrdFramework({ configDir, packageRoot }); |
| 72 | + const healthy = await doctorSrdFramework({ configDir, packageRoot }); |
| 73 | + |
| 74 | + expect(healthy.healthy).toBe(true); |
| 75 | + |
| 76 | + await writeFile(join(configDir, "commands", "srd-assess.md"), "drifted", "utf8"); |
| 77 | + const unhealthy = await doctorSrdFramework({ configDir, packageRoot }); |
| 78 | + |
| 79 | + expect(unhealthy.healthy).toBe(false); |
| 80 | + expect(unhealthy.driftedFiles).toContain("commands/srd-assess.md"); |
| 81 | + }); |
| 82 | +}); |
0 commit comments