Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/db/src/event-webhook-attempts.server.test.ts
T

63 lines
2.2 KiB
TypeScript

import { describe, expect, it, vi } from "vitest";
import {
findLegacyEventWebhookAttemptById,
updateLegacyEventWebhookAttempt,
type LegacyEventWebhookAttemptClient,
} from "./event-webhook-attempts.server.js";
describe("legacy event webhook attempt helpers", () => {
it("loads attempts with their tenant scope", async () => {
const findUnique = vi.fn().mockResolvedValue({
id: "attempt_1",
endpoint: "https://example.test/webhook",
event: { tenantId: "tenant_1" },
});
const client = createClient({ findUnique });
await expect(findLegacyEventWebhookAttemptById("attempt_1", client)).resolves.toEqual({
id: "attempt_1",
endpoint: "https://example.test/webhook",
tenantId: "tenant_1",
});
expect(findUnique).toHaveBeenCalledWith({
where: { id: "attempt_1" },
select: { id: true, endpoint: true, event: { select: { tenantId: true } } },
});
});
it("updates attempts through id and tenant scope", async () => {
const updateMany = vi.fn().mockResolvedValue({ count: 1 });
const client = createClient({ updateMany });
const finishedAt = new Date("2026-01-01T00:00:00.000Z");
await updateLegacyEventWebhookAttempt({ id: "attempt_1", tenantId: "tenant_1" }, { finishedAt, success: true }, client);
expect(updateMany).toHaveBeenCalledWith({
where: { id: "attempt_1", event: { tenantId: "tenant_1" } },
data: { finishedAt, success: true },
});
});
it("throws when scoped attempt update does not match one row", async () => {
const client = createClient({ updateMany: vi.fn().mockResolvedValue({ count: 0 }) });
await expect(updateLegacyEventWebhookAttempt({ id: "missing", tenantId: "tenant_1" }, { success: false }, client)).rejects.toThrow("Event webhook attempt missing not found");
});
});
function createClient({
findUnique = vi.fn().mockResolvedValue(null),
updateMany = vi.fn().mockResolvedValue({ count: 0 }),
}: {
findUnique?: ReturnType<typeof vi.fn>;
updateMany?: ReturnType<typeof vi.fn>;
} = {}): LegacyEventWebhookAttemptClient {
return {
eventWebhookAttempt: {
findUnique,
updateMany,
},
} as unknown as LegacyEventWebhookAttemptClient;
}