import { describe, it, expect } from "vitest"; import { parseEnvInput, sortScripts, statusTone, shortId, timeAgo, isActive, activeProcess, type ManagedProcess, } from "../operate"; describe("parseEnvInput", () => { it("parses KEY=value lines, skipping blanks + comments", () => { expect(parseEnvInput("A=1\n\n# c\nB=two")).toEqual({ A: "1", B: "two" }); }); it("strips matching surrounding quotes", () => { expect(parseEnvInput(`A="x y"\nB='z'`)).toEqual({ A: "x y", B: "z" }); }); it("rejects malformed keys", () => { expect(parseEnvInput("1BAD=x\n=y\nGOOD=ok")).toEqual({ GOOD: "ok" }); }); }); describe("sortScripts", () => { it("puts nwire-prefixed first, each group alphabetical", () => { const out = sortScripts([ { name: "test", command: "vitest" }, { name: "nwire:dev", command: "nwire dev" }, { name: "build", command: "tsc" }, { name: "nwire:cache", command: "nwire cache" }, ]); expect(out.map((s) => s.name)).toEqual(["nwire:cache", "nwire:dev", "build", "test"]); }); }); describe("statusTone", () => { it("maps process status to a StatusBadge tone", () => { expect(statusTone("running")).toBe("live"); expect(statusTone("starting")).toBe("warn"); expect(statusTone("stopping")).toBe("warn"); expect(statusTone("crashed")).toBe("error"); expect(statusTone("exited")).toBe("idle"); expect(statusTone("idle")).toBe("idle"); }); }); describe("shortId", () => { it("takes the first uuid segment", () => { expect(shortId("abc12345-de-fg")).toBe("abc12345"); expect(shortId("plainid")).toBe("plainid"); }); }); describe("timeAgo", () => { const base = Date.parse("2026-06-18T12:00:00.000Z"); it("formats seconds / minutes / hours", () => { expect(timeAgo("2026-06-18T11:59:50.000Z", base)).toBe("10s ago"); expect(timeAgo("2026-06-18T11:55:00.000Z", base)).toBe("5m ago"); expect(timeAgo("2026-06-18T10:00:00.000Z", base)).toBe("2h ago"); }); it("never goes negative", () => { expect(timeAgo("2026-06-18T12:00:30.000Z", base)).toBe("0s ago"); }); }); const proc = (over: Partial): ManagedProcess => ({ id: "x", topology: "t", startedAt: "2026-01-01T00:00:00Z", status: "idle", ...over, }); describe("isActive", () => { it("is true for running / starting / stopping", () => { expect(isActive("running")).toBe(true); expect(isActive("starting")).toBe(true); expect(isActive("stopping")).toBe(true); expect(isActive("exited")).toBe(false); }); }); describe("activeProcess", () => { it("returns the most-recently-started running process", () => { const list = [ proc({ id: "a", status: "running", startedAt: "2026-01-01T00:00:00Z" }), proc({ id: "b", status: "running", startedAt: "2026-01-02T00:00:00Z" }), proc({ id: "c", status: "exited", startedAt: "2026-01-03T00:00:00Z" }), ]; expect(activeProcess(list)?.id).toBe("b"); }); it("returns undefined when nothing is running", () => { expect(activeProcess([proc({ status: "exited" })])).toBeUndefined(); expect(activeProcess([])).toBeUndefined(); }); });