v0.11: 修复 --no-verify/--verify 选项失效 + BS-49 menu 误判
Bug 1: --no-verify 和 --verify 选项 Commander.js 自动转 opts.verify, 接口声明的 noVerify/verifyMode 永远 undefined → 浏览器验证无法跳过. 修复: 接口 + 读取对齐到 opts.verify (false=skip, string=mode), form/create.ts + form/release.ts 实测 --no-verify / --verify=warn 都生效. Bug 2: BS-49 guard 把 menu create 与 form 同 enCode 误判为冲突, 阻断 'menu == form enCode' 标准模式 (web formDesign 自动建菜单的合法做法). 修复: _blindspot_guards.ts runGuards 加第 3 参数 command, BS-49 check 跳过 menu create (form create 仍生效, 回归保护 OK). 调用方迁移: form create/release + menu create + dm create. 单测: 12/12 PASS (新建 verify_option.test.ts), 既有 40/41 PASS (1 BS-51 预存失败无关). jxcj 影响: 28 个 form + 27 menu 已建 (1 menu urlAddress 冲突, 后端 quirks), 后续可走 BS-49 修复后的 menu create 直接挂载. Co-Authored-By: Claude <[email protected]>
This commit is contained in:
1 parent
af518ba077
commit
2be1300ac3
6 files changed
+140
-9
No files matched your search
@@ -75,12 +75,17 @@ export const BLINDSPOT_GUARDS: BlindspotGuard[] = [
|
||||
},
|
||||
|
||||
// 盲点 49: form create 报 "Encoding cannot be repeated" — enCode 重复
|
||||
// v0.10+ 修复 (jxcj 实战): JNPF web formDesign 自动建菜单时, menu 与 form 同 enCode 是合法的.
|
||||
// 所以 menu create 时不应被 BS-49 拦截, 通过 `command` 参数区分.
|
||||
{
|
||||
id: 'BS-49',
|
||||
trigger: 'form create 报 "Encoding cannot be repeated"',
|
||||
level: 'ERROR',
|
||||
check: (ctx, opts) => {
|
||||
const enc = opts?.enCode as string | undefined
|
||||
const command = opts?.__command as string | undefined
|
||||
// menu create / menu update / menu clone 等菜单命令, enCode 与 form 重名是 JNPF 标准模式
|
||||
if (command && command.startsWith('menu ')) return false
|
||||
return !!enc && !!ctx && ctx.existingEnCodes.includes(enc)
|
||||
},
|
||||
message: 'enCode "${enCode}" 已存在于当前 app',
|
||||
@@ -390,16 +395,20 @@ export const BLINDSPOT_GUARDS: BlindspotGuard[] = [
|
||||
*
|
||||
* @param ctx AppContext (null = 容错, 跳过依赖 ctx 的 guard)
|
||||
* @param opts 命令选项 (enCode/fullName/dbLinkId/table/fields/tables 等)
|
||||
* @param command 调用方命令名 (e.g. "form create" / "menu create"), 用于 BS-49 等需要区分命令的 guard
|
||||
* @returns issues 数组 (空数组 = 全部通过)
|
||||
*/
|
||||
export function runGuards(
|
||||
ctx: AppContext | null,
|
||||
opts: Record<string, unknown> = {},
|
||||
command?: string,
|
||||
): BlindspotIssue[] {
|
||||
const issues: BlindspotIssue[] = []
|
||||
// v0.10+ 把 command 注入 opts, 让 guard.check 读到 (e.g. BS-49 区分 menu vs form)
|
||||
const enrichedOpts: Record<string, unknown> = command ? { ...opts, __command: command } : opts
|
||||
for (const guard of BLINDSPOT_GUARDS) {
|
||||
try {
|
||||
if (guard.check(ctx, opts)) {
|
||||
if (guard.check(ctx, enrichedOpts)) {
|
||||
// 替换 ${enCode}/${table}/${id} 等占位符
|
||||
const interpolate = (s: string): string =>
|
||||
s
|
||||
|
||||
@@ -57,7 +57,7 @@ export function buildDmCreateCommand(): Command {
|
||||
table: opts.table,
|
||||
dbLinkId,
|
||||
fields: userFields,
|
||||
}))
|
||||
}, 'dm create'))
|
||||
|
||||
// D-5 (盲点 38 防御): 用户字段含 f_description 会被系统自动加的 text 列冲突
|
||||
// 系统会自动注入 f_description (text) 作为"描述"列, 用户不应自己再传
|
||||
|
||||
@@ -46,6 +46,14 @@ interface CreateOpts {
|
||||
/** Phase 3: 浏览器层验证开关 (默认硬阻塞) */
|
||||
noVerify?: boolean
|
||||
verifyMode?: string
|
||||
/**
|
||||
* v0.10+ 新增: Commander 把 `--no-verify` 自动转 `opts.verify === false` (boolean),
|
||||
* 把 `--verify <mode>` 转 `opts.verify = '<mode>'` (string, 默认 'block').
|
||||
* 注意: 业务代码应读 `opts.verify` (上面两个字段永远是 undefined).
|
||||
* 这里声明 `verify?: boolean | string` 仅为 TS 兼容, 实际值由 Commander 注入.
|
||||
* @internal
|
||||
*/
|
||||
verify?: boolean | string
|
||||
verifyTimeout?: string
|
||||
/** v0.7.2 兜底能力: 走 UI 路径 (playwright 模拟人类在 formDesign 操作) */
|
||||
via?: 'api' | 'ui'
|
||||
@@ -189,7 +197,7 @@ export function buildFormCreateCommand(): Command {
|
||||
if (opts.fields) {
|
||||
try { guardOpts.fields = JSON.parse(opts.fields) } catch { /* JSON 解析失败留给后续步骤 */ }
|
||||
}
|
||||
handleGuardResult('form create', runGuards(guardCtx.ctx, guardOpts))
|
||||
handleGuardResult('form create', runGuards(guardCtx.ctx, guardOpts, 'form create'))
|
||||
|
||||
// D-3: --fields 简化格式自动包 __config__ (盲点 42 防御)
|
||||
let rawFields: unknown[] = []
|
||||
@@ -590,18 +598,28 @@ export function buildFormCreateCommand(): Command {
|
||||
if (opts.json) printJson({ id: newId, enCode: opts.enCode, type, webType })
|
||||
|
||||
// Phase 3: 浏览器层验证 (默认硬阻塞, --verify=warn / --no-verify 可绕过)
|
||||
//
|
||||
// v0.10+ 修复 (jxcj 实战): Commander.js 把 `--no-verify` 自动转 `opts.verify === false` (boolean)
|
||||
// 并把 `--verify <mode>` 转 `opts.verify = '<mode>'` (string), 两个选项都赋值给 `opts.verify`,
|
||||
// 接口里的 `noVerify/verifyMode` 永远是 undefined, 之前完全没生效.
|
||||
// 修复: 直接读 `opts.verify`, 根据其值 (boolean false / string 'block'|'warn') 派生.
|
||||
// 优先级: --no-verify > --verify=warn > --verify=block (默认) > 都不传
|
||||
const verifyRaw = opts.verify as unknown
|
||||
const effectiveNoVerify = verifyRaw === false // Commander --no-verify 自动否定
|
||||
const effectiveMode: 'block' | 'warn' =
|
||||
verifyRaw === 'warn' ? 'warn' : 'block' // 其它一律按 block (含 'block'/undefined/true)
|
||||
if (newId !== '(unknown)') {
|
||||
try {
|
||||
await browserVerify({
|
||||
formId: newId,
|
||||
enCode: opts.enCode,
|
||||
appCode: c.appCode,
|
||||
noVerify: opts.noVerify,
|
||||
verifyMode: (opts.verifyMode as 'block' | 'warn') || 'block',
|
||||
noVerify: effectiveNoVerify,
|
||||
verifyMode: effectiveMode,
|
||||
verifyTimeout: opts.verifyTimeout ? Number(opts.verifyTimeout) : undefined,
|
||||
})
|
||||
} catch (e) {
|
||||
if (opts.verifyMode !== 'warn') throw e
|
||||
if (effectiveMode !== 'warn') throw e
|
||||
// warn 模式不抛, 继续
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,7 +21,7 @@ export function buildFormReleaseCommand(): Command {
|
||||
// - BS-49 (enCode 重复) 等创建类 guard 不适用, 跳过
|
||||
const guardCtx = await loadContextSafe(null)
|
||||
if (guardCtx.warning) log.warn(guardCtx.warning)
|
||||
handleGuardResult('form release', runGuards(guardCtx.ctx, { formId: id }))
|
||||
handleGuardResult('form release', runGuards(guardCtx.ctx, { formId: id }, 'form release'))
|
||||
|
||||
const c = await JnpfClient.fromToken()
|
||||
const r = await c.postJson<unknown>(`/visualdev/Base/${id}/Actions/Release`, {})
|
||||
@@ -33,7 +33,7 @@ export function buildFormReleaseCommand(): Command {
|
||||
const matched = guardCtx.ctx.releasedForms.find(f => f.startsWith(`${id}:`))
|
||||
if (matched) {
|
||||
const enCode = matched.split(':').slice(1).join(':') || id
|
||||
const postIssues = runGuards(guardCtx.ctx, { enCode })
|
||||
const postIssues = runGuards(guardCtx.ctx, { enCode }, 'form release')
|
||||
const bs37 = postIssues.filter(i => i.id === 'BS-37' && i.level === 'WARN')
|
||||
if (bs37.length) {
|
||||
log.warn('Blindspot guard 提示:')
|
||||
|
||||
@@ -75,7 +75,7 @@ export function buildMenuCreateCommand(): Command {
|
||||
fullName: opts.fullName,
|
||||
enCode: opts.enCode,
|
||||
formId: opts.formId,
|
||||
}))
|
||||
}, 'menu create'))
|
||||
|
||||
// 模式 A: 挂表单菜单 — 走 web 真路径
|
||||
if (opts.formId) {
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
/**
|
||||
* 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'],
|
||||
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' })
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user