11import { expect } from '@esm-bundle/chai' ;
22import { checkCoworkerAccess } from '../../../../../nx2/blocks/chat-ao/coworker/access.js' ;
3- import {
4- resolveCoworkerAccessBase , resolveAoHttpBase ,
5- } from '../../../../../nx2/blocks/chat-ao/coworker/endpoints.js' ;
3+ import { resolveAoHttpBase } from '../../../../../nx2/blocks/chat-ao/coworker/endpoints.js' ;
64import { resetMockIms , setMockIms } from '../../../mocks/ims.js' ;
75
86const orgId = 'selected@AdobeOrg' ;
@@ -19,24 +17,21 @@ function context({ org = orgId, region = 'CAN2', environment = 'PROD' } = {}) {
1917 } ] ;
2018}
2119
22- function response ( effect = 'permit' , overrides = { } ) {
23- return { responses : [ { resource : { uri } , action : 'use' , effect , ... overrides } ] } ;
20+ function response ( allowed = true ) {
21+ return { allowed , reason : null , message : null } ;
2422}
2523
2624describe ( 'Coworker access endpoints' , ( ) => {
2725 [ 'can2' , 'new1' ] . forEach ( ( region ) => {
28- it ( `derives the ACS host from the AO region (${ region } ) without an allowlist` , ( ) => {
26+ it ( `resolves the AO region (${ region } ) without an allowlist` , ( ) => {
2927 const profile = context ( { region : region . toUpperCase ( ) } ) ;
3028 expect ( resolveAoHttpBase ( profile ) )
3129 . to . equal ( `https://agent-orchestrator-prod-${ region } .adobe.io` ) ;
32- expect ( resolveCoworkerAccessBase ( profile ) )
33- . to . equal ( `https://cx-accesscontrol-${ region } .adobe.io` ) ;
3430 } ) ;
3531 } ) ;
3632
3733 it ( 'uses the same production VA7 fallback as AO when location is unavailable' , ( ) => {
3834 expect ( resolveAoHttpBase ( ) ) . to . equal ( 'https://agent-orchestrator-prod-va7.adobe.io' ) ;
39- expect ( resolveCoworkerAccessBase ( ) ) . to . equal ( 'https://cx-accesscontrol-va7.adobe.io' ) ;
4035 } ) ;
4136} ) ;
4237
@@ -65,25 +60,36 @@ describe('checkCoworkerAccess', () => {
6560 } ) ;
6661
6762 it ( 'checks chat use with the selected IMS org and current user token' , async ( ) => {
68- setMockIms ( { token : { token : 'selected-org-token' } } ) ;
63+ setMockIms ( { token : { token : 'selected-org-token' } , imsClientId : 'darkalley' } ) ;
6964 expect ( await checkCoworkerAccess ( ) ) . to . deep . equal ( { allowed : true } ) ;
7065 expect ( calls ) . to . have . length ( 1 ) ;
7166 const [ { url, opts } ] = calls ;
72- expect ( url ) . to . equal ( 'https://cx-accesscontrol- can2.adobe.io/enforcement/cx-coworker ' ) ;
73- expect ( opts . method ) . to . equal ( 'POST ' ) ;
67+ expect ( url ) . to . equal ( 'https://agent-orchestrator-prod- can2.adobe.io/api/access ' ) ;
68+ expect ( opts . method ) . to . equal ( 'GET ' ) ;
7469 expect ( opts . headers ) . to . deep . equal ( {
7570 authorization : 'Bearer selected-org-token' ,
76- 'x-gw-ims-org -id' : orgId ,
77- 'content-type ' : 'application/json ' ,
71+ 'x-tenant -id' : orgId ,
72+ 'x-api-key ' : 'darkalley ' ,
7873 accept : 'application/json' ,
7974 } ) ;
80- expect ( JSON . parse ( opts . body ) ) . to . deep . equal ( {
81- requests : [ { resource : { uri } , action : 'use' } ] ,
82- } ) ;
75+ expect ( opts . body ) . to . equal ( undefined ) ;
76+ expect ( opts . cache ) . to . equal ( 'no-store' ) ;
77+ } ) ;
78+
79+ it ( 'uses the AO fallback host when the selected org has no location' , async ( ) => {
80+ setMockIms ( { projectedProductContext : [ { prodCtx : { owningEntity : orgId } } ] } ) ;
81+ expect ( await checkCoworkerAccess ( ) ) . to . deep . equal ( { allowed : true } ) ;
82+ expect ( calls [ 0 ] . url ) . to . equal ( 'https://agent-orchestrator-prod-va7.adobe.io/api/access' ) ;
83+ } ) ;
84+
85+ it ( 'does not request access without a configured IMS client' , async ( ) => {
86+ setMockIms ( { imsClientId : null } ) ;
87+ expect ( await checkCoworkerAccess ( ) ) . to . deep . equal ( { error : 'configuration' } ) ;
88+ expect ( calls ) . to . have . length ( 0 ) ;
8389 } ) ;
8490
8591 it ( 'reports an explicit denial separately from a failed check' , async ( ) => {
86- body = response ( 'deny' ) ;
92+ body = response ( false ) ;
8793 expect ( await checkCoworkerAccess ( ) ) . to . deep . equal ( { allowed : false } ) ;
8894 } ) ;
8995
@@ -95,14 +101,14 @@ describe('checkCoworkerAccess', () => {
95101 } ) ;
96102
97103 [
98- { responses : [ ] } ,
99- { responses : { } } ,
100- { responses : [ null ] } ,
101- { responses : [ ... response ( ) . responses , ... response ( ) . responses ] } ,
102- response ( 'allow' ) ,
103- response ( 'PERMIT' ) ,
104- response ( 'permit' , { action : 'manage ' } ) ,
105- response ( 'permit' , { resource : { uri : '/orgs/other/apps/coworker/chat/*' } } ) ,
104+ { } ,
105+ null ,
106+ { allowed : null } ,
107+ { allowed : 'true' } ,
108+ { allowed : 'false' } ,
109+ { allowed : 1 } ,
110+ { allowed : 'permit ' } ,
111+ { responses : [ { resource : { uri } , action : 'use' , effect : 'permit' } ] } ,
106112 ] . forEach ( ( invalidBody , index ) => {
107113 it ( `rejects a malformed or mismatched decision (${ index + 1 } )` , async ( ) => {
108114 body = invalidBody ;
@@ -140,18 +146,19 @@ describe('checkCoworkerAccess', () => {
140146 { environment : 'STAGE' } ,
141147 { environment : 'DEV' } ,
142148 ] . forEach ( ( location ) => {
143- it ( `does not invent an ACS host for ${ JSON . stringify ( location ) } ` , async ( ) => {
149+ it ( `uses the same AO environment as chat for ${ JSON . stringify ( location ) } ` , async ( ) => {
144150 setMockIms ( { projectedProductContext : context ( location ) } ) ;
145- expect ( await checkCoworkerAccess ( ) ) . to . deep . equal ( { error : 'configuration' } ) ;
146- expect ( calls ) . to . have . length ( 0 ) ;
151+ expect ( await checkCoworkerAccess ( ) ) . to . deep . equal ( { allowed : true } ) ;
152+ expect ( calls [ 0 ] . url )
153+ . to . equal ( `https://agent-orchestrator-${ location . environment . toLowerCase ( ) } -can2.adobe.io/api/access` ) ;
147154 } ) ;
148155 } ) ;
149156
150157 it ( 'shares concurrent checks but does not cache completed decisions' , async ( ) => {
151158 const results = await Promise . all ( [ checkCoworkerAccess ( ) , checkCoworkerAccess ( ) ] ) ;
152159 expect ( results ) . to . deep . equal ( [ { allowed : true } , { allowed : true } ] ) ;
153160 expect ( calls ) . to . have . length ( 1 ) ;
154- body = response ( 'deny' ) ;
161+ body = response ( false ) ;
155162 expect ( await checkCoworkerAccess ( ) ) . to . deep . equal ( { allowed : false } ) ;
156163 expect ( calls ) . to . have . length ( 2 ) ;
157164 } ) ;
@@ -164,28 +171,44 @@ describe('checkCoworkerAccess', () => {
164171 expect ( calls ) . to . have . length ( 2 ) ;
165172 } ) ;
166173
167- it ( 'does not share in-flight checks across organizations or tokens ' , async ( ) => {
174+ it ( 'separates in-flight checks by org, token, AO region, and IMS client ' , async ( ) => {
168175 const pending = [ ] ;
176+ const started = [ ] ;
177+ const nextRequest = ( ) => new Promise ( ( resolve ) => { started . push ( resolve ) ; } ) ;
169178 window . fetch = ( url , opts ) => {
170179 calls . push ( { url, opts } ) ;
180+ started . shift ( ) ( ) ;
171181 return new Promise ( ( resolve ) => { pending . push ( resolve ) ; } ) ;
172182 } ;
183+ const firstStarted = nextRequest ( ) ;
173184 const first = checkCoworkerAccess ( ) ;
174- await Promise . resolve ( ) ;
185+ await firstStarted ;
175186 setMockIms ( { projectedProductContext : context ( { org : 'other@AdobeOrg' } ) } ) ;
187+ const secondStarted = nextRequest ( ) ;
176188 const second = checkCoworkerAccess ( ) ;
177- await Promise . resolve ( ) ;
189+ await secondStarted ;
178190 setMockIms ( { token : { token : 'refreshed-token' } } ) ;
191+ const thirdStarted = nextRequest ( ) ;
179192 const third = checkCoworkerAccess ( ) ;
180- await Promise . resolve ( ) ;
181- expect ( calls ) . to . have . length ( 3 ) ;
182- pending . forEach ( ( resolve , index ) => {
183- const result = response ( 'permit' , {
184- resource : { uri : JSON . parse ( calls [ index ] . opts . body ) . requests [ 0 ] . resource . uri } ,
185- } ) ;
186- resolve ( new Response ( JSON . stringify ( result ) ) ) ;
187- } ) ;
188- expect ( await Promise . all ( [ first , second , third ] ) )
189- . to . deep . equal ( [ { allowed : true } , { allowed : true } , { allowed : true } ] ) ;
193+ await thirdStarted ;
194+ setMockIms ( { projectedProductContext : context ( { org : 'other@AdobeOrg' , region : 'NEW1' } ) } ) ;
195+ const fourthStarted = nextRequest ( ) ;
196+ const fourth = checkCoworkerAccess ( ) ;
197+ await fourthStarted ;
198+ setMockIms ( { imsClientId : 'another-client' } ) ;
199+ const fifthStarted = nextRequest ( ) ;
200+ const fifth = checkCoworkerAccess ( ) ;
201+ await fifthStarted ;
202+ expect ( calls ) . to . have . length ( 5 ) ;
203+ expect ( calls [ 4 ] . opts . headers [ 'x-api-key' ] ) . to . equal ( 'another-client' ) ;
204+ pending . forEach ( ( resolve ) => resolve ( new Response ( JSON . stringify ( response ( ) ) ) ) ) ;
205+ expect ( await Promise . all ( [ first , second , third , fourth , fifth ] ) )
206+ . to . deep . equal ( [
207+ { allowed : true } ,
208+ { allowed : true } ,
209+ { allowed : true } ,
210+ { allowed : true } ,
211+ { allowed : true } ,
212+ ] ) ;
190213 } ) ;
191214} ) ;
0 commit comments