Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/cli.test.ts
Original file line number Diff line number Diff line change
@@ -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.");
});
4 changes: 2 additions & 2 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,13 @@ function recall(args: ParsedArgs): void {
db.close();
}

function forget(args: ParsedArgs): void {
async function forget(args: ParsedArgs): Promise<void> {
// 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));
}

Expand Down