99 lines
4.4 KiB
TypeScript
99 lines
4.4 KiB
TypeScript
import type { TenantRlsClient } from "~/utils/db/.server/tenantRls.server";
|
|
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { SHK_V2_TENANT_BOOTSTRAP_CONFIG, SHK_V2_TENANT_BOOTSTRAP_CONFIG_DIGEST } from "./shk-v2.config";
|
|
import { SHK_FIRST_STEPS, SHK_FIRST_STEPS_CHECKLIST_CODE, SHK_FIRST_STEPS_CHECKLIST_ID } from "./tenantBootstrap.constants";
|
|
import { bootstrapShkV2TenantWithTx } from "./tenantBootstrapService.server";
|
|
import { prepareShkFormTemplates } from "~/modules/forms/services/shkFormTemplates.server";
|
|
|
|
const MAX_BOOTSTRAP_TRANSACTION_QUERIES = 26;
|
|
|
|
describe("tenant bootstrap transaction performance contract", () => {
|
|
it("executes the real helper graph within the sequential query budget", async () => {
|
|
const calls: string[] = [];
|
|
const query = <T>(name: string, result: T) =>
|
|
vi.fn(async () => {
|
|
calls.push(name);
|
|
return result;
|
|
});
|
|
const permissions = [...new Set(SHK_V2_TENANT_BOOTSTRAP_CONFIG.tenantRoles.flatMap((role) => role.permissionKeys))].map((name, index) => ({
|
|
id: `permission_${index}`,
|
|
name,
|
|
}));
|
|
const roles = SHK_V2_TENANT_BOOTSTRAP_CONFIG.tenantRoles.map((role, index) => ({
|
|
id: `role_${index}`,
|
|
name: role.name,
|
|
}));
|
|
const pipelines = SHK_V2_TENANT_BOOTSTRAP_CONFIG.salesPipelines.map((pipeline, index) => ({
|
|
id: `pipeline_${index}`,
|
|
name: pipeline.name,
|
|
}));
|
|
const tx = {
|
|
idempotencyKey: {
|
|
upsert: vi.fn(async ({ create }: { create: Record<string, unknown> }) => {
|
|
calls.push("marker.acquire");
|
|
return create;
|
|
}),
|
|
update: query("marker.complete", {}),
|
|
},
|
|
role: {
|
|
findMany: query("role.admin", [{ id: "role_admin" }]),
|
|
createManyAndReturn: query("roles.create", roles),
|
|
},
|
|
tenantUser: { upsert: query("owner.create", {}) },
|
|
user: { update: query("user.defaultTenant", {}) },
|
|
userRole: {
|
|
deleteMany: query("userRoles.prune", { count: 0 }),
|
|
createMany: query("userRoles.admin", { count: 1 }),
|
|
},
|
|
tenant: {
|
|
update: vi.fn(async ({ data }: { data: { active?: boolean } }) => {
|
|
calls.push("tenant.activate");
|
|
return { id: "tenant_1", active: Boolean(data.active), deactivatedReason: data.active ? null : "pending_owner" };
|
|
}),
|
|
},
|
|
permission: { findMany: query("permissions.read", permissions) },
|
|
rolePermission: { createMany: query("rolePermissions.create", { count: permissions.length }) },
|
|
pipeline: { createManyAndReturn: query("pipelines.create", pipelines) },
|
|
pipelineStage: { createMany: query("pipelineStages.create", { count: 15 }) },
|
|
lostReason: { createMany: query("lostReasons.create", { count: 5 }) },
|
|
acceptanceFormTemplate: { upsert: query("acceptanceTemplate.upsert", { id: "acceptance_1" }) },
|
|
projectTemplate: { upsert: query("projectTemplate.upsert", { id: "project_template_1" }) },
|
|
form: { upsert: query("form.upsert", { id: "form_1" }) },
|
|
onboarding: {
|
|
findUnique: query("onboarding.read", {
|
|
id: SHK_FIRST_STEPS_CHECKLIST_ID,
|
|
title: SHK_FIRST_STEPS_CHECKLIST_CODE,
|
|
type: "page",
|
|
realtime: false,
|
|
active: true,
|
|
canBeDismissed: true,
|
|
height: null,
|
|
steps: SHK_FIRST_STEPS.map((step) => ({ ...step })),
|
|
}),
|
|
},
|
|
onboardingSession: { upsert: query("onboardingSession.upsert", { id: "session_1" }) },
|
|
onboardingSessionStep: { createMany: query("onboardingSteps.create", { count: 4 }) },
|
|
automationTemplate: { createMany: query("automationPlaceholders.create", { count: 3 }) },
|
|
portal: { upsert: query("portal.scaffold", { id: "portal_1" }) },
|
|
};
|
|
|
|
await expect(
|
|
bootstrapShkV2TenantWithTx(tx as unknown as TenantRlsClient, {
|
|
tenantId: "tenant_1",
|
|
tenantName: "Heizkraft GmbH",
|
|
tenantSlug: "heizkraft",
|
|
userId: "user_1",
|
|
attemptId: "attempt_1",
|
|
t: (key) => key,
|
|
configDigest: SHK_V2_TENANT_BOOTSTRAP_CONFIG_DIGEST,
|
|
preparedFormTemplates: prepareShkFormTemplates((key) => key),
|
|
})
|
|
).resolves.toMatchObject({ replayed: false, tenant: { id: "tenant_1", active: true } });
|
|
|
|
expect(calls.length).toBeLessThanOrEqual(MAX_BOOTSTRAP_TRANSACTION_QUERIES);
|
|
expect(calls.at(-2)).toBe("marker.complete");
|
|
expect(calls.at(-1)).toBe("tenant.activate");
|
|
});
|
|
});
|