|
1 | 1 | /** |
2 | 2 | * Entrypoint for the `bai-smoke` CLI. |
3 | 3 | * |
4 | | - * MVP scaffold (FR-2876): |
5 | | - * - `list` — print the catalog of smoke categories. Functional. |
6 | | - * - `version` — print CLI version + bundled SHA + Playwright version. Functional. |
7 | | - * - `run` — stub. The actual Playwright runner ships in FR-2877. |
| 4 | + * Subcommands (FR-2877 MVP): |
| 5 | + * - `list` — print the catalog of smoke categories. |
| 6 | + * - `version` — print CLI + WebUI + Playwright version info. |
| 7 | + * - `run` — execute the smoke suite against a customer endpoint. |
8 | 8 | * |
9 | | - * Global option parsing (`--endpoint`, `--email`, etc.) is declared on `run` |
10 | | - * so `--help` documents the eventual flag surface, but every flag is a no-op |
11 | | - * at this stage. |
| 9 | + * Phase 2 subcommands (`doctor`, `preflight`) ship under FR-2878+. |
12 | 10 | */ |
13 | | -import { Command } from 'commander'; |
| 11 | +import { Command, Option } from 'commander'; |
| 12 | +import path from 'node:path'; |
14 | 13 |
|
15 | 14 | import { SMOKE_CATALOG } from './catalog.js'; |
| 15 | +import { |
| 16 | + parseDuration, |
| 17 | + splitCsvArg, |
| 18 | + type SmokeRoleSelection, |
| 19 | + type SmokeRunOptions, |
| 20 | +} from './config.js'; |
| 21 | +import { runSmoke } from './runner.js'; |
16 | 22 | import { |
17 | 23 | CLI_NAME, |
18 | 24 | CLI_VERSION, |
@@ -61,39 +67,147 @@ program |
61 | 67 | process.stdout.write(` platform : ${process.platform}-${process.arch}\n`); |
62 | 68 | }); |
63 | 69 |
|
64 | | -// `run` is a stub until FR-2877 wires the Playwright runner. |
65 | 70 | program |
66 | 71 | .command('run') |
67 | | - .description( |
68 | | - 'Run the smoke suite against an endpoint. Not yet implemented — coming in FR-2877.', |
| 72 | + .description('Run the smoke suite against a Backend.AI WebUI endpoint.') |
| 73 | + .addOption( |
| 74 | + new Option('--endpoint <url>', 'Backend.AI WebUI endpoint URL.') |
| 75 | + .env('BAI_SMOKE_ENDPOINT') |
| 76 | + .makeOptionMandatory(true), |
| 77 | + ) |
| 78 | + .option( |
| 79 | + '--webserver <url>', |
| 80 | + 'Backend.AI webserver endpoint URL. Defaults to --endpoint when omitted.', |
| 81 | + ) |
| 82 | + .addOption( |
| 83 | + new Option('--email <email>', 'Login email or username.') |
| 84 | + .env('BAI_SMOKE_EMAIL') |
| 85 | + .makeOptionMandatory(true), |
| 86 | + ) |
| 87 | + .addOption( |
| 88 | + new Option( |
| 89 | + '--password <password>', |
| 90 | + 'Login password. Prefer --password-stdin or BAI_SMOKE_PASSWORD env.', |
| 91 | + ).env('BAI_SMOKE_PASSWORD'), |
| 92 | + ) |
| 93 | + .option('--password-stdin', 'Read the password from stdin instead of --password.') |
| 94 | + .addOption( |
| 95 | + new Option('--role <role>', 'Role selection: auto, admin, or user.') |
| 96 | + .choices(['auto', 'admin', 'user']) |
| 97 | + .default('auto'), |
69 | 98 | ) |
70 | | - .option('--endpoint <url>', 'Backend.AI webui endpoint URL.') |
71 | | - .option('--webserver <url>', 'Backend.AI webserver endpoint URL.') |
72 | | - .option('--email <email>', 'Account email or username.') |
73 | | - .option('--password <password>', 'Account password (prefer --password-stdin).') |
74 | | - .option('--password-stdin', 'Read the password from stdin.') |
| 99 | + .option('--include <tags>', 'Comma-separated extra tags to include (e.g. "@critical").') |
| 100 | + .option('--exclude <tags>', 'Comma-separated tags to exclude.') |
75 | 101 | .option( |
76 | | - '--role <role>', |
77 | | - 'Force role selection: auto | admin | user | monitor.', |
78 | | - 'auto', |
| 102 | + '--pages <names>', |
| 103 | + 'Comma-separated page directory names (e.g. "session,vfolder").', |
| 104 | + ) |
| 105 | + .option('--workers <n>', 'Playwright worker count.', (v) => Number.parseInt(v, 10)) |
| 106 | + .option( |
| 107 | + '--timeout <duration>', |
| 108 | + 'Per-test timeout. Accepts "180s", "3m", or raw ms.', |
| 109 | + '180s', |
| 110 | + ) |
| 111 | + .option( |
| 112 | + '--output <dir>', |
| 113 | + 'Output directory for the smoke report.', |
| 114 | + () => defaultOutputDir(), |
| 115 | + defaultOutputDir(), |
79 | 116 | ) |
80 | | - .option('--include <tags...>', 'Additional tags to include in the run.') |
81 | | - .option('--exclude <tags...>', 'Tags to exclude from the run.') |
82 | | - .option('--pages <pages...>', 'Restrict the run to specific page categories.') |
83 | | - .option('--workers <n>', 'Playwright worker count.', '1') |
84 | | - .option('--timeout <ms>', 'Per-test timeout in milliseconds.', '120000') |
85 | | - .option('--output <dir>', 'Output directory for the smoke report.', './smoke-report') |
86 | 117 | .option('--headed', 'Run the browser in headed mode (debugging only).', false) |
87 | 118 | .option('--insecure-tls', 'Accept self-signed TLS certificates.', false) |
88 | | - .action(() => { |
89 | | - process.stderr.write( |
90 | | - 'bai-smoke run: Not yet implemented. Coming in FR-2877.\n', |
91 | | - ); |
92 | | - process.exit(2); |
| 119 | + .action(async (raw: Record<string, unknown>) => { |
| 120 | + const password = await resolvePassword(raw); |
| 121 | + if (!password) { |
| 122 | + process.stderr.write( |
| 123 | + 'bai-smoke run: --password, --password-stdin, or BAI_SMOKE_PASSWORD is required.\n', |
| 124 | + ); |
| 125 | + process.exit(2); |
| 126 | + } |
| 127 | + |
| 128 | + const endpoint = String(raw.endpoint); |
| 129 | + const webserver = |
| 130 | + typeof raw.webserver === 'string' && raw.webserver.length > 0 |
| 131 | + ? raw.webserver |
| 132 | + : endpoint; |
| 133 | + if (webserver === endpoint && !raw.webserver) { |
| 134 | + process.stderr.write( |
| 135 | + '[bai-smoke] --webserver not supplied; reusing --endpoint as the webserver URL.\n', |
| 136 | + ); |
| 137 | + } |
| 138 | + |
| 139 | + let timeoutMs: number; |
| 140 | + try { |
| 141 | + timeoutMs = parseDuration(String(raw.timeout ?? '180s')); |
| 142 | + } catch (err) { |
| 143 | + process.stderr.write(`bai-smoke run: ${(err as Error).message}\n`); |
| 144 | + process.exit(2); |
| 145 | + return; |
| 146 | + } |
| 147 | + |
| 148 | + const opts: SmokeRunOptions = { |
| 149 | + endpoint, |
| 150 | + webserver, |
| 151 | + email: String(raw.email), |
| 152 | + password, |
| 153 | + role: (raw.role as SmokeRoleSelection) ?? 'auto', |
| 154 | + include: splitCsvArg(raw.include as string | string[] | undefined), |
| 155 | + exclude: splitCsvArg(raw.exclude as string | string[] | undefined), |
| 156 | + pages: splitCsvArg(raw.pages as string | string[] | undefined), |
| 157 | + workers: typeof raw.workers === 'number' && raw.workers > 0 ? raw.workers : undefined, |
| 158 | + timeoutMs, |
| 159 | + outputDir: path.resolve(String(raw.output ?? defaultOutputDir())), |
| 160 | + headed: raw.headed === true, |
| 161 | + insecureTls: raw.insecureTls === true, |
| 162 | + }; |
| 163 | + |
| 164 | + const { exitCode, reportPath, summary } = await runSmoke(opts); |
| 165 | + |
| 166 | + process.stdout.write('\n'); |
| 167 | + process.stdout.write('bai-smoke summary\n'); |
| 168 | + process.stdout.write(`${'-'.repeat(60)}\n`); |
| 169 | + process.stdout.write(` endpoint : ${summary.endpoint}\n`); |
| 170 | + process.stdout.write(` webserver : ${summary.webserver}\n`); |
| 171 | + process.stdout.write(` role : ${summary.role} (selection: ${summary.roleSelection})\n`); |
| 172 | + if (summary.results) { |
| 173 | + const { total, passed, failed, skipped, flaky } = summary.results; |
| 174 | + process.stdout.write( |
| 175 | + ` results : ${passed} passed, ${failed} failed, ${skipped} skipped, ${flaky} flaky (total ${total})\n`, |
| 176 | + ); |
| 177 | + } else { |
| 178 | + process.stdout.write(' results : (no JSON reporter output found)\n'); |
| 179 | + } |
| 180 | + process.stdout.write(` report : ${reportPath}\n`); |
| 181 | + process.stdout.write(` summary : ${path.join(opts.outputDir, 'summary.json')}\n`); |
| 182 | + process.exit(exitCode); |
93 | 183 | }); |
94 | 184 |
|
95 | 185 | program.parseAsync(process.argv).catch((err: unknown) => { |
96 | 186 | // eslint-disable-next-line no-console |
97 | 187 | console.error(err); |
98 | 188 | process.exit(1); |
99 | 189 | }); |
| 190 | + |
| 191 | +function defaultOutputDir(): string { |
| 192 | + const iso = new Date().toISOString().replace(/[:.]/g, '-'); |
| 193 | + return path.resolve(process.cwd(), `smoke-report-${iso}`); |
| 194 | +} |
| 195 | + |
| 196 | +async function resolvePassword(raw: Record<string, unknown>): Promise<string | undefined> { |
| 197 | + if (typeof raw.password === 'string' && raw.password.length > 0) { |
| 198 | + return raw.password; |
| 199 | + } |
| 200 | + if (raw.passwordStdin === true) { |
| 201 | + return readStdin(); |
| 202 | + } |
| 203 | + return undefined; |
| 204 | +} |
| 205 | + |
| 206 | +function readStdin(): Promise<string> { |
| 207 | + return new Promise((resolve, reject) => { |
| 208 | + const chunks: Buffer[] = []; |
| 209 | + process.stdin.on('data', (c: Buffer) => chunks.push(c)); |
| 210 | + process.stdin.on('end', () => resolve(Buffer.concat(chunks).toString('utf8').trim())); |
| 211 | + process.stdin.on('error', reject); |
| 212 | + }); |
| 213 | +} |
0 commit comments