11/**
22 * @vitest -environment node
33 */
4- import { workspace } from '@sim/db/schema'
54import { createSessionPrincipal } from '@sim/testing/factories/principal.factory'
6- import { auditMock , auditMockFns } from '@sim/testing/mocks/audit.mock'
7- import { dbChainMockFns , queueTableRows , resetDbChainMock } from '@sim/testing/mocks/database.mock'
5+ import { auditMock } from '@sim/testing/mocks/audit.mock'
6+ import { resetDbChainMock } from '@sim/testing/mocks/database.mock'
87import { permissionsMock , permissionsMockFns } from '@sim/testing/mocks/permissions.mock'
98import { posthogServerMock } from '@sim/testing/mocks/posthog-server.mock'
109import {
@@ -33,7 +32,6 @@ vi.mock('@/ee/workspace-forking/lib/lineage/lineage-root', () => workspaceForkin
3332import { setForkSyncDefault } from '@/ee/workspace-forking/application/sync-default'
3433
3534const principal = createSessionPrincipal ( { userId : 'actor-1' } )
36- const LINEAGE = [ 'root-ws' , 'fork-a' , 'fork-b' , 'grandchild' ]
3735
3836beforeEach ( ( ) => {
3937 resetDbChainMock ( )
@@ -45,50 +43,21 @@ beforeEach(() => {
4543 } )
4644 workspaceAuthorizationMockFns . mockAuthorizeWorkspaceOperation . mockResolvedValue ( undefined )
4745 mockResolveForkLineageRootId . mockResolvedValue ( 'root-ws' )
48- mockResolveForkLineageWorkspaceIds . mockResolvedValue ( LINEAGE )
49- // The live-member row lock, then the `UPDATE ... RETURNING` of the members that changed.
50- queueTableRows (
51- workspace ,
52- LINEAGE . map ( ( id ) => ( { id } ) )
53- )
54- dbChainMockFns . returning . mockResolvedValue ( LINEAGE . map ( ( id ) => ( { id, name : `Name of ${ id } ` } ) ) )
5546} )
5647
57- const run = ( excludeNewWorkflows : boolean ) =>
58- setForkSyncDefault . execute ( {
59- principal,
60- input : { workspaceId : 'fork-a' , excludeNewWorkflows } ,
61- } )
62-
48+ /**
49+ * The lineage-wide write and its audit fan-out are proven against real Postgres in
50+ * `fork-sync.integration.ts`. This covers the one branch that suite cannot stage: an unlink
51+ * committing between the root walk and the lineage lock.
52+ */
6353describe ( 'setForkSyncDefault' , ( ) => {
64- /**
65- * One entry per member, because the default genuinely changed for all of them. A single
66- * entry on the calling workspace would leave the other members' admins with no record.
67- */
68- it ( "files one audit entry per updated member, in that member's own workspace and name" , async ( ) => {
69- await run ( true )
70- const audited = auditMockFns . mockRecordAudit . mock . calls . map ( ( [ entry ] ) => entry )
71- expect ( audited ) . toHaveLength ( LINEAGE . length )
72- expect ( audited . map ( ( entry ) => entry . resourceId ) . sort ( ) ) . toEqual ( [ ...LINEAGE ] . sort ( ) )
73- for ( const entry of audited ) {
74- expect ( entry . action ) . toBe ( 'workspace.fork_sync_default_changed' )
75- // Filed in the workspace it describes. Without an explicit workspaceId the wrapper
76- // defaults it to the caller's workspace, so every entry would pile into one log and
77- // the other members' admins would see nothing.
78- expect ( entry . workspaceId ) . toBe ( entry . resourceId )
79- // The member's OWN name, not a raw id and not the caller's name.
80- expect ( entry . resourceName ) . toBe ( `Name of ${ entry . resourceId } ` )
81- expect ( entry . metadata ) . toMatchObject ( {
82- forkSyncNewWorkflowsExcluded : true ,
83- originWorkspaceId : 'fork-a' ,
84- originWorkspaceName : 'Fork A' ,
85- } )
86- }
87- } )
88-
89- /** An unlink that moved the caller out of the locked root's lineage must not be written. */
9054 it ( 'refuses when the locked root no longer reaches the calling workspace' , async ( ) => {
9155 mockResolveForkLineageWorkspaceIds . mockResolvedValue ( [ 'root-ws' , 'fork-b' ] )
92- await expect ( run ( true ) ) . rejects . toMatchObject ( { statusCode : 409 } )
56+ await expect (
57+ setForkSyncDefault . execute ( {
58+ principal,
59+ input : { workspaceId : 'fork-a' , excludeNewWorkflows : true } ,
60+ } )
61+ ) . rejects . toMatchObject ( { statusCode : 409 } )
9362 } )
9463} )
0 commit comments