@@ -10,6 +10,9 @@ import type { License } from '@/license';
1010import { AiGatewayService } from '@/services/ai-gateway.service' ;
1111import type { Project , User , UserRepository } from '@n8n/db' ;
1212import type { OwnershipService } from '@/services/ownership.service' ;
13+ import type { UrlService } from '@/services/url.service' ;
14+
15+ const INSTANCE_BASE_URL = 'https://my-n8n.example.com' ;
1316
1417const BASE_URL = 'http://gateway.test' ;
1518const INSTANCE_ID = 'test-instance-id' ;
@@ -34,6 +37,9 @@ function makeService({
3437 isAiGatewayLicensed = true ,
3538 ownershipService = mock < OwnershipService > ( ) ,
3639 userRepository = mock < UserRepository > ( { findOneBy : jest . fn ( ) . mockResolvedValue ( null ) } ) ,
40+ urlService = mock < UrlService > ( {
41+ getInstanceBaseUrl : jest . fn ( ) . mockReturnValue ( INSTANCE_BASE_URL ) ,
42+ } ) ,
3743} = { } ) {
3844 const globalConfig = {
3945 aiAssistant : { baseUrl : baseUrl ?? undefined } ,
@@ -53,6 +59,7 @@ function makeService({
5359 instanceSettings ,
5460 ownershipService ,
5561 userRepository ,
62+ urlService ,
5663 ) ;
5764}
5865
@@ -189,11 +196,21 @@ describe('AiGatewayService', () => {
189196 'x-n8n-version' : N8N_VERSION ,
190197 'x-instance-id' : INSTANCE_ID ,
191198 } ,
192- body : JSON . stringify ( { licenseCert : LICENSE_CERT } ) ,
199+ body : JSON . stringify ( { licenseCert : LICENSE_CERT , instanceUrl : INSTANCE_BASE_URL } ) ,
193200 } ) ,
194201 ) ;
195202 } ) ;
196203
204+ it ( 'includes instanceUrl in token body' , async ( ) => {
205+ mockConfigThenToken ( fetchMock ) ;
206+ const service = makeService ( ) ;
207+
208+ await service . getSyntheticCredential ( { credentialType : 'googlePalmApi' , userId : USER_ID } ) ;
209+
210+ const body = JSON . parse ( fetchMock . mock . calls [ 1 ] [ 1 ] . body as string ) ;
211+ expect ( body . instanceUrl ) . toBe ( INSTANCE_BASE_URL ) ;
212+ } ) ;
213+
197214 it ( 'includes userEmail and userName in token body when user exists' , async ( ) => {
198215 const userRepository = mock < UserRepository > ( {
199216 findOneBy : jest
@@ -215,6 +232,7 @@ describe('AiGatewayService', () => {
215232 licenseCert : LICENSE_CERT ,
216233 userEmail : 'alice@example.com' ,
217234 userName : 'Alice Smith' ,
235+ instanceUrl : INSTANCE_BASE_URL ,
218236 } ) ,
219237 } ) ,
220238 ) ;
@@ -245,7 +263,7 @@ describe('AiGatewayService', () => {
245263 await service . getSyntheticCredential ( { credentialType : 'googlePalmApi' , userId : USER_ID } ) ;
246264
247265 const body = JSON . parse ( fetchMock . mock . calls [ 1 ] [ 1 ] . body as string ) ;
248- expect ( body ) . toEqual ( { licenseCert : LICENSE_CERT } ) ;
266+ expect ( body ) . toEqual ( { licenseCert : LICENSE_CERT , instanceUrl : INSTANCE_BASE_URL } ) ;
249267 } ) ;
250268
251269 it ( 'caches config and token — second call makes no additional fetches' , async ( ) => {
@@ -416,7 +434,7 @@ describe('AiGatewayService', () => {
416434 `${ BASE_URL } /v1/gateway/credentials` ,
417435 expect . objectContaining ( {
418436 method : 'POST' ,
419- body : JSON . stringify ( { licenseCert : LICENSE_CERT } ) ,
437+ body : JSON . stringify ( { licenseCert : LICENSE_CERT , instanceUrl : INSTANCE_BASE_URL } ) ,
420438 } ) ,
421439 ) ;
422440 expect ( fetchMock ) . toHaveBeenNthCalledWith ( 2 , `${ BASE_URL } /v1/gateway/wallet` , {
0 commit comments