Skip to content

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

4.8 KB
Raw Blame
import 'fake-indexeddb/auto'
import { afterEach, beforeEach, describe, expect, test } from 'bun:test'

import { BUILTIN_IO_FORMATS, IORegistry } from '@open-pencil/core/io'
import { parseFigFile } from '@open-pencil/core/io/formats/fig'
import { SceneGraph } from '@open-pencil/scene-graph'

import { makeFigmaFromStore } from '@/app/automation/bridge/figma-factory'
import { createAutomationCommandHandlers } from '@/app/automation/bridge/handlers'
import type { AutomationTarget } from '@/app/automation/bridge/target'
import { createEditorStore, type EditorStore } from '@/app/editor/session/create'

const { handleTargetCommand } = createAutomationCommandHandlers(makeFigmaFromStore)
let stores: EditorStore[] = []

beforeEach(() => {
  // The automation FigmaAPI reads the viewport size from the window.
  Object.assign(globalThis, { window: { innerWidth: 1024, innerHeight: 768 } })
})

afterEach(() => {
  for (const store of stores) store.dispose()
  stores = []
  Reflect.deleteProperty(globalThis, 'window')
})

/** A `.fig` opened like the app does: only the first page has its layers. */
async function storeWithUnshownPage(): Promise<{
  store: EditorStore
  pageId: string
  frameId: string
}> {
  const source = new SceneGraph()
  source.createNode('FRAME', source.getPages()[0].id, { name: 'First frame' })
  const second = source.addPage('Second')
  const frame = source.createNode('FRAME', second.id, {
    name: 'Second frame',
    width: 70,
    height: 35
  })
  const written = await new IORegistry(BUILTIN_IO_FORMATS).writeDocument('fig', source)
  const bytes = written.data as Uint8Array
  const graph = await parseFigFile(bytes.slice().buffer, { populate: 'first-page' })
  const store = createEditorStore(graph)
  stores.push(store)
  const page = graph.getPages().find((p) => p.name === 'Second')
  if (!page) throw new Error('Missing second page')
  expect(graph.getChildren(page.id)).toHaveLength(0)
  return { store, pageId: page.id, frameId: frame.id }
}

function target(store: EditorStore, pageId: string): AutomationTarget {
  return {
    store,
    documentId: 'tab-1',
    documentName: 'Document',
    pageId,
    pageName: store.graph.getNode(pageId)?.name ?? ''
  }
}

describe('automation tools on a page that has not been shown', () => {
  test('a layer named by its ID loads its page, as in Figma', async () => {
    const { store, pageId, frameId } = await storeWithUnshownPage()
    const shown = store.state.currentPageId

    const response = (await handleTargetCommand(target(store, shown), 'tool', {
      name: 'get_node',
      args: { id: frameId, depth: 0 }
    })) as { result: { name: string } }

    expect(response.result.name).toBe('Second frame')
    expect(store.graph.getChildren(pageId)).toHaveLength(1)
    expect(store.state.currentPageId).toBe(shown)
  })

  test('get_page_tree rooted at a layer there loads its page', async () => {
    const { store, frameId } = await storeWithUnshownPage()

    const response = (await handleTargetCommand(target(store, store.state.currentPageId), 'tool', {
      name: 'get_page_tree',
      args: { root_id: frameId }
    })) as { result: { root: string; tree: { name: string } } }

    expect(response.result).toMatchObject({ root: frameId, tree: { name: 'Second frame' } })
  })

  test('find_nodes sees the layers of the target page', async () => {
    const { store, pageId } = await storeWithUnshownPage()
    const shown = store.state.currentPageId

    const response = (await handleTargetCommand(target(store, pageId), 'tool', {
      name: 'find_nodes',
      args: { name: 'Second frame' }
    })) as { result: { count: number } }

    expect(response.result.count).toBe(1)
    expect(store.state.currentPageId).toBe(shown)
  })

  test('a shape created there joins the existing layers', async () => {
    const { store, pageId } = await storeWithUnshownPage()

    await handleTargetCommand(target(store, pageId), 'tool', {
      name: 'create_shape',
      args: { type: 'RECTANGLE', x: 0, y: 0, width: 10, height: 10, name: 'Added' }
    })

    expect(store.graph.getChildren(pageId).map((node) => node.name)).toEqual([
      'Second frame',
      'Added'
    ])
  })

  test('a tool measures the target page after its layout runs', async () => {
    const store = createEditorStore()
    stores.push(store)
    const other = store.graph.addPage('Other').id
    // Not laid out yet: the row hugs one 80 px child but still says 10 px.
    const row = store.graph.createNode('FRAME', other, {
      width: 10,
      height: 20,
      layoutMode: 'HORIZONTAL',
      primaryAxisSizing: 'HUG',
      counterAxisSizing: 'FIXED'
    })
    store.graph.createNode('FRAME', row.id, { width: 80, height: 20 })

    const response = (await handleTargetCommand(target(store, other), 'tool', {
      name: 'get_node',
      args: { id: row.id }
    })) as { result: { width: number } }

    expect(response.result.width).toBe(80)
  })
})