gongxiangp2/scripts/check-kb-contract-drift.test.mjs

80 lines
3.9 KiB
JavaScript

import assert from 'node:assert/strict'
import { spawnSync } from 'node:child_process'
import { mkdirSync, mkdtempSync, rmSync, writeFileSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { dirname, join } from 'node:path'
import { fileURLToPath } from 'node:url'
import test from 'node:test'
const script = fileURLToPath(new URL('./check-kb-contract-drift.mjs', import.meta.url))
const binding = {
inventory: 'DEMO/inventory.json',
mapping: 'DEMO/map.json',
original: 'DEMO/detail.psd',
}
const bindings = { detail: binding }
const assets = [
{ path: binding.original, type: 'original', component: 'detail' },
{ path: 'DEMO/font.otf', type: 'font', component: 'shared' },
]
test('compares current JinBao releases and ignores legacy keys and mapping version differences', t => {
const root = mkdtempSync(join(tmpdir(), 'kb-contract-'))
t.after(() => rmSync(root, { recursive: true, force: true }))
const source = join(root, 'source')
const release = join(root, 'release')
const active = join(release, 'DEMO', '2026-12-05.1')
function write(dir, name, value) {
const path = join(dir, name)
mkdirSync(dirname(path), { recursive: true })
writeFileSync(path, JSON.stringify(value))
}
const mapping = {
fields: [{ field_id: 'D01-image', screen_id: 'D01', type: '图片', ps_location: { path: 'Group/Product' } }],
screens: [{ screen_id: 'D01', bounds: [0, 0, 100, 100] }],
}
const rows = [{ id: 10, asset_id: 'original', path: 'Group/Product' }]
const sourceAsset = { id: 'original', sha256: 'AB', canvas: [100, 100] }
const releasedAsset = { asset_id: 'original', ps_sha256: 'ab', canvas_px: [100, 100] }
const production = { intake: { barcodeFields: [{ id: 'color', type: 'text', required: true }] } }
write(source, 'DEMO/小壹/release.json', { bindings, assets })
write(source, binding.mapping, { ...mapping, ps_assets: [sourceAsset] })
write(source, binding.inventory, { rows })
write(source, 'DEMO/小壹/production.json', production)
write(release, 'DEMO/index.json', { current: '2026-12-05.1' })
write(active, 'manifest.json', {
templateId: 'DEMO',
supplement: 'DEMO/小壹/无人值守补充说明.md',
productionConfig: 'DEMO/小壹/production.json',
bindings,
assets: assets.map(asset => asset.type === 'original' ? { ...asset, sha256: 'ab' } : asset),
files: ['inventory', 'mapping'].map(type => ({ path: binding[type], type })),
})
write(active, binding.mapping, { ...mapping, mapping_version: '1', source: { assets: [releasedAsset] } })
write(active, binding.inventory, { rows, mapping_version: '1' })
write(active, 'DEMO/小壹/production.json', production)
const check = dir => spawnSync(process.execPath, [script, source, dir], { encoding: 'utf8' })
const match = check(release)
assert.equal(match.status, 0, match.stderr || match.stdout)
assert.match(match.stdout, /\[DEMO\] 契约一致/u)
const changedMapping = { ...mapping, fields: [{ ...mapping.fields[0], ps_location: { path: 'Group/NewProduct' } }] }
write(source, binding.mapping, { ...changedMapping, ps_assets: [sourceAsset] })
write(source, binding.inventory, { rows: [...rows, { id: 11, asset_id: 'original', path: 'Group/Detail' }] })
write(source, 'DEMO/小壹/production.json', { intake: { barcodeFields: [{ id: 'color', type: 'text' }] } })
const diff = check(active)
assert.equal(diff.status, 1, diff.stderr || diff.stdout)
assert.match(diff.stdout, /detail\/mapping 字段 D01-image.*Group\/NewProduct.*Group\/Product/u)
assert.match(diff.stdout, /\+ 待补入小壹包 detail\/inventory 图层 original\/11/u)
assert.match(diff.stdout, /intake\/barcodeFields color/u)
assert.match(diff.stdout, /契约差异 3 条/u)
rmSync(join(active, binding.inventory))
const missing = check(release)
assert.equal(missing.status, 2, missing.stderr || missing.stdout)
assert.match(missing.stderr, /inventory\.json 无法读取为 JSON/u)
})