Skip to content
Merged
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
4 changes: 3 additions & 1 deletion packages/cli/src/utils/mail.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
import { existsSync, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, unlinkSync, writeFileSync } from "node:fs";
import { join } from "node:path";
import { homedir } from "node:os";
import { randomUUID } from "node:crypto";
Expand Down Expand Up @@ -256,6 +256,8 @@ export function ackMessage(agent: string, id: string): MailMessage | null {
delete msg.checkedOutBy;
delete msg.retryAfter;
writeMessageFile(path, msg);
// Remove the file from cur/ now that it's acked — audit trail is in log/
try { unlinkSync(path); } catch { /* best effort — don't fail ack if cleanup fails */ }
return msg;
}

Expand Down
42 changes: 41 additions & 1 deletion packages/cli/test/mail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { mkdtempSync, rmSync, readdirSync, mkdirSync, writeFileSync } from "node
import { join, resolve } from "node:path";
import { tmpdir } from "node:os";
import { spawnSync } from "node:child_process";
import { checkMessages, getInbox, inboxExists, listMessages, sendMessage } from "../src/utils/mail.js";
import { checkMessages, getInbox, inboxExists, listMessages, sendMessage, ackMessage, countInboxMessages } from "../src/utils/mail.js";

const TPS_BIN = resolve(import.meta.dir, "../bin/tps.ts");

Expand Down Expand Up @@ -87,6 +87,46 @@ describe("mail utils", () => {
expect(inboxExists("../etc/passwd")).toBe(false);
expect(inboxExists("")).toBe(false);
});

test("ackMessage removes the file from cur/", () => {
const m = sendMessage("kern", "ack-test", "anvil");
expect(m.to).toBe("kern");
const inbox = getInbox("kern");

// Move from new -> cur via check
checkMessages("kern");
expect(readdirSync(inbox.fresh).filter((f) => f.endsWith(".json")).length).toBe(0);

const curFilesBefore = readdirSync(inbox.cur).filter((f) => f.endsWith(".json"));
expect(curFilesBefore.length).toBe(1);

// Ack removes it
const acked = ackMessage("kern", m.id);
expect(acked).not.toBeNull();

const curFilesAfter = readdirSync(inbox.cur).filter((f) => f.endsWith(".json"));
expect(curFilesAfter.length).toBe(0);
});

test("countInboxMessages drops after ack", () => {
// Send 5 messages
for (let i = 0; i < 5; i++) {
sendMessage("kern", `msg-${i}`, "anvil");
}
expect(countInboxMessages("kern")).toBe(5);

// Check all (moves new -> cur)
const msgs = checkMessages("kern");
expect(msgs.length).toBe(5);
expect(countInboxMessages("kern")).toBe(5);

// Ack 3
ackMessage("kern", msgs[0]!.id);
ackMessage("kern", msgs[1]!.id);
ackMessage("kern", msgs[2]!.id);

expect(countInboxMessages("kern")).toBe(2);
});
});

describe("mail command", () => {
Expand Down
Loading