import { describe, expect, test } from "bun:test";
import { EventBus } from "./eventbus";

describe("EventBus", () => {
	test("subscribers receive published events", () => {
		const bus = new EventBus();
		const received: unknown[] = [];

		bus.subscribe((event) => received.push(event));
		bus.publish({ type: "new_mail", threadId: "t001" });

		expect(received).toHaveLength(1);
		expect(received[0]).toEqual({ type: "new_mail", threadId: "t001" });
	});

	test("unsubscribe stops receiving events", () => {
		const bus = new EventBus();
		const received: unknown[] = [];

		const unsubscribe = bus.subscribe((event) => received.push(event));
		bus.publish({ type: "a" });
		unsubscribe();
		bus.publish({ type: "b" });

		expect(received).toHaveLength(1);
	});
});
