本仓 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,未来以本仓历史为主
156 lines
6.7 KiB
TypeScript
156 lines
6.7 KiB
TypeScript
/**
|
|
* workflow launch actionable task 验证 + auto-authorize 失败回执
|
|
*
|
|
* 关注 launch 后的两个强不变量:
|
|
* 1. 流程发起后, 当前用户必须能查到 actionable operator (待办/待签)
|
|
* 否则 launch "成功" 是假阳性 — 没人能办理.
|
|
* 2. --auto-authorize 失败必须有显式回执, 不允许静默吞错.
|
|
*
|
|
* verifyLaunched 收口在 _launch_verify.ts, 是纯函数 + webapi 注入契约,
|
|
* 单元测试用 vi.fn() 模拟 webapi, 不依赖真实 JNPF.
|
|
*/
|
|
import { describe, expect, it, vi } from 'vitest'
|
|
import { verifyLaunched, emitVerifyFailure } from '../../src/commands/workflow/_launch_verify.ts'
|
|
|
|
describe('workflow launch verify (Task 6)', () => {
|
|
it('launch 成功但 operator 为空 → actionableTaskCreated=false', async () => {
|
|
const webapi = {
|
|
getFlowTaskOperatorList: vi.fn().mockResolvedValue([]),
|
|
}
|
|
const r = await verifyLaunched(webapi, { flowId: 'f1', taskId: 't1', currentUserId: 'u1' })
|
|
expect(r.actionableTaskCreated).toBe(false)
|
|
expect(r.exitCode).toBe(2)
|
|
})
|
|
|
|
it('launch 成功 + 有 operator → actionableTaskCreated=true', async () => {
|
|
const webapi = {
|
|
getFlowTaskOperatorList: vi.fn().mockResolvedValue([{ id: 'op1', taskNodeId: 't1', userId: 'u1' }]),
|
|
}
|
|
const r = await verifyLaunched(webapi, { flowId: 'f1', taskId: 't1', currentUserId: 'u1' })
|
|
expect(r.actionableTaskCreated).toBe(true)
|
|
expect(r.operatorId).toBe('op1')
|
|
expect(r.exitCode).toBe(0)
|
|
})
|
|
|
|
it('--auto-authorize POST 404 → 非零退出', async () => {
|
|
const webapi = {
|
|
getFlowTaskOperatorList: vi.fn().mockResolvedValue([{ id: 'op1', userId: 'u1' }]),
|
|
postAuthorize: vi.fn().mockRejectedValue({ status: 404, message: 'Not Found' }),
|
|
}
|
|
const r = await verifyLaunched(webapi, { flowId: 'f1', taskId: 't1', currentUserId: 'u1', autoAuthorize: true })
|
|
expect(r.autoAuthorize).toBe('failed: 404')
|
|
expect(r.exitCode).toBe(3)
|
|
})
|
|
})
|
|
|
|
/**
|
|
* launch.ts 退出码传播验证 (T6 fix).
|
|
*
|
|
* 背景: 之前 launch.ts 用 ctx.partial(...) 但 partial 不调 process.exit
|
|
* (见 src/commands/_output_filter.ts:218-220), 严格策略下 verify 失败会 silent PASS.
|
|
* 改为 emitVerifyFailure 后, exitCode=2/3 直接透传到 process.exit.
|
|
*
|
|
* 这里测试 emitVerifyFailure (launch.ts 调用的 helper) 的退出码传播行为.
|
|
* 真实 launch.ts 调用链: action → verifyLaunched → emitVerifyFailure → process.exit.
|
|
*/
|
|
describe('workflow launch verify → exit code propagation (T6 fix)', () => {
|
|
it('actionableTaskCreated=false → emitVerifyFailure 调用 process.exit(2)', async () => {
|
|
const webapi = { getFlowTaskOperatorList: vi.fn().mockResolvedValue([]) }
|
|
const verify = await verifyLaunched(webapi, { flowId: 'f1', taskId: 't1', currentUserId: 'u1' })
|
|
expect(verify.exitCode).toBe(2)
|
|
|
|
const exitSpy = vi.spyOn(process, 'exit').mockImplementation((code?: string | number | null) => {
|
|
throw new Error(`PROCESS_EXIT_THROWN:${code}`)
|
|
})
|
|
const stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true)
|
|
|
|
expect(() => emitVerifyFailure(verify, { json: true })).toThrow('PROCESS_EXIT_THROWN:2')
|
|
expect(exitSpy).toHaveBeenCalledWith(2)
|
|
|
|
exitSpy.mockRestore()
|
|
stdoutSpy.mockRestore()
|
|
})
|
|
|
|
it('autoAuthorize=failed → emitVerifyFailure 调用 process.exit(3)', async () => {
|
|
const webapi = {
|
|
getFlowTaskOperatorList: vi.fn().mockResolvedValue([{ id: 'op1', userId: 'u1' }]),
|
|
postAuthorize: vi.fn().mockRejectedValue({ status: 404, message: 'Not Found' }),
|
|
}
|
|
const verify = await verifyLaunched(webapi, { flowId: 'f1', taskId: 't1', currentUserId: 'u1', autoAuthorize: true })
|
|
expect(verify.exitCode).toBe(3)
|
|
|
|
const exitSpy = vi.spyOn(process, 'exit').mockImplementation((code?: string | number | null) => {
|
|
throw new Error(`PROCESS_EXIT_THROWN:${code}`)
|
|
})
|
|
const stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true)
|
|
|
|
expect(() => emitVerifyFailure(verify, { json: true })).toThrow('PROCESS_EXIT_THROWN:3')
|
|
expect(exitSpy).toHaveBeenCalledWith(3)
|
|
|
|
exitSpy.mockRestore()
|
|
stdoutSpy.mockRestore()
|
|
})
|
|
|
|
it('exitCode=0 → emitVerifyFailure 也走 sink (sanity, launch.ts 入口守卫避免误调)', async () => {
|
|
const webapi = { getFlowTaskOperatorList: vi.fn().mockResolvedValue([{ id: 'op1', userId: 'u1' }]) }
|
|
const verify = await verifyLaunched(webapi, { flowId: 'f1', taskId: 't1', currentUserId: 'u1' })
|
|
expect(verify.exitCode).toBe(0)
|
|
|
|
const exitSpy = vi.spyOn(process, 'exit').mockImplementation((code?: string | number | null) => {
|
|
throw new Error(`PROCESS_EXIT_THROWN:${code}`)
|
|
})
|
|
const stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation(() => true)
|
|
|
|
// launch.ts 入口有 if (verify.exitCode !== 0) 守卫, 不会走到 emitVerifyFailure.
|
|
// 但 emitVerifyFailure 直接调用: exitCode=0 时也走 sink + process.exit(0) — sanity.
|
|
expect(() => emitVerifyFailure(verify, { json: true })).toThrow('PROCESS_EXIT_THROWN:0')
|
|
expect(exitSpy).toHaveBeenCalledWith(0)
|
|
|
|
exitSpy.mockRestore()
|
|
stdoutSpy.mockRestore()
|
|
})
|
|
|
|
/**
|
|
* stdout 不重复 (a455556 fix regression guard):
|
|
* - sink.onJson 提供时, emitVerifyFailure 必须 独占 stdout 路由
|
|
* - 不再额外 process.stdout.write, 否则 JSON.parse 失败 ("Extra data")
|
|
*/
|
|
it('onJson + extra 透传 → stdout 只一份 JSON (no duplicate)', async () => {
|
|
const webapi = { getFlowTaskOperatorList: vi.fn().mockResolvedValue([]) }
|
|
const verify = await verifyLaunched(webapi, { flowId: 'f1', taskId: 't1', currentUserId: 'u1' })
|
|
expect(verify.exitCode).toBe(2)
|
|
|
|
let captured = ''
|
|
const onJsonSpy = vi.fn((s: unknown) => { captured = JSON.stringify(s) })
|
|
const exitSpy = vi.spyOn(process, 'exit').mockImplementation((code?: string | number | null) => {
|
|
throw new Error(`PROCESS_EXIT_THROWN:${code}`)
|
|
})
|
|
const stdoutSpy = vi.spyOn(process.stdout, 'write').mockImplementation((chunk: any) => {
|
|
captured += String(chunk)
|
|
return true
|
|
})
|
|
|
|
expect(() =>
|
|
emitVerifyFailure(verify, {
|
|
json: true,
|
|
onJson: onJsonSpy,
|
|
extra: { flowId: 'flowX', formId: 'formY', taskId: 'taskZ' },
|
|
}),
|
|
).toThrow('PROCESS_EXIT_THROWN:2')
|
|
|
|
expect(onJsonSpy).toHaveBeenCalledTimes(1)
|
|
expect(stdoutSpy).not.toHaveBeenCalled() // sink.onJson 提供时, 不再 process.stdout.write
|
|
|
|
// 单次 JSON.parse 应成功 (无 "Extra data")
|
|
expect(() => JSON.parse(captured)).not.toThrow()
|
|
const parsed = JSON.parse(captured)
|
|
expect(parsed.verifyExitCode).toBe(2)
|
|
expect(parsed.flowId).toBe('flowX')
|
|
expect(parsed.formId).toBe('formY')
|
|
expect(parsed.taskId).toBe('taskZ')
|
|
|
|
exitSpy.mockRestore()
|
|
stdoutSpy.mockRestore()
|
|
})
|
|
})
|