1- import { describe , test , expect , beforeEach , afterEach } from "vitest" ;
1+ import { describe , test , expect , beforeEach , afterEach , vi } from "vitest" ;
22import nock from "nock" ;
33import { createClient } from "../../src/index.ts" ;
44
@@ -14,6 +14,7 @@ declare module "../../src/modules/functions.types.ts" {
1414describe ( "Functions Module" , ( ) => {
1515 let base44 : ReturnType < typeof createClient > ;
1616 let scope ;
17+ let fetchMock : ReturnType < typeof vi . fn > ;
1718 const appId = "test-app-id" ;
1819 const serverUrl = "https://api.base44.com" ;
1920
@@ -33,13 +34,18 @@ describe("Functions Module", () => {
3334 console . log ( `Nock: No match for ${ req . method } ${ req . path } ` ) ;
3435 console . log ( "Headers:" , req . getHeaders ( ) ) ;
3536 } ) ;
37+
38+ fetchMock = vi . fn ( ) ;
39+ vi . stubGlobal ( "fetch" , fetchMock ) ;
3640 } ) ;
3741
3842 afterEach ( ( ) => {
3943 // Clean up any pending mocks
4044 nock . cleanAll ( ) ;
4145 nock . emitter . removeAllListeners ( "no match" ) ;
4246 nock . enableNetConnect ( ) ;
47+ vi . unstubAllGlobals ( ) ;
48+ vi . clearAllMocks ( ) ;
4349 } ) ;
4450
4551 test ( "should call a function with JSON data" , async ( ) => {
@@ -452,4 +458,74 @@ describe("Functions Module", () => {
452458 // Verify all mocks were called
453459 expect ( scope . isDone ( ) ) . toBe ( true ) ;
454460 } ) ;
461+
462+ test ( "should fetch function endpoint directly" , async ( ) => {
463+ fetchMock . mockResolvedValueOnce ( new Response ( "ok" , { status : 200 } ) ) ;
464+
465+ await base44 . functions . fetch ( "/my_function" , {
466+ method : "GET" ,
467+ } ) ;
468+
469+ expect ( fetchMock ) . toHaveBeenCalledTimes ( 1 ) ;
470+ expect ( fetchMock ) . toHaveBeenCalledWith (
471+ `${ serverUrl } /api/functions/my_function` ,
472+ expect . any ( Object )
473+ ) ;
474+ } ) ;
475+
476+
477+ test ( "should include Authorization header when using functions.fetch" , async ( ) => {
478+ const userToken = "user-streaming-token" ;
479+ const authenticatedBase44 = createClient ( {
480+ serverUrl,
481+ appId,
482+ token : userToken ,
483+ } ) ;
484+ fetchMock . mockResolvedValueOnce ( new Response ( "ok" , { status : 200 } ) ) ;
485+
486+ await authenticatedBase44 . functions . fetch ( "streaming_demo" , {
487+ method : "POST" ,
488+ body : JSON . stringify ( { mode : "text" } ) ,
489+ } ) ;
490+
491+ const requestInit = fetchMock . mock . calls [ 0 ] [ 1 ] ;
492+ const headers = new Headers ( requestInit . headers ) ;
493+ expect ( headers . get ( "Authorization" ) ) . toBe ( `Bearer ${ userToken } ` ) ;
494+ } ) ;
495+
496+ test ( "should normalize path with and without leading slash" , async ( ) => {
497+ // Test with leading slash
498+ fetchMock . mockResolvedValueOnce ( new Response ( "ok" , { status : 200 } ) ) ;
499+ await base44 . functions . fetch ( "/my_function" ) ;
500+ expect ( fetchMock ) . toHaveBeenCalledWith (
501+ `${ serverUrl } /api/functions/my_function` ,
502+ expect . any ( Object )
503+ ) ;
504+
505+ // Test without leading slash
506+ fetchMock . mockResolvedValueOnce ( new Response ( "ok" , { status : 200 } ) ) ;
507+ await base44 . functions . fetch ( "my_function" ) ;
508+ expect ( fetchMock ) . toHaveBeenCalledWith (
509+ `${ serverUrl } /api/functions/my_function` ,
510+ expect . any ( Object )
511+ ) ;
512+ } ) ;
513+
514+ test ( "should include service role Authorization header when using asServiceRole.functions.fetch" , async ( ) => {
515+ const serviceToken = "service-role-token" ;
516+ const serviceRoleBase44 = createClient ( {
517+ serverUrl,
518+ appId,
519+ serviceToken,
520+ } ) ;
521+ fetchMock . mockResolvedValueOnce ( new Response ( "ok" , { status : 200 } ) ) ;
522+
523+ await serviceRoleBase44 . asServiceRole . functions . fetch ( "/service_function" , {
524+ method : "GET" ,
525+ } ) ;
526+
527+ const requestInit = fetchMock . mock . calls [ 0 ] [ 1 ] ;
528+ const headers = new Headers ( requestInit . headers ) ;
529+ expect ( headers . get ( "Authorization" ) ) . toBe ( `Bearer ${ serviceToken } ` ) ;
530+ } ) ;
455531} ) ;
0 commit comments