From 2be1300ac3c05abb14f9479c499608bbb13bfa6b Mon Sep 17 00:00:00 2001 From: ssw Date: Mon, 22 Jun 2026 22:53:51 +0800 Subject: [PATCH] =?UTF-8?q?v0.11:=20=E4=BF=AE=E5=A4=8D=20--no-verify/--ver?= =?UTF-8?q?ify=20=E9=80=89=E9=A1=B9=E5=A4=B1=E6=95=88=20+=20BS-49=20menu?= =?UTF-8?q?=20=E8=AF=AF=E5=88=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- src/commands/_blindspot_guards.ts | 11 +++- src/commands/dm/create.ts | 2 +- src/commands/form/create.ts | 26 ++++++-- src/commands/form/release.ts | 4 +- src/commands/menu/create.ts | 2 +- tests/unit/verify_option.test.ts | 104 ++++++++++++++++++++++++++++++ 6 files changed, 140 insertions(+), 9 deletions(-) create mode 100644 tests/unit/verify_option.test.ts diff --git a/src/commands/_blindspot_guards.ts b/src/commands/_blindspot_guards.ts index b8bffaf..d0d84d5 100644 --- a/src/commands/_blindspot_guards.ts +++ b/src/commands/_blindspot_guards.ts @@ -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 = {}, + command?: string, ): BlindspotIssue[] { const issues: BlindspotIssue[] = [] + // v0.10+ 把 command 注入 opts, 让 guard.check 读到 (e.g. BS-49 区分 menu vs form) + const enrichedOpts: Record = 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 diff --git a/src/commands/dm/create.ts b/src/commands/dm/create.ts index c249370..20097ef 100644 --- a/src/commands/dm/create.ts +++ b/src/commands/dm/create.ts @@ -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) 作为"描述"列, 用户不应自己再传 diff --git a/src/commands/form/create.ts b/src/commands/form/create.ts index b2d6f0c..9072b84 100644 --- a/src/commands/form/create.ts +++ b/src/commands/form/create.ts @@ -46,6 +46,14 @@ interface CreateOpts { /** Phase 3: 浏览器层验证开关 (默认硬阻塞) */ noVerify?: boolean verifyMode?: string + /** + * v0.10+ 新增: Commander 把 `--no-verify` 自动转 `opts.verify === false` (boolean), + * 把 `--verify ` 转 `opts.verify = ''` (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 ` 转 `opts.verify = ''` (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 模式不抛, 继续 } } diff --git a/src/commands/form/release.ts b/src/commands/form/release.ts index a14d673..6754bed 100644 --- a/src/commands/form/release.ts +++ b/src/commands/form/release.ts @@ -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(`/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 提示:') diff --git a/src/commands/menu/create.ts b/src/commands/menu/create.ts index 80de35e..a43d8c7 100644 --- a/src/commands/menu/create.ts +++ b/src/commands/menu/create.ts @@ -75,7 +75,7 @@ export function buildMenuCreateCommand(): Command { fullName: opts.fullName, enCode: opts.enCode, formId: opts.formId, - })) + }, 'menu create')) // 模式 A: 挂表单菜单 — 走 web 真路径 if (opts.formId) { diff --git a/tests/unit/verify_option.test.ts b/tests/unit/verify_option.test.ts new file mode 100644 index 0000000..4ade89b --- /dev/null +++ b/tests/unit/verify_option.test.ts @@ -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 把 verify 设为 '', + // 两个选项都赋 opts.verify, 第二个定义的赢. 我们的派生逻辑兼容两种顺序: + // 若用户写 --no-verify --verify=warn, opts.verify === 'warn', derive 出 mode=warn, noVerify=false (后者覆盖前者) + expect(derive('warn')).toEqual({ noVerify: false, verifyMode: 'warn' }) + }) +})