|
1 | 1 | import { readdirSync, readFileSync } from 'node:fs' |
2 | 2 | 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' |
4 | 6 | import { OperationClient } from './called-operations' |
5 | 7 |
|
| 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 | + |
6 | 34 | describe('a declared operation client', () => { |
7 | 35 | /** |
8 | 36 | * The generated handler reaches its client through the command, so its calls are typed |
9 | 37 | * as any operation. A call outside the declaration has to be refused before it goes |
10 | 38 | * out, or the inventory would describe a command that calls more than it says. |
11 | 39 | */ |
12 | 40 | 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[] = [] |
19 | 42 |
|
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' |
22 | 45 | ) |
23 | | - expect(request).not.toHaveBeenCalled() |
| 46 | + expect(sent).toEqual([]) |
24 | 47 | }) |
25 | 48 |
|
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[] = [] |
35 | 51 |
|
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' }, |
38 | 54 | body: { value: 'x' }, |
39 | 55 | }) |
| 56 | + |
| 57 | + expect(sent).toEqual([{ method: 'PUT', url: 'https://sim.example/api/v2/secrets/a%2Fb%3Fc' }]) |
40 | 58 | }) |
41 | 59 | }) |
42 | 60 |
|
|
0 commit comments