/** * blindspot_guards.spec.ts — P3.2 集成测试 * * 验证 24 条 blindspot guards 真的接入了 form/dm/menu create / form release 命令: * - 缺 enCode / dbLinkId → exit 2 + 输出 BS-48/49/5 等提示 * - 正常参数不被误伤 * * 关键: JNPF 可能没起, 测试的是"pre-check + guard"层, 不调真实 API */ import { describe, it, expect } from 'vitest' import { execFileSync } from 'node:child_process' import { resolve } from 'node:path' const CLI = resolve(__dirname, '../../src/cli.ts') interface CliResult { stdout: string stderr: string code: number combined: string } function runCli(args: string[], timeoutMs = 15000): CliResult { try { const stdout = execFileSync('npx', ['tsx', CLI, ...args], { encoding: 'utf8', timeout: timeoutMs, env: { ...process.env, NO_COLOR: '1' }, }) return { stdout, stderr: '', code: 0, combined: stdout } } catch (e: any) { const stdout = e.stdout?.toString() || '' const stderr = e.stderr?.toString() || '' return { stdout, stderr, code: e.status || 1, combined: stdout + stderr, } } } describe('blindspot guard integration — form create', () => { it('不传 fullName/enCode → exit 2 (早期校验拦截, 在 guard 之前)', () => { const r = runCli(['form', 'create']) expect(r.code).toBe(2) // 早期校验 --fullName/--enCode 必填 expect(r.combined).toMatch(/--fullName.*--enCode|必填|必填/) }) it('缺 dbLinkId (type=1) → 应触发 pre-check BS-48 等', () => { // 没 --dbLinkId 且 type=1 (非视图) → pre-check 触发 dbLinkId 错误 const r = runCli([ 'form', 'create', '--fullName=test', '--enCode=testGuardX1', '--type=1', '--webType=2', ], 30000) // JNPF 没起 → pre-check 应报 dbLinkId 缺失 // JNPF 起了但 token 缺失 → loadContextSafe 会 fail, 然后 pre-check 跳过 // 两种情况都可能 exit 2 (pre-check/guard) 或 exit 1 (loadAppContext 失败) // 关键是 exit code != 0 expect(r.code).not.toBe(0) }) it('dbLinkId="0" + type=1 → 应触发 BS-48 (ERROR, exit 2)', () => { const r = runCli([ 'form', 'create', '--fullName=test', '--enCode=testGuardX2', '--type=1', '--webType=2', '--dbLinkId=0', ], 30000) // JNPF 不可达 → ctx 加载失败 → guard 跳过 → 跑到 API → API 失败 → exit 1 // JNPF 可达 → ctx 加载成功 → BS-48 ERROR → exit 2 // 两种都是"非 0", 我们只验不会成功 expect(r.code).not.toBe(0) }) }) describe('blindspot guard integration — dm create', () => { it('缺 --table → commander requiredOption 错误, exit != 0', () => { const r = runCli(['dm', 'create', '--tableName=test', '--fields=[]']) expect(r.code).not.toBe(0) }) it('缺 --tableName → commander requiredOption 错误, exit != 0', () => { const r = runCli(['dm', 'create', '--table=test', '--fields=[]']) expect(r.code).not.toBe(0) }) it('缺 --fields → commander requiredOption 错误, exit != 0', () => { const r = runCli(['dm', 'create', '--table=test', '--tableName=test']) expect(r.code).not.toBe(0) }) it('dbLinkId="0" + 业务表 → 应触发 pre-check dbLinkId 错误 (exit 2)', () => { const r = runCli([ 'dm', 'create', '--table=testGuardDmX1', '--tableName=test', '--fields=[{"field":"f_name","fieldName":"名称","dataType":"varchar","dataLength":"100","allowNull":0}]', '--dbLinkId=0', ], 30000) expect(r.code).not.toBe(0) }) }) describe('blindspot guard integration — menu create', () => { it('缺 --fullName → commander requiredOption 错误, exit != 0', () => { const r = runCli(['menu', 'create']) expect(r.code).not.toBe(0) }) it('缺 --formId 模式 B (目录) → 应到 loadContextSafe / API 调用', () => { const r = runCli([ 'menu', 'create', '--fullName=testMenuGuardX1', '--type=1', ], 30000) expect(r.code).not.toBe(0) }) it('缺 --formId + --urlAddress + type=7 → 早期拦截 (login 或 urlAddress 校验, exit != 0)', () => { const r = runCli([ 'menu', 'create', '--fullName=testMenuGuardX2', '--type=7', ], 30000) // 关键: 永远不能 exit 0 (缺 urlAddress 必失败) // JNPF 不可达 → login fail → exit 1 // JNPF 可达 → type=7 缺 urlAddress → exit 2 // 都满足 != 0 expect(r.code).not.toBe(0) }) }) describe('blindspot guard integration — form release', () => { it('缺 id 参数 → commander argument 错误, exit != 0', () => { const r = runCli(['form', 'release']) expect(r.code).not.toBe(0) }) it('传 id 但 JNPF 不可达 → loadContextSafe 失败, guard 跳过, API 失败 → exit 1', () => { const r = runCli(['form', 'release', '999999999999']) expect(r.code).not.toBe(0) }) }) describe('blindspot guard integration — 单元层覆盖验证', () => { /** * 这些 case 不依赖 CLI 子进程, 直接通过验证 guards 已正确接入 * (因为 JNPF 不可达时 guard 会被 ctx=null 跳过, 无法在子进程测触发) */ it('form create 集成代码含 handleGuardResult 调用', async () => { const fs = await import('fs') const src = fs.readFileSync(resolve(__dirname, '../../src/commands/form/create.ts'), 'utf8') expect(src).toContain("handleGuardResult('form create'") expect(src).toContain('runGuards') }) it('dm create 集成代码含 handleGuardResult 调用', async () => { const fs = await import('fs') const src = fs.readFileSync(resolve(__dirname, '../../src/commands/dm/create.ts'), 'utf8') expect(src).toContain("handleGuardResult('dm create'") expect(src).toContain('runGuards') }) it('menu create 集成代码含 handleGuardResult 调用', async () => { const fs = await import('fs') const src = fs.readFileSync(resolve(__dirname, '../../src/commands/menu/create.ts'), 'utf8') expect(src).toContain("handleGuardResult('menu create'") expect(src).toContain('runGuards') }) it('form release 集成代码含 handleGuardResult 调用', async () => { const fs = await import('fs') const src = fs.readFileSync(resolve(__dirname, '../../src/commands/form/release.ts'), 'utf8') expect(src).toContain("handleGuardResult('form release'") expect(src).toContain('runGuards') }) })