- dacli/ 整目录被 .gitignore 忽略,本次 force-add 其业务源码(766 文件) - 排除 node_modules/.env(含 PGPASSWORD 等敏感信息)/.DS_Store/__pycache__/*.pyc - 新增 dacli/.gitignore 长期管理内部噪声,新增文件仍需 git add -f - 根 .gitignore 补充 .cache/ 与诊断脚本忽略,并加 dacli 段说明
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',
|
|
})
|
|
})
|
|
}) |