97 lines
4.1 KiB
JavaScript
97 lines
4.1 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import { mkdtemp, mkdir, readFile, rm, writeFile } from 'node:fs/promises'
|
|
import os from 'node:os'
|
|
import path from 'node:path'
|
|
import test from 'node:test'
|
|
import { fileURLToPath } from 'node:url'
|
|
|
|
import { validateRuntimeAssetRegistry } from '../scripts/runtime-asset-inventory.mjs'
|
|
|
|
const runtimeManifest = (scope, imports = []) => ({
|
|
schemaVersion: 1,
|
|
kind: 'runtime-asset-inventory',
|
|
scope,
|
|
imports,
|
|
assets: [],
|
|
})
|
|
|
|
const testDirectory = path.dirname(fileURLToPath(import.meta.url))
|
|
const pipelineDirectory = path.resolve(testDirectory, '..')
|
|
const workspaceDirectory = path.resolve(pipelineDirectory, '..')
|
|
const realManifestsDirectory = path.join(pipelineDirectory, 'manifests')
|
|
|
|
test('顶层注册表拒绝未进入导入闭包的正式 owner', async (t) => {
|
|
const workspace = await mkdtemp(path.join(os.tmpdir(), 'jiapu-runtime-registry-'))
|
|
t.after(() => rm(workspace, { recursive: true, force: true }))
|
|
const manifestsDirectory = path.join(workspace, 'design-pipeline', 'manifests')
|
|
await mkdir(manifestsDirectory, { recursive: true })
|
|
const write = (name, value) => writeFile(path.join(manifestsDirectory, name), `${JSON.stringify(value)}\n`, 'utf8')
|
|
|
|
await write('runtime-assets.json', runtimeManifest('schema-v3'))
|
|
await write('orphan.json', runtimeManifest('orphan'))
|
|
|
|
await assert.rejects(
|
|
() => validateRuntimeAssetRegistry(
|
|
path.join(manifestsDirectory, 'runtime-assets.json'),
|
|
workspace,
|
|
manifestsDirectory,
|
|
),
|
|
/unregistered manifest/i,
|
|
)
|
|
})
|
|
|
|
test('真实 schema v3 注册表覆盖应用资产与保留生成资产', async () => {
|
|
const registry = JSON.parse(await readFile(path.join(realManifestsDirectory, 'runtime-assets.json'), 'utf8'))
|
|
const auth = JSON.parse(await readFile(path.join(realManifestsDirectory, 'auth-runtime-assets.json'), 'utf8'))
|
|
const retained = JSON.parse(await readFile(path.join(realManifestsDirectory, 'retained-generated-runtime-assets.json'), 'utf8'))
|
|
|
|
assert.equal(registry.scope, 'schema-v3')
|
|
assert.deepEqual(registry.imports, [
|
|
'design-pipeline/manifests/auth-runtime-assets.json',
|
|
'design-pipeline/manifests/application-runtime-assets.json',
|
|
'design-pipeline/manifests/retained-generated-runtime-assets.json',
|
|
])
|
|
assert.deepEqual(auth.imports, [])
|
|
assert.equal(retained.assets.length, 11)
|
|
assert(retained.assets.every((asset) => asset.provenance === 'committed-binary'))
|
|
assert(retained.assets.every((asset) => asset.rebuildable === false))
|
|
await validateRuntimeAssetRegistry(
|
|
path.join(realManifestsDirectory, 'runtime-assets.json'),
|
|
workspaceDirectory,
|
|
realManifestsDirectory,
|
|
)
|
|
})
|
|
|
|
test('注册表根 scope 不是 schema-v3 时拒绝验证', async (t) => {
|
|
const workspace = await mkdtemp(path.join(os.tmpdir(), 'jiapu-runtime-registry-scope-'))
|
|
t.after(() => rm(workspace, { recursive: true, force: true }))
|
|
const manifestsDirectory = path.join(workspace, 'design-pipeline', 'manifests')
|
|
await mkdir(manifestsDirectory, { recursive: true })
|
|
const root = path.join(manifestsDirectory, 'runtime-assets.json')
|
|
await writeFile(root, `${JSON.stringify(runtimeManifest('auth'))}\n`, 'utf8')
|
|
|
|
await assert.rejects(
|
|
() => validateRuntimeAssetRegistry(root, workspace, manifestsDirectory),
|
|
/root scope must be schema-v3/i,
|
|
)
|
|
})
|
|
|
|
test('未声明的旧格式或未知 kind 清单不能被静默跳过', async (t) => {
|
|
const workspace = await mkdtemp(path.join(os.tmpdir(), 'jiapu-runtime-registry-unknown-'))
|
|
t.after(() => rm(workspace, { recursive: true, force: true }))
|
|
const manifestsDirectory = path.join(workspace, 'design-pipeline', 'manifests')
|
|
await mkdir(manifestsDirectory, { recursive: true })
|
|
const root = path.join(manifestsDirectory, 'runtime-assets.json')
|
|
await writeFile(root, `${JSON.stringify(runtimeManifest('schema-v3'))}\n`, 'utf8')
|
|
await writeFile(
|
|
path.join(manifestsDirectory, 'unknown.json'),
|
|
`${JSON.stringify({ schemaVersion: 1, kind: 'legacy-owner', outputs: [] })}\n`,
|
|
'utf8',
|
|
)
|
|
|
|
await assert.rejects(
|
|
() => validateRuntimeAssetRegistry(root, workspace, manifestsDirectory),
|
|
/undeclared legacy manifest/i,
|
|
)
|
|
})
|