Skip to content

Commit 10cb4b3

Browse files
authored
Merge pull request #13 from seamapi/test-retry
2 parents b7f0e80 + bae8de6 commit 10cb4b3

File tree

5 files changed

+137
-7
lines changed

5 files changed

+137
-7
lines changed

package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@
8888
"axios-retry": "^3.8.0"
8989
},
9090
"devDependencies": {
91-
"@seamapi/fake-seam-connect": "^1.21.0",
91+
"@seamapi/fake-seam-connect": "^1.22.0",
9292
"@seamapi/types": "^1.24.0",
9393
"@types/eslint": "^8.44.2",
9494
"@types/node": "^18.11.18",

test/fixtures/seam/connect/api.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,18 @@
1-
import { createFake, type Seed } from '@seamapi/fake-seam-connect'
1+
import {
2+
createFake,
3+
type Database,
4+
type Seed,
5+
} from '@seamapi/fake-seam-connect'
26
import type { ExecutionContext } from 'ava'
37
import fetch from 'node-fetch' // TODO: Remove node-fetch when Node v16 support is dropped.
48

59
export const getTestServer = async (
610
t: ExecutionContext,
7-
): Promise<{ endpoint: string; seed: Seed }> => {
11+
): Promise<{
12+
endpoint: string
13+
seed: Seed
14+
db: Database
15+
}> => {
816
const fake = await createFake()
917
const seed = await fake.seed()
1018

@@ -21,5 +29,6 @@ export const getTestServer = async (
2129
return {
2230
endpoint,
2331
seed,
32+
db: fake.database,
2433
}
2534
}

test/seam/connect/client.test.ts

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ import { getTestServer } from 'fixtures/seam/connect/api.js'
33

44
import { SeamHttp } from '@seamapi/http/connect'
55

6+
import type {
7+
DevicesGetResponse,
8+
DevicesListBody,
9+
DevicesListResponse,
10+
} from 'lib/seam/connect/routes/devices.js'
11+
612
test('SeamHttp: fromClient returns instance that uses client', async (t) => {
713
const { seed, endpoint } = await getTestServer(t)
814
const seam = SeamHttp.fromClient(
@@ -26,3 +32,77 @@ test('SeamHttp: constructor returns instance that uses client', async (t) => {
2632
t.is(device.workspace_id, seed.seed_workspace_1)
2733
t.is(device.device_id, seed.august_device_1)
2834
})
35+
36+
test('SeamHttp: can use client to make requests', async (t) => {
37+
const { seed, endpoint } = await getTestServer(t)
38+
const seam = new SeamHttp({
39+
client: SeamHttp.fromApiKey(seed.seam_apikey1_token, { endpoint }).client,
40+
})
41+
const {
42+
data: { device },
43+
status,
44+
} = await seam.client.get<DevicesGetResponse>('/devices/get', {
45+
params: { device_id: seed.august_device_1 },
46+
})
47+
t.is(status, 200)
48+
t.is(device.workspace_id, seed.seed_workspace_1)
49+
t.is(device.device_id, seed.august_device_1)
50+
})
51+
52+
test('SeamHttp: client serializes array params', async (t) => {
53+
const { seed, endpoint } = await getTestServer(t)
54+
const seam = new SeamHttp({
55+
client: SeamHttp.fromApiKey(seed.seam_apikey1_token, { endpoint }).client,
56+
})
57+
const params: DevicesListBody = {
58+
device_ids: [seed.august_device_1],
59+
}
60+
const {
61+
data: { devices },
62+
status,
63+
} = await seam.client.get<DevicesListResponse>('/devices/list', {
64+
params,
65+
})
66+
t.is(status, 200)
67+
t.is(devices.length, 1)
68+
const [device] = devices
69+
t.is(device?.workspace_id, seed.seed_workspace_1)
70+
t.is(device?.device_id, seed.august_device_1)
71+
})
72+
73+
test('SeamHttp: merges axiosOptions when creating client', async (t) => {
74+
const { seed, endpoint } = await getTestServer(t)
75+
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, {
76+
endpoint,
77+
axiosOptions: {
78+
transformResponse: [
79+
(data) =>
80+
JSON.parse(
81+
data.replaceAll(seed.august_device_1, 'transformed-device-id'),
82+
),
83+
],
84+
},
85+
})
86+
const device = await seam.devices.get({
87+
device_id: seed.august_device_1,
88+
})
89+
t.is(device.workspace_id, seed.seed_workspace_1)
90+
t.is(device.device_id, 'transformed-device-id')
91+
})
92+
93+
test('SeamHttp: merges axios headers when creating client', async (t) => {
94+
const { seed, endpoint } = await getTestServer(t)
95+
const seam = SeamHttp.fromApiKey('seam_invalidapikey_token', {
96+
endpoint,
97+
axiosOptions: {
98+
headers: {
99+
Authorization: `Bearer ${seed.seam_apikey1_token}`,
100+
},
101+
},
102+
})
103+
const device = await seam.devices.get({
104+
device_id: seed.august_device_1,
105+
})
106+
t.is(device.workspace_id, seed.seed_workspace_1)
107+
t.is(device.device_id, seed.august_device_1)
108+
})

test/seam/connect/retry.test.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import test from 'ava'
2+
import { AxiosError } from 'axios'
3+
import { getTestServer } from 'fixtures/seam/connect/api.js'
4+
5+
import { SeamHttp } from '@seamapi/http/connect'
6+
7+
test('SeamHttp: retries 503 status errors twice by default ', async (t) => {
8+
const { seed, endpoint, db } = await getTestServer(t)
9+
const expectedRetryCount = 2
10+
11+
db.simulateWorkspaceOutage(seed.seed_workspace_1, {
12+
routes: ['/devices/get'],
13+
})
14+
15+
t.plan(expectedRetryCount + 2)
16+
17+
const seam = SeamHttp.fromApiKey(seed.seam_apikey1_token, {
18+
endpoint,
19+
axiosRetryOptions: {
20+
onRetry: (retryCount) => {
21+
t.true(retryCount <= expectedRetryCount)
22+
},
23+
},
24+
})
25+
26+
const err = await t.throwsAsync(
27+
async () =>
28+
// UPSTREAM: This test should use seam.devices.get({ device_id: '...' }).
29+
// Only idempotent methods, e.g., GET not POST, are retried by default.
30+
// The SDK should use GET over POST once that method is supported upstream.
31+
// https://github.com/seamapi/nextlove/issues/117
32+
await seam.client.get('/devices/get', {
33+
params: {
34+
device_id: seed.august_device_1,
35+
},
36+
}),
37+
{ instanceOf: AxiosError },
38+
)
39+
40+
t.is(err?.response?.status, 503)
41+
})

0 commit comments

Comments
 (0)