setup-test-db-template.test.mjs
2.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// lib/setup-test-db-template.test.mjs — 校验生成模板 scripts/setup-test-db.mjs 的最小 DB reset 逻辑。
// 跑的是真实模板产物:复制到临时 scripts/ 下、写一个 ../config-vars.yaml、再 node 执行。
// 所有用例的 host/port 故意指向 127.0.0.1:1(必拒连),不会触碰真实库。
import { test } from 'node:test'
import assert from 'node:assert/strict'
import { spawnSync } from 'node:child_process'
import { mkdtempSync, mkdirSync, copyFileSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'
import { fileURLToPath } from 'node:url'
const TEMPLATE = fileURLToPath(new URL('../skills/plan/skeleton-gen/templates/scripts-setup-test-db-template.mjs', import.meta.url))
function runWithSchema(schemaLine) {
return runWithDb({ schemaLine })
}
function runWithDb({ host = '127.0.0.1', port = '1', user = 'root', password = 'x', schemaLine = 'schema: erp_dev' }) {
const dir = mkdtempSync(join(tmpdir(), 'erp-stdb-'))
mkdirSync(join(dir, 'scripts'))
copyFileSync(TEMPLATE, join(dir, 'scripts', 'setup-test-db.mjs'))
writeFileSync(
join(dir, 'config-vars.yaml'),
['database:', ` host: ${host}`, ` port: ${port}`, ` user: ${user}`, ` password: ${password}`, ' ' + schemaLine, ''].join('\n'),
)
return spawnSync('node', [join(dir, 'scripts', 'setup-test-db.mjs')], { encoding: 'utf8' })
}
test('setup-test-db: empty schema fails before mysql is invoked', () => {
const r = runWithSchema('schema:')
assert.equal(r.status, 1)
assert.match(r.stderr, /database\.schema/, '应是 schema 缺失报错而非连库失败 — stderr: ' + r.stderr)
})
test('setup-test-db: unfilled 【人工填写】 config placeholders fail before mysql is invoked', () => {
for (const cfg of [
{ host: '【人工填写:MySQL host】' },
{ port: '【人工填写:MySQL port】' },
{ user: '【人工填写:账号】' },
{ password: '【人工填写:密码】' },
{ schemaLine: 'schema: 【人工填写:schema 名】' },
]) {
const r = runWithDb(cfg)
assert.equal(r.status, 1)
assert.match(r.stderr, /仍是占位/, 'stderr: ' + r.stderr)
}
})
test('setup-test-db: schema with a backtick is quoted for MySQL instead of rejected', () => {
const r = runWithSchema('schema: ev`il')
assert.match(r.stdout, /`ev``il`/, 'stdout: ' + r.stdout)
assert.doesNotMatch(r.stderr, /database\.schema/, 'stderr: ' + r.stderr)
})
test('setup-test-db: an ordinary dev schema proceeds to mysql', () => {
const r = runWithSchema('schema: erp_dev')
assert.match(r.stdout, /DROP \+ CREATE `erp_dev`/, 'stdout: ' + r.stdout)
})
test('setup-test-db: non-test names are allowed in sandbox/dev workflows', () => {
const r = runWithSchema('schema: erp_prod')
assert.match(r.stdout, /DROP \+ CREATE `erp_prod`/, 'stdout: ' + r.stdout)
})