diff --git a/src/commands/menu/_lookup.ts b/src/commands/menu/_lookup.ts index a78fe20..12c11a4 100644 --- a/src/commands/menu/_lookup.ts +++ b/src/commands/menu/_lookup.ts @@ -51,4 +51,8 @@ export async function lookupMenuIdByEnCode(enCode: string): Promise Promise<{ data?: any[]; list?: any[] }> }, + input: LookupInput, +): Promise { + 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, + } +} diff --git a/src/commands/menu/create.ts b/src/commands/menu/create.ts index 33b52f9..c1b8d82 100644 --- a/src/commands/menu/create.ts +++ b/src/commands/menu/create.ts @@ -26,7 +26,7 @@ import { runGuards, handleGuardResult } from '../_blindspot_guards.ts' import { validateMenuCreate, type MenuNodeLite, type FormLite } from './_validate.ts' import { enhanceFromBackend, formatErrorLine } from '../_error_codes.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 { 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 合并) @@ -392,19 +392,54 @@ export function buildMenuCreateCommand(): Command { log.ok(' ↳ 后端已自动构造 moduleId/propertyJson/urlAddress/pageAddress + 给当前角色授权') // 模式 A Actions/Module 不返 id, 用 lookup 反查 (镜像 B-15 dict) // 后端 menu enCode 默认 = form enCode, 用 form enCode 查 + // 真实资源回查: 优先 formId 反查, 命中拿 actualEnCode (含 6字符 hash 后缀) + urlAddress + urlMatch let menuId: string | null = null + let looked: Awaited> | null = null 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) + await initWebapi(c) + const webapiShim = { + listMenu: async () => { + 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 { - // 拉 form 失败也不阻断 (lookup 仍按 fullName 查) - menuId = await lookupMenuIdByEnCode(opts.fullName) + // 拉 list 失败, 退化到 B-19 lookupMenuIdByEnCode + } + 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)`) - if (opts.json) printJson({ id: menuId, formId: opts.formId, fullName: opts.fullName }) - ctx.success({ id: menuId, formId: opts.formId, fullName: opts.fullName, mode: 'A' }) + if (looked?.found && looked.actualEnCode) { + 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 = { + 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 } diff --git a/tests/unit/menu_resource_lookup.test.ts b/tests/unit/menu_resource_lookup.test.ts new file mode 100644 index 0000000..c48e12f --- /dev/null +++ b/tests/unit/menu_resource_lookup.test.ts @@ -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') + }) +})