import { describe, expect, test } from "bun:test";
import { parseSearchResults, parseThreadMessages } from "./notmuch";

const searchFixture = await Bun.file("test/fixtures/notmuch-search-output.json").json();
const showFixture = await Bun.file("test/fixtures/notmuch-show-output.json").json();

describe("parseSearchResults", () => {
	test("parses notmuch search JSON into thread summaries", () => {
		const threads = parseSearchResults(searchFixture);
		expect(threads).toHaveLength(2);
		expect(threads[0].threadId).toBe("0000000000000001");
		expect(threads[0].subject).toBe("Q1 Planning");
		expect(threads[0].authors).toBe("Alice, Bob");
		expect(threads[0].totalMessages).toBe(3);
		expect(threads[0].tags).toContain("unread");
		expect(threads[0].timestamp).toBe(1709884532);
	});
});

describe("parseThreadMessages", () => {
	test("flattens notmuch show JSON into ordered messages", () => {
		const messages = parseThreadMessages(showFixture);
		expect(messages).toHaveLength(2);
		expect(messages[0].messageId).toBe("msg001@example.com");
		expect(messages[0].from).toBe("Alice <alice@example.com>");
		expect(messages[0].subject).toBe("Q1 Planning");
		expect(messages[0].body).toBe("Hey, let's plan Q1.\n");
		expect(messages[1].messageId).toBe("msg003@example.com");
		expect(messages[1].inReplyTo).toBe("msg001@example.com");
	});
});
