Files
dacli/tests/unit/datasource.test.ts
T

86 lines
2.7 KiB
TypeScript

/**
* datasource.test.ts — datasource 父命令烟囱测试
*
* 不依赖 JNPF, 只验证 commander 能解析命令树 + --ai-help/--example/--source
*/
import { describe, it, expect } from 'vitest'
import { execFileSync } from 'node:child_process'
import { resolve } from 'node:path'
import { fileURLToPath } from 'node:url'
const __dirname = fileURLToPath(new URL('.', import.meta.url))
const CLI = resolve(__dirname, '../../src/cli.ts')
function run(args: string[]): string {
try {
return execFileSync('npx', ['tsx', CLI, ...args], {
encoding: 'utf-8',
stdio: ['pipe', 'pipe', 'pipe'],
timeout: 30_000,
})
} catch (e) {
const err = e as { stdout?: Buffer | string; status?: number }
return (err.stdout?.toString() || '')
}
}
describe('datasource command tree (W8-D1)', () => {
it('datasource --help 列出 4 个子命令', () => {
const out = run(['datasource', '--help'])
expect(out).toContain('list')
expect(out).toContain('create')
expect(out).toContain('test')
expect(out).toContain('delete')
})
it('datasource list --help 描述正确', () => {
const out = run(['datasource', 'list', '--help'])
expect(out).toContain('列出数据源')
})
it('datasource create --help 包含 required 标记', () => {
const out = run(['datasource', 'create', '--help'])
expect(out).toContain('--fullName')
expect(out).toContain('--enCode')
expect(out).toContain('--dbType')
expect(out).toContain('--host')
expect(out).toContain('--port')
expect(out).toContain('--user')
})
it('datasource test --help 包含 --id', () => {
const out = run(['datasource', 'test', '--help'])
expect(out).toContain('--id')
})
it('datasource delete --help 包含 --id 和 --yes', () => {
const out = run(['datasource', 'delete', '--help'])
expect(out).toContain('--id')
expect(out).toContain('--yes')
})
it('datasource list --ai-help 输出 markdown', () => {
const out = run(['datasource', 'list', '--ai-help'])
expect(out).toContain('## 用途')
expect(out).toContain('## 来源')
expect(out).toContain('/api/system/DataSource')
})
it('datasource create --example 输出可执行命令', () => {
const out = run(['datasource', 'create', '--example'])
expect(out).toContain('dacli datasource create')
expect(out).toContain('--dbType=MySQL')
})
it('datasource list --source 链接 web 源码', () => {
const out = run(['datasource', 'list', '--source'])
expect(out).toContain('web:')
expect(out).toContain('api:')
})
it('顶层 --help 包含 datasource', () => {
const out = run(['--help'])
expect(out).toContain('datasource')
})
})