snapshot.test.js
1.1 KB
import { describe, it, expect } from 'vitest'
import { readFileSync } from 'node:fs'
import { fileURLToPath } from 'node:url'
import { dumpSchemas } from '../src/index.js'
/**
* 包内 schema gate 镜像(CI 同时跑根级 `pnpm schema:check`,双保险)。
* 当前 `dumpSchemas()` 必须与 git 内 `schema-snapshot.json` 一致,漂移即失败。
*/
const snapshotUrl = new URL('../schema-snapshot.json', import.meta.url)
/** 与 tools/scripts/schema-check.js 的 stableStringify 一致:键排序 + 2 空格缩进。 */
function sortKeys(value) {
if (Array.isArray(value)) return value.map(sortKeys)
if (value && typeof value === 'object') {
const out = {}
for (const k of Object.keys(value).sort()) out[k] = sortKeys(value[k])
return out
}
return value
}
describe('schema 快照锁定', () => {
it('dumpSchemas() 与 git 内 schema-snapshot.json 一致', () => {
const committed = readFileSync(fileURLToPath(snapshotUrl), 'utf8').replace(/\r\n/g, '\n').trim()
const current = JSON.stringify(sortKeys(dumpSchemas()), null, 2).trim()
expect(current).toBe(committed)
})
})