42 lines
1.8 KiB
TypeScript
42 lines
1.8 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import { AssetManager } from '@/lib/assets/assetManager'
|
|
import { buildAssetsMetadata, type AssetMetaItem } from '@/lib/exporter/assets.meta'
|
|
|
|
function u8(n: number): Uint8Array { const a = new Uint8Array(n); for (let i=0;i<n;i++) a[i]=i&0xff; return a }
|
|
|
|
describe('referencedAt matrix across modes and multiple assets', () => {
|
|
it.each([
|
|
{ mode: 'flat' as const },
|
|
{ mode: 'grouped' as const },
|
|
])('tracks only referenced originals once per asset ($mode)', async ({ mode }) => {
|
|
const am = new AssetManager({ maxBytes: 1024*1024, allowedExts: ['png','woff2'], pathStrategy: mode })
|
|
await am.add({ name: 'hero.png', type: 'image/png', bytes: u8(10) })
|
|
await am.add({ name: 'logo.png', type: 'image/png', bytes: u8(12) })
|
|
await am.add({ name: 'main.woff2', type: 'font/woff2', bytes: u8(14) })
|
|
|
|
// reference some assets multiple times; leave logo.png unreferenced
|
|
am.rewriteUrl('local:hero.png')
|
|
am.rewriteUrl('local:hero.png')
|
|
am.rewriteUrl('local:main.woff2')
|
|
|
|
const metaBytes = buildAssetsMetadata(am)
|
|
const obj = JSON.parse(new TextDecoder().decode(metaBytes)) as { assets: AssetMetaItem[] }
|
|
const byName = new Map(obj.assets.map(a => [a.originalName, a]))
|
|
|
|
const hero = byName.get('hero.png')!
|
|
const logo = byName.get('logo.png')!
|
|
const font = byName.get('main.woff2')!
|
|
|
|
expect(Array.isArray(hero.referencedAt)).toBe(true)
|
|
expect(hero.referencedAt).toContain('local:hero.png')
|
|
expect(hero.referencedAt.length).toBe(1)
|
|
|
|
expect(Array.isArray(font.referencedAt)).toBe(true)
|
|
expect(font.referencedAt).toContain('local:main.woff2')
|
|
expect(font.referencedAt.length).toBe(1)
|
|
|
|
expect(Array.isArray(logo.referencedAt)).toBe(true)
|
|
expect(logo.referencedAt.length).toBe(0)
|
|
})
|
|
})
|