32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import { AssetManager } from '@/lib/assets/assetManager'
|
|
import { buildAssetsMetadata, type AssetMetaItem } from '@/lib/exporter/assets.meta'
|
|
import crypto from 'node:crypto'
|
|
|
|
function u8pattern(n: number): Uint8Array {
|
|
const arr = new Uint8Array(n)
|
|
for (let i = 0; i < n; i++) arr[i] = (i * 131 + 7) & 0xff
|
|
return arr
|
|
}
|
|
|
|
function sha256B64(bytes: Uint8Array): string {
|
|
const h = crypto.createHash('sha256')
|
|
h.update(Buffer.from(bytes))
|
|
return h.digest('base64')
|
|
}
|
|
|
|
describe('assets.json integrity = real SHA-256(base64)', () => {
|
|
it('integrity equals sha256-<base64(sha256(bytes))>', async () => {
|
|
const am = new AssetManager({ maxBytes: 1024 * 1024, allowedExts: ['png'] })
|
|
const bytes = u8pattern(64)
|
|
await am.add({ name: 'hero.png', type: 'image/png', bytes })
|
|
const out = buildAssetsMetadata(am)
|
|
const json = new TextDecoder().decode(out)
|
|
const obj = JSON.parse(json) as { assets: AssetMetaItem[] }
|
|
expect(obj.assets.length).toBe(1)
|
|
const a = obj.assets[0]
|
|
const expected = 'sha256-' + sha256B64(bytes)
|
|
expect(a.integrity).toBe(expected)
|
|
})
|
|
})
|