import { describe, it, expect, beforeEach, afterEach } from "vitest"; import { mkdirSync, writeFileSync, rmSync } from "node:fs"; import { resolve } from "node:path"; import { tmpdir } from "node:os"; // Telemetry-run readers (listTelemetryRuns / readTelemetryRun) are re-exported // from `@nwire/scan` and covered by that package's telemetry-runs test. This // file covers nwire-read's own surface: readManifest + foldTopology. import { readManifest, foldTopology } from "../nwire-read"; // ── fixture helpers ─────────────────────────────────────────────────────────── let tmp: string; function nwireDir(): string { return resolve(tmp, ".nwire"); } function writeManifest(content: string): void { mkdirSync(nwireDir(), { recursive: true }); writeFileSync(resolve(nwireDir(), "manifest.json"), content, "utf8"); } beforeEach(() => { tmp = resolve(tmpdir(), `nwire-read-test-${process.pid}-${Date.now()}`); mkdirSync(tmp, { recursive: true }); }); afterEach(() => { rmSync(tmp, { recursive: true, force: true }); }); // ── readManifest ───────────────────────────────────────────────────────────── describe("readManifest", () => { it("returns null when .nwire/ does not exist", () => { expect(readManifest(tmp)).toBeNull(); }); it("returns null when manifest.json is missing", () => { mkdirSync(nwireDir(), { recursive: true }); expect(readManifest(tmp)).toBeNull(); }); it("returns the raw JSON string when file is present", () => { const raw = JSON.stringify({ apps: [], actions: [] }); writeManifest(raw); expect(readManifest(tmp)).toBe(raw); }); it("preserves exact bytes — no re-serialisation", () => { const raw = '{"apps":[],"note":"trailing-space" }'; writeManifest(raw); expect(readManifest(tmp)).toBe(raw); }); }); // ── foldTopology ───────────────────────────────────────────────────────────── describe("foldTopology", () => { it("returns null for null input", () => { expect(foldTopology(null, tmp)).toBeNull(); }); it("returns null for invalid JSON", () => { expect(foldTopology("{broken json", tmp)).toBeNull(); }); it("parses and returns the manifest object", () => { const obj = { apps: [{ name: "orders" }], actions: [] }; const result = foldTopology(JSON.stringify(obj), tmp) as typeof obj; expect(result).toEqual(obj); }); it("round-trips through readManifest correctly", () => { const obj = { apps: [{ name: "billing" }], actions: [], events: [] }; writeManifest(JSON.stringify(obj)); const raw = readManifest(tmp); expect(foldTopology(raw, tmp)).toEqual(obj); }); });