Skip to content

Commit 1d79426

Browse files
committed
test(forks): match sync-default audit rows by origin and assert the exact member set
1 parent ca1d616 commit 1d79426

1 file changed

Lines changed: 39 additions & 17 deletions

File tree

‎apps/sim/lib/workspaces/__integration__/fork-sync.integration.ts‎

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import {
1717
workspaceSandbox,
1818
} from '@sim/db/schema'
1919
import { generateId } from '@sim/utils/id'
20-
import { and, eq, inArray } from 'drizzle-orm'
20+
import { and, eq, inArray, sql } from 'drizzle-orm'
2121
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest'
2222
import { withWorkspaceInvocationScope } from '@/lib/core/application/workspace-invocation-scope'
2323
import { processOutboxEventById } from '@/lib/core/outbox/service'
@@ -790,6 +790,22 @@ describe('authorized fork and sync against PostgreSQL', () => {
790790
)[0]?.excluded
791791
const setDefault = (workspaceId: string, excludeNewWorkflows: boolean) =>
792792
setForkSyncDefault.execute({ principal, input: { workspaceId, excludeNewWorkflows } })
793+
/** Every audit entry a change issued from `originId` filed, whichever workspace it named. */
794+
const auditedFrom = (originId: string) =>
795+
db
796+
.select({
797+
workspaceId: auditLog.workspaceId,
798+
resourceId: auditLog.resourceId,
799+
resourceName: auditLog.resourceName,
800+
metadata: auditLog.metadata,
801+
})
802+
.from(auditLog)
803+
.where(
804+
and(
805+
eq(auditLog.action, AuditAction.WORKSPACE_FORK_SYNC_DEFAULT_CHANGED),
806+
sql`${auditLog.metadata} ->> 'originWorkspaceId' = ${originId}`
807+
)
808+
)
793809
try {
794810
const first = await setDefault(childId, true)
795811
expect(first.changedWorkspaces.map((member) => member.id)).toEqual(
@@ -814,21 +830,12 @@ describe('authorized fork and sync against PostgreSQL', () => {
814830
)
815831
await vi.waitFor(
816832
async () => {
817-
const entries = await db
818-
.select({
819-
workspaceId: auditLog.workspaceId,
820-
resourceId: auditLog.resourceId,
821-
resourceName: auditLog.resourceName,
822-
metadata: auditLog.metadata,
823-
})
824-
.from(auditLog)
825-
.where(
826-
and(
827-
eq(auditLog.action, AuditAction.WORKSPACE_FORK_SYNC_DEFAULT_CHANGED),
828-
inArray(auditLog.resourceId, [...changed.keys()])
829-
)
830-
)
831-
expect(entries).toHaveLength(changed.size)
833+
const entries = await auditedFrom(childId)
834+
// Exactly the changed members, once each: no missing, duplicate, or extra entry,
835+
// including from the no-op repeat issued from the same workspace.
836+
expect(entries.map((entry) => entry.resourceId).sort()).toEqual(
837+
[...changed.keys()].sort()
838+
)
832839
for (const entry of entries) {
833840
expect(entry.workspaceId).toBe(entry.resourceId)
834841
expect(entry.resourceName).toBe(changed.get(entry.resourceId!))
@@ -885,10 +892,25 @@ describe('authorized fork and sync against PostgreSQL', () => {
885892
).toEqual([{ excluded: true }])
886893

887894
await db.update(workspace).set({ archivedAt: new Date() }).where(eq(workspace.id, childId))
888-
await setDefault(grandchildId, false)
895+
const fromGrandchild = await setDefault(grandchildId, false)
889896
expect(await policyOf(sourceWorkspaceId)).toBe(false)
890897
expect(await policyOf(grandchildId)).toBe(false)
891898
expect(await policyOf(childId)).toBe(true)
899+
// The archived member was neither written nor audited.
900+
const expectedFromGrandchild = fromGrandchild.changedWorkspaces.map((member) => member.id)
901+
expect(expectedFromGrandchild).toEqual(
902+
expect.arrayContaining([sourceWorkspaceId, grandchildId])
903+
)
904+
expect(expectedFromGrandchild).not.toContain(childId)
905+
await vi.waitFor(
906+
async () => {
907+
const entries = await auditedFrom(grandchildId)
908+
expect(entries.map((entry) => entry.resourceId).sort()).toEqual(
909+
[...expectedFromGrandchild].sort()
910+
)
911+
},
912+
{ timeout: 5000 }
913+
)
892914
} finally {
893915
await db.update(workspace).set({ archivedAt: null }).where(eq(workspace.id, childId))
894916
await setDefault(sourceWorkspaceId, false)

0 commit comments

Comments
 (0)