normalization-verification.spec.ts 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. import sharp from 'sharp'
  2. import { afterEach, describe, expect, it, vi } from 'vitest'
  3. const control = vi.hoisted(() => ({ mismatch: false }))
  4. vi.mock('../src/image.ts', async (importOriginal) => {
  5. const actual = await importOriginal<typeof import('../src/image.ts')>()
  6. return {
  7. ...actual,
  8. async detectImage(data: Uint8Array): Promise<Awaited<ReturnType<typeof actual.detectImage>>> {
  9. const detected = await actual.detectImage(data)
  10. return control.mismatch ? { ...detected, width: detected.width + 1 } : detected
  11. },
  12. }
  13. })
  14. import { normalizeImage } from '../src/normalization.ts'
  15. import { detectImage } from '../src/image.ts'
  16. afterEach(() => {
  17. control.mismatch = false
  18. })
  19. describe('normalization verification', () => {
  20. it('rejects a normalized output whose decoded facts disagree with the encoder result', async () => {
  21. const data = new Uint8Array(await sharp({
  22. create: { width: 10, height: 6, channels: 3, background: { r: 12, g: 200, b: 64 } },
  23. }).png().toBuffer())
  24. const detected = await detectImage(data)
  25. control.mismatch = true
  26. await expect(normalizeImage(data, detected, { maxPixels: 2048 * 2048, maxDimension: 5, maxBytes: 4 * 1024 * 1024 }))
  27. .rejects.toMatchObject({
  28. code: 'ATTACHMENT_WRITE_FAILED',
  29. message: 'Image normalization did not produce a single-frame 8-bit sRGB image with matching metadata.',
  30. })
  31. })
  32. })