-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.test.ts
152 lines (142 loc) · 4.56 KB
/
client.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
import test from 'ava'
import { getTestServer } from 'fixtures/seam/connect/api.js'
import {
type DevicesGetResponse,
type DevicesListParams,
type DevicesListResponse,
SeamHttp,
SeamHttpMultiWorkspace,
type WorkspacesListResponse,
} from '@seamapi/http/connect'
test('SeamHttp: fromClient returns instance that uses client', async (t) => {
const { seed, endpoint } = await getTestServer(t)
const seam = SeamHttp.fromClient(
SeamHttp.fromApiKey(seed.seam_apikey1_token, { endpoint }).client,
)
const device = await seam.devices.get({
device_id: seed.august_device_1,
})
t.is(device.workspace_id, seed.seed_workspace_1)
t.is(device.device_id, seed.august_device_1)
})
test('SeamHttp: constructor returns instance that uses client', async (t) => {
const { seed, endpoint } = await getTestServer(t)
const seam = new SeamHttp({
client: SeamHttp.fromApiKey(seed.seam_apikey1_token, { endpoint }).client,
})
const device = await seam.devices.get({
device_id: seed.august_device_1,
})
t.is(device.workspace_id, seed.seed_workspace_1)
t.is(device.device_id, seed.august_device_1)
})
test('SeamHttp: can use client to make requests', async (t) => {
const { seed, endpoint } = await getTestServer(t)
const seam = new SeamHttp({
client: SeamHttp.fromApiKey(seed.seam_apikey1_token, { endpoint }).client,
})
const {
data: { device },
status,
} = await seam.client.get<DevicesGetResponse>('/devices/get', {
params: { device_id: seed.august_device_1 },
})
t.is(status, 200)
t.is(device.workspace_id, seed.seed_workspace_1)
t.is(device.device_id, seed.august_device_1)
})
test('SeamHttp: client serializes array params', async (t) => {
const { seed, endpoint } = await getTestServer(t)
const seam = new SeamHttp({
client: SeamHttp.fromApiKey(seed.seam_apikey1_token, { endpoint }).client,
})
const params: DevicesListParams = {
device_ids: [seed.august_device_1],
}
const {
data: { devices },
status,
} = await seam.client.get<DevicesListResponse>('/devices/list', {
params,
})
t.is(status, 200)
t.is(devices.length, 1)
const [device] = devices
t.is(device?.workspace_id, seed.seed_workspace_1)
t.is(device?.device_id, seed.august_device_1)
})
test('SeamHttp: merges axiosOptions when creating client', async (t) => {
const { seed, endpoint } = await getTestServer(t)
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, {
endpoint,
axiosOptions: {
transformResponse: [
(data: string) =>
JSON.parse(
data.replaceAll(seed.august_device_1, 'transformed-device-id'),
),
],
},
})
const device = await seam.devices.get({
device_id: seed.august_device_1,
})
t.is(device.workspace_id, seed.seed_workspace_1)
t.is(device.device_id, 'transformed-device-id')
})
test('SeamHttp: merges axios headers when creating client', async (t) => {
const { seed, endpoint } = await getTestServer(t)
const seam = SeamHttp.fromApiKey('seam_invalidapikey_token', {
endpoint,
axiosOptions: {
headers: {
Authorization: `Bearer ${seed.seam_apikey1_token}`,
},
},
})
const device = await seam.devices.get({
device_id: seed.august_device_1,
})
t.is(device.workspace_id, seed.seed_workspace_1)
t.is(device.device_id, seed.august_device_1)
})
test('SeamHttpMultiWorkspace: fromClient returns instance that uses client', async (t) => {
const { seed, endpoint } = await getTestServer(t)
const seam = SeamHttpMultiWorkspace.fromClient(
SeamHttpMultiWorkspace.fromPersonalAccessToken(seed.seam_at1_token, {
endpoint,
}).client,
)
const workspaces = await seam.workspaces.list()
t.true(workspaces.length > 0)
})
test('SeamHttpMultiWorkspace: constructor returns instance that uses client', async (t) => {
const { seed, endpoint } = await getTestServer(t)
const seam = new SeamHttpMultiWorkspace({
client: SeamHttpMultiWorkspace.fromPersonalAccessToken(
seed.seam_at1_token,
{
endpoint,
},
).client,
})
const workspaces = await seam.workspaces.list()
t.true(workspaces.length > 0)
})
test('SeamHttpMultiWorkspace: can use client to make requests', async (t) => {
const { seed, endpoint } = await getTestServer(t)
const seam = new SeamHttpMultiWorkspace({
client: SeamHttpMultiWorkspace.fromPersonalAccessToken(
seed.seam_at1_token,
{
endpoint,
},
).client,
})
const {
data: { workspaces },
status,
} = await seam.client.get<WorkspacesListResponse>('/workspaces/list')
t.is(status, 200)
t.true(workspaces.length > 0)
})