Files
dacli/tests/unit/form_table_ssw_fail.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

131 lines
4.1 KiB
TypeScript

/**
* Command-level contract for `form create --table` when SSW AI is unavailable.
* The table path must fail closed: no local schema fallback and no form publish.
*/
import { beforeEach, afterEach, describe, expect, it, vi } from 'vitest'
const mocks = vi.hoisted(() => ({
fromToken: vi.fn(),
initDmWebapi: vi.fn(),
getDataModelFieldList: vi.fn(),
create: vi.fn(),
release: vi.fn(),
errors: [] as string[],
stdout: [] as string[],
exit: vi.fn(),
}))
vi.mock('../../src/core/client.ts', () => ({
JnpfClient: { fromToken: (...args: unknown[]) => mocks.fromToken(...args) },
}))
vi.mock('../../src/core/log.ts', () => ({
log: {
setQuiet: vi.fn(),
setJsonMode: vi.fn(),
isJsonMode: vi.fn(),
isQuiet: vi.fn(() => true),
flush: vi.fn(),
discard: vi.fn(),
info: vi.fn(),
warn: vi.fn(),
error: (...args: unknown[]) => mocks.errors.push(args.map(String).join(' ')),
ok: vi.fn(),
dim: vi.fn(),
raw: vi.fn(),
debug: vi.fn(),
},
printJson: vi.fn(),
}))
vi.mock('../../src/webapi/dataModel.ts', () => ({
initWebapi: (...args: unknown[]) => mocks.initDmWebapi(...args),
loadDataModel: { getDataModelFieldList: (...args: unknown[]) => mocks.getDataModelFieldList(...args) },
}))
vi.mock('../../src/webapi/visualDev.ts', () => ({
initWebapi: vi.fn(),
loadVisualDev: { create: (...args: unknown[]) => mocks.create(...args), release: (...args: unknown[]) => mocks.release(...args) },
}))
vi.mock('../../src/core/logfile.ts', () => ({
saveCommandLog: vi.fn(),
formatOneLineSummary: vi.fn(() => 'form create failed'),
}))
vi.spyOn(process, 'exit').mockImplementation(((code?: number) => {
mocks.exit(code)
}) as never)
import { buildFormCreateCommand } from '../../src/commands/form/create.ts'
let originalWrite: typeof process.stdout.write
beforeEach(() => {
mocks.fromToken.mockResolvedValue({
appCode: 'eventhub',
dbLinkId: 'db-1',
postJson: vi.fn(),
resolveDbLinkId: (value?: string) => value || 'db-1',
})
mocks.initDmWebapi.mockResolvedValue(undefined)
mocks.getDataModelFieldList.mockResolvedValue({
list: [{ field: 'f_name', fieldName: '名称', dataType: 'varchar' }],
})
mocks.create.mockReset()
mocks.release.mockReset()
mocks.errors = []
mocks.stdout = []
mocks.exit.mockReset()
originalWrite = process.stdout.write.bind(process.stdout)
process.stdout.write = ((chunk: string | Uint8Array): boolean => {
mocks.stdout.push(typeof chunk === 'string' ? chunk : new TextDecoder().decode(chunk))
return true
}) as typeof process.stdout.write
})
afterEach(() => {
process.stdout.write = originalWrite
})
async function runTableCreateWith(postJson: ReturnType<typeof vi.fn>) {
const client = await mocks.fromToken()
client.postJson = postJson
mocks.fromToken.mockResolvedValue(client)
const command = buildFormCreateCommand()
await command.parseAsync([
'node', 'test',
'--table', 'event_signup',
'--enCode', 'eventSignup',
'--fullName', '活动报名',
'--no-verify',
])
return [...mocks.stdout, ...mocks.errors].join('\n')
}
describe('form create --table: SSW AI failure is blocking', () => {
it('code=400 with empty data fails with diagnostics and skips create/publish', async () => {
const postJson = vi.fn().mockResolvedValue({ code: 400, msg: 'AI unavailable', data: {} })
const output = await runTableCreateWith(postJson)
expect(mocks.exit).toHaveBeenCalledWith(1)
expect(output).toContain('base_ai')
expect(output).toContain('/api/ssw/ai/openai-proxy')
expect(mocks.create).not.toHaveBeenCalled()
expect(mocks.release).not.toHaveBeenCalled()
})
it('thrown infer-fields request fails with diagnostics and skips create/publish', async () => {
const postJson = vi.fn().mockRejectedValue(new Error('proxy network unavailable'))
const output = await runTableCreateWith(postJson)
expect(mocks.exit).toHaveBeenCalledWith(1)
expect(output).toContain('base_ai')
expect(output).toContain('/api/ssw/ai/openai-proxy')
expect(mocks.create).not.toHaveBeenCalled()
expect(mocks.release).not.toHaveBeenCalled()
})
})