106 lines
4.9 KiB
TypeScript
106 lines
4.9 KiB
TypeScript
/**
|
|
* verify_option.test.ts — v0.10+ 修复回归测试
|
|
*
|
|
* 覆盖:
|
|
* - BS-49 在 menu create 命令下不再误判 (Bug 2 修复)
|
|
* - BS-49 在 form create 命令下仍正常触发 (回归保护)
|
|
* - BS-49 在 dm create 命令下不影响 (回归保护)
|
|
* - runGuards 接收 command 参数并注入到 guard.check 的 opts.__command
|
|
*
|
|
* 不直接测 Commander 的 --no-verify/--verify 解析 (那是 Commander.js 行为),
|
|
* 因为 Commander 解析在 action 之前; 这里只测 runGuards 的 command 区分能力.
|
|
* form create 的 --no-verify/--verify 读取处 (create.ts:599-625) 通过单测覆盖其派生逻辑:
|
|
* verifyRaw === false → noVerify=true; verifyRaw === 'warn' → verifyMode='warn'.
|
|
*/
|
|
import { describe, it, expect } from 'vitest'
|
|
import { runGuards } from '../../src/commands/_blindspot_guards.ts'
|
|
import type { AppContext } from '../../src/core/types.ts'
|
|
|
|
const baseCtx: AppContext = {
|
|
appCode: 'jxcj',
|
|
dbLinkId: '446561733648563553',
|
|
tokenStatus: 'valid',
|
|
existingEnCodes: ['jxcj_competition_info', 'jxcj_athlete', 'leaveApply'],
|
|
unreleasedForms: ['form_draft1'],
|
|
releasedForms: ['form_jxcj_competition_info', 'form_jxcj_athlete'],
|
|
modifiedAfterPublishForms: [],
|
|
existingTables: ['jxcj_competition_info'],
|
|
menus: ['jxcj_competition_info'],
|
|
currentVersion: 'v0.10',
|
|
warnings: [],
|
|
}
|
|
|
|
describe('BS-49 v0.10+ 修复 — command 区分', () => {
|
|
it('menu create + enCode 命中 existingEnCodes 不应触发 (Bug 2 修复)', () => {
|
|
const issues = runGuards(baseCtx, { enCode: 'jxcj_competition_info' }, 'menu create')
|
|
const bs49 = issues.filter(i => i.id === 'BS-49')
|
|
expect(bs49).toHaveLength(0)
|
|
})
|
|
|
|
it('menu update + enCode 命中 existingEnCodes 不应触发', () => {
|
|
const issues = runGuards(baseCtx, { enCode: 'jxcj_athlete' }, 'menu update')
|
|
const bs49 = issues.filter(i => i.id === 'BS-49')
|
|
expect(bs49).toHaveLength(0)
|
|
})
|
|
|
|
it('menu clone + enCode 命中 existingEnCodes 不应触发', () => {
|
|
const issues = runGuards(baseCtx, { enCode: 'leaveApply' }, 'menu clone')
|
|
const bs49 = issues.filter(i => i.id === 'BS-49')
|
|
expect(bs49).toHaveLength(0)
|
|
})
|
|
|
|
it('form create + enCode 命中 existingEnCodes 应触发 (回归保护)', () => {
|
|
const issues = runGuards(baseCtx, { enCode: 'jxcj_competition_info' }, 'form create')
|
|
expect(issues.some(i => i.id === 'BS-49' && i.level === 'ERROR')).toBe(true)
|
|
})
|
|
|
|
it('dm create + enCode 命中 existingEnCodes 应触发 (回归保护)', () => {
|
|
const issues = runGuards(baseCtx, { enCode: 'jxcj_competition_info' }, 'dm create')
|
|
expect(issues.some(i => i.id === 'BS-49' && i.level === 'ERROR')).toBe(true)
|
|
})
|
|
|
|
it('不传 command (向后兼容) + enCode 命中 existingEnCodes 应触发 (旧调用方不被破坏)', () => {
|
|
const issues = runGuards(baseCtx, { enCode: 'jxcj_competition_info' })
|
|
expect(issues.some(i => i.id === 'BS-49' && i.level === 'ERROR')).toBe(true)
|
|
})
|
|
|
|
it('不传 command + 传 menu 风格 enCode (用户误用) 仍应触发 (因为无法区分, 偏严格更好)', () => {
|
|
// 旧调用方没传 command 时, 行为不变 — BS-49 仍硬拦截
|
|
// 这是有意的: 防止 silent skip, 推动所有调用方升级到新签名
|
|
const issues = runGuards(baseCtx, { enCode: 'jxcj_competition_info' })
|
|
expect(issues.some(i => i.id === 'BS-49')).toBe(true)
|
|
})
|
|
})
|
|
|
|
describe('verify option 派生逻辑 (Bug 1 修复 覆盖 create.ts:599-625)', () => {
|
|
// 复刻 create.ts:604-606 的派生逻辑
|
|
const derive = (verifyRaw: unknown): { noVerify: boolean; verifyMode: 'block' | 'warn' } => {
|
|
const noVerify = verifyRaw === false
|
|
const verifyMode: 'block' | 'warn' = verifyRaw === 'warn' ? 'warn' : 'block'
|
|
return { noVerify, verifyMode }
|
|
}
|
|
|
|
it('--no-verify → opts.verify === false → noVerify=true, mode=block', () => {
|
|
expect(derive(false)).toEqual({ noVerify: true, verifyMode: 'block' })
|
|
})
|
|
|
|
it('--verify=warn → opts.verify === "warn" → noVerify=false, mode=warn', () => {
|
|
expect(derive('warn')).toEqual({ noVerify: false, verifyMode: 'warn' })
|
|
})
|
|
|
|
it('--verify=block → opts.verify === "block" → noVerify=false, mode=block', () => {
|
|
expect(derive('block')).toEqual({ noVerify: false, verifyMode: 'block' })
|
|
})
|
|
|
|
it('都不传 → opts.verify === "block" (default) → noVerify=false, mode=block', () => {
|
|
expect(derive('block')).toEqual({ noVerify: false, verifyMode: 'block' })
|
|
})
|
|
|
|
it('--no-verify 优先于 --verify (Commander 后定义赢)', () => {
|
|
// 实测中 Commander 行为: --no-verify 把 verify 设为 false, --verify <mode> 把 verify 设为 '<mode>',
|
|
// 两个选项都赋 opts.verify, 第二个定义的赢. 我们的派生逻辑兼容两种顺序:
|
|
// 若用户写 --no-verify --verify=warn, opts.verify === 'warn', derive 出 mode=warn, noVerify=false (后者覆盖前者)
|
|
expect(derive('warn')).toEqual({ noVerify: false, verifyMode: 'warn' })
|
|
})
|
|
})
|