Skip to content

Commit ea1980b

Browse files
committed
test(sim-cli): assert declared-operation routing at the transport
1 parent 2c70a71 commit ea1980b

1 file changed

Lines changed: 39 additions & 21 deletions

File tree

‎packages/sim-cli/src/runtime/called-operations.test.ts‎

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,60 @@
11
import { readdirSync, readFileSync } from 'node:fs'
22
import { join, relative } from 'node:path'
3-
import { describe, expect, it, vi } from 'vitest'
3+
import { describe, expect, it } from 'vitest'
4+
import type { V2OperationName } from '../generated/v2-api'
5+
import { SimClient } from '../http/client'
46
import { OperationClient } from './called-operations'
57

8+
interface SentRequest {
9+
method: string
10+
url: string
11+
}
12+
13+
/** A real client whose transport records what reaches the wire. */
14+
function wireClient(operations: V2OperationName[], sent: SentRequest[]) {
15+
const http = new SimClient({
16+
name: 'fixture',
17+
authProfile: 'fixture',
18+
endpoint: 'https://sim.example',
19+
apiKey: 'fixture-key',
20+
oauth: null,
21+
workspaceId: 'ws-1',
22+
output: 'json',
23+
sources: { endpoint: 'flag', credential: 'flag', workspaceId: 'flag', output: 'flag' },
24+
transport: async (input, init) => {
25+
sent.push({ method: init?.method ?? 'GET', url: String(input) })
26+
return new Response(JSON.stringify({ data: {} }), {
27+
headers: { 'content-type': 'application/json' },
28+
})
29+
},
30+
})
31+
return new OperationClient(http, new Set(operations), 'sim secrets set')
32+
}
33+
634
describe('a declared operation client', () => {
735
/**
836
* The generated handler reaches its client through the command, so its calls are typed
937
* as any operation. A call outside the declaration has to be refused before it goes
1038
* out, or the inventory would describe a command that calls more than it says.
1139
*/
1240
it('refuses an operation its command did not declare, before any request', async () => {
13-
const request = vi.fn()
14-
const client = new OperationClient(
15-
{ request, requestRaw: vi.fn(), requireWorkspace: vi.fn() },
16-
new Set(['getMeta']),
17-
'sim meta status'
18-
)
41+
const sent: SentRequest[] = []
1942

20-
await expect(client.request('listWorkspaces')).rejects.toThrow(
21-
'"sim meta status" calls listWorkspaces, which it does not declare'
43+
await expect(wireClient(['setSecret'], sent).request('listWorkspaces')).rejects.toThrow(
44+
'"sim secrets set" calls listWorkspaces, which it does not declare'
2245
)
23-
expect(request).not.toHaveBeenCalled()
46+
expect(sent).toEqual([])
2447
})
2548

26-
it('addresses a declared operation by its route and method, encoding each id', async () => {
27-
const request = vi.fn().mockResolvedValue({ data: {} })
28-
const client = new OperationClient(
29-
{ request, requestRaw: vi.fn(), requireWorkspace: vi.fn() },
30-
new Set(['setSecret']),
31-
'sim secrets set'
32-
)
33-
34-
await client.request('setSecret', { params: { name: 'a/b?c' }, body: { value: 'x' } })
49+
it('sends a declared operation to its route with its method, encoding each id', async () => {
50+
const sent: SentRequest[] = []
3551

36-
expect(request).toHaveBeenCalledWith('/api/v2/secrets/a%2Fb%3Fc', {
37-
method: 'PUT',
52+
await wireClient(['setSecret'], sent).request('setSecret', {
53+
params: { name: 'a/b?c' },
3854
body: { value: 'x' },
3955
})
56+
57+
expect(sent).toEqual([{ method: 'PUT', url: 'https://sim.example/api/v2/secrets/a%2Fb%3Fc' }])
4058
})
4159
})
4260

0 commit comments

Comments
 (0)