197 lines
12 KiB
JavaScript
197 lines
12 KiB
JavaScript
import assert from 'node:assert/strict'
|
|
import { createHash } from 'node:crypto'
|
|
import { readFile } from 'node:fs/promises'
|
|
import { spawnSync } from 'node:child_process'
|
|
import path from 'node:path'
|
|
import test from 'node:test'
|
|
import { pathToFileURL } from 'node:url'
|
|
import { collectMaterialPreviewInventory, main, writeInventoryArtifacts } from './export-material-preview-health-manifest.mjs'
|
|
import { validateManifest } from './check-material-preview-health.mjs'
|
|
import { LEGACY_DOMAINS } from './check-legacy-storage-boundaries.mjs'
|
|
|
|
const current = id => `https://s3.jiansun.vip/gongxiangpan/display/${id}.webp`
|
|
const row = (id, previewUrl = current(id), materialType = '材质') => ({ id, previewUrl, materialType })
|
|
const hash = value => createHash('sha256').update(value).digest('hex')
|
|
|
|
function database(rows, { reportedCount = rows.length, findMany } = {}) {
|
|
const events = [], reads = []
|
|
const prisma = {
|
|
async $transaction(fn, options) {
|
|
events.push(['transaction', options])
|
|
return fn({
|
|
async $executeRawUnsafe(sql) { events.push(['sql', sql]) },
|
|
material: {
|
|
async count(query) { events.push(['count', query]); return reportedCount },
|
|
async findMany(query) {
|
|
reads.push(query)
|
|
events.push(['page'])
|
|
if (findMany) return findMany(query, reads.length)
|
|
const start = query.cursor ? rows.findIndex(item => item.id === query.cursor.id) + query.skip : 0
|
|
return rows.slice(start, start + query.take)
|
|
},
|
|
},
|
|
})
|
|
},
|
|
async $disconnect() { events.push(['disconnect']) },
|
|
}
|
|
return { prisma, events, reads }
|
|
}
|
|
|
|
function memoryFs({ failFile } = {}) {
|
|
const writes = [], directories = new Set()
|
|
return {
|
|
writes,
|
|
async mkdir(directory, options) {
|
|
assert.deepEqual(options, { mode: 0o700, recursive: false })
|
|
if (directories.has(directory)) throw new Error('EEXIST')
|
|
directories.add(directory)
|
|
},
|
|
async writeFile(file, raw, options) {
|
|
assert.deepEqual(options, { flag: 'wx', mode: 0o600 })
|
|
if (file.endsWith(failFile || '\0')) throw new Error('simulated write failure')
|
|
writes.push({ file, raw })
|
|
},
|
|
}
|
|
}
|
|
|
|
test('all active rows are read in a consistent read-only snapshot, with no HISTORY_IMPORT filter', async () => {
|
|
const db = database(Array.from({ length: 103 }, (_, i) => row(`m-${String(i).padStart(3, '0')}`)))
|
|
const { audit, manifests } = await collectMaterialPreviewInventory(db.prisma, { pageSize: 25 })
|
|
assert.deepEqual(db.events[0], ['transaction', { isolationLevel: 'RepeatableRead', timeout: 60_000, maxWait: 5000 }])
|
|
assert.deepEqual(db.events[1], ['sql', 'SET TRANSACTION READ ONLY'])
|
|
assert.deepEqual(db.events[2], ['count', { where: { deletedAt: null } }])
|
|
for (const query of db.reads) {
|
|
assert.deepEqual(query.where, { deletedAt: null })
|
|
assert.deepEqual(query.select, { id: true, materialType: true, previewUrl: true })
|
|
assert.deepEqual(query.orderBy, { id: 'asc' })
|
|
assert.equal(query.take, 25)
|
|
assert.ok(!JSON.stringify(query).includes('HISTORY_IMPORT'))
|
|
}
|
|
assert.deepEqual(db.reads[1].cursor, { id: 'm-024' })
|
|
assert.equal(db.reads[1].skip, 1)
|
|
assert.equal(audit.activeCount, 103)
|
|
assert.equal(audit.exportedCount, 103)
|
|
assert.equal(audit.uncheckableCount, 0)
|
|
assert.equal(audit.pages, 6, 'the final empty page proves pagination is exhausted')
|
|
assert.equal(audit.inventoryComplete, true)
|
|
assert.equal(audit.healthChecksExecuted, false)
|
|
assert.equal(audit.materialTypeCounts['材质'], 103)
|
|
assert.deepEqual(manifests.map(item => item.materials.length), [50, 50, 3])
|
|
assert.equal(new Set(manifests.flatMap(item => validateManifest(item).map(entry => entry.materialId))).size, 103)
|
|
})
|
|
|
|
test('missing and unregistered sources remain counted and cannot become a full health pass', async () => {
|
|
const secret = 'do-not-print-signed-query'
|
|
const db = database([row('one'), row('two', null), row('three', '/uploads/old.webp'),
|
|
row('four', `https://unregistered.example/image.webp?signature=${secret}`, '__proto__')])
|
|
const { audit, manifests } = await collectMaterialPreviewInventory(db.prisma)
|
|
assert.equal(audit.activeCount, 4)
|
|
assert.equal(audit.exportedCount + audit.uncheckableCount, audit.activeCount)
|
|
assert.equal(audit.uncheckableCount, 3)
|
|
assert.equal(audit.readyForHealthChecks, false)
|
|
assert.equal(audit.materialTypeCounts.__proto__, 1)
|
|
assert.deepEqual(manifests.flatMap(item => item.materials).map(item => item.materialId), ['one'])
|
|
assert.deepEqual(audit.uncheckable.map(item => item.materialId), ['two', 'three', 'four'])
|
|
assert.ok(!JSON.stringify(audit).includes(secret))
|
|
assert.ok(audit.uncheckable.every(item => /^[a-f0-9]{64}$/.test(item.sourceSha256)))
|
|
})
|
|
|
|
test('legacy baseline is exact and reports drift rather than reusing 483 as current truth', async () => {
|
|
const rows = Array.from({ length: 483 }, (_, i) => row(`legacy-${i}`, `https://s3.1080p.hk/jsxiangche/${i}.webp`))
|
|
rows.push(row('different-host', 'https://s3.yydd.ai/gongxiangpan/a.webp'), row('new-source'))
|
|
const db = database(rows)
|
|
const inventory = await collectMaterialPreviewInventory(db.prisma, { baselineHost: 's3.1080p.hk', baselineCount: 483 })
|
|
assert.deepEqual(inventory.audit.legacyBaseline, { host: 's3.1080p.hk', expectedCount: 483, actualCount: 483, delta: 0, matches: true })
|
|
assert.equal(inventory.audit.activeCount, 485)
|
|
assert.equal(inventory.audit.batchCount, 10)
|
|
const changed = await collectMaterialPreviewInventory(database(rows.slice(1)).prisma, { baselineHost: 's3.1080p.hk', baselineCount: 483 })
|
|
assert.equal(changed.audit.legacyBaseline.actualCount, 482)
|
|
assert.equal(changed.audit.legacyBaseline.matches, false)
|
|
assert.equal(changed.audit.legacyBaseline.delta, -1)
|
|
})
|
|
|
|
test('duplicates, incomplete counts, invalid pages and changed pagination fail closed', async () => {
|
|
await assert.rejects(collectMaterialPreviewInventory(database([row('same'), row('same')]).prisma), { code: 'duplicate_or_invalid_inventory_identity' })
|
|
await assert.rejects(collectMaterialPreviewInventory(database([row('one')], { reportedCount: 2 }).prisma), { code: 'inventory_count_mismatch' })
|
|
await assert.rejects(collectMaterialPreviewInventory(database([row('one')], { reportedCount: 0 }).prisma), { code: 'inventory_count_mismatch' })
|
|
await assert.rejects(collectMaterialPreviewInventory(database([], { findMany: () => null }).prisma), { code: 'invalid_inventory_page' })
|
|
await assert.rejects(collectMaterialPreviewInventory(database([row('one'), row('two')], { findMany: () => [row('one'), row('two')] }).prisma, { pageSize: 1 }), { code: 'invalid_inventory_page' })
|
|
await assert.rejects(collectMaterialPreviewInventory(database([row('one'), row('two')], { findMany: () => [row('one')] }).prisma, { pageSize: 1 }), { code: 'duplicate_or_invalid_inventory_identity' })
|
|
})
|
|
|
|
test('empty inventory is explicit and invalid limits cause no database read', async () => {
|
|
const db = database([])
|
|
const { audit, manifests } = await collectMaterialPreviewInventory(db.prisma)
|
|
assert.equal(audit.activeCount, 0)
|
|
assert.equal(audit.inventoryComplete, true)
|
|
assert.deepEqual(manifests, [])
|
|
const other = database([])
|
|
for (const options of [{ batchSize: 51 }, { pageSize: 1001 }, { baselineCount: 483 }, { baselineHost: 'evil.example', baselineCount: 1 }]) {
|
|
await assert.rejects(collectMaterialPreviewInventory(other.prisma, options))
|
|
}
|
|
assert.equal(other.events.length, 0)
|
|
})
|
|
|
|
test('fresh private files contain all batches and audit is written last with both hash identities', async () => {
|
|
const inventory = await collectMaterialPreviewInventory(database([row('one'), row('two')]).prisma, { batchSize: 1 })
|
|
const fsImpl = memoryFs()
|
|
const directory = path.resolve('test-output-not-created')
|
|
const { audit } = await writeInventoryArtifacts(directory, inventory, { fsImpl })
|
|
assert.deepEqual(fsImpl.writes.map(item => path.basename(item.file)), ['manifest-000001.json', 'manifest-000002.json', 'audit.json'])
|
|
for (let i = 0; i < 2; i++) {
|
|
assert.equal(audit.batches[i].sha256, hash(fsImpl.writes[i].raw))
|
|
assert.equal(audit.batches[i].manifestSha256, hash(JSON.stringify(inventory.manifests[i])))
|
|
assert.equal(audit.batches[i].materialCount, 1)
|
|
}
|
|
await assert.rejects(writeInventoryArtifacts(directory, inventory, { fsImpl }), /EEXIST/)
|
|
await assert.rejects(writeInventoryArtifacts('relative-output', inventory, { fsImpl }), { code: 'absolute_new_output_directory_required' })
|
|
})
|
|
|
|
test('partial disk failure cannot leave a complete audit receipt or overwrite another run', async () => {
|
|
const inventory = await collectMaterialPreviewInventory(database([row('one'), row('two')]).prisma, { batchSize: 1 })
|
|
const fsImpl = memoryFs({ failFile: 'manifest-000002.json' })
|
|
await assert.rejects(writeInventoryArtifacts(path.resolve('not-created'), inventory, { fsImpl }), /simulated write failure/)
|
|
assert.equal(fsImpl.writes.length, 1)
|
|
assert.ok(!fsImpl.writes.some(item => item.file.endsWith('audit.json')))
|
|
})
|
|
|
|
test('CLI supports injected runtime dependencies and rejects bad arguments before database access', async () => {
|
|
const db = database([row('one')])
|
|
const fsImpl = memoryFs()
|
|
const result = await main(['--output-dir', path.resolve('not-created'), '--baseline-host', 's3.1080p.hk', '--baseline-count', '483'], { prismaClient: db.prisma, fsImpl })
|
|
assert.equal(result.audit.activeCount, 1)
|
|
assert.equal(result.audit.legacyBaseline.matches, false)
|
|
assert.ok(!db.events.some(item => item[0] === 'disconnect'), 'an injected Prisma client is not owned by the CLI')
|
|
const before = db.events.length
|
|
await assert.rejects(main(['--output-dir', 'relative'], { prismaClient: db.prisma, fsImpl }), { code: 'absolute_new_output_directory_required' })
|
|
await assert.rejects(main(['--output-dir', path.resolve('not-created'), '--batch-size', '500'], { prismaClient: db.prisma, fsImpl }), { code: 'usage_output_directory_required' })
|
|
assert.equal(db.events.length, before)
|
|
})
|
|
|
|
test('stdin adapter resolves runtime CJS dependencies without deployed script files or a database connection', async () => {
|
|
const data = source => 'data:text/javascript;base64,' + Buffer.from(source).toString('base64')
|
|
const runtimeBase = pathToFileURL(path.resolve('package.json')).href
|
|
const adapt = source => source.replaceAll('import.meta.url', JSON.stringify(runtimeBase))
|
|
const registry = data(`export const LEGACY_DOMAINS=${JSON.stringify(LEGACY_DOMAINS)}`)
|
|
const health = data(adapt(await readFile(new URL('./check-material-preview-health.mjs', import.meta.url), 'utf8'))
|
|
.replace("'./check-legacy-storage-boundaries.mjs'", JSON.stringify(registry)))
|
|
const exporter = data(adapt(await readFile(new URL('./export-material-preview-health-manifest.mjs', import.meta.url), 'utf8'))
|
|
.replace("'./check-legacy-storage-boundaries.mjs'", JSON.stringify(registry))
|
|
.replace("'./check-material-preview-health.mjs'", JSON.stringify(health)))
|
|
const script = `import {createRequire} from 'node:module';
|
|
const {collectMaterialPreviewInventory}=await import(${JSON.stringify(exporter)});
|
|
const {checkMaterialPreviewHealth}=await import(${JSON.stringify(health)});
|
|
const sharp=createRequire(${JSON.stringify(runtimeBase)})('sharp');
|
|
const preview=await sharp({create:{width:3,height:2,channels:3,background:'#345678'}}).webp().toBuffer();
|
|
let pages=0;
|
|
const inventory=await collectMaterialPreviewInventory({$transaction:async fn=>fn({
|
|
$executeRawUnsafe:async sql=>{if(sql!=='SET TRANSACTION READ ONLY')throw Error('unexpected SQL')},
|
|
material:{count:async()=>1,findMany:async()=>++pages===1?[{id:'one',materialType:'材质',previewUrl:${JSON.stringify(current('one'))}}]:[]}
|
|
})});
|
|
const result=await checkMaterialPreviewHealth(inventory.manifests[0],{fetchImpl:async()=>new Response(preview,{headers:{'content-type':'image/webp'}})});
|
|
console.log(JSON.stringify({count:inventory.audit.activeCount,passed:result.passed,decoded:result.results[0].decoded}));`
|
|
const child = spawnSync(process.execPath, ['--input-type=module', '-'], { input: script, encoding: 'utf8', timeout: 10_000 })
|
|
assert.equal(child.status, 0, child.stderr)
|
|
assert.deepEqual(JSON.parse(child.stdout), { count: 1, passed: true, decoded: { width: 3, height: 2 } })
|
|
})
|