@@ -12,6 +12,8 @@ describe('SuperfaceClient', () => {
1212
1313 beforeEach ( ( ) => {
1414 fetchSpy = jest . spyOn ( global , 'fetch' ) . mockResolvedValue ( {
15+ ok : true ,
16+ status : 200 ,
1517 json : ( ) =>
1618 Promise . resolve ( [
1719 {
@@ -50,6 +52,92 @@ describe('SuperfaceClient', () => {
5052 expect ( fetchSpy ) . toHaveBeenCalledTimes ( 2 ) ;
5153 jest . useRealTimers ( ) ;
5254 } ) ;
55+
56+ it ( 'throws when user is not authenticated' , async ( ) => {
57+ fetchSpy . mockReset ( ) . mockResolvedValue ( {
58+ ok : false ,
59+ status : 401 ,
60+ json : ( ) =>
61+ Promise . resolve ( {
62+ title : 'Unauthorized' ,
63+ detail : `Toolhub URL (https://pod.superface.ai) for Function Descriptors is unauthorized` ,
64+ } ) ,
65+ } as Response ) ;
66+
67+ const client = new Superface ( { apiKey : 'stub' , cacheTimeout : 50 } ) ;
68+
69+ expect ( client . getTools ( ) ) . rejects . toThrow ( SuperfaceError ) ;
70+ } ) ;
71+ } ) ;
72+
73+ describe ( 'runTool' , ( ) => {
74+ let fetchSpy : jest . SpyInstance ;
75+
76+ beforeEach ( ( ) => {
77+ fetchSpy = jest . spyOn ( global , 'fetch' ) . mockResolvedValue ( {
78+ ok : true ,
79+ status : 200 ,
80+ json : ( ) =>
81+ Promise . resolve ( [
82+ {
83+ type : 'function' ,
84+ function : {
85+ name : 'stub' ,
86+ description : 'description' ,
87+ parameters : { } ,
88+ } ,
89+ } ,
90+ ] ) ,
91+ } as Response ) ;
92+ } ) ;
93+
94+ afterEach ( ( ) => {
95+ fetchSpy . mockRestore ( ) ;
96+ } ) ;
97+
98+ it ( 'throws when user is not authenticated' , async ( ) => {
99+ fetchSpy . mockReset ( ) . mockResolvedValue ( {
100+ ok : false ,
101+ status : 401 ,
102+ json : ( ) =>
103+ Promise . resolve ( {
104+ title : 'Unauthorized' ,
105+ detail : `Toolhub URL (https://pod.superface.ai) for Function Descriptors is unauthorized` ,
106+ } ) ,
107+ } as Response ) ;
108+
109+ const client = new Superface ( { apiKey : 'stub' } ) ;
110+
111+ expect (
112+ client . runTool ( { userId : 'example_user' , name : 'stub' , args : { } } )
113+ ) . rejects . toThrow ( SuperfaceError ) ;
114+ } ) ;
115+ } ) ;
116+
117+ describe ( 'linkToUserConnections' , ( ) => {
118+ let fetchSpy : jest . SpyInstance ;
119+
120+ afterEach ( ( ) => {
121+ fetchSpy . mockRestore ( ) ;
122+ } ) ;
123+
124+ it ( 'throws when user is not authenticated' , async ( ) => {
125+ fetchSpy = jest . spyOn ( global , 'fetch' ) . mockResolvedValue ( {
126+ ok : false ,
127+ status : 401 ,
128+ json : ( ) =>
129+ Promise . resolve ( {
130+ title : 'Unauthorized' ,
131+ detail : `Toolhub URL (https://pod.superface.ai) for Function Descriptors is unauthorized` ,
132+ } ) ,
133+ } as Response ) ;
134+
135+ const client = new Superface ( { apiKey : 'stub' } ) ;
136+
137+ expect (
138+ client . linkToUserConnections ( { userId : 'example_user' } )
139+ ) . rejects . toThrow ( SuperfaceError ) ;
140+ } ) ;
53141 } ) ;
54142} ) ;
55143
0 commit comments