Skip to content

AI-native design editor. Open-source Figma alternative.

1.6 KB
Raw Blame
import { describe, expect, test } from 'bun:test'

import { materializeDocument } from '@open-pencil/fig'
import type { NodeChange } from '@open-pencil/kiwi/fig/codec'

import { canvas, doc, node } from '../helpers'

describe('fig-import: image fills', () => {
  test('image fill with hash', () => {
    const hash: Record<string, number> = {}
    for (let i = 0; i < 20; i++) hash[String(i)] = i + 10
    // Kiwi decodes a byte field into a numeric record; the codec type only models encoded hashes.
    const image: { hash: string | Record<string, number>; name: string } = {
      hash,
      name: 'test-image'
    }

    const graph = materializeDocument([
      doc(),
      canvas(),
      node('RECTANGLE', 10, 1, {
        fillPaints: [
          {
            type: 'IMAGE',
            opacity: 1,
            visible: true,
            blendMode: 'NORMAL',
            image,
            imageScaleMode: 'FILL',
            transform: { m00: 1, m01: 0, m02: 0, m10: 0, m11: 1, m12: 0 }
          }
        ] as NodeChange['fillPaints']
      })
    ]).graph
    const n = graph.getChildren(graph.getPages()[0].id)[0]
    expect(n.fills[0].type).toBe('IMAGE')
    expect(n.fills[0].imageHash).toBeDefined()
    expect(n.fills[0].imageHash?.length).toBe(40)
    expect(n.fills[0].imageScaleMode).toBe('FILL')
  })

  test('images stored on graph', () => {
    const images = new Map<string, Uint8Array>()
    images.set('abc123', new Uint8Array([1, 2, 3]))
    const graph = materializeDocument([doc(), canvas()], [], { images }).graph
    expect(graph.images.get('abc123')).toEqual(new Uint8Array([1, 2, 3]))
  })
})