Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/utils/integrations/projects/auftragService.defaults-with-tx.test.ts
T

90 lines
3.5 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
const mocks = vi.hoisted(() => ({
withTenantRLS: vi.fn(),
acceptanceUpsert: vi.fn(),
projectUpsert: vi.fn(),
}));
vi.mock("~/utils/db/.server/tenantRls.server", () => ({ withTenantRLS: mocks.withTenantRLS }));
vi.mock("@hivegrid/activity", () => ({ logActivity: vi.fn() }));
import {
ensureDefaultAcceptanceFormTemplate,
ensureDefaultAcceptanceFormTemplateWithTx,
ensureDefaultProjectTemplate,
ensureDefaultProjectTemplateWithTx,
} from "./auftragService.server";
const tx = {
acceptanceFormTemplate: { upsert: mocks.acceptanceUpsert },
projectTemplate: { upsert: mocks.projectUpsert },
};
beforeEach(() => {
vi.clearAllMocks();
mocks.acceptanceUpsert.mockResolvedValue({ id: "acceptance-default" });
mocks.projectUpsert.mockResolvedValue({ id: "project-default" });
mocks.withTenantRLS.mockImplementation(async (_tenantId, callback) => callback(tx));
});
describe("order defaults transaction boundary", () => {
it("uses a caller-provided transaction and preserves the acceptance defaults", async () => {
await expect(ensureDefaultAcceptanceFormTemplateWithTx(tx as never, "tenant-atomic-bootstrap")).resolves.toEqual({
id: "acceptance-default",
});
expect(mocks.withTenantRLS).not.toHaveBeenCalled();
expect(mocks.acceptanceUpsert).toHaveBeenCalledWith({
where: { tenantId_name: { tenantId: "tenant-atomic-bootstrap", name: "SHK Standard-Abnahme" } },
update: { archivedAt: null, isDefault: true, isDemoData: false },
create: {
tenantId: "tenant-atomic-bootstrap",
name: "SHK Standard-Abnahme",
definition: {
sections: [
{ key: "leistungen", labelKey: "projects.defaults.acceptance.sections.leistungen" },
{ key: "maengel", labelKey: "projects.defaults.acceptance.sections.maengel" },
{ key: "unterschrift", labelKey: "projects.defaults.acceptance.sections.unterschrift" },
],
},
isDefault: true,
isDemoData: false,
},
});
});
it("uses a caller-provided transaction and preserves the project defaults", async () => {
await expect(ensureDefaultProjectTemplateWithTx(tx as never, "tenant-atomic-bootstrap")).resolves.toEqual({
id: "project-default",
});
expect(mocks.withTenantRLS).not.toHaveBeenCalled();
expect(mocks.projectUpsert).toHaveBeenCalledWith({
where: { tenantId_name: { tenantId: "tenant-atomic-bootstrap", name: "SHK Standard-Projekt" } },
update: { archivedAt: null, isDemoData: false },
create: {
tenantId: "tenant-atomic-bootstrap",
name: "SHK Standard-Projekt",
defaultMembers: [],
defaultTasks: [
{ titleKey: "projects.defaults.tasks.prepareSite", priority: "normal" },
{ titleKey: "projects.defaults.tasks.checkMaterial", priority: "normal" },
{ titleKey: "projects.defaults.tasks.prepareAcceptance", priority: "high" },
],
customFields: {},
isDemoData: false,
},
});
});
it("keeps the existing wrappers and binds one RLS transaction per call", async () => {
await ensureDefaultAcceptanceFormTemplate({ tenantId: "tenant-wrapper" });
await ensureDefaultProjectTemplate({ tenantId: "tenant-wrapper" });
expect(mocks.withTenantRLS).toHaveBeenCalledTimes(2);
expect(mocks.withTenantRLS).toHaveBeenNthCalledWith(1, "tenant-wrapper", expect.any(Function));
expect(mocks.withTenantRLS).toHaveBeenNthCalledWith(2, "tenant-wrapper", expect.any(Function));
});
});