@@ -5,55 +5,82 @@ import { SeamHttp } from '@seamapi/http/connect'
5
5
6
6
import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js'
7
7
8
- test ( 'returns a SeamHttpRequest' , async ( t ) => {
8
+ test ( 'SeamHttp: returns a SeamHttpRequest' , async ( t ) => {
9
9
const { seed, endpoint } = await getTestServer ( t )
10
10
const seam = SeamHttp . fromApiKey ( seed . seam_apikey1_token , { endpoint } )
11
11
12
- const deviceRequest = seam . devices . get ( { device_id : seed . august_device_1 } )
12
+ const request = seam . devices . get ( { device_id : seed . august_device_1 } )
13
13
14
- t . true ( deviceRequest instanceof SeamHttpRequest )
15
- t . is ( deviceRequest . url . pathname , '/devices/get' )
16
- t . deepEqual ( deviceRequest . body , {
14
+ t . true ( request instanceof SeamHttpRequest )
15
+ t . truthy ( request . url )
16
+ t . is ( request . responseKey , 'device' )
17
+ t . deepEqual ( request . body , {
17
18
device_id : seed . august_device_1 ,
18
19
} )
19
- t . is ( deviceRequest . responseKey , 'device' )
20
- const device = await deviceRequest
20
+
21
+ const device = await request
21
22
t . is ( device . workspace_id , seed . seed_workspace_1 )
22
23
t . is ( device . device_id , seed . august_device_1 )
23
24
24
25
// Ensure that the type of the response is correct.
25
- type Expected = ResponseFromSeamHttpRequest < typeof deviceRequest >
26
-
26
+ type Expected = ResponseFromSeamHttpRequest < typeof request >
27
27
const validDeviceType : Expected [ 'device_type' ] = 'august_lock'
28
28
t . truthy ( validDeviceType )
29
29
30
- // @ts -expect-error because it's an invalid device type.
30
+ // @ts -expect-error invalid device type.
31
31
const invalidDeviceType : Expected [ 'device_type' ] = 'invalid_device_type'
32
32
t . truthy ( invalidDeviceType )
33
33
} )
34
34
35
- test ( "populates SeamHttpRequest's url property" , async ( t ) => {
35
+ test ( 'SeamHttpRequest: url is a URL for post requests' , async ( t ) => {
36
36
const { seed, endpoint } = await getTestServer ( t )
37
37
const seam = SeamHttp . fromApiKey ( seed . seam_apikey1_token , { endpoint } )
38
38
39
- const deviceRequest = seam . devices . get ( { device_id : 'abc123' } )
39
+ const { url } = seam . devices . get ( { device_id : 'abc123' } )
40
+
41
+ t . true ( url instanceof URL )
42
+ t . deepEqual (
43
+ toPlainUrlObject ( url ) ,
44
+ toPlainUrlObject ( new URL ( `${ endpoint } /devices/get` ) ) ,
45
+ )
46
+ } )
40
47
41
- t . is ( deviceRequest . url . pathname , '/devices/get' )
42
- t . is ( deviceRequest . url . search , '' )
48
+ test ( 'SeamHttpRequest: url is a URL for get requests' , async ( t ) => {
49
+ const { seed, endpoint } = await getTestServer ( t )
50
+ const seam = SeamHttp . fromApiKey ( seed . seam_apikey1_token , { endpoint } )
43
51
44
- const connectWebviewsViewRequest = seam . connectWebviews . view ( {
45
- connect_webview_id : 'abc123 ' ,
46
- auth_token : 'invalid ' ,
52
+ const { url } = seam . connectWebviews . view ( {
53
+ connect_webview_id : 'connect_webview_1 ' ,
54
+ auth_token : 'auth_token_1 ' ,
47
55
} )
48
56
49
- t . is ( connectWebviewsViewRequest . url . pathname , '/connect_webviews/view' )
50
- t . is ( connectWebviewsViewRequest . url . searchParams . get ( 'auth_token' ) , 'invalid' )
51
- t . is (
52
- connectWebviewsViewRequest . url . searchParams . get ( 'connect_webview_id' ) ,
53
- 'abc123' ,
57
+ t . true ( url instanceof URL )
58
+ t . deepEqual (
59
+ toPlainUrlObject ( url ) ,
60
+ toPlainUrlObject (
61
+ new URL (
62
+ `${ endpoint } /connect_webviews/view?auth_token=auth_token_1&connect_webview_id=connect_webview_1` ,
63
+ ) ,
64
+ ) ,
54
65
)
55
66
} )
56
67
68
+ const toPlainUrlObject = ( url : URL ) : Omit < URL , 'searchParams' | 'toJSON' > => {
69
+ return {
70
+ pathname : url . pathname ,
71
+ hash : url . hash ,
72
+ hostname : url . hostname ,
73
+ protocol : url . protocol ,
74
+ username : url . username ,
75
+ port : url . port ,
76
+ password : url . password ,
77
+ host : url . host ,
78
+ href : url . href ,
79
+ origin : url . origin ,
80
+ search : url . search ,
81
+ }
82
+ }
83
+
57
84
type ResponseFromSeamHttpRequest < T > =
58
85
T extends SeamHttpRequest < any , infer TResponse , infer TResponseKey >
59
86
? TResponseKey extends keyof TResponse
0 commit comments