57 lines
3.1 KiB
TypeScript
57 lines
3.1 KiB
TypeScript
import { describe, it, expect } from 'vitest'
|
|
import { AssetManager } from '@/lib/assets/assetManager'
|
|
import { buildAssetsIndex } from '@/lib/exporter/assets.index'
|
|
|
|
function u8(n: number): Uint8Array { const a = new Uint8Array(n); for (let i=0;i<n;i++) a[i]=i&0xff; return a }
|
|
|
|
describe('Assets index builder', () => {
|
|
it('groups assets by mime into images/fonts/other and lists zipPath', async () => {
|
|
const am = new AssetManager({ maxBytes: 1024*1024, allowedExts: ['png','woff2','txt'] })
|
|
await am.add({ name: 'a.png', type: 'image/png', bytes: u8(4) })
|
|
await am.add({ name: 'b.woff2', type: 'font/woff2', bytes: u8(6) })
|
|
await am.add({ name: 'c.txt', type: 'text/plain', bytes: u8(2) })
|
|
const bytes = buildAssetsIndex(am)
|
|
const obj = JSON.parse(new TextDecoder().decode(bytes)) as { images: string[]; fonts: string[]; other: string[] }
|
|
expect(obj.images.length).toBe(1)
|
|
expect(obj.fonts.length).toBe(1)
|
|
expect(obj.other.length).toBe(1)
|
|
expect(obj.images[0]).toMatch(/^assets\/[0-9a-f]{8}\.png$/)
|
|
expect(obj.fonts[0]).toMatch(/^assets\/[0-9a-f]{8}\.woff2$/)
|
|
expect(obj.other[0]).toMatch(/^assets\/[0-9a-f]{8}\.txt$/)
|
|
})
|
|
|
|
it('sorts each group array by path (ascending)', async () => {
|
|
const am = new AssetManager({ maxBytes: 1024*1024, allowedExts: ['png','txt','woff2'] })
|
|
// Add in shuffled order so that sort is required
|
|
await am.add({ name: 'z.png', type: 'image/png', bytes: u8(3) })
|
|
await am.add({ name: 'a.png', type: 'image/png', bytes: u8(5) })
|
|
await am.add({ name: 'm.txt', type: 'text/plain', bytes: u8(2) })
|
|
await am.add({ name: 'b.txt', type: 'text/plain', bytes: u8(4) })
|
|
await am.add({ name: 'f.woff2', type: 'font/woff2', bytes: u8(6) })
|
|
await am.add({ name: 'e.woff2', type: 'font/woff2', bytes: u8(7) })
|
|
const bytes = buildAssetsIndex(am)
|
|
const obj = JSON.parse(new TextDecoder().decode(bytes)) as { images: string[]; fonts: string[]; other: string[] }
|
|
const imgs = [...obj.images]
|
|
const fonts = [...obj.fonts]
|
|
const others = [...obj.other]
|
|
const sortedImgs = [...imgs].sort()
|
|
const sortedFonts = [...fonts].sort()
|
|
const sortedOthers = [...others].sort()
|
|
expect(imgs).toEqual(sortedImgs)
|
|
expect(fonts).toEqual(sortedFonts)
|
|
expect(others).toEqual(sortedOthers)
|
|
})
|
|
|
|
it('respects grouped pathStrategy and outputs grouped paths in index', async () => {
|
|
const am = new AssetManager({ maxBytes: 1024*1024, allowedExts: ['png','woff2','txt'], pathStrategy: 'grouped' })
|
|
await am.add({ name: 'hero.png', type: 'image/png', bytes: u8(10) })
|
|
await am.add({ name: 'main.woff2', type: 'font/woff2', bytes: u8(12) })
|
|
await am.add({ name: 'readme.txt', type: 'text/plain', bytes: u8(1) })
|
|
const bytes = buildAssetsIndex(am)
|
|
const obj = JSON.parse(new TextDecoder().decode(bytes)) as { images: string[]; fonts: string[]; other: string[] }
|
|
expect(obj.images[0]).toMatch(/^assets\/images\/[0-9a-f]{8}\.png$/)
|
|
expect(obj.fonts[0]).toMatch(/^assets\/fonts\/[0-9a-f]{8}\.woff2$/)
|
|
expect(obj.other[0]).toMatch(/^assets\/other\/[0-9a-f]{8}\.txt$/)
|
|
})
|
|
})
|