import { mkdtemp, writeFile } from "node:fs/promises"; import { tmpdir } from "node:os"; import { join } from "node:path"; import { describe, expect, it } from "vitest"; import { main, USAGE } from "./cli.ts"; import { SyncState } from "./state.ts"; async function setUp(credentials = "BookOrbit admin : diego / s3cret (diego@example.com)\n") { const directory = await mkdtemp(join(tmpdir(), "performative-male-")); const credentialsFile = join(directory, "credentials.txt"); const stateFile = join(directory, "state.sqlite"); await writeFile(credentialsFile, credentials); const env = { GOODREADS_DEVELOPER_KEY: "key", GOODREADS_DEVELOPER_SECRET: "secret", BLUESKY_HANDLE: "diego.codes", BLUESKY_APP_PASSWORD: "app-password", BOOKORBIT_CREDENTIALS_FILE: credentialsFile, STATE_FILE: stateFile, }; const stdout: string[] = []; const stderr: string[] = []; const fetchCalls: string[] = []; const io = { stdout: (line: string) => stdout.push(line), stderr: (line: string) => stderr.push(line), waitForEnter: async () => {}, fetch: (async (input: RequestInfo | URL) => { fetchCalls.push(String(input)); throw new Error("no network in this test"); }) as typeof fetch, }; return { env, io, stdout, stderr, fetchCalls, credentialsFile, stateFile }; } describe("main", () => { it("exits with code 1 and names the file when the admin line is missing, with no network request", async () => { const context = await setUp("KOReader account : kodiego / other\n"); const exitCode = await main(["run"], context.env, context.io); expect(exitCode).toBe(1); expect(context.stderr).toEqual([`credentials: no "BookOrbit admin" line in ${context.credentialsFile}`]); expect(context.fetchCalls).toEqual([]); }); it("exits with code 1 when the Goodreads key is missing, with no network request", async () => { const context = await setUp(); const exitCode = await main(["run"], { ...context.env, GOODREADS_DEVELOPER_KEY: "" }, context.io); expect(exitCode).toBe(1); expect(context.stderr).toEqual(["config: missing GOODREADS_DEVELOPER_KEY or GOODREADS_DEVELOPER_SECRET"]); expect(context.fetchCalls).toEqual([]); }); it("saves a match set by hand and clears the problem of that book", async () => { const context = await setUp(); const state = SyncState.open(context.stateFile, { readOnly: false }); state.recordProblem("goodreads", 30, "no match", new Date("2026-10-20T15:00:00.000Z")); state.close(); const exitCode = await main(["link", "30", "goodreads", "54493401"], context.env, context.io); const reopened = SyncState.open(context.stateFile, { readOnly: true }); expect(exitCode).toBe(0); expect(reopened.getMatch(30, "goodreads")).toEqual({ targetBookId: "54493401", method: "manual" }); expect(reopened.listProblems()).toEqual([]); }); it("refuses a link to an unknown target", async () => { const context = await setUp(); expect(await main(["link", "30", "storygraph", "1"], context.env, context.io)).toBe(1); expect(context.stderr).toEqual([USAGE]); }); it("lists every problem in status", async () => { const context = await setUp(); const state = SyncState.open(context.stateFile, { readOnly: false }); state.recordProblem("bookorbit", null, "bookorbit: login rejected", new Date("2026-10-20T15:00:00.000Z")); state.recordProblem("bookhive", 30, "no match", new Date("2026-10-20T15:05:00.000Z")); state.close(); await main(["status"], context.env, context.io); expect(context.stdout).toEqual([ "bookorbit: bookorbit: login rejected (since 2026-10-20T15:00:00.000Z)", "bookhive book 30: no match (since 2026-10-20T15:05:00.000Z)", ]); }); it("says so when there are no problems", async () => { const context = await setUp(); await main(["status"], context.env, context.io); expect(context.stdout).toEqual(["No problems."]); }); it("prints the usage for an unknown command", async () => { const context = await setUp(); expect(await main(["sync"], context.env, context.io)).toBe(1); expect(context.stderr).toEqual([USAGE]); }); });