118 lines
3.9 KiB
TypeScript
118 lines
3.9 KiB
TypeScript
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
|
|
import { PLUGIN_HOOK_ENTITY_CREATED } from "@hivegrid/plugin-runtime";
|
|
|
|
// The entity-row hook test mocks this module out entirely; here we exercise the
|
|
// REAL `emitPluginHookFireAndForget` and only stub the @hivegrid/db seams so the
|
|
// transaction/outbox plumbing and both error branches run without a database.
|
|
const { getDbClientMock, publishOutboxEventMock } = vi.hoisted(() => ({
|
|
getDbClientMock: vi.fn(),
|
|
publishOutboxEventMock: vi.fn(),
|
|
}));
|
|
|
|
vi.mock("@hivegrid/db/client.server", () => ({
|
|
getDbClient: getDbClientMock,
|
|
}));
|
|
|
|
vi.mock("@hivegrid/db/outbox", () => ({
|
|
publishOutboxEvent: publishOutboxEventMock,
|
|
}));
|
|
|
|
const { emitPluginHookFireAndForget } = await import("./plugin-hook-publisher.js");
|
|
|
|
const TX_SENTINEL = { __tx: true } as const;
|
|
|
|
/** Default happy-path db: $transaction runs the callback against a sentinel tx. */
|
|
function installRunningTransaction() {
|
|
getDbClientMock.mockReturnValue({
|
|
$transaction: async (runner: (tx: unknown) => Promise<unknown>) => runner(TX_SENTINEL),
|
|
});
|
|
}
|
|
|
|
const VALID_PAYLOAD = {
|
|
entityType: "contact",
|
|
entityId: "row_1",
|
|
tenantId: "tenant_1",
|
|
userId: null as string | null,
|
|
occurredAt: "2026-01-01T00:00:00.000Z",
|
|
};
|
|
|
|
beforeEach(() => {
|
|
publishOutboxEventMock.mockReset();
|
|
publishOutboxEventMock.mockResolvedValue(undefined);
|
|
getDbClientMock.mockReset();
|
|
});
|
|
|
|
afterEach(() => {
|
|
vi.clearAllMocks();
|
|
});
|
|
|
|
describe("emitPluginHookFireAndForget", () => {
|
|
test("publishes a validated payload to the outbox inside a transaction", async () => {
|
|
installRunningTransaction();
|
|
|
|
await emitPluginHookFireAndForget(PLUGIN_HOOK_ENTITY_CREATED, VALID_PAYLOAD, {
|
|
tenantId: "tenant_1",
|
|
dispatchKey: "entity:contact:row_1:created",
|
|
});
|
|
|
|
expect(publishOutboxEventMock).toHaveBeenCalledTimes(1);
|
|
expect(publishOutboxEventMock).toHaveBeenCalledWith(
|
|
TX_SENTINEL,
|
|
expect.objectContaining({
|
|
tenantId: "tenant_1",
|
|
// emitPluginHook maps the definition to its outbox event type.
|
|
type: "plugin.hook.entity.created",
|
|
dispatchKey: "entity:contact:row_1:created",
|
|
payload: expect.objectContaining({ entityType: "contact", entityId: "row_1" }),
|
|
}),
|
|
);
|
|
});
|
|
|
|
test("defaults tenantId and dispatchKey to null when no options are supplied", async () => {
|
|
installRunningTransaction();
|
|
|
|
await emitPluginHookFireAndForget(PLUGIN_HOOK_ENTITY_CREATED, VALID_PAYLOAD);
|
|
|
|
expect(publishOutboxEventMock).toHaveBeenCalledWith(
|
|
TX_SENTINEL,
|
|
expect.objectContaining({ tenantId: null, dispatchKey: null }),
|
|
);
|
|
});
|
|
|
|
test("swallows validation errors and never publishes an invalid payload", async () => {
|
|
installRunningTransaction();
|
|
|
|
// Missing required fields => PluginHookValidationError inside emitPluginHook.
|
|
await expect(
|
|
emitPluginHookFireAndForget(PLUGIN_HOOK_ENTITY_CREATED, { entityType: "contact" } as never, {
|
|
tenantId: "tenant_1",
|
|
}),
|
|
).resolves.toBeUndefined();
|
|
|
|
expect(publishOutboxEventMock).not.toHaveBeenCalled();
|
|
});
|
|
|
|
test("swallows outbox publish failures (fire-and-forget never rejects)", async () => {
|
|
installRunningTransaction();
|
|
publishOutboxEventMock.mockRejectedValue(new Error("outbox unreachable"));
|
|
|
|
await expect(
|
|
emitPluginHookFireAndForget(PLUGIN_HOOK_ENTITY_CREATED, VALID_PAYLOAD, { tenantId: "tenant_1" }),
|
|
).resolves.toBeUndefined();
|
|
|
|
expect(publishOutboxEventMock).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
test("swallows transaction/database failures without publishing", async () => {
|
|
getDbClientMock.mockReturnValue({
|
|
$transaction: vi.fn().mockRejectedValue(new Error("Can't reach database server")),
|
|
});
|
|
|
|
await expect(
|
|
emitPluginHookFireAndForget(PLUGIN_HOOK_ENTITY_CREATED, VALID_PAYLOAD, { tenantId: "tenant_1" }),
|
|
).resolves.toBeUndefined();
|
|
|
|
expect(publishOutboxEventMock).not.toHaveBeenCalled();
|
|
});
|
|
});
|