diff --git a/src/cli.test.ts b/src/cli.test.ts new file mode 100644 index 0000000..e87760f --- /dev/null +++ b/src/cli.test.ts @@ -0,0 +1,49 @@ +import { afterEach, expect, test } from "bun:test"; +import { mkdtempSync, rmSync } from "node:fs"; +import { tmpdir } from "node:os"; +import { join } from "node:path"; + +const homes: string[] = []; +const cli = join(import.meta.dir, "cli.ts"); + +afterEach(() => { + for (const home of homes.splice(0)) { + rmSync(home, { recursive: true, force: true }); + } +}); + +function createHome(): string { + const home = mkdtempSync(join(tmpdir(), "mem-cli-test-")); + homes.push(home); + return home; +} + +function runCli(home: string, args: string[], stdin?: string) { + return Bun.spawnSync({ + cmd: [process.execPath, cli, ...args], + env: { ...process.env, HOME: home }, + stdin: stdin === undefined ? undefined : new TextEncoder().encode(stdin), + stdout: "pipe", + stderr: "pipe", + }); +} + +test("forget reads multiple memory IDs from stdin", () => { + const home = createHome(); + const first = runCli(home, ["+", "alpha memory"]); + const second = runCli(home, ["+", "beta memory"]); + + expect(first.exitCode).toBe(0); + expect(second.exitCode).toBe(0); + + const firstId = first.stdout.toString().trim(); + const secondId = second.stdout.toString().trim(); + const forgotten = runCli(home, ["-"], `${firstId}\n${secondId}\n`); + + expect(forgotten.exitCode).toBe(0); + expect(forgotten.stdout.toString().trim()).toBe("deleted 2"); + + const remaining = runCli(home, ["--json"]); + expect(remaining.exitCode).toBe(0); + expect(remaining.stdout.toString().trim()).toBe("No memories."); +}); diff --git a/src/cli.ts b/src/cli.ts index 2cec96b..5e360c4 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -225,13 +225,13 @@ function recall(args: ParsedArgs): void { db.close(); } -function forget(args: ParsedArgs): void { +async function forget(args: ParsedArgs): Promise { // Support reading IDs from stdin for bulk delete const ids = args.positionals; if (ids.length === 0 && !process.stdin.isTTY) { // Read IDs from stdin (one per line) - const input = Buffer.from(Bun.stdin.stream() as any).toString("utf-8").trim(); + const input = await readStdin(); if (input) ids.push(...input.split("\n").map((s) => s.trim()).filter(Boolean)); }