/** * 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) { 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() }) })