11import * as assert from 'node:assert' ;
22import { beforeEach , describe , it , mock } from 'node:test' ;
33
4+ import type { IRead , IModify , IHttp , IPersistence } from '@rocket.chat/apps-engine/definition/accessors' ;
5+ import type { IApiRequest , IApiEndpointInfo , IApiResponse } from '@rocket.chat/apps-engine/definition/api' ;
46import type { IApiEndpoint } from '@rocket.chat/apps-engine/definition/api/IApiEndpoint' ;
57import { JsonRpcError } from 'jsonrpc-lite' ;
68
@@ -9,13 +11,71 @@ import apiHandler from '../api-handler';
911import { createMockRequest } from './helpers/mod' ;
1012
1113describe ( 'handlers > api' , ( ) => {
12- const mockEndpoint : IApiEndpoint = {
14+ const mockEndpoint : Required < Omit < IApiEndpoint , 'delete' > > = {
1315 path : '/test' ,
14- get : ( request : any , endpoint : any , read : any , modify : any , http : any , persis : any ) => Promise . resolve ( 'ok' ) ,
15- post : ( request : any , endpoint : any , read : any , modify : any , http : any , persis : any ) => Promise . resolve ( 'ok' ) ,
16- put : ( request : any , endpoint : any , read : any , modify : any , http : any , persis : any ) => {
16+ examples : { } ,
17+ authRequired : false ,
18+ _availableMethods : [ ] ,
19+ get (
20+ _request : IApiRequest ,
21+ _endpoint : IApiEndpointInfo ,
22+ _read : IRead ,
23+ _modify : IModify ,
24+ _http : IHttp ,
25+ _persis : IPersistence ,
26+ ) : Promise < IApiResponse > {
27+ return Promise . resolve ( { status : 200 } ) ;
28+ } ,
29+ post (
30+ _request : IApiRequest ,
31+ _endpoint : IApiEndpointInfo ,
32+ _read : IRead ,
33+ _modify : IModify ,
34+ _http : IHttp ,
35+ _persis : IPersistence ,
36+ ) : Promise < IApiResponse > {
37+ return Promise . resolve ( { status : 200 } ) ;
38+ } ,
39+ put (
40+ _request : IApiRequest ,
41+ _endpoint : IApiEndpointInfo ,
42+ _read : IRead ,
43+ _modify : IModify ,
44+ _http : IHttp ,
45+ _persis : IPersistence ,
46+ ) : Promise < IApiResponse > {
1747 throw new Error ( 'Method execution error example' ) ;
1848 } ,
49+ head (
50+ _request : IApiRequest ,
51+ _endpoint : IApiEndpointInfo ,
52+ _read : IRead ,
53+ _modify : IModify ,
54+ _http : IHttp ,
55+ _persis : IPersistence ,
56+ ) : Promise < IApiResponse > {
57+ throw new Error ( 'Function not implemented.' ) ;
58+ } ,
59+ options (
60+ _request : IApiRequest ,
61+ _endpoint : IApiEndpointInfo ,
62+ _read : IRead ,
63+ _modify : IModify ,
64+ _http : IHttp ,
65+ _persis : IPersistence ,
66+ ) : Promise < IApiResponse > {
67+ throw new Error ( 'Function not implemented.' ) ;
68+ } ,
69+ patch (
70+ _request : IApiRequest ,
71+ _endpoint : IApiEndpointInfo ,
72+ _read : IRead ,
73+ _modify : IModify ,
74+ _http : IHttp ,
75+ _persis : IPersistence ,
76+ ) : Promise < IApiResponse > {
77+ throw new Error ( 'Function not implemented.' ) ;
78+ } ,
1979 } ;
2080
2181 beforeEach ( ( ) => {
@@ -28,7 +88,7 @@ describe('handlers > api', () => {
2888
2989 const result = await apiHandler ( createMockRequest ( { method : 'api:/test:get' , params : [ 'request' , 'endpointInfo' ] } ) ) ;
3090
31- assert . deepStrictEqual ( result , 'ok' ) ;
91+ assert . deepStrictEqual ( result , { status : 200 } ) ;
3292 assert . deepStrictEqual ( _spy . mock . calls [ 0 ] . arguments . length , 6 ) ;
3393 assert . deepStrictEqual ( _spy . mock . calls [ 0 ] . arguments [ 0 ] , 'request' ) ;
3494 assert . deepStrictEqual ( _spy . mock . calls [ 0 ] . arguments [ 1 ] , 'endpointInfo' ) ;
@@ -41,7 +101,7 @@ describe('handlers > api', () => {
41101
42102 const result = await apiHandler ( createMockRequest ( { method : 'api:/test:post' , params : [ 'request' , 'endpointInfo' ] } ) ) ;
43103
44- assert . deepStrictEqual ( result , 'ok' ) ;
104+ assert . deepStrictEqual ( result , { status : 200 } ) ;
45105 assert . deepStrictEqual ( _spy . mock . calls [ 0 ] . arguments . length , 6 ) ;
46106 assert . deepStrictEqual ( _spy . mock . calls [ 0 ] . arguments [ 0 ] , 'request' ) ;
47107 assert . deepStrictEqual ( _spy . mock . calls [ 0 ] . arguments [ 1 ] , 'endpointInfo' ) ;
@@ -53,8 +113,8 @@ describe('handlers > api', () => {
53113 const result = await apiHandler ( createMockRequest ( { method : `api:/test:delete` , params : [ 'request' , 'endpointInfo' ] } ) ) ;
54114
55115 assert . ok ( result instanceof JsonRpcError , `Expected instance of ${ JsonRpcError . name } ` ) ;
56- assert . strictEqual ( ( result as any ) . message , `/test's delete not exists` ) ;
57- assert . strictEqual ( ( result as any ) . code , - 32000 ) ;
116+ assert . strictEqual ( result . message , `/test's delete not exists` ) ;
117+ assert . strictEqual ( result . code , - 32000 ) ;
58118 } ) ;
59119
60120 it ( 'correctly handles an error if endpoint not exists' , async ( ) => {
@@ -74,9 +134,11 @@ describe('handlers > api', () => {
74134 } ) ;
75135
76136 it ( 'correctly handles dynamic paths with parameters (e.g., webhook/:event)' , async ( ) => {
77- const mockDynamicEndpoint : IApiEndpoint = {
137+ const mockDynamicEndpoint = {
138+ ...mockEndpoint ,
78139 path : 'webhook/:event' ,
79- post : ( request : any , endpoint : any , read : any , modify : any , http : any , persis : any ) => Promise . resolve ( 'webhook handled' ) ,
140+ post : ( _request : any , _endpoint : any , _read : any , _modify : any , _http : any , _persis : any ) =>
141+ Promise . resolve ( 'webhook handled' as any ) ,
80142 } ;
81143
82144 AppObjectRegistry . set ( 'api:webhook/:event' , mockDynamicEndpoint ) ;
@@ -94,9 +156,10 @@ describe('handlers > api', () => {
94156 } ) ;
95157
96158 it ( 'correctly handles paths with multiple segments and colons' , async ( ) => {
97- const mockComplexEndpoint : IApiEndpoint = {
159+ const mockComplexEndpoint = {
160+ ...mockEndpoint ,
98161 path : 'api/v1/:resource/:id' ,
99- get : ( request : any , endpoint : any , read : any , modify : any , http : any , persis : any ) => Promise . resolve ( 'complex path' ) ,
162+ get : ( _request : any , _endpoint : any , _read : any , _modify : any , _http : any , _persis : any ) => Promise . resolve ( { status : 201 } ) ,
100163 } ;
101164
102165 AppObjectRegistry . set ( 'api:api/v1/:resource/:id' , mockComplexEndpoint ) ;
@@ -105,7 +168,7 @@ describe('handlers > api', () => {
105168
106169 const result = await apiHandler ( createMockRequest ( { method : 'api:api/v1/:resource/:id:get' , params : [ 'request' , 'endpointInfo' ] } ) ) ;
107170
108- assert . deepStrictEqual ( result , 'complex path' ) ;
171+ assert . deepStrictEqual ( result , { status : 201 } ) ;
109172 assert . deepStrictEqual ( _spy . mock . calls [ 0 ] . arguments . length , 6 ) ;
110173
111174 _spy . mock . restore ( ) ;
0 commit comments