promote-to-source-template.test.mjs
2.28 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
// lib/promote-to-source-template.test.mjs — 校验生成模板 scripts/promote-to-source.mjs 的离线守卫。
// 只覆盖任何 mysql 调用 / 文件锁之前的本地校验(占位 / 源库名空 / COPY===SOURCE 守卫);
// delta 计算与迁移重放需真实 DB,不在离线测试范围。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-promote-to-source-template.mjs', import.meta.url))
function runWithDb({ host = '127.0.0.1', port = '1', user = 'root', password = 'x', schemaLine = 'schema: erp_dev', env = {} } = {}) {
const dir = mkdtempSync(join(tmpdir(), 'erp-prom-'))
mkdirSync(join(dir, 'scripts'))
copyFileSync(TEMPLATE, join(dir, 'scripts', 'promote-to-source.mjs'))
writeFileSync(
join(dir, 'config-vars.yaml'),
['database:', ` host: ${host}`, ` port: ${port}`, ` user: ${user}`, ` password: ${password}`, ' ' + schemaLine, ''].join('\n'),
)
const childEnv = { ...process.env, ...env }
if (!('ERP_TEST_DB_SCHEMA' in env)) delete childEnv.ERP_TEST_DB_SCHEMA
return spawnSync('node', [join(dir, 'scripts', 'promote-to-source.mjs')], { encoding: 'utf8', env: childEnv })
}
test('promote-to-source: unfilled placeholders fail before lock/mysql', () => {
const r = runWithDb({ schemaLine: 'schema: 【人工填写:schema 名】' })
assert.equal(r.status, 1)
assert.match(r.stderr, /仍是占位/, 'stderr: ' + r.stderr)
})
test('promote-to-source: empty source schema fails before lock/mysql', () => {
const r = runWithDb({ schemaLine: 'schema:' })
assert.equal(r.status, 1)
assert.match(r.stderr, /database\.schema/, 'stderr: ' + r.stderr)
})
// COPY===SOURCE 守卫先于文件锁与任何 mysql 调用。
test('promote-to-source: copy name equal to source is rejected before lock/mysql', () => {
const r = runWithDb({ env: { ERP_TEST_DB_SCHEMA: 'erp_dev' } })
assert.equal(r.status, 1)
assert.match(r.stderr, /副本名与源库名相同/, 'stderr: ' + r.stderr)
})