Files
dacli/tests/unit/menu_actions.test.ts
sushub f403a7e80e import: ssw/dacli@eea0ebc (2026-08-12) 起点版本
本仓 2026-08-12 从 ssw main (192.168.1.18:13300/ssw/ssw-mvp) 的
dacli/ subpath 抽取,作为独立仓托管到 gitea:3000/ssw/dacli.git。

- 工作树为 ssw main dacli subpath 最新版 (110 commit, 08-12)
- 历史未带 ssw main 整体历史(避免大仓污染)
- 此为新仓基线 commit,未来以本仓历史为主
2026-08-12 15:58:06 +08:00

179 lines
5.8 KiB
TypeScript

/**
* unit/menu_actions.test.ts — fetchAllFormReleasedMenus 单测
*
* 覆盖:
* - 拉 /visualdev/Base 拿 form 列表
* - 逐个 form 调 /visualdev/Base/{id}/getReleaseMenu
* - pcIsRelease=1 + pcReleaseName → 收一条 PC 记录
* - 同一菜单名 (pc/app) 多次出现要去重
* - 单 form 失败不影响整体 (catch 后跳过, failed++ 计数)
* - pcIsRelease=0/null 的 form 不入结果
*/
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
import { fetchAllFormReleasedMenus } from '../../src/commands/menu/_helpers.ts'
import { JnpfClient } from '../../src/core/client.ts'
interface CapturedCall {
url: string
method: string
}
let calls: CapturedCall[] = []
let stubbed: ReturnType<typeof vi.fn> | null = null
beforeEach(() => {
calls = []
stubbed = vi.fn(async (url: string, init?: RequestInit) => {
const method = init?.method || 'GET'
calls.push({ url, method })
// /visualdev/Base → 返 form 列表 (注意: 必须排除 /visualdev/Base/{id}/getReleaseMenu)
if (url.includes('/visualdev/Base') && !url.includes('/getReleaseMenu')) {
return {
text: async () => JSON.stringify({
code: 200,
msg: 'ok',
data: {
list: [
{ id: 'F1', enCode: 'jxcj_competition', fullName: '赛事' },
{ id: 'F2', enCode: 'jxcj_player', fullName: '选手' },
{ id: 'F3', enCode: 'jxcj_draft', fullName: '草稿' }, // pcIsRelease=0
{ id: 'F4', enCode: 'jxcj_broken', fullName: '坏' }, // getReleaseMenu 抛错
],
},
}),
} as Response
}
// 单 form getReleaseMenu
const m = url.match(/\/visualdev\/Base\/(F[1-4])\/getReleaseMenu/)
if (m) {
const fid = m[1]
if (fid === 'F4') {
// 模拟网络异常
throw new Error('network error')
}
if (fid === 'F3') {
// 模拟未发布
return {
text: async () => JSON.stringify({ code: 200, msg: 'ok', data: { pcIsRelease: 0, appIsRelease: 0 } }),
} as Response
}
// F1: PC released; F2: APP released
if (fid === 'F1') {
return {
text: async () => JSON.stringify({
code: 200, msg: 'ok',
data: {
pcIsRelease: 1,
pcReleaseName: '赛事管理/赛事列表',
appIsRelease: 0,
},
}),
} as Response
}
if (fid === 'F2') {
return {
text: async () => JSON.stringify({
code: 200, msg: 'ok',
data: {
pcIsRelease: 0,
appIsRelease: 1,
appReleaseName: '选手管理/选手列表',
},
}),
} as Response
}
}
return {
text: async () => JSON.stringify({ code: 404, msg: 'unmocked' }),
} as Response
})
vi.stubGlobal('fetch', stubbed)
})
afterEach(() => {
vi.unstubAllGlobals()
})
function makeClient(): JnpfClient {
const c = new JnpfClient({
baseUrl: 'http://127.0.0.1:30000/api',
appCode: 'jxcj',
account: 'admin',
password: 'x',
})
c.token = 'mock-token'
return c
}
describe('fetchAllFormReleasedMenus', () => {
it('遍历 form 列表, 收集 pc/app release, 失败 form 跳过', async () => {
const c = makeClient()
const r = await fetchAllFormReleasedMenus(c)
// 扫了 4 个 form, 1 个失败 (F4)
expect(r.scanned).toBe(4)
expect(r.failed).toBe(1)
// F1 → PC, F2 → APP, F3/F4 → 跳过
expect(r.menus.length).toBe(2)
const pc = r.menus.find((m) => m.pcName)
const app = r.menus.find((m) => m.appName)
expect(pc?.formId).toBe('F1')
expect(pc?.formEnCode).toBe('jxcj_competition')
expect(pc?.pcName).toBe('赛事管理/赛事列表')
expect(pc?.pcIsRelease).toBe(1)
expect(app?.formId).toBe('F2')
expect(app?.formEnCode).toBe('jxcj_player')
expect(app?.appName).toBe('选手管理/选手列表')
})
it('调用顺序: 先 list, 然后逐个 getReleaseMenu', async () => {
const c = makeClient()
await fetchAllFormReleasedMenus(c)
// 1 次 list + 4 次 getReleaseMenu
const listCall = calls.find((c) => c.url.includes('/visualdev/Base') && !c.url.includes('/getReleaseMenu'))
expect(listCall).toBeTruthy()
const getCalls = calls.filter((c) => /\/visualdev\/Base\/(F[1-4])\/getReleaseMenu/.test(c.url))
expect(getCalls.length).toBe(4)
})
it('同一菜单名 (跨 form) 去重', async () => {
const dedupStub = vi.fn(async (url: string) => {
if (url.includes('/visualdev/Base') && !url.includes('/getReleaseMenu')) {
return { text: async () => JSON.stringify({ code: 200, msg: 'ok', data: { list: [
{ id: 'F1', enCode: 'a' },
{ id: 'F2', enCode: 'b' },
] } }) } as Response
}
if (/\/visualdev\/Base\/(F[12])\/getReleaseMenu/.test(url)) {
return { text: async () => JSON.stringify({ code: 200, msg: 'ok', data: {
pcIsRelease: 1, pcReleaseName: 'SAME/PC/PATH',
appIsRelease: 1, appReleaseName: 'SAME/APP/PATH',
} }) } as Response
}
return { text: async () => JSON.stringify({ code: 404 }) } as Response
})
vi.stubGlobal('fetch', dedupStub)
const c = makeClient()
const r = await fetchAllFormReleasedMenus(c)
expect(r.menus.length).toBe(2) // pc 1 条 + app 1 条 (各去重一次)
expect(r.menus.find((m) => m.pcName === 'SAME/PC/PATH')).toBeTruthy()
expect(r.menus.find((m) => m.appName === 'SAME/APP/PATH')).toBeTruthy()
})
it('onProgress 回调被调用', async () => {
const c = makeClient()
const progress: Array<[number, number]> = []
await fetchAllFormReleasedMenus(c, {
onProgress: (done, total) => progress.push([done, total]),
})
expect(progress.length).toBe(4)
expect(progress[progress.length - 1]).toEqual([4, 4])
})
})