Refresh HiveGrid Cloud reference snapshot

This commit is contained in:
david committed 2026-08-03 15:31:14 +02:00
1 parent 5e99e14772
commit 7f88026f79
5955 files changed
+1177325 -188362

No files matched your search

@@ -3,9 +3,11 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
type SeedService = typeof import("./SeedService").default;
const dbMock = {
$queryRaw: vi.fn(async () => [{ marked: true }]),
user: {
findUnique: vi.fn(async ({ where }: { where: { email: string } }) => ({ id: `user-${where.email}` })),
findUnique: vi.fn(async ({ where }: { where: { email: string } }) => ({ id: `user-${where.email}`, active: true })),
create: vi.fn(),
update: vi.fn(async ({ where, data }: { where: { id: string }; data: { active: boolean } }) => ({ id: where.id, active: data.active })),
},
adminUser: {
findUnique: vi.fn(async () => ({ userId: "admin-id" })),
@@ -95,12 +97,37 @@ const dbMock = {
app: {
upsert: vi.fn(),
},
automationTemplate: {
findFirst: vi.fn(async () => null),
create: vi.fn(),
update: vi.fn(),
},
bookingPage: {
upsert: vi.fn(async ({ create }: { create: { slug: string; tenantId: string } }) => ({ id: `booking-page-${create.tenantId}-${create.slug}` })),
},
bookingPageProvider: {
upsert: vi.fn(),
},
availabilityRule: {
upsert: vi.fn(),
},
availabilityException: {
upsert: vi.fn(),
},
appointment: {
upsert: vi.fn(),
},
calendarEvent: {
upsert: vi.fn(),
deleteMany: vi.fn(),
},
};
const hashMock = vi.fn(async () => "hashed-password");
const genSaltMock = vi.fn(async () => "generated-salt");
const seedRolesAndPermissionsMock = vi.fn();
const seedStandardEntitlementsMock = vi.fn();
const seedStagingDomainDemoMock = vi.fn();
const originalEnv = { ...process.env };
const loggerInfo = vi.fn();
@@ -123,6 +150,7 @@ vi.mock("@hivegrid/db/seed.server", async () => {
vi.mock("~/utils/services/emailService", () => ({ getAvailableTenantInboundAddress: vi.fn(async () => "seed@example.test") }));
vi.mock("~/utils/services/rolesAndPermissionsService", () => ({ seedRolesAndPermissions: seedRolesAndPermissionsMock }));
vi.mock("~/utils/db.server", () => ({ db: dbMock }));
vi.mock("./stagingDomainDemoSeed.server", () => ({ seedStagingDomainDemo: seedStagingDomainDemoMock }));
async function loadSeedService(): Promise<SeedService> {
vi.resetModules();
@@ -133,11 +161,21 @@ async function loadSeedService(): Promise<SeedService> {
describe("SeedService", () => {
beforeEach(() => {
vi.clearAllMocks();
dbMock.user.findUnique.mockImplementation(async ({ where }: { where: { email: string } }) => ({ id: `user-${where.email}`, active: true }));
dbMock.user.update.mockImplementation(async ({ where, data }: { where: { id: string }; data: { active: boolean } }) => ({ id: where.id, active: data.active }));
process.env = { ...originalEnv };
delete process.env.SEED_ADMIN_EMAIL;
delete process.env.SEED_ADMIN_PASSWORD;
delete process.env.SEED_DEMO_DATA;
delete process.env.ALLOW_STAGING_DEMO_SEED;
delete process.env.SEED_DEMO_PRO_MANAGED;
delete process.env.LOCAL_DEV_CREDENTIALS;
// These unit cases exercise explicit seed environments. The dedicated
// local-credential contract suite covers the fail-closed CI branch.
delete process.env.CI;
delete process.env.SENTRY_ENVIRONMENT;
delete process.env.RLS_RUNTIME_MODE;
delete process.env.ADMIN_APP_HOST;
process.env.NODE_ENV = "test";
});
@@ -189,6 +227,87 @@ describe("SeedService", () => {
expect(JSON.stringify(consoleLog.mock.calls)).not.toContain("safe-production-password");
});
it("keeps the existing seed override path when the canonical local launcher flag is absent", async () => {
process.env.SEED_ADMIN_EMAIL = "owner@example.com";
process.env.SEED_ADMIN_PASSWORD = "safe-development-password";
const SeedService = await loadSeedService();
await SeedService.seed();
expect(dbMock.user.findUnique).toHaveBeenCalledWith({ where: { email: "owner@example.com" } });
expect(dbMock.user.findUnique).not.toHaveBeenCalledWith({ where: { email: "admin@email.com" } });
expect(hashMock).toHaveBeenCalledWith("safe-development-password", "generated-salt");
expect(dbMock.user.update).not.toHaveBeenCalled();
expect(dbMock.$queryRaw).not.toHaveBeenCalled();
});
it("restores canonical seed credentials only through the verified local launcher contract", async () => {
process.env.NODE_ENV = "development";
process.env.LOCAL_DEV_CREDENTIALS = "1";
process.env.DATABASE_URL = "postgresql://local:local@127.0.0.1:5432/hivegrid";
process.env.ADMIN_APP_HOST = "admin.localhost:3000";
process.env.SEED_ADMIN_EMAIL = "owner@example.com";
process.env.SEED_ADMIN_PASSWORD = "safe-production-password";
dbMock.user.findUnique.mockResolvedValueOnce({ id: "local-admin", active: false } as never);
dbMock.user.update.mockResolvedValueOnce({ id: "local-admin", active: true } as never);
const SeedService = await loadSeedService();
await SeedService.seed();
expect(dbMock.user.findUnique).toHaveBeenCalledWith({ where: { email: "admin@email.com" } });
expect(dbMock.user.findUnique).not.toHaveBeenCalledWith({ where: { email: "owner@example.com" } });
expect(hashMock).toHaveBeenCalledWith("password", "generated-salt");
expect(dbMock.$queryRaw).toHaveBeenCalledOnce();
expect(dbMock.user.update).toHaveBeenCalledWith({
where: { id: "local-admin" },
data: { passwordHash: "hashed-password", active: true },
});
});
it("fails before seed work when the local launcher flag targets a remote database", async () => {
process.env.NODE_ENV = "development";
process.env.LOCAL_DEV_CREDENTIALS = "1";
process.env.DATABASE_URL = "postgresql://app:secret@staging-db:5432/hivegrid";
process.env.ADMIN_APP_HOST = "admin.localhost:3000";
const SeedService = await loadSeedService();
await expect(SeedService.seed()).rejects.toThrow("loopback PostgreSQL");
expect(dbMock.user.findUnique).not.toHaveBeenCalled();
expect(dbMock.user.create).not.toHaveBeenCalled();
expect(hashMock).not.toHaveBeenCalled();
expect(seedRolesAndPermissionsMock).not.toHaveBeenCalled();
expect(dbMock.$queryRaw).not.toHaveBeenCalled();
});
it("fails before seed work when a loopback database lacks the disposable-local marker", async () => {
process.env.NODE_ENV = "development";
process.env.LOCAL_DEV_CREDENTIALS = "1";
process.env.DATABASE_URL = "postgresql://local:local@127.0.0.1:5432/hivegrid";
process.env.ADMIN_APP_HOST = "admin.localhost:3000";
dbMock.$queryRaw.mockResolvedValueOnce([{ marked: false }]);
const SeedService = await loadSeedService();
await expect(SeedService.seed()).rejects.toThrow("database marked as disposable local development data");
expect(dbMock.user.findUnique).not.toHaveBeenCalled();
expect(dbMock.user.create).not.toHaveBeenCalled();
expect(hashMock).not.toHaveBeenCalled();
expect(seedRolesAndPermissionsMock).not.toHaveBeenCalled();
});
it("creates seed-managed users as active", async () => {
dbMock.user.findUnique.mockResolvedValueOnce(null as never);
dbMock.user.create.mockResolvedValueOnce({ id: "new-admin", active: true } as never);
const SeedService = await loadSeedService();
await SeedService.seed();
expect(dbMock.user.create).toHaveBeenCalledWith({
data: expect.objectContaining({ active: true }),
});
});
it("rejects blank admin credential overrides in production before DB writes", async () => {
process.env.NODE_ENV = "production";
process.env.SEED_ADMIN_EMAIL = "";
@@ -220,7 +339,7 @@ describe("SeedService", () => {
it("skips local demo users and tenants unless explicitly enabled", async () => {
const SeedService = await loadSeedService();
const consoleLog = vi.spyOn(console, "log").mockImplementation(() => undefined);
vi.spyOn(console, "log").mockImplementation(() => undefined);
await SeedService.seed();
@@ -237,7 +356,7 @@ describe("SeedService", () => {
it("seeds local demo users and tenants only when explicitly enabled outside production", async () => {
process.env.SEED_DEMO_DATA = "true";
const SeedService = await loadSeedService();
const consoleLog = vi.spyOn(console, "log").mockImplementation(() => undefined);
vi.spyOn(console, "log").mockImplementation(() => undefined);
await SeedService.seed();
@@ -268,12 +387,87 @@ describe("SeedService", () => {
expect(dbMock.tenantProductConfiguration.upsert).toHaveBeenCalledTimes(3);
expect(dbMock.knowledgeBase.upsert).toHaveBeenCalledTimes(3);
expect(dbMock.supportTicket.upsert).toHaveBeenCalledTimes(6);
expect(seedStagingDomainDemoMock).toHaveBeenCalledOnce();
expect(seedStagingDomainDemoMock).toHaveBeenCalledWith({
tenantId: "tenant-demo-business-crm",
ownerUserId: "user-john.doe@company.com",
});
// AK-P16a/b: booking + calendar demo data per demo tenant (2 pages, 2
// providers each, Mo-Fr rules x 2 providers, 2 exceptions, 4 appointments,
// 3 internal mirrors + 1 external event; the cancelled appointment clears
// its mirror instead).
expect(dbMock.bookingPage.upsert).toHaveBeenCalledTimes(6);
expect(dbMock.bookingPageProvider.upsert).toHaveBeenCalledTimes(12);
expect(dbMock.availabilityRule.upsert).toHaveBeenCalledTimes(30);
expect(dbMock.availabilityException.upsert).toHaveBeenCalledTimes(6);
expect(dbMock.appointment.upsert).toHaveBeenCalledTimes(12);
expect(dbMock.calendarEvent.upsert).toHaveBeenCalledTimes(12);
expect(dbMock.calendarEvent.deleteMany).toHaveBeenCalledTimes(3);
expect(dbMock.bookingPage.upsert).toHaveBeenCalledWith(
expect.objectContaining({
where: { tenantId_slug: { tenantId: "tenant-demo-starter-crm", slug: "beratungstermin" } },
create: expect.objectContaining({ isDemoData: true, isPublished: true }),
}),
);
expect(dbMock.appointment.upsert).toHaveBeenCalledWith(
expect.objectContaining({
where: { id: "seed-demo-starter-crm-appt-cancelled-consult" },
create: expect.objectContaining({ status: "cancelled", isDemoData: true, internalCalendarEventId: null }),
}),
);
expect(loggerInfo).toHaveBeenCalledWith(
expect.objectContaining({ op: "seed.createDemoUsers", count: 2 }),
"Creating CRM SaaS demo users with tenants",
);
});
it("keeps the managed demo-pro tenant outside the legacy boot demo seed", async () => {
process.env.SEED_DEMO_DATA = "true";
process.env.SEED_DEMO_PRO_MANAGED = "true";
const SeedService = await loadSeedService();
vi.spyOn(console, "log").mockImplementation(() => undefined);
await SeedService.seed();
expect(dbMock.tenant.findUnique).toHaveBeenCalledWith({ where: { slug: "demo-starter-crm" } });
expect(dbMock.tenant.findUnique).toHaveBeenCalledWith({ where: { slug: "demo-business-crm" } });
expect(dbMock.tenant.findUnique).not.toHaveBeenCalledWith(expect.objectContaining({ where: { slug: "demo-pro-crm" } }));
expect(dbMock.tenantUser.create).toHaveBeenCalledTimes(6);
expect(dbMock.subscriptionProduct.findFirst).toHaveBeenCalledWith({
where: { billingProviderId: "hivegrid_entitlement_plan_starter" },
select: { id: true },
});
expect(dbMock.subscriptionProduct.findFirst).not.toHaveBeenCalledWith({
where: { billingProviderId: "hivegrid_entitlement_plan_pro" },
select: { id: true },
});
expect(dbMock.subscriptionProduct.findFirst).toHaveBeenCalledWith({
where: { billingProviderId: "hivegrid_entitlement_plan_business" },
select: { id: true },
});
expect(dbMock.tenantSubscriptionProduct.create).toHaveBeenCalledTimes(2);
expect(dbMock.tenantProductConfiguration.upsert).toHaveBeenCalledTimes(2);
expect(dbMock.knowledgeBase.upsert).toHaveBeenCalledTimes(2);
expect(dbMock.supportTicket.upsert).toHaveBeenCalledTimes(4);
expect(dbMock.bookingPage.upsert).toHaveBeenCalledTimes(4);
expect(dbMock.bookingPageProvider.upsert).toHaveBeenCalledTimes(8);
expect(dbMock.availabilityRule.upsert).toHaveBeenCalledTimes(20);
expect(dbMock.availabilityException.upsert).toHaveBeenCalledTimes(4);
expect(dbMock.appointment.upsert).toHaveBeenCalledTimes(8);
expect(dbMock.calendarEvent.upsert).toHaveBeenCalledTimes(8);
expect(dbMock.calendarEvent.deleteMany).toHaveBeenCalledTimes(2);
expect(seedStagingDomainDemoMock).toHaveBeenCalledOnce();
expect(seedStagingDomainDemoMock).toHaveBeenCalledWith({
tenantId: "tenant-demo-business-crm",
ownerUserId: "user-john.doe@company.com",
});
expect(seedRolesAndPermissionsMock).toHaveBeenCalledWith({
adminEmail: "admin@email.com",
diagnostics: expect.any(Function),
excludedTenantSlugs: ["demo-pro-crm"],
});
});
it("does not reassign a demo tenant plan after self-service checkout", async () => {
process.env.SEED_DEMO_DATA = "true";
dbMock.tenant.findUnique.mockImplementation(async ({ where, include }: { where: { slug: string }; include?: unknown }) => ({
@@ -329,14 +523,28 @@ describe("SeedService", () => {
process.env.ALLOW_STAGING_DEMO_SEED = "true";
process.env.SEED_ADMIN_EMAIL = "owner@example.com";
process.env.SEED_ADMIN_PASSWORD = "safe-production-password";
const consoleLog = vi.spyOn(console, "log").mockImplementation(() => undefined);
vi.spyOn(console, "log").mockImplementation(() => undefined);
const SeedService = await loadSeedService();
dbMock.user.findUnique
.mockResolvedValueOnce({ id: "admin-user", active: true } as never)
.mockResolvedValueOnce({ id: "demo-user-1", active: false } as never)
.mockResolvedValueOnce({ id: "demo-user-2", active: false } as never);
await SeedService.seed();
expect(dbMock.user.findUnique).toHaveBeenCalledWith({ where: { email: "john.doe@company.com" } });
expect(dbMock.tenant.findUnique).toHaveBeenCalledWith({ where: { slug: "demo-starter-crm" } });
expect(dbMock.tenantProductConfiguration.upsert).toHaveBeenCalled();
expect(dbMock.user.update).toHaveBeenCalledTimes(2);
expect(dbMock.user.update).toHaveBeenCalledWith({
where: { id: "demo-user-1" },
data: { active: true },
});
expect(dbMock.user.update).toHaveBeenCalledWith({
where: { id: "demo-user-2" },
data: { active: true },
});
expect(loggerInfo).toHaveBeenCalledWith(
expect.objectContaining({ op: "db-seed", phase: "start", nodeEnv: "staging-demo" }),
"DB seed event",
@@ -371,6 +579,8 @@ describe("SeedService", () => {
expect(seedStandardEntitlementsMock).toHaveBeenCalledOnce();
expect(seedStandardEntitlementsMock).toHaveBeenCalledWith({
client: dbMock,
includeCustomerPortalCommercialProducts: true,
includeEmailMarketingCommercialProducts: true,
diagnostics: expect.any(Function),
});
expect(calls).toEqual(["roles", "entitlements"]);
@@ -415,7 +625,7 @@ describe("SeedService", () => {
it("logs redacted roles and permissions seed diagnostics from the helper", async () => {
const SeedService = await loadSeedService();
const consoleLog = vi.spyOn(console, "log").mockImplementation(() => undefined);
vi.spyOn(console, "log").mockImplementation(() => undefined);
seedRolesAndPermissionsMock.mockImplementation(async ({ diagnostics }) => {
await diagnostics?.({
phase: "success",
@@ -453,7 +663,7 @@ describe("SeedService", () => {
it("logs redacted entitlements seed diagnostics from the helper", async () => {
const SeedService = await loadSeedService();
const consoleLog = vi.spyOn(console, "log").mockImplementation(() => undefined);
vi.spyOn(console, "log").mockImplementation(() => undefined);
seedStandardEntitlementsMock.mockImplementation(async ({ diagnostics }) => {
await diagnostics?.({
phase: "failure",