Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/packages/cli/src/commands/tenant/globals-runner.test.ts
T
2026-07-08 16:15:37 +02:00

214 lines
8.0 KiB
TypeScript

import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { mkdir, mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { tmpdir } from "node:os";
import { collectAndWriteGlobals, type GlobalsPrismaRunner } from "./globals-runner.js";
function makeRunner(rows: Record<string, Record<string, unknown>[]>): {
runner: GlobalsPrismaRunner;
calls: Array<{ model: string; ids: readonly string[] }>;
} {
const calls: Array<{ model: string; ids: readonly string[] }> = [];
const runner: GlobalsPrismaRunner = {
async findManyByIds(modelName, ids) {
calls.push({ model: modelName, ids });
const table = rows[modelName] ?? [];
return table.filter((r) => typeof r.id === "string" && ids.includes(r.id as string));
},
};
return { runner, calls };
}
describe("collectAndWriteGlobals", () => {
let workdir: string;
let postgresDir: string;
let globalsDir: string;
beforeEach(async () => {
workdir = await mkdtemp(join(tmpdir(), "globals-runner-test-"));
postgresDir = join(workdir, "postgres");
globalsDir = join(workdir, "globals");
await mkdir(postgresDir, { recursive: true });
await mkdir(globalsDir, { recursive: true });
});
afterEach(async () => {
await rm(workdir, { recursive: true, force: true });
});
it("emits an empty JSONL when no tenant rows reference the global", async () => {
// No JSONL files in postgresDir — every collectFkIdsFromJsonl returns
// an empty set, so findManyByIds is never invoked.
const { runner, calls } = makeRunner({});
const result = await collectAndWriteGlobals({
postgresDir,
globalsDir,
prisma: runner,
profile: "full-anonymized",
ctx: { salt: "deadbeef", profileName: "full-anonymized" },
});
expect(result.entries).toHaveLength(2); // User + McpOAuthClient today
const userEntry = result.entries.find((e) => e.model === "User");
expect(userEntry).toBeDefined();
expect(userEntry?.rowCount).toBe(0);
expect(userEntry?.path).toBe("globals/User.jsonl");
const clientEntry = result.entries.find((e) => e.model === "McpOAuthClient");
expect(clientEntry).toBeDefined();
expect(clientEntry?.rowCount).toBe(0);
expect(calls).toHaveLength(0);
const userFile = await readFile(join(globalsDir, "User.jsonl"), "utf8");
expect(userFile).toBe("");
});
it("deduplicates user ids referenced by multiple tenant rows", async () => {
// Two TenantUser rows reference the same userId; the user is fetched once.
await writeFile(
join(postgresDir, "TenantUser.jsonl"),
`${JSON.stringify({ id: "tu1", tenantId: "t1", userId: "u1" })}\n${JSON.stringify({ id: "tu2", tenantId: "t1", userId: "u1" })}\n`,
);
const { runner, calls } = makeRunner({
User: [
{
id: "u1",
email: "real@example.org",
firstName: "Real",
lastName: "Person",
phone: "+10000",
avatar: "https://cdn/old.png",
passwordHash: "$argon2id$realhash",
createdAt: "2026-05-25T00:00:00.000Z",
updatedAt: "2026-05-25T00:00:00.000Z",
active: true,
pendingDeletion: false,
pseudonymized: false,
},
],
});
const result = await collectAndWriteGlobals({
postgresDir,
globalsDir,
prisma: runner,
profile: "full-anonymized",
ctx: { salt: "saltsaltsalt", profileName: "full-anonymized" },
});
expect(calls).toHaveLength(1);
expect(calls[0]).toEqual({ model: "User", ids: ["u1"] });
const userEntry = result.entries.find((e) => e.model === "User");
expect(userEntry?.rowCount).toBe(1);
const written = (await readFile(join(globalsDir, "User.jsonl"), "utf8")).trim();
const row = JSON.parse(written);
expect(row.id).toBe("u1");
// Anonymized — no original email leaks through.
expect(written).not.toContain("real@example.org");
expect(row.email).toMatch(/@anonymized\.test$/);
expect(row.passwordHash).toBeNull();
});
it("collects ids across multiple source models and FK columns", async () => {
// Three FK columns on TenantUserInvitation point at User; two on
// ImpersonationAudit do too. Union the lot in one chunked call.
await writeFile(
join(postgresDir, "TenantUserInvitation.jsonl"),
`${JSON.stringify({ id: "inv1", tenantId: "t1", fromUserId: "u_from", acceptedByUserId: "u_acc", revokedByUserId: null })}\n`,
);
await writeFile(
join(postgresDir, "ImpersonationAudit.jsonl"),
`${JSON.stringify({ id: "imp1", actorUserId: "u_actor", targetUserId: "u_target" })}\n`,
);
const userRows = ["u_from", "u_acc", "u_actor", "u_target"].map((id) => ({
id,
email: `${id}@x.org`,
firstName: "Fn",
lastName: "Ln",
active: true,
pendingDeletion: false,
pseudonymized: false,
}));
const { runner, calls } = makeRunner({ User: userRows });
const result = await collectAndWriteGlobals({
postgresDir,
globalsDir,
prisma: runner,
profile: "full-anonymized",
ctx: { salt: "x", profileName: "full-anonymized" },
});
expect(calls).toHaveLength(1);
expect(new Set(calls[0].ids)).toEqual(new Set(["u_from", "u_acc", "u_actor", "u_target"]));
expect(result.entries.find((e) => e.model === "User")?.rowCount).toBe(4);
});
it("warns and continues when a referenced id is missing from the source DB", async () => {
await writeFile(
join(postgresDir, "TenantUser.jsonl"),
`${JSON.stringify({ id: "tu1", tenantId: "t1", userId: "u_present" })}\n${JSON.stringify({ id: "tu2", tenantId: "t1", userId: "u_missing" })}\n`,
);
const { runner } = makeRunner({
User: [{ id: "u_present", email: "p@x.org", firstName: "P", lastName: "X" }],
});
const warn = vi.fn();
const result = await collectAndWriteGlobals({
postgresDir,
globalsDir,
prisma: runner,
profile: "full-anonymized",
ctx: { salt: "x", profileName: "full-anonymized" },
logger: { info: () => undefined, warn },
});
expect(result.entries.find((e) => e.model === "User")?.rowCount).toBe(1);
expect(result.missingByModel["User"]).toBe(1);
expect(warn).toHaveBeenCalledWith(expect.stringContaining("dangling FK"));
});
it("chunks findManyByIds calls according to chunkSize", async () => {
// 7 ids with chunkSize=3 → ceil(7/3) = 3 calls.
const ids = ["u1", "u2", "u3", "u4", "u5", "u6", "u7"];
await writeFile(
join(postgresDir, "TenantUser.jsonl"),
ids.map((u, i) => JSON.stringify({ id: `tu${i}`, tenantId: "t1", userId: u })).join("\n") + "\n",
);
const userRows = ids.map((id) => ({ id, email: `${id}@x.org`, firstName: "F", lastName: "L" }));
const { runner, calls } = makeRunner({ User: userRows });
const result = await collectAndWriteGlobals({
postgresDir,
globalsDir,
prisma: runner,
profile: "full-anonymized",
ctx: { salt: "x", profileName: "full-anonymized" },
chunkSize: 3,
});
expect(calls.length).toBe(3);
expect(calls[0].ids.length).toBe(3);
expect(calls[1].ids.length).toBe(3);
expect(calls[2].ids.length).toBe(1);
expect(result.entries.find((e) => e.model === "User")?.rowCount).toBe(7);
});
it("produces a checksum that matches the file contents", async () => {
await writeFile(
join(postgresDir, "TenantUser.jsonl"),
`${JSON.stringify({ id: "tu1", tenantId: "t1", userId: "u1" })}\n`,
);
const { runner } = makeRunner({
User: [{ id: "u1", email: "e@x.org", firstName: "F", lastName: "L" }],
});
const result = await collectAndWriteGlobals({
postgresDir,
globalsDir,
prisma: runner,
profile: "full-anonymized",
ctx: { salt: "x", profileName: "full-anonymized" },
});
const entry = result.entries.find((e) => e.model === "User");
expect(entry?.checksum.algorithm).toBe("sha256");
expect(entry?.checksum.value).toMatch(/^[a-f0-9]{64}$/);
expect(entry?.sizeBytes).toBeGreaterThan(0);
});
});