本仓 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,未来以本仓历史为主
90 lines
2.6 KiB
TypeScript
90 lines
2.6 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest'
|
|
import { reusableCreate, reuseSuccessPayload } from '../../src/commands/_reusable_create.ts'
|
|
|
|
describe('reusableCreate wrapper', () => {
|
|
it('runs create when reuseIfExists=false', async () => {
|
|
const runCreate = vi.fn()
|
|
const result = await reusableCreate({
|
|
cmdName: 'test',
|
|
enCode: 'x',
|
|
reuseIfExists: false,
|
|
findExisting: vi.fn(),
|
|
runCreate,
|
|
})
|
|
expect(result.reused).toBe(false)
|
|
expect(runCreate).toHaveBeenCalledOnce()
|
|
})
|
|
|
|
it('runs create when enCode is empty', async () => {
|
|
const runCreate = vi.fn()
|
|
const result = await reusableCreate({
|
|
cmdName: 'test',
|
|
enCode: '',
|
|
reuseIfExists: true,
|
|
findExisting: vi.fn(),
|
|
runCreate,
|
|
})
|
|
expect(result.reused).toBe(false)
|
|
expect(runCreate).toHaveBeenCalledOnce()
|
|
})
|
|
|
|
it('returns existing when found and reuseIfExists=true', async () => {
|
|
const findExisting = vi.fn().mockResolvedValue({ id: '123', enCode: 'x', state: 1 })
|
|
const runCreate = vi.fn()
|
|
const result = await reusableCreate({
|
|
cmdName: 'test',
|
|
enCode: 'x',
|
|
reuseIfExists: true,
|
|
findExisting,
|
|
runCreate,
|
|
})
|
|
expect(result.reused).toBe(true)
|
|
if (result.reused) {
|
|
expect(result.existing.id).toBe('123')
|
|
expect(result.existing.state).toBe(1)
|
|
}
|
|
expect(runCreate).not.toHaveBeenCalled()
|
|
})
|
|
|
|
it('runs create when findExisting returns null (enCode不存在)', async () => {
|
|
const findExisting = vi.fn().mockResolvedValue(null)
|
|
const runCreate = vi.fn()
|
|
const result = await reusableCreate({
|
|
cmdName: 'test',
|
|
enCode: 'x',
|
|
reuseIfExists: true,
|
|
findExisting,
|
|
runCreate,
|
|
})
|
|
expect(result.reused).toBe(false)
|
|
expect(runCreate).toHaveBeenCalledOnce()
|
|
})
|
|
|
|
it('runs create when findExisting throws (容错不阻断)', async () => {
|
|
const findExisting = vi.fn().mockRejectedValue(new Error('network down'))
|
|
const runCreate = vi.fn()
|
|
const result = await reusableCreate({
|
|
cmdName: 'test',
|
|
enCode: 'x',
|
|
reuseIfExists: true,
|
|
findExisting,
|
|
runCreate,
|
|
})
|
|
expect(result.reused).toBe(false)
|
|
expect(runCreate).toHaveBeenCalledOnce()
|
|
})
|
|
})
|
|
|
|
describe('reuseSuccessPayload', () => {
|
|
it('returns uniform shape for ctx.success', () => {
|
|
const out = reuseSuccessPayload({ id: '42', enCode: 'foo', state: 1, fullName: 'Foo' }, 'reuse-if-exists')
|
|
expect(out).toEqual({
|
|
enCode: 'foo',
|
|
id: '42',
|
|
fullName: 'Foo',
|
|
state: 1,
|
|
reused: true,
|
|
mode: 'reuse-if-exists',
|
|
})
|
|
})
|
|
}) |