|
| 1 | +import { db } from '@sim/db' |
| 2 | +import { member, organization, outboxEvent, user } from '@sim/db/schema' |
| 3 | +import { readTestDatabaseUrl } from '@sim/db/testing/test-infrastructure' |
| 4 | +import { generateId } from '@sim/utils/id' |
| 5 | +import { and, eq, inArray, sql } from 'drizzle-orm' |
| 6 | +import { afterAll, beforeAll, expect, it } from 'vitest' |
| 7 | + |
| 8 | +readTestDatabaseUrl() |
| 9 | + |
| 10 | +const ownerId = generateId() |
| 11 | +const memberId = generateId() |
| 12 | +const externalId = generateId() |
| 13 | +const organizationId = generateId() |
| 14 | +const membershipId = generateId() |
| 15 | +let runtime: { |
| 16 | + membership: typeof import('@/lib/billing/organizations/membership') |
| 17 | + resources: typeof import('@/lib/organizations/resource-cleanup') |
| 18 | + event: typeof import('@/lib/mothership/memories/cleanup-event') |
| 19 | +} |
| 20 | + |
| 21 | +beforeAll(async () => { |
| 22 | + // The cleanup outbox reads its worker credential and destination when it loads. |
| 23 | + process.env.SIM_AGENT_API_URL = 'http://127.0.0.1:9' |
| 24 | + process.env.COPILOT_API_KEY = 'memory-offboarding-fixture-key' |
| 25 | + const [membership, resources, event] = await Promise.all([ |
| 26 | + import('@/lib/billing/organizations/membership'), |
| 27 | + import('@/lib/organizations/resource-cleanup'), |
| 28 | + import('@/lib/mothership/memories/cleanup-event'), |
| 29 | + ]) |
| 30 | + runtime = { membership, resources, event } |
| 31 | + await db.insert(user).values( |
| 32 | + [ownerId, memberId, externalId].map((id) => ({ |
| 33 | + id, |
| 34 | + name: 'Offboarding fixture', |
| 35 | + email: `${id}@memory.test`, |
| 36 | + emailVerified: true, |
| 37 | + createdAt: new Date(), |
| 38 | + updatedAt: new Date(), |
| 39 | + })) |
| 40 | + ) |
| 41 | + await db |
| 42 | + .insert(organization) |
| 43 | + .values({ id: organizationId, name: 'Offboarding', slug: organizationId }) |
| 44 | + await db.insert(member).values([ |
| 45 | + { id: generateId(), organizationId, userId: ownerId, role: 'owner', createdAt: new Date() }, |
| 46 | + { id: membershipId, organizationId, userId: memberId, role: 'member', createdAt: new Date() }, |
| 47 | + ]) |
| 48 | +}, 60_000) |
| 49 | + |
| 50 | +afterAll(async () => { |
| 51 | + await db |
| 52 | + .delete(outboxEvent) |
| 53 | + .where( |
| 54 | + and( |
| 55 | + eq(outboxEvent.eventType, runtime.event.MEMORY_CLEANUP_EVENT), |
| 56 | + sql`${outboxEvent.payload}->'request'->'target'->>'organizationId' = ${organizationId}` |
| 57 | + ) |
| 58 | + ) |
| 59 | + await db.delete(organization).where(eq(organization.id, organizationId)) |
| 60 | + await db.delete(user).where(inArray(user.id, [ownerId, memberId, externalId])) |
| 61 | +}) |
| 62 | + |
| 63 | +/** The memory erasures queued for this organization, as `kind:userId` (or `kind`). */ |
| 64 | +async function queued(): Promise<string[]> { |
| 65 | + const rows = await db |
| 66 | + .select({ payload: outboxEvent.payload }) |
| 67 | + .from(outboxEvent) |
| 68 | + .where( |
| 69 | + and( |
| 70 | + eq(outboxEvent.eventType, runtime.event.MEMORY_CLEANUP_EVENT), |
| 71 | + sql`${outboxEvent.payload}->'request'->'target'->>'organizationId' = ${organizationId}` |
| 72 | + ) |
| 73 | + ) |
| 74 | + return rows |
| 75 | + .map(({ payload }) => { |
| 76 | + const target = (payload as { request: { target: { kind: string; userId?: string } } }).request |
| 77 | + .target |
| 78 | + return target.userId ? `${target.kind}:${target.userId}` : target.kind |
| 79 | + }) |
| 80 | + .sort() |
| 81 | +} |
| 82 | + |
| 83 | +it('queues private memory erasure with each committed departure and organization deletion, never on rollback', async () => { |
| 84 | + // A removal whose actor lacks authority rolls back with nothing queued. |
| 85 | + await expect( |
| 86 | + runtime.membership.removeUserFromOrganization({ |
| 87 | + organizationId, |
| 88 | + userId: memberId, |
| 89 | + memberId: membershipId, |
| 90 | + actorUserId: externalId, |
| 91 | + skipBillingLogic: true, |
| 92 | + onError: 'throw', |
| 93 | + }) |
| 94 | + ).rejects.toThrow() |
| 95 | + expect(await queued()).toEqual([]) |
| 96 | + |
| 97 | + await runtime.membership.removeUserFromOrganization({ |
| 98 | + organizationId, |
| 99 | + userId: memberId, |
| 100 | + memberId: membershipId, |
| 101 | + skipBillingLogic: true, |
| 102 | + onError: 'throw', |
| 103 | + }) |
| 104 | + await runtime.membership.removeExternalUserFromOrganizationWorkspaces({ |
| 105 | + organizationId, |
| 106 | + userId: externalId, |
| 107 | + }) |
| 108 | + expect(await queued()).toEqual( |
| 109 | + [`organization_member:${externalId}`, `organization_member:${memberId}`].sort() |
| 110 | + ) |
| 111 | + |
| 112 | + // Organization deletion queues the erasure in the delete transaction: a rollback drops it. |
| 113 | + await expect( |
| 114 | + db.transaction(async (tx) => { |
| 115 | + await runtime.resources.enqueueOrganizationResourceCleanup(tx, organizationId) |
| 116 | + throw new Error('rollback fixture') |
| 117 | + }) |
| 118 | + ).rejects.toThrow('rollback fixture') |
| 119 | + expect(await queued()).not.toContain('organization') |
| 120 | + await db.transaction(async (tx) => { |
| 121 | + await runtime.resources.enqueueOrganizationResourceCleanup(tx, organizationId) |
| 122 | + }) |
| 123 | + expect(await queued()).toContain('organization') |
| 124 | +}, 30_000) |
0 commit comments