|
| 1 | +import { describe, expect, it } from 'vitest' |
| 2 | +import { runEmbeddedCli } from '../embed' |
| 3 | + |
| 4 | +const identity = { endpoint: 'https://sim.test', apiKey: 'fixture', workspaceId: 'workspace' } |
| 5 | + |
| 6 | +/** Runs one embedded invocation, recording the path of each request it sends. */ |
| 7 | +async function run(argv: string[]) { |
| 8 | + const paths: string[] = [] |
| 9 | + const result = await runEmbeddedCli(argv, { |
| 10 | + ...identity, |
| 11 | + transport: async (url) => { |
| 12 | + paths.push(new URL(String(url)).pathname) |
| 13 | + return Response.json({ data: [] }) |
| 14 | + }, |
| 15 | + }) |
| 16 | + return { ...result, paths } |
| 17 | +} |
| 18 | + |
| 19 | +describe('a nested group that defaults to its list', () => { |
| 20 | + it('lists credential providers when the providers group is named, as list does', async () => { |
| 21 | + const bare = await run(['credentials', 'providers']) |
| 22 | + const listed = await run(['credentials', 'providers', 'list']) |
| 23 | + expect(bare.exitCode).toBe(0) |
| 24 | + expect(bare.paths).toEqual(['/api/v2/credentials/providers']) |
| 25 | + expect(listed.paths).toEqual(bare.paths) |
| 26 | + }) |
| 27 | + |
| 28 | + it('keeps each spelling’s own options: a leaf option reaches the leaf, the bare group takes it too', async () => { |
| 29 | + const searchOf = async (argv: string[]) => { |
| 30 | + const queries: string[] = [] |
| 31 | + await runEmbeddedCli(argv, { |
| 32 | + ...identity, |
| 33 | + transport: async (url) => { |
| 34 | + queries.push(new URL(String(url)).searchParams.get('search') ?? '') |
| 35 | + return Response.json({ data: [] }) |
| 36 | + }, |
| 37 | + }) |
| 38 | + return queries |
| 39 | + } |
| 40 | + expect(await searchOf(['credentials', 'providers', 'list', '--search', 'github'])).toEqual([ |
| 41 | + 'github', |
| 42 | + ]) |
| 43 | + expect(await searchOf(['credentials', 'providers', '--search', 'github'])).toEqual(['github']) |
| 44 | + }) |
| 45 | + |
| 46 | + it('leaves a group without a default printing its usage, sending nothing', async () => { |
| 47 | + const group = await run(['credentials']) |
| 48 | + expect(group.paths).toEqual([]) |
| 49 | + expect(group.stderr + group.stdout).toContain('Usage: sim credentials') |
| 50 | + }) |
| 51 | +}) |
0 commit comments