@@ -31,6 +31,7 @@ vi.mock('@/lib/api-server', () => ({
3131} ) ) ;
3232vi . mock ( '@/lib/permissions' , ( ) => ( {
3333 canAccessApp : vi . fn ( ) . mockReturnValue ( true ) ,
34+ parseRolesString : vi . fn ( ) . mockReturnValue ( [ 'owner' ] ) ,
3435} ) ) ;
3536vi . mock ( '@/lib/permissions.server' , ( ) => ( {
3637 resolveUserPermissions : vi . fn ( ) . mockResolvedValue ( [ ] ) ,
@@ -44,7 +45,7 @@ vi.mock('next/dynamic', () => ({
4445vi . mock ( '@aws-sdk/client-s3' , ( ) => ( { GetObjectCommand : vi . fn ( ) } ) ) ;
4546vi . mock ( '@aws-sdk/s3-request-presigner' , ( ) => ( { getSignedUrl : vi . fn ( ) } ) ) ;
4647
47- import { createMockSession , setupAuthMocks } from '@/test-utils/mocks/auth' ;
48+ import { createMockSession , mockAuthApi , setupAuthMocks } from '@/test-utils/mocks/auth' ;
4849import { mockDb } from '@/test-utils/mocks/db' ;
4950
5051const { default : Layout } = await import ( './layout' ) ;
@@ -70,10 +71,10 @@ describe('Layout activeOrganizationId sync', () => {
7071 deactivated : false ,
7172 } ) ;
7273 mockDb . onboarding . findFirst . mockResolvedValue ( null ) ;
73- mockDb . session . update . mockResolvedValue ( { } ) ;
74+ mockAuthApi . setActiveOrganization . mockResolvedValue ( { } ) ;
7475 } ) ;
7576
76- it ( 'should update session directly in DB when session org differs from URL org' , async ( ) => {
77+ it ( 'should call setActiveOrganization via auth API when session org differs from URL org' , async ( ) => {
7778 setupAuthMocks ( {
7879 session : createMockSession ( { id : sessionId , activeOrganizationId : 'org_other' } ) ,
7980 } ) ;
@@ -83,13 +84,13 @@ describe('Layout activeOrganizationId sync', () => {
8384 params : Promise . resolve ( { orgId : requestedOrgId } ) ,
8485 } ) ;
8586
86- expect ( mockDb . session . update ) . toHaveBeenCalledWith ( {
87- where : { id : sessionId } ,
88- data : { activeOrganizationId : requestedOrgId } ,
87+ expect ( mockAuthApi . setActiveOrganization ) . toHaveBeenCalledWith ( {
88+ headers : expect . anything ( ) ,
89+ body : { organizationId : requestedOrgId } ,
8990 } ) ;
9091 } ) ;
9192
92- it ( 'should update session when activeOrganizationId is null' , async ( ) => {
93+ it ( 'should call setActiveOrganization when activeOrganizationId is null' , async ( ) => {
9394 setupAuthMocks ( {
9495 session : createMockSession ( { id : sessionId , activeOrganizationId : null } ) ,
9596 } ) ;
@@ -99,13 +100,13 @@ describe('Layout activeOrganizationId sync', () => {
99100 params : Promise . resolve ( { orgId : requestedOrgId } ) ,
100101 } ) ;
101102
102- expect ( mockDb . session . update ) . toHaveBeenCalledWith ( {
103- where : { id : sessionId } ,
104- data : { activeOrganizationId : requestedOrgId } ,
103+ expect ( mockAuthApi . setActiveOrganization ) . toHaveBeenCalledWith ( {
104+ headers : expect . anything ( ) ,
105+ body : { organizationId : requestedOrgId } ,
105106 } ) ;
106107 } ) ;
107108
108- it ( 'should NOT update session when session org matches URL org' , async ( ) => {
109+ it ( 'should NOT call setActiveOrganization when session org matches URL org' , async ( ) => {
109110 setupAuthMocks ( {
110111 session : createMockSession ( { id : sessionId , activeOrganizationId : requestedOrgId } ) ,
111112 } ) ;
@@ -115,14 +116,27 @@ describe('Layout activeOrganizationId sync', () => {
115116 params : Promise . resolve ( { orgId : requestedOrgId } ) ,
116117 } ) ;
117118
119+ expect ( mockAuthApi . setActiveOrganization ) . not . toHaveBeenCalled ( ) ;
120+ } ) ;
121+
122+ it ( 'should not do a direct DB session update' , async ( ) => {
123+ setupAuthMocks ( {
124+ session : createMockSession ( { id : sessionId , activeOrganizationId : 'org_other' } ) ,
125+ } ) ;
126+
127+ await Layout ( {
128+ children : null ,
129+ params : Promise . resolve ( { orgId : requestedOrgId } ) ,
130+ } ) ;
131+
118132 expect ( mockDb . session . update ) . not . toHaveBeenCalled ( ) ;
119133 } ) ;
120134
121- it ( 'should continue rendering even if session update fails' , async ( ) => {
135+ it ( 'should continue rendering even if setActiveOrganization fails' , async ( ) => {
122136 setupAuthMocks ( {
123137 session : createMockSession ( { id : sessionId , activeOrganizationId : 'org_other' } ) ,
124138 } ) ;
125- mockDb . session . update . mockRejectedValue ( new Error ( 'db update failed' ) ) ;
139+ mockAuthApi . setActiveOrganization . mockRejectedValue ( new Error ( 'API call failed' ) ) ;
126140
127141 const consoleSpy = vi . spyOn ( console , 'error' ) . mockImplementation ( ( ) => { } ) ;
128142
0 commit comments