-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathseam-http-request.test.ts
177 lines (149 loc) · 5.32 KB
/
seam-http-request.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import test from 'ava'
import { getTestServer } from 'fixtures/seam/connect/api.js'
import { SeamHttp } from '@seamapi/http/connect'
import { SeamHttpRequest } from 'lib/seam/connect/seam-http-request.js'
test('SeamHttp: returns a SeamHttpRequest', async (t) => {
const { seed, endpoint } = await getTestServer(t)
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { endpoint })
const request = seam.devices.get({ device_id: seed.august_device_1 })
t.true(request instanceof SeamHttpRequest)
t.truthy(request.url)
t.is(request.responseKey, 'device')
t.deepEqual(request.body, {
device_id: seed.august_device_1,
})
const device = await request.execute()
t.is(device.workspace_id, seed.seed_workspace_1)
t.is(device.device_id, seed.august_device_1)
type Expected = ResponseFromSeamHttpRequest<typeof request>
const validDeviceType: Expected['device_type'] = 'august_lock'
t.truthy(validDeviceType)
// @ts-expect-error Test invalid device type.
const invalidDeviceType: Expected['device_type'] = 'invalid_device_type'
t.truthy(invalidDeviceType)
})
test('SeamHttpRequest: does not make the request until awaited', async (t) => {
const { seed, endpoint } = await getTestServer(t)
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { endpoint })
const request = seam.devices.get({ device_id: seed.august_device_1 })
const device = await request
t.is(device.workspace_id, seed.seed_workspace_1)
t.is(device.device_id, seed.august_device_1)
})
test('SeamHttpRequest: url is a URL for requests without query string', async (t) => {
const { seed, endpoint } = await getTestServer(t)
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { endpoint })
const { url } = seam.devices.list()
t.true(url instanceof URL)
t.deepEqual(
toPlainUrlObject(url),
toPlainUrlObject(new URL(`${endpoint}/devices/list`)),
)
})
// UPSTREAM: The Seam API does not yet consistently support GET requests, so only POST is used.
test.failing(
'SeamHttpRequest: url is a URL for requests with query string',
async (t) => {
const { seed, endpoint } = await getTestServer(t)
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, { endpoint })
const { url } = seam.devices.list({
limit: 10,
device_ids: [seed.august_device_1, seed.ecobee_device_1],
})
t.true(url instanceof URL)
t.deepEqual(
toPlainUrlObject(url),
toPlainUrlObject(
new URL(
`${endpoint}/devices/get?device_ids=${seed.august_device_1}&device_ids=${seed.ecobee_device_1}&limit=10`,
),
),
)
},
)
test('SeamHttpRequest: url is a URL when endpoint is a url without a path', async (t) => {
const { seed } = await getTestServer(t)
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, {
endpoint: 'https://example.com',
})
const { url } = seam.devices.get({ device_id: 'abc123' })
t.true(url instanceof URL)
t.deepEqual(
toPlainUrlObject(url),
toPlainUrlObject(new URL('https://example.com/devices/get')),
)
})
test('SeamHttpRequest: url is a URL when endpoint is a url with a path', async (t) => {
const { seed } = await getTestServer(t)
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, {
endpoint: 'https://example.com/some/sub/path',
})
const { url } = seam.devices.get({ device_id: 'abc123' })
t.true(url instanceof URL)
t.deepEqual(
toPlainUrlObject(url),
toPlainUrlObject(new URL('https://example.com/some/sub/path/devices/get')),
)
})
test.failing(
'SeamHttpRequest: url is a URL when endpoint is path',
async (t) => {
const { seed } = await getTestServer(t)
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, {
endpoint: '/some/sub/path',
})
const { url } = seam.devices.get({ device_id: 'abc123' })
t.true(url instanceof URL)
t.deepEqual(
toPlainUrlObject(url),
toPlainUrlObject(
new URL('https://example.com/some/sub/path/devices/get'),
),
)
},
)
test.failing(
'SeamHttpRequest: url is a URL when endpoint is empty',
async (t) => {
const { seed } = await getTestServer(t)
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, {
endpoint: '',
})
// TODO: Set globalThis.location.origin = 'https://example.com'
const { url } = seam.devices.get({ device_id: 'abc123' })
t.true(url instanceof URL)
t.deepEqual(
toPlainUrlObject(url),
toPlainUrlObject(new URL('https://example.com/devices/get')),
)
},
)
test('SeamHttpRequest: url throws if unable to resolve origin', async (t) => {
const { seed } = await getTestServer(t)
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, {
endpoint: '',
})
const request = seam.devices.get({ device_id: 'abc123' })
t.throws(() => request.url, { message: /Cannot resolve origin/ })
})
const toPlainUrlObject = (url: URL): Omit<URL, 'searchParams' | 'toJSON'> => {
return {
pathname: url.pathname,
hash: url.hash,
hostname: url.hostname,
protocol: url.protocol,
username: url.username,
port: url.port,
password: url.password,
host: url.host,
href: url.href,
origin: url.origin,
search: url.search,
}
}
type ResponseFromSeamHttpRequest<T> =
T extends SeamHttpRequest<infer TResponse, infer TResponseKey>
? TResponseKey extends keyof TResponse
? TResponse[TResponseKey]
: undefined
: never