144 files moved with git mv (pure renames). Root package.json delegates dev/typecheck/test:run/lint/build to the packages; Node 24 LTS and pnpm 10.13.1 pinned (.nvmrc, .node-version, engines, packageManager, root devDependency, engine-strict). Dockerfile builds @faunosteo/app on node:24-alpine and copies apps/app/dist; nginx and runtime config unchanged. CI workflow for Gitea Actions. packages/ reserved (README only). Docs synced: WORKSPACE.md, CLAUDE.md, README, DEPLOY, CHANGELOG, plan. 271/271 tests, typecheck, lint, build green from the root.
86 lines
2.5 KiB
TypeScript
86 lines
2.5 KiB
TypeScript
import { describe, it, expect, vi, beforeEach } from 'vitest'
|
|
import { rasterizeSvg } from '@/pdf/render/svgToPng'
|
|
|
|
const PNG_BYTES = new Uint8Array([
|
|
0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x00,
|
|
])
|
|
|
|
class MockImage {
|
|
decoding: 'sync' | 'async' | 'auto' = 'auto'
|
|
crossOrigin: string | null = null
|
|
onload: (() => void) | null = null
|
|
onerror: ((err?: unknown) => void) | null = null
|
|
private _src = ''
|
|
get src(): string {
|
|
return this._src
|
|
}
|
|
set src(value: string) {
|
|
this._src = value
|
|
queueMicrotask(() => this.onload?.())
|
|
}
|
|
}
|
|
|
|
describe('rasterizeSvg', () => {
|
|
beforeEach(() => {
|
|
vi.stubGlobal('Image', MockImage)
|
|
vi.stubGlobal('URL', {
|
|
createObjectURL: () => 'blob:mock',
|
|
revokeObjectURL: () => undefined,
|
|
})
|
|
})
|
|
|
|
it('returns PNG bytes for a valid SVG', async () => {
|
|
const canvas = document.createElement('canvas')
|
|
canvas.getContext = (() => ({
|
|
fillStyle: '',
|
|
fillRect: () => undefined,
|
|
drawImage: () => undefined,
|
|
})) as unknown as HTMLCanvasElement['getContext']
|
|
canvas.toBlob = ((cb: BlobCallback) => {
|
|
cb(new Blob([PNG_BYTES], { type: 'image/png' }))
|
|
}) as HTMLCanvasElement['toBlob']
|
|
vi.spyOn(document, 'createElement').mockImplementation(
|
|
((tag: string) =>
|
|
tag === 'canvas'
|
|
? canvas
|
|
: ({}) as HTMLElement) as typeof document.createElement,
|
|
)
|
|
|
|
const out = await rasterizeSvg(
|
|
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10"/>',
|
|
)
|
|
expect(out).toBeInstanceOf(Uint8Array)
|
|
expect(out[0]).toBe(0x89)
|
|
expect(out[1]).toBe(0x50)
|
|
expect(out[2]).toBe(0x4e)
|
|
expect(out[3]).toBe(0x47)
|
|
})
|
|
|
|
it('falls back to toDataURL when toBlob returns null', async () => {
|
|
const canvas = document.createElement('canvas')
|
|
canvas.getContext = (() => ({
|
|
fillStyle: '',
|
|
fillRect: () => undefined,
|
|
drawImage: () => undefined,
|
|
})) as unknown as HTMLCanvasElement['getContext']
|
|
canvas.toBlob = ((cb: BlobCallback) => {
|
|
cb(null)
|
|
}) as HTMLCanvasElement['toBlob']
|
|
canvas.toDataURL = () =>
|
|
'data:image/png;base64,' +
|
|
Buffer.from(PNG_BYTES).toString('base64')
|
|
vi.spyOn(document, 'createElement').mockImplementation(
|
|
((tag: string) =>
|
|
tag === 'canvas'
|
|
? canvas
|
|
: ({}) as HTMLElement) as typeof document.createElement,
|
|
)
|
|
|
|
const out = await rasterizeSvg(
|
|
'<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 10 10"/>',
|
|
)
|
|
expect(out[0]).toBe(0x89)
|
|
expect(out[1]).toBe(0x50)
|
|
})
|
|
})
|