import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { mkdtemp, rm } from "node:fs/promises"; import { join } from "node:path"; import { tmpdir } from "node:os"; import { Readable } from "node:stream"; import { mockClient } from "aws-sdk-client-mock"; import { GetObjectCommand, ListObjectsV2Command, S3Client } from "@aws-sdk/client-s3"; import { Upload } from "@aws-sdk/lib-storage"; import { runTenantSnapshot, type PrismaRunner } from "./snapshot.js"; import { isGlobalsRestoreMode, runTenantRestore, type PrismaRestoreRunner, } from "./restore.js"; class FakeSourcePrisma implements PrismaRunner { constructor( private readonly rows: Record[]>, private readonly globals: Record[]> = {}, ) {} // eslint-disable-next-line @typescript-eslint/no-unused-vars async *findManyByTenant( modelName: string, tenantField: string, tenantId: string, pageSize: number, ): AsyncIterable> { void tenantField; void tenantId; void pageSize; const list = this.rows[modelName] ?? []; for (const r of list) yield r; } async *findManyByWhere( modelName: string, where: Readonly>, pageSize: number, ): AsyncIterable> { void where; void pageSize; const list = this.rows[modelName] ?? []; for (const r of list) yield r; } async findManyByIds(modelName: string, ids: readonly string[]): Promise[]> { const list = this.globals[modelName] ?? []; return list.filter((r) => typeof r.id === "string" && ids.includes(r.id as string)); } async findManyByForeignKeys( modelName: string, foreignKey: string, ids: readonly string[], ): Promise[]> { const list = this.globals[modelName] ?? []; return list.filter( (row) => typeof row[foreignKey] === "string" && ids.includes(row[foreignKey] as string), ); } } class FakeTargetPrisma implements PrismaRestoreRunner { readonly writes: Record[]> = {}; readonly writeOrder: string[] = []; /** Rows that already exist on the target — used by upsertManyById to simulate prior state. */ readonly preExisting: Record> = {}; async createMany(modelName: string, rows: Record[]): Promise<{ written: number }> { if (rows.length > 0) this.writeOrder.push(modelName); this.writes[modelName] = (this.writes[modelName] ?? []).concat(rows); return { written: rows.length }; } async upsertManyById( modelName: string, rows: Record[], ): Promise<{ created: number; updated: number }> { const existing = this.preExisting[modelName] ?? new Set(); let created = 0; let updated = 0; for (const row of rows) { const id = row.id as string; if (existing.has(id)) { updated += 1; continue; } this.writes[modelName] = (this.writes[modelName] ?? []).concat(row); created += 1; } return { created, updated }; } } /** Variant without upsertManyById — exercises the legacy-runner fallback. */ class LegacyTargetPrisma implements PrismaRestoreRunner { readonly writes: Record[]> = {}; async createMany(modelName: string, rows: Record[]): Promise<{ written: number }> { this.writes[modelName] = (this.writes[modelName] ?? []).concat(rows); return { written: rows.length }; } } class PartialWriteTargetPrisma extends FakeTargetPrisma { constructor(private readonly partialModel: string) { super(); } override async createMany(modelName: string, rows: Record[]): Promise<{ written: number }> { this.writes[modelName] = (this.writes[modelName] ?? []).concat(rows); return { written: modelName === this.partialModel ? Math.max(0, rows.length - 1) : rows.length }; } } describe("runTenantRestore", () => { let workdir: string; beforeEach(async () => { workdir = await mkdtemp(join(tmpdir(), "tenant-restore-test-")); }); afterEach(async () => { await rm(workdir, { recursive: true, force: true }); }); it("recognizes supported globals restore modes", () => { expect(isGlobalsRestoreMode("upsert")).toBe(true); expect(isGlobalsRestoreMode("skip")).toBe(true); expect(isGlobalsRestoreMode("error")).toBe(true); expect(isGlobalsRestoreMode("merge")).toBe(false); expect(isGlobalsRestoreMode("")).toBe(false); }); it("round-trips a snapshot through restore with FK-remapped tenantId", async () => { // 1. Snapshot from FakeSourcePrisma. const sourcePrisma = new FakeSourcePrisma({ TenantUser: [ { id: "tu1", tenantId: "tenant_a", userId: "u1", type: 1, joined: 1, status: 1 }, { id: "tu2", tenantId: "tenant_a", userId: "u2", type: 1, joined: 1, status: 1 }, ], TenantUserInvitation: [ { id: "inv1", tenantId: "tenant_a", email: "x@y.io", firstName: "X", lastName: "Y" }, ], }); const snapshotPath = join(workdir, "snap.tar.gz"); await runTenantSnapshot({ tenantId: "tenant_a", profile: "full-anonymized", prisma: sourcePrisma, output: { kind: "local", path: snapshotPath }, logger: { info: vi.fn(), warn: vi.fn() }, }); // 2. Restore into FakeTargetPrisma. const target = new FakeTargetPrisma(); const result = await runTenantRestore({ tenantId: "tenant_a_clone", source: snapshotPath, prisma: target, yes: true, logger: { info: vi.fn(), warn: vi.fn() }, }); expect(result.manifest.sourceTenantId).toBe("tenant_a"); const tuEntry = result.perModel.find((m) => m.model === "TenantUser"); expect(tuEntry).toEqual({ model: "TenantUser", written: 2, expected: 2 }); // All rows should have the new tenantId. expect(target.writes.TenantUser).toHaveLength(2); for (const row of target.writes.TenantUser) { expect(row.tenantId).toBe("tenant_a_clone"); } // Anonymization preserved on the way in. expect(target.writes.TenantUserInvitation?.[0]?.email).toMatch(/@anonymized\.test$/u); }, 15_000); it("restores FK-derived children without adding tenantId and in parent-first order", async () => { const sourcePrisma = new FakeSourcePrisma({ TenantInboundAddress: [{ id: "inbound_1", tenantId: "tenant_a", address: "in@local.test" }], Email: [{ id: "email_1", tenantInboundAddressId: "inbound_1", messageId: "message_1" }], KnowledgeBase: [{ id: "kb_1", tenantId: "tenant_a" }], KnowledgeBaseCategory: [{ id: "category_1", knowledgeBaseId: "kb_1" }], Workflow: [{ id: "workflow_1", tenantId: "tenant_a" }], WorkflowBlock: [{ id: "block_1", workflowId: "workflow_1" }], WorkflowExecution: [ { id: "execution_1", tenantId: "tenant_a", workflowId: "workflow_1", waitingBlockId: "block_1" }, ], Portal: [{ id: "portal_1", tenantId: "tenant_a" }], PortalPage: [{ id: "page_1", portalId: "portal_1" }], }); const snapshotPath = join(workdir, "derived.tar.gz"); await runTenantSnapshot({ tenantId: "tenant_a", profile: "none", superAdminConfirmed: true, prisma: sourcePrisma, output: { kind: "local", path: snapshotPath }, includeGlobals: false, logger: { info: vi.fn(), warn: vi.fn() }, }); const target = new FakeTargetPrisma(); await runTenantRestore({ tenantId: "tenant_clone", source: snapshotPath, prisma: target, yes: true, logger: { info: vi.fn(), warn: vi.fn() }, }); for (const model of ["Email", "KnowledgeBaseCategory", "WorkflowBlock", "PortalPage"]) { expect(target.writes[model]?.[0]).not.toHaveProperty("tenantId"); } expect(target.writes.TenantInboundAddress?.[0]?.tenantId).toBe("tenant_clone"); expect(target.writeOrder.indexOf("TenantInboundAddress")).toBeLessThan(target.writeOrder.indexOf("Email")); expect(target.writeOrder.indexOf("KnowledgeBase")).toBeLessThan(target.writeOrder.indexOf("KnowledgeBaseCategory")); expect(target.writeOrder.indexOf("WorkflowBlock")).toBeLessThan(target.writeOrder.indexOf("WorkflowExecution")); expect(target.writeOrder.indexOf("Portal")).toBeLessThan(target.writeOrder.indexOf("PortalPage")); }); it("aborts when the operator answers 'no' to the confirmation prompt", async () => { const sourcePrisma = new FakeSourcePrisma({ TenantUser: [{ id: "tu1", tenantId: "tenant_a", userId: "u1", type: 1, joined: 1, status: 1 }], }); const snapshotPath = join(workdir, "snap.tar.gz"); await runTenantSnapshot({ tenantId: "tenant_a", profile: "full-anonymized", prisma: sourcePrisma, output: { kind: "local", path: snapshotPath }, logger: { info: vi.fn(), warn: vi.fn() }, }); const target = new FakeTargetPrisma(); const { Readable } = await import("node:stream"); const { Writable } = await import("node:stream"); const stdin = Readable.from(["n\n"]); const stdout = new Writable({ write: (_c, _e, cb) => cb() }); await expect( runTenantRestore({ tenantId: "tenant_a_clone", source: snapshotPath, prisma: target, yes: false, stdin: stdin as NodeJS.ReadableStream, stdout, logger: { info: vi.fn(), warn: vi.fn() }, }), ).rejects.toThrow(/Aborted/u); }); it("refuses to run in production without --allow-production-restore", async () => { const prev = process.env.NODE_ENV; process.env.NODE_ENV = "production"; try { await expect( runTenantRestore({ tenantId: "tenant_a_clone", source: "/nonexistent.tar.gz", prisma: new FakeTargetPrisma(), yes: true, logger: { info: vi.fn(), warn: vi.fn() }, }), ).rejects.toThrow(/production/u); } finally { process.env.NODE_ENV = prev; } }); it("rejects malformed s3 sources before attempting a restore", async () => { await expect( runTenantRestore({ tenantId: "tenant_a_clone", source: "s3://bucket-only", prisma: new FakeTargetPrisma(), yes: true, logger: { info: vi.fn(), warn: vi.fn() }, }), ).rejects.toThrow(/Invalid --source/u); }); it("restores storage objects under the target tenant prefix", async () => { const sourcePrisma = new FakeSourcePrisma({ TenantUser: [{ id: "tu1", tenantId: "tenant_a", userId: "u1", type: 1, joined: 1, status: 1 }], }); const sourceS3 = mockClient(S3Client as never); sourceS3.on(ListObjectsV2Command as never).callsFake(() => ({ Contents: [{ Key: "tenants/tenant_a/uploads/file.txt", Size: 11, ETag: '"etag"' }], IsTruncated: false, })); sourceS3.on(GetObjectCommand as never).callsFake(() => ({ Body: Readable.from([Buffer.from("hello world")]), })); const snapshotPath = join(workdir, "snap-with-storage.tar.gz"); await runTenantSnapshot({ tenantId: "tenant_a", profile: "full-anonymized", prisma: sourcePrisma, output: { kind: "local", path: snapshotPath }, s3Client: sourceS3 as unknown as S3Client, storageBucket: "app-storage", logger: { info: vi.fn(), warn: vi.fn() }, }); const uploadedKeys: string[] = []; const origDone = Upload.prototype.done; Upload.prototype.done = vi.fn(function (this: Upload) { const params = this["params"] as { Key?: string }; if (params.Key) uploadedKeys.push(params.Key); return Promise.resolve({ ETag: '"uploaded"' }); }) as never; try { const result = await runTenantRestore({ tenantId: "tenant_b", source: snapshotPath, prisma: new FakeTargetPrisma(), storageBucket: "target-storage", s3Client: new S3Client({ region: "us-east-1" }), yes: true, logger: { info: vi.fn(), warn: vi.fn() }, }); expect(result.storageObjectsUploaded).toBe(1); expect(uploadedKeys).toEqual(["tenants/tenant_b/uploads/file.txt"]); } finally { Upload.prototype.done = origDone; } }); it("warns when tenant-scoped model writes do not match manifest counts", async () => { const sourcePrisma = new FakeSourcePrisma({ TenantUser: [ { id: "tu1", tenantId: "tenant_a", userId: "u1", type: 1, joined: 1, status: 1 }, { id: "tu2", tenantId: "tenant_a", userId: "u2", type: 1, joined: 1, status: 1 }, ], }); const snapshotPath = join(workdir, "snap.tar.gz"); await runTenantSnapshot({ tenantId: "tenant_a", profile: "full-anonymized", prisma: sourcePrisma, output: { kind: "local", path: snapshotPath }, includeGlobals: false, logger: { info: vi.fn(), warn: vi.fn() }, }); const warn = vi.fn(); const result = await runTenantRestore({ tenantId: "tenant_a_clone", source: snapshotPath, prisma: new PartialWriteTargetPrisma("TenantUser"), yes: true, logger: { info: vi.fn(), warn }, }); expect(result.perModel.find((m) => m.model === "TenantUser")).toEqual({ model: "TenantUser", written: 1, expected: 2, }); expect(warn).toHaveBeenCalledWith(expect.stringContaining("Model TenantUser: wrote 1 rows")); expect(warn).toHaveBeenCalledWith(expect.stringContaining("Row-count mismatch")); }); describe("globals replay", () => { async function snapshotWithUsers(snapshotPath: string): Promise { const sourcePrisma = new FakeSourcePrisma( { TenantUser: [ { id: "tu1", tenantId: "tenant_a", userId: "u1", type: 1, joined: 1, status: 1 }, { id: "tu2", tenantId: "tenant_a", userId: "u2", type: 1, joined: 1, status: 1 }, ], }, { User: [ { id: "u1", email: "first@real.com", firstName: "First", lastName: "Real" }, { id: "u2", email: "second@real.com", firstName: "Second", lastName: "Real" }, ], }, ); await runTenantSnapshot({ tenantId: "tenant_a", profile: "full-anonymized", prisma: sourcePrisma, output: { kind: "local", path: snapshotPath }, logger: { info: vi.fn(), warn: vi.fn() }, }); } it("replays globals BEFORE tenant-scoped models (default upsert mode)", async () => { const snapshotPath = join(workdir, "snap.tar.gz"); await snapshotWithUsers(snapshotPath); const target = new FakeTargetPrisma(); const result = await runTenantRestore({ tenantId: "tenant_a_clone", source: snapshotPath, prisma: target, yes: true, logger: { info: vi.fn(), warn: vi.fn() }, }); const userGlobal = result.globals.find((g) => g.model === "User"); expect(userGlobal).toEqual({ model: "User", expected: 2, created: 2, existed: 0 }); // Users landed in target. expect(target.writes.User).toHaveLength(2); // And TenantUser rows landed under the new tenantId. expect(target.writes.TenantUser).toHaveLength(2); for (const row of target.writes.TenantUser) { expect(row.tenantId).toBe("tenant_a_clone"); } }); it("upsert mode reports existed=N when globals already exist on target", async () => { const snapshotPath = join(workdir, "snap.tar.gz"); await snapshotWithUsers(snapshotPath); const target = new FakeTargetPrisma(); target.preExisting.User = new Set(["u1"]); // one of two users already present const result = await runTenantRestore({ tenantId: "tenant_a_clone", source: snapshotPath, prisma: target, yes: true, globalsMode: "upsert", logger: { info: vi.fn(), warn: vi.fn() }, }); expect(result.globals.find((g) => g.model === "User")).toEqual({ model: "User", expected: 2, created: 1, existed: 1, }); // Only the new user landed; the existing u1 was left in place. expect(target.writes.User).toHaveLength(1); expect((target.writes.User[0] as { id: string }).id).toBe("u2"); }); it("error mode aborts when a global already exists on target", async () => { const snapshotPath = join(workdir, "snap.tar.gz"); await snapshotWithUsers(snapshotPath); const target = new FakeTargetPrisma(); target.preExisting.User = new Set(["u1"]); await expect( runTenantRestore({ tenantId: "tenant_a_clone", source: snapshotPath, prisma: target, yes: true, globalsMode: "error", logger: { info: vi.fn(), warn: vi.fn() }, }), ).rejects.toThrow(/--globals=error/u); }); it("skip mode falls back to createMany semantics for legacy runners", async () => { const snapshotPath = join(workdir, "snap.tar.gz"); await snapshotWithUsers(snapshotPath); const target = new LegacyTargetPrisma(); const result = await runTenantRestore({ tenantId: "tenant_a_clone", source: snapshotPath, prisma: target, yes: true, globalsMode: "skip", logger: { info: vi.fn(), warn: vi.fn() }, }); expect(result.globals.find((g) => g.model === "User")?.created).toBe(2); expect(target.writes.User).toHaveLength(2); }); it("default upsert mode warns and falls back for legacy runners", async () => { const snapshotPath = join(workdir, "snap.tar.gz"); await snapshotWithUsers(snapshotPath); const target = new LegacyTargetPrisma(); const warn = vi.fn(); const result = await runTenantRestore({ tenantId: "tenant_a_clone", source: snapshotPath, prisma: target, yes: true, logger: { info: vi.fn(), warn }, }); expect(result.globals.find((g) => g.model === "User")).toEqual({ model: "User", expected: 2, created: 2, existed: 0, }); expect(warn).toHaveBeenCalledWith(expect.stringContaining("does not implement upsertManyById")); }); it("skip mode reports existing globals from partial createMany writes", async () => { const snapshotPath = join(workdir, "snap.tar.gz"); await snapshotWithUsers(snapshotPath); const result = await runTenantRestore({ tenantId: "tenant_a_clone", source: snapshotPath, prisma: new PartialWriteTargetPrisma("User"), yes: true, globalsMode: "skip", logger: { info: vi.fn(), warn: vi.fn() }, }); expect(result.globals.find((g) => g.model === "User")).toEqual({ model: "User", expected: 2, created: 1, existed: 1, }); }); it("legacy snapshot (no globals slice) is tolerated with a warn line", async () => { // Build a snapshot using a runner without findManyByIds — manifest will // have no `globals` array (matches pre-feature snapshots). const legacySource: PrismaRunner = { async *findManyByTenant() { // empty }, async *findManyByWhere() { // empty }, }; const snapshotPath = join(workdir, "legacy.tar.gz"); await runTenantSnapshot({ tenantId: "tenant_a", profile: "full-anonymized", prisma: legacySource, output: { kind: "local", path: snapshotPath }, logger: { info: vi.fn(), warn: vi.fn() }, }); const warn = vi.fn(); const result = await runTenantRestore({ tenantId: "tenant_a_clone", source: snapshotPath, prisma: new FakeTargetPrisma(), yes: true, logger: { info: vi.fn(), warn }, }); expect(result.globals).toEqual([]); expect(warn).toHaveBeenCalledWith(expect.stringContaining("no globals slice")); }); }); });