Repository navigation
Expand file tree
/
Copy pathcalled-operations.test.ts
More file actions
90 lines (82 loc) · 3.5 KB
/
Copy pathcalled-operations.test.ts
File metadata and controls
90 lines (82 loc) · 3.5 KB
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
import { readdirSync, readFileSync } from 'node:fs'
import { join, relative } from 'node:path'
import { describe, expect, it } from 'vitest'
import type { V2OperationName } from '../generated/v2-api'
import { SimClient } from '../http/client'
import { OperationClient } from './called-operations'
interface SentRequest {
method: string
url: string
}
/** A real client whose transport records what reaches the wire. */
function wireClient(operations: V2OperationName[], sent: SentRequest[]) {
const http = new SimClient({
name: 'fixture',
authProfile: 'fixture',
endpoint: 'https://sim.example',
apiKey: 'fixture-key',
oauth: null,
workspaceId: 'ws-1',
output: 'json',
sources: { endpoint: 'flag', credential: 'flag', workspaceId: 'flag', output: 'flag' },
transport: async (input, init) => {
sent.push({ method: init?.method ?? 'GET', url: String(input) })
return new Response(JSON.stringify({ data: {} }), {
headers: { 'content-type': 'application/json' },
})
},
})
return new OperationClient(http, new Set(operations), 'sim secrets set')
}
describe('a declared operation client', () => {
/**
* The generated handler reaches its client through the command, so its calls are typed
* as any operation. A call outside the declaration has to be refused before it goes
* out, or the inventory would describe a command that calls more than it says.
*/
it('refuses an operation its command did not declare, before any request', async () => {
const sent: SentRequest[] = []
await expect(wireClient(['setSecret'], sent).request('listWorkspaces')).rejects.toThrow(
'"sim secrets set" calls listWorkspaces, which it does not declare'
)
expect(sent).toEqual([])
})
it('sends a declared operation to its route with its method, encoding each id', async () => {
const sent: SentRequest[] = []
await wireClient(['setSecret'], sent).request('setSecret', {
params: { name: 'a/b?c' },
body: { value: 'x' },
})
expect(sent).toEqual([{ method: 'PUT', url: 'https://sim.example/api/v2/secrets/a%2Fb%3Fc' }])
})
})
/**
* The inventory is only as true as the declarations, and a declaration only binds a
* command that cannot reach the API another way. A raw client is the one way around
* it, so building one stays confined to the places that wrap it in a declaration.
*/
it('leaves no command a raw client to call the API with', () => {
const root = join(import.meta.dirname, '..')
const construct = 'new SimClient('
const connectRaw = 'clientFrom('
const allowed: Record<string, string[]> = {
'context.ts': [construct, connectRaw],
'runtime/called-operations.ts': [connectRaw],
// The public embed API hands server code a client of its own; no command uses it.
'embed.ts': [construct],
// Rebuilt for cleanup through `over`, which keeps the session's declaration.
'transfer/upload-session.ts': [construct],
}
const raw: string[] = []
for (const entry of readdirSync(root, { recursive: true, withFileTypes: true })) {
if (!entry.isFile() || !entry.name.endsWith('.ts') || entry.name.endsWith('.test.ts')) continue
const file = join(entry.parentPath, entry.name)
const name = relative(root, file)
if (name.startsWith('test/') || name.startsWith('generated/')) continue
const text = readFileSync(file, 'utf8')
for (const call of [construct, connectRaw]) {
if (text.includes(call) && !allowed[name]?.includes(call)) raw.push(`${name}: ${call}`)
}
}
expect(raw).toEqual([])
})