文件
hyb-oa/server/tests/unit/services/events.test.js
T

68 行
2.3 KiB
JavaScript

const {
buildInvoiceUpdatedEvent,
canReceiveEvent,
createAppEventBus,
toClientEvent,
} = require("../../../src/services/events");
describe("app events", () => {
it("publishes minimal invoice updated events", () => {
const event = buildInvoiceUpdatedEvent({
invoiceId: "11111111-1111-4111-8111-111111111111",
status: "available",
audienceEmployeeIds: ["employee-1", "employee-1", ""],
updatedAt: "2026-05-09T10:00:00.000Z",
});
expect(event).toEqual({
type: "invoice.updated",
invoiceId: "11111111-1111-4111-8111-111111111111",
status: "available",
updatedAt: "2026-05-09T10:00:00.000Z",
audienceEmployeeIds: ["employee-1"],
});
expect(toClientEvent({ id: "evt-1", createdAt: "2026-05-09T10:00:01.000Z", ...event })).toEqual({
id: "evt-1",
type: "invoice.updated",
invoiceId: "11111111-1111-4111-8111-111111111111",
status: "available",
updatedAt: "2026-05-09T10:00:00.000Z",
createdAt: "2026-05-09T10:00:01.000Z",
});
});
it("filters invoice events by employee audience unless invoice manager", () => {
const event = buildInvoiceUpdatedEvent({
invoiceId: "11111111-1111-4111-8111-111111111111",
status: "available",
audienceEmployeeIds: ["employee-1"],
});
expect(canReceiveEvent({ id: "employee-1" }, event)).toBe(true);
expect(canReceiveEvent({ id: "employee-2" }, event)).toBe(false);
expect(canReceiveEvent({ id: "employee-2" }, event, { canManageInvoice: true })).toBe(true);
});
it("supports subscribe and unsubscribe on the in-process event bus", () => {
const bus = createAppEventBus();
const handler = vi.fn();
const unsubscribe = bus.subscribe(handler);
bus.publish({ type: "invoice.updated", invoiceId: "invoice-1", audienceEmployeeIds: ["employee-1"] });
unsubscribe();
bus.publish({ type: "invoice.updated", invoiceId: "invoice-2", audienceEmployeeIds: ["employee-1"] });
expect(handler).toHaveBeenCalledTimes(1);
expect(handler.mock.calls[0][0]).toEqual(
expect.objectContaining({
id: expect.any(String),
createdAt: expect.any(String),
type: "invoice.updated",
invoiceId: "invoice-1",
audienceEmployeeIds: ["employee-1"],
}),
);
});
});