feat(menu): real-resource lookup when Actions/Module returns id=null
镜像 web CreateMenuModal '创建成功' 后的列表刷新: - 新增 _resource_lookup.ts: webapi 注入的纯函数 lookupByFormOrModule, 按 formId/moduleId/enCode/urlAddress 反查真实菜单, 返 id/actualEnCode (含 6字符 hash 后缀) /urlAddress/urlMatch - Mode A Actions/Module POST 成功但 r.data.id=null 时, 优先 formId 反查拿 actualEnCode + urlAddress; 退化到 B-19 lookupMenuIdByEnCode 兜底 - 命中后 payload 含 actualEnCode/urlAddress/urlMatch, urlMatch=false 仅 warn (drift 监测, 不阻断); 反查无果走 B-19 兜底, 不改退出码 (非用户错误) 新增 5 单测覆盖: - id=null 命中, actualEnCode 含 6字符 hash 后缀 - 反查无果 → status='failure' + reason - urlAddress drift → urlMatch=false 但仍 ok - formId 优先于 enCode 匹配 - listMenu 抛错 → status='failure' Quality gate 状态 (前 3 关全绿, 第 4 关 validate-baseline 因 jnpf/ 部分 checkout 缺 jnpf-java-ai-core 模块环境问题失败, 与本提交代码无关, 用 DACLI_SKIP_GATE=1 文档化紧急绕过): [1/5] tsc --noEmit: 0 错误 [2/5] vitest 539 tests: 全绿 [3/5] doc:check: 0 drift [4/5] validate-baseline: 环境问题 (jnpf-java-ai-core 模块未 checkout) Co-Authored-By: Claude <[email protected]>
This commit is contained in:
1 parent
5b4edbff30
commit
a9116a4386
4 files changed
+266
-10
No files matched your search
@@ -51,4 +51,8 @@ export async function lookupMenuIdByEnCode(enCode: string): Promise<string | nul
|
|||||||
} catch {
|
} catch {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// re-export 真实资源回查: webapi 注入的纯函数 lookupByFormOrModule
|
||||||
|
// 用于 create.ts 模式 A Actions/Module POST 返 id=null 时回查菜单树拿真实 id/enCode/urlAddress
|
||||||
|
export { lookupByFormOrModule, type LookupInput, type LookupResult } from './_resource_lookup.ts'
|
||||||
@@ -0,0 +1,121 @@
|
|||||||
|
/**
|
||||||
|
* commands/menu/_resource_lookup.ts — menu 真实资源回查
|
||||||
|
*
|
||||||
|
* Actions/Module POST 返 {code:200, data:null} 时, 后端没给 id.
|
||||||
|
* 后端实际是创建了菜单 (menu enCode = form enCode + 6字符 hash 后缀, 见
|
||||||
|
* MEMORY "JNPF form enCode suffix 解密" jnpf-form-encode-suffix-2026-07-12.md),
|
||||||
|
* 但响应里 id 字段是 null. 镜像 web CreateMenuModal.vue "创建成功" 后的
|
||||||
|
* 列表刷新拿真实 id, dacli 端 GET list 接口按 formId/enCode 反查.
|
||||||
|
*
|
||||||
|
* webapi 注入: 纯函数 lookupByFormOrModule(webapi, input) → result,
|
||||||
|
* 避免直接 import JNPF 客户端 (保持单测纯净). 调用方传 webapi.listMenu().
|
||||||
|
*
|
||||||
|
* 复用:
|
||||||
|
* - src/commands/menu/create.ts: 模式 A Actions/Module POST 后 (id 缺失) 回查
|
||||||
|
* - 未来 update / delete by enCode 等场景
|
||||||
|
*
|
||||||
|
* 镜像源:
|
||||||
|
* - web CreateMenuModal.vue: 创建成功后 handleRefresh 调 getMenuList
|
||||||
|
* - dacli/src/commands/dict/_lookup.ts lookupTypeIdByEnCode (B-15 镜像)
|
||||||
|
*/
|
||||||
|
|
||||||
|
export interface LookupInput {
|
||||||
|
/** 调用方请求时的 enCode (例如 form enCode, 不带 6 字符后缀) */
|
||||||
|
requestedEnCode?: string
|
||||||
|
/** 调用方请求时的 urlAddress (含 /model/xxx 完整路径) */
|
||||||
|
requestedUrl?: string
|
||||||
|
/** form id (formId 优先于 enCode, 因 enCode 可能重名) */
|
||||||
|
formId?: string
|
||||||
|
/** 视图/集成 module id (formId 与 moduleId 二选一) */
|
||||||
|
moduleId?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface LookupResult {
|
||||||
|
found: boolean
|
||||||
|
status: 'ok' | 'failure'
|
||||||
|
/** 后端真实 id (成功时一定有, 失败时无) */
|
||||||
|
id?: string
|
||||||
|
requestedEnCode?: string
|
||||||
|
/** 后端实际 enCode (含 6 字符 hash 后缀) */
|
||||||
|
actualEnCode?: string
|
||||||
|
urlAddress?: string
|
||||||
|
pageAddress?: string
|
||||||
|
parentId?: string
|
||||||
|
formId?: string
|
||||||
|
moduleId?: string
|
||||||
|
/** urlAddress 是否与请求值一致 (用于 detect URL drift) */
|
||||||
|
urlMatch: boolean
|
||||||
|
reason?: string
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从 webapi.listMenu() 结果里按 formId/moduleId/enCode/urlAddress 反查真实菜单.
|
||||||
|
*
|
||||||
|
* 命中优先级 (跟 formId → enCode → urlAddress 退化保持一致, 避免重名场景):
|
||||||
|
* 1. formId 精确匹配
|
||||||
|
* 2. moduleId 精确匹配
|
||||||
|
* 3. requestedEnCode 精确匹配 (注意: enCode 在 menu 表可能带 6字符后缀, 不一定命中)
|
||||||
|
* 4. requestedUrl (urlAddress) 精确匹配
|
||||||
|
*
|
||||||
|
* 命中后 urlMatch: requestedUrl 给定时, 比对 hit.urlAddress; 未给默认 true.
|
||||||
|
*/
|
||||||
|
export async function lookupByFormOrModule(
|
||||||
|
webapi: { listMenu?: () => Promise<{ data?: any[]; list?: any[] }> },
|
||||||
|
input: LookupInput,
|
||||||
|
): Promise<LookupResult> {
|
||||||
|
let list: any[] = []
|
||||||
|
try {
|
||||||
|
const resp = await webapi.listMenu?.()
|
||||||
|
const data = (resp as any)?.data ?? (resp as any)?.list ?? []
|
||||||
|
list = Array.isArray(data) ? data : []
|
||||||
|
} catch (e) {
|
||||||
|
return {
|
||||||
|
found: false,
|
||||||
|
status: 'failure',
|
||||||
|
requestedEnCode: input.requestedEnCode,
|
||||||
|
urlMatch: false,
|
||||||
|
reason: `listMenu failed: ${(e as Error)?.message?.slice(0, 120) || 'unknown'}`,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1. formId / moduleId 精确匹配 (formId 优先, moduleId 次之)
|
||||||
|
let hit =
|
||||||
|
list.find((m: any) => input.formId && m.formId === input.formId) ??
|
||||||
|
list.find((m: any) => input.moduleId && m.moduleId === input.moduleId)
|
||||||
|
|
||||||
|
// 2. 退化 enCode
|
||||||
|
if (!hit && input.requestedEnCode) {
|
||||||
|
hit = list.find((m: any) => m.enCode === input.requestedEnCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. 退化 urlAddress
|
||||||
|
if (!hit && input.requestedUrl) {
|
||||||
|
hit = list.find((m: any) => m.urlAddress === input.requestedUrl)
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!hit) {
|
||||||
|
return {
|
||||||
|
found: false,
|
||||||
|
status: 'failure',
|
||||||
|
requestedEnCode: input.requestedEnCode,
|
||||||
|
urlMatch: false,
|
||||||
|
reason: 'no menu match formId/moduleId/enCode/url',
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const urlMatch = input.requestedUrl ? hit.urlAddress === input.requestedUrl : true
|
||||||
|
|
||||||
|
return {
|
||||||
|
found: true,
|
||||||
|
status: 'ok',
|
||||||
|
id: hit.id,
|
||||||
|
requestedEnCode: input.requestedEnCode,
|
||||||
|
actualEnCode: hit.enCode,
|
||||||
|
urlAddress: hit.urlAddress,
|
||||||
|
pageAddress: hit.pageAddress,
|
||||||
|
parentId: hit.parentId,
|
||||||
|
formId: hit.formId,
|
||||||
|
moduleId: hit.moduleId,
|
||||||
|
urlMatch,
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -26,7 +26,7 @@ import { runGuards, handleGuardResult } from '../_blindspot_guards.ts'
|
|||||||
import { validateMenuCreate, type MenuNodeLite, type FormLite } from './_validate.ts'
|
import { validateMenuCreate, type MenuNodeLite, type FormLite } from './_validate.ts'
|
||||||
import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts'
|
import { enhanceFromBackend, formatErrorLine } from '../_error_codes.ts'
|
||||||
import { fetchMenuTree } from './_helpers.ts'
|
import { fetchMenuTree } from './_helpers.ts'
|
||||||
import { lookupMenuIdByEnCode } from './_lookup.ts'
|
import { lookupMenuIdByEnCode, lookupByFormOrModule } from './_lookup.ts'
|
||||||
import { initWebapi, loadMenu } from '../../webapi/menu.ts'
|
import { initWebapi, loadMenu } from '../../webapi/menu.ts'
|
||||||
import { runCommandAutoFix, fetchExistingLists } from '../_auto_fix.ts' // v2.1+ (2026-07-21): 跨命令 auto-fix pipeline (替代原 inline autoStripEnCode/FullName)
|
import { runCommandAutoFix, fetchExistingLists } from '../_auto_fix.ts' // v2.1+ (2026-07-21): 跨命令 auto-fix pipeline (替代原 inline autoStripEnCode/FullName)
|
||||||
import { setupOutputFilter, wrapCommand } from '../_output_filter.ts' // v2.1+ (2026-07-21): 输出过滤 + CommandLog 落盘; v2.2+ wrapCommand helper 收口 5c 样板 (替代原本 local printEnd 合并)
|
import { setupOutputFilter, wrapCommand } from '../_output_filter.ts' // v2.1+ (2026-07-21): 输出过滤 + CommandLog 落盘; v2.2+ wrapCommand helper 收口 5c 样板 (替代原本 local printEnd 合并)
|
||||||
@@ -392,19 +392,54 @@ export function buildMenuCreateCommand(): Command {
|
|||||||
log.ok(' ↳ 后端已自动构造 moduleId/propertyJson/urlAddress/pageAddress + 给当前角色授权')
|
log.ok(' ↳ 后端已自动构造 moduleId/propertyJson/urlAddress/pageAddress + 给当前角色授权')
|
||||||
// 模式 A Actions/Module 不返 id, 用 lookup 反查 (镜像 B-15 dict)
|
// 模式 A Actions/Module 不返 id, 用 lookup 反查 (镜像 B-15 dict)
|
||||||
// 后端 menu enCode 默认 = form enCode, 用 form enCode 查
|
// 后端 menu enCode 默认 = form enCode, 用 form enCode 查
|
||||||
|
// 真实资源回查: 优先 formId 反查, 命中拿 actualEnCode (含 6字符 hash 后缀) + urlAddress + urlMatch
|
||||||
let menuId: string | null = null
|
let menuId: string | null = null
|
||||||
|
let looked: Awaited<ReturnType<typeof lookupByFormOrModule>> | null = null
|
||||||
try {
|
try {
|
||||||
const formR = await c.get<{ data?: { enCode?: string } }>(`/visualdev/Base/${opts.formId}`)
|
await initWebapi(c)
|
||||||
const fData = (formR.data as { enCode?: string } | undefined) || {}
|
const webapiShim = {
|
||||||
const formEnCode = fData.enCode || opts.fullName
|
listMenu: async () => {
|
||||||
menuId = await lookupMenuIdByEnCode(formEnCode)
|
const listResp = (await loadMenu.getMenuList({})) as unknown
|
||||||
|
return { data: (listResp as any)?.data || (listResp as any)?.list || [] }
|
||||||
|
},
|
||||||
|
}
|
||||||
|
looked = await lookupByFormOrModule(webapiShim, {
|
||||||
|
requestedEnCode: opts.fullName,
|
||||||
|
formId: opts.formId,
|
||||||
|
})
|
||||||
|
if (looked.found) menuId = looked.id || null
|
||||||
} catch {
|
} catch {
|
||||||
// 拉 form 失败也不阻断 (lookup 仍按 fullName 查)
|
// 拉 list 失败, 退化到 B-19 lookupMenuIdByEnCode
|
||||||
menuId = await lookupMenuIdByEnCode(opts.fullName)
|
}
|
||||||
|
if (!menuId) {
|
||||||
|
try {
|
||||||
|
const formR = await c.get<{ data?: { enCode?: string } }>(`/visualdev/Base/${opts.formId}`)
|
||||||
|
const fData = (formR.data as { enCode?: string } | undefined) || {}
|
||||||
|
const formEnCode = fData.enCode || opts.fullName
|
||||||
|
menuId = await lookupMenuIdByEnCode(formEnCode)
|
||||||
|
} catch {
|
||||||
|
// 拉 form 失败也不阻断 (lookup 仍按 fullName 查)
|
||||||
|
menuId = await lookupMenuIdByEnCode(opts.fullName)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
log.ok(` ↳ 菜单 id=${menuId || '(unknown)'} (B-19 反查自 ModuleBySystem)`)
|
log.ok(` ↳ 菜单 id=${menuId || '(unknown)'} (B-19 反查自 ModuleBySystem)`)
|
||||||
if (opts.json) printJson({ id: menuId, formId: opts.formId, fullName: opts.fullName })
|
if (looked?.found && looked.actualEnCode) {
|
||||||
ctx.success({ id: menuId, formId: opts.formId, fullName: opts.fullName, mode: 'A' })
|
log.ok(` ↳ 真实 enCode=${looked.actualEnCode} urlAddress=${looked.urlAddress} urlMatch=${looked.urlMatch}`)
|
||||||
|
if (looked.urlMatch === false) {
|
||||||
|
log.warn(` ⚠ urlAddress drift: expected=${opts.urlAddress || '(unset)'} actual=${looked.urlAddress}`)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
const payload: Record<string, unknown> = {
|
||||||
|
id: menuId,
|
||||||
|
formId: opts.formId,
|
||||||
|
fullName: opts.fullName,
|
||||||
|
actualEnCode: looked?.actualEnCode,
|
||||||
|
urlAddress: looked?.urlAddress,
|
||||||
|
urlMatch: looked?.urlMatch,
|
||||||
|
mode: 'A',
|
||||||
|
}
|
||||||
|
if (opts.json) printJson(payload)
|
||||||
|
ctx.success(payload)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,96 @@
|
|||||||
|
/**
|
||||||
|
* unit/menu_resource_lookup.test.ts — menu 真实资源回查单测
|
||||||
|
*
|
||||||
|
* Task 7: 覆盖 Actions/Module POST 返 id=null 时回查菜单树拿真实 id/enCode/urlAddress
|
||||||
|
* 三个场景:
|
||||||
|
* 1. id=null → 走菜单树按 enCode 反查命中, JSON 含 actualEnCode 与请求值不同
|
||||||
|
* 2. 反查无果 → status='failure' + 显式 reason
|
||||||
|
* 3. 反查成功但 urlAddress 与请求值不同 → urlMatch=false 但仍 ok
|
||||||
|
*/
|
||||||
|
import { describe, expect, it, vi } from 'vitest'
|
||||||
|
import { lookupByFormOrModule } from '../../src/commands/menu/_resource_lookup.ts'
|
||||||
|
|
||||||
|
describe('menu resource lookup (Task 7)', () => {
|
||||||
|
it('Actions/Module id=null → 菜单树反查命中 (enCode 实际带 hash 后缀)', async () => {
|
||||||
|
const webapi = {
|
||||||
|
listMenu: vi.fn().mockResolvedValue({
|
||||||
|
data: [{
|
||||||
|
id: 'm1',
|
||||||
|
enCode: 'tdd5supplyrequestd89a7',
|
||||||
|
urlAddress: '/jnpf_app_tdd5/model/tdd5supplyrequestd89a7',
|
||||||
|
formId: '851759413638204101',
|
||||||
|
parentId: '-1',
|
||||||
|
}],
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
const r = await lookupByFormOrModule(webapi, {
|
||||||
|
requestedEnCode: 'tdd5supplyrequest',
|
||||||
|
formId: '851759413638204101',
|
||||||
|
})
|
||||||
|
expect(r.found).toBe(true)
|
||||||
|
expect(r.status).toBe('ok')
|
||||||
|
expect(r.actualEnCode).toBe('tdd5supplyrequestd89a7')
|
||||||
|
expect(r.requestedEnCode).toBe('tdd5supplyrequest')
|
||||||
|
expect(r.urlAddress).toContain('/model/tdd5supplyrequestd89a7')
|
||||||
|
expect(r.id).toBe('m1')
|
||||||
|
expect(r.urlMatch).toBe(true)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('反查无果 → status=failure + reason', async () => {
|
||||||
|
const webapi = { listMenu: vi.fn().mockResolvedValue({ data: [] }) }
|
||||||
|
const r = await lookupByFormOrModule(webapi, {
|
||||||
|
requestedEnCode: 'tdd5supplyrequest',
|
||||||
|
formId: 'X',
|
||||||
|
})
|
||||||
|
expect(r.found).toBe(false)
|
||||||
|
expect(r.status).toBe('failure')
|
||||||
|
expect(r.urlMatch).toBe(false)
|
||||||
|
expect(r.reason).toContain('no menu match')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('实际 urlAddress 与请求 URL 不符 → urlMatch=false 但仍 ok', async () => {
|
||||||
|
const webapi = {
|
||||||
|
listMenu: vi.fn().mockResolvedValue({
|
||||||
|
data: [{ id: 'm1', enCode: 'x', urlAddress: '/other', formId: 'X' }],
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
const r = await lookupByFormOrModule(webapi, {
|
||||||
|
requestedEnCode: 'x',
|
||||||
|
requestedUrl: '/expected',
|
||||||
|
formId: 'X',
|
||||||
|
})
|
||||||
|
expect(r.found).toBe(true)
|
||||||
|
expect(r.status).toBe('ok')
|
||||||
|
expect(r.urlMatch).toBe(false)
|
||||||
|
})
|
||||||
|
|
||||||
|
it('优先 formId 精确匹配, 退化 enCode, 退化 urlAddress', async () => {
|
||||||
|
// formId 命中: 避免 enCode 重复场景
|
||||||
|
const webapi = {
|
||||||
|
listMenu: vi.fn().mockResolvedValue({
|
||||||
|
data: [
|
||||||
|
{ id: 'a', enCode: 'other', urlAddress: '/u', formId: 'OTHER' },
|
||||||
|
{ id: 'b', enCode: 'xxx', urlAddress: '/v', formId: 'F1' },
|
||||||
|
],
|
||||||
|
}),
|
||||||
|
}
|
||||||
|
const r = await lookupByFormOrModule(webapi, {
|
||||||
|
requestedEnCode: 'other', // enCode 也匹配第一个
|
||||||
|
formId: 'F1', // 但 formId 精确匹配第二个, 优先 formId
|
||||||
|
})
|
||||||
|
expect(r.found).toBe(true)
|
||||||
|
expect(r.id).toBe('b')
|
||||||
|
expect(r.actualEnCode).toBe('xxx')
|
||||||
|
})
|
||||||
|
|
||||||
|
it('listMenu 抛错 → failure', async () => {
|
||||||
|
const webapi = { listMenu: vi.fn().mockRejectedValue(new Error('network')) }
|
||||||
|
const r = await lookupByFormOrModule(webapi, {
|
||||||
|
requestedEnCode: 'x',
|
||||||
|
formId: 'X',
|
||||||
|
})
|
||||||
|
expect(r.found).toBe(false)
|
||||||
|
expect(r.status).toBe('failure')
|
||||||
|
expect(r.reason).toContain('listMenu failed')
|
||||||
|
})
|
||||||
|
})
|
||||||
Reference in new issue
Block a user