request-image-verification.spec.ts 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import { mkdtemp, rm } from 'node:fs/promises'
  2. import { tmpdir } from 'node:os'
  3. import { join } from 'node:path'
  4. import { Context } from '@deepseek-ai/cordis'
  5. import sharp from 'sharp'
  6. import { afterEach, describe, expect, it, vi } from 'vitest'
  7. const control = vi.hoisted(() => ({ mismatch: false }))
  8. vi.mock('../src/image.ts', async (importOriginal) => {
  9. const actual = await importOriginal<typeof import('../src/image.ts')>()
  10. return {
  11. ...actual,
  12. async detectImage(data: Uint8Array): Promise<Awaited<ReturnType<typeof actual.detectImage>>> {
  13. const detected = await actual.detectImage(data)
  14. return control.mismatch ? { ...detected, width: detected.width + 1 } : detected
  15. },
  16. }
  17. })
  18. import LocalAttachmentStore from '../src/index.ts'
  19. const homes: string[] = []
  20. afterEach(async () => {
  21. control.mismatch = false
  22. await Promise.all(homes.splice(0).map(home => rm(home, { recursive: true, force: true })))
  23. })
  24. describe('request image verification', () => {
  25. it('rejects an encoded request whose decoded facts disagree with the encoder result', async () => {
  26. const dshHome = await mkdtemp(join(tmpdir(), 'dsh-request-verification-'))
  27. homes.push(dshHome)
  28. const attachments = new LocalAttachmentStore(new Context(), { dshHome })
  29. const source = new Uint8Array(await sharp({
  30. create: { width: 64, height: 32, channels: 3, background: { r: 12, g: 34, b: 56 } },
  31. }).png().toBuffer())
  32. const attachment = await attachments.saveImage({ data: source, mediaType: 'image/png' })
  33. control.mismatch = true
  34. await expect(attachments.readImageRequest(attachment, { width: 22, height: 11, maxBytes: 1024 * 1024 }))
  35. .rejects.toMatchObject({
  36. code: 'ATTACHMENT_WRITE_FAILED',
  37. message: 'Encoded model-request image does not match its verified 8-bit sRGB metadata.',
  38. })
  39. })
  40. })