linkwarden-cli/test/helpers.ts
KaKi87 0a484efe2f Add Linkwarden CLI
Bun-based command-line client covering the Linkwarden API: links,
collections, tags, highlights, RSS, tokens, settings, import and export.
Includes unit tests, an in-memory mock server suite, and an opt-in e2e
suite for a real server.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-11-10 16:54:19 +01:00

43 lines
1.2 KiB
TypeScript

import { mkdtemp, rm } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { Cli } from "../src/cli.ts";
import { buildNodes } from "../src/commands/index.ts";
import { setColor } from "../src/format.ts";
export interface RunResult {
code: number;
stdout: string;
stderr: string;
json<T = any>(): T;
}
export interface Env {
configPath: string;
dir: string;
run(args: string[], env?: Record<string, string | undefined>): Promise<RunResult>;
cleanup(): Promise<void>;
}
export async function createEnv(defaults: Record<string, string | undefined> = {}): Promise<Env> {
setColor(false);
const dir = await mkdtemp(join(tmpdir(), "lw-test-"));
const configPath = join(dir, "config.json");
const cli = new Cli("linkwarden", "0.0.0-test", buildNodes());
return {
dir,
configPath,
async run(args, env = {}) {
let stdout = "";
let stderr = "";
const code = await cli.run(args, {
env: { ...defaults, ...env },
configPath,
stdout: (t) => void (stdout += t),
stderr: (t) => void (stderr += t),
});
return { code, stdout, stderr, json: () => JSON.parse(stdout) };
},
cleanup: () => rm(dir, { recursive: true, force: true }),
};
}