Skip to content

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

14.0 KB
Raw Blame
import type { Page } from '@playwright/test'

import { expect, test } from '#tests/e2e/fixtures'
import { CanvasHelper } from '#tests/helpers/canvas'
import { cloudDiscoveryFixture } from '#tests/helpers/cloud/discovery'
import {
  formulaSceneState,
  pasteFormulaText,
  seedFormulaScene,
  updateFormulaNumber
} from '#tests/helpers/variables/formula'

test.beforeEach(async ({ page }) => {
  await page.route('https://cloud.openpencil.dev/.well-known/openpencil', (route) =>
    route.fulfill({ json: cloudDiscoveryFixture('https://cloud.openpencil.dev') })
  )
})

async function openToken(page: Page, id: string) {
  await page
    .getByRole('region', { name: 'Variables' })
    .getByRole('button', { name: 'Open variables' })
    .click()
  const dialog = page.getByTestId('variables-dialog')
  const row = dialog.locator(`[data-variable-id="${id}"]`)
  await row.click()
  const inspector = dialog.getByTestId('token-inspector')
  return { dialog, row, inspector }
}

test('a number field takes literals, references and formulas, and reports errors on commit', async ({
  page
}) => {
  const canvas = new CanvasHelper(page)
  await page.goto('/?test')
  await canvas.waitForInit()
  const ids = await seedFormulaScene(page)
  const { inspector, row } = await openToken(page, ids.width)
  const value = inspector.getByRole('textbox', { name: 'Value', exact: true })
  await expect(value).toHaveText('8')
  await value.fill('{base} * 3')
  await expect(inspector.getByText('= 12px', { exact: true })).toBeVisible()
  expect((await formulaSceneState(page, ids)).width).toBe(8)
  await value.press('Enter')
  await expect.poll(async () => (await formulaSceneState(page, ids)).width).toBe(12)
  await expect(row).toContainText('12px')

  await value.fill('1 / 0')
  await expect(value).toHaveAttribute('aria-invalid', 'false')
  await value.press('Enter')
  await expect(value).toHaveAttribute('aria-invalid', 'true')
  await expect(inspector.getByText('Cannot divide by zero')).toBeVisible()
  expect((await formulaSceneState(page, ids)).width).toBe(12)
  await value.press('Escape')
  await expect(value).toContainText('* 3')

  await value.fill('base')
  await value.press('Enter')
  expect((await formulaSceneState(page, ids)).numberDefinition).toEqual({ aliasId: ids.base })
  await expect(value.locator('[data-reference]')).toHaveCount(1)
  await value.fill('16')
  await value.press('Enter')
  expect((await formulaSceneState(page, ids)).numberDefinition).toBe(16)

  await value.fill('{base} * 2')
  await value.press('End')
  await value.press('Shift+ArrowLeft')
  await inspector.getByRole('button', { name: 'Use a variable', exact: true }).click()
  await page.getByRole('option', { name: 'base 4px', exact: true }).click()
  await expect(value.locator('[data-reference]')).toHaveCount(2)
  await expect(value).toBeFocused()
  await value.press('Enter')
  expect((await formulaSceneState(page, ids)).width).toBe(16)

  const css = inspector.getByRole('textbox', { name: 'CSS expression', exact: true })
  const cssToggle = inspector.getByTestId('variables-css-expression')
  await expect(css).toHaveCount(0)
  await expect(cssToggle).toHaveAttribute('aria-pressed', 'false')
  await cssToggle.click()
  await expect(css).toBeFocused()
  await page.keyboard.insertText('clamp(1rem, 4vw, 2rem)')
  await css.press('Enter')
  await expect(page.getByTestId('token-output')).toContainText('clamp(1rem, 4vw, 2rem)')
  expect((await formulaSceneState(page, ids)).width).toBe(16)
  await value.fill('24')
  await value.press('Enter')
  await expect(css).toHaveText('clamp(1rem, 4vw, 2rem)')
  await expect(cssToggle).toHaveAttribute('aria-pressed', 'true')
  await cssToggle.click()
  await expect(css).toHaveCount(0)
  await expect(page.getByTestId('token-output')).not.toContainText('clamp(')
  canvas.assertNoErrors()
})

test('a true/false value keeps its switch until a formula is asked for', async ({ page }) => {
  const canvas = new CanvasHelper(page)
  await page.goto('/?test')
  await canvas.waitForInit()
  const ids = await seedFormulaScene(page)
  const { inspector } = await openToken(page, ids.visible)
  const value = inspector.getByRole('textbox', { name: 'Value', exact: true })
  await expect(inspector.getByRole('switch', { name: 'Value' })).toBeChecked()
  await expect(value).toHaveCount(0)
  await inspector.getByRole('button', { name: 'Use a formula' }).click()
  await expect(value).toBeFocused()
  await page.keyboard.insertText('not {enabled}')
  await expect(inspector.getByText('= false', { exact: true })).toBeVisible()
  await value.press('Enter')
  await expect.poll(async () => (await formulaSceneState(page, ids)).visible).toBe(false)

  await value.fill('{size} > 4')
  await expect(value.locator('[data-reference]')).toHaveCount(1)
  await expect(inspector.getByText('= true', { exact: true })).toBeVisible()
  await value.press('Enter')
  await expect.poll(async () => (await formulaSceneState(page, ids)).visible).toBe(true)

  await value.fill('false')
  await value.press('Enter')
  expect((await formulaSceneState(page, ids)).booleanDefinition).toBe(false)
  await expect(inspector.getByRole('switch', { name: 'Value' })).not.toBeChecked()
  canvas.assertNoErrors()
})

test('references are atomic pills with editable operators and plain source on copy', async ({
  page
}) => {
  const canvas = new CanvasHelper(page)
  await page.goto('/?test')
  await canvas.waitForInit()
  const ids = await seedFormulaScene(page)
  const { inspector } = await openToken(page, ids.width)
  const value = inspector.getByRole('textbox', { name: 'Value', exact: true })
  await value.fill('{base} * 2')
  const pill = value.locator('[data-reference]')
  await expect(pill).toHaveText('base')
  await expect(pill).toHaveAttribute('contenteditable', 'false')
  await expect(value).not.toContainText('{')
  await value.press('Meta+a')
  const copied = await value.evaluate((element) => {
    const clipboard = new DataTransfer()
    element.dispatchEvent(
      new ClipboardEvent('copy', { clipboardData: clipboard, bubbles: true, cancelable: true })
    )
    return clipboard.getData('text/plain')
  })
  expect(copied).toBe('{base} * 2')

  await value.fill('{base}')
  await value.press('End')
  await value.press('Backspace')
  await expect(pill).toHaveCount(0)
  await expect(value.locator('.cm-placeholder')).toHaveText('Value or formula')
  await pasteFormulaText(value, '{base} + 1')
  await value.press('End')
  await page.keyboard.insertText('0')
  await expect(inspector.getByText('= 14px', { exact: true })).toBeVisible()
  await value.press('Meta+z')
  await expect(inspector.getByText('= 5px', { exact: true })).toBeVisible()
  await value.press('Meta+Shift+z')
  await expect(inspector.getByText('= 14px', { exact: true })).toBeVisible()
  await value.press('Home')
  await page.keyboard.type('2 * ')
  await expect(inspector.getByText('= 18px', { exact: true })).toBeVisible()
  await value.press('Enter')
  expect((await formulaSceneState(page, ids)).width).toBe(18)
  canvas.assertNoErrors()
})

test('the variable picker shows the numeric value of the edited mode', async ({ page }) => {
  const canvas = new CanvasHelper(page)
  await page.goto('/?test')
  await canvas.waitForInit()
  const ids = await seedFormulaScene(page)
  await page.evaluate(({ base, width }) => {
    const editor = window.openPencil?.getStore?.()
    const variable = editor?.graph.variables.get(base)
    if (!editor || !variable) throw new Error('Missing variable')
    const mode = editor.graph.createMode(variable.collectionId, 'Dense')
    if (!mode) throw new Error('Missing mode')
    editor.updateVariableValue(base, mode, 2)
    editor.updateVariableValue(width, mode, 8)
  }, ids)
  const { inspector } = await openToken(page, ids.width)
  const dense = inspector.getByRole('textbox', { name: 'Dense', exact: true })
  await dense.fill('{base} * 2')
  await expect(dense.locator('[data-reference]')).toHaveText('base')
  await expect(inspector.getByText('= 4px', { exact: true })).toBeVisible()
  await inspector.getByRole('button', { name: 'Use a variable', exact: true }).last().click()
  await expect(page.getByRole('option', { name: 'base 2px', exact: true })).toBeVisible()
  canvas.assertNoErrors()
})

test('redo preserves the selection restored after an external value update', async ({ page }) => {
  const canvas = new CanvasHelper(page)
  await page.goto('/?test')
  await canvas.waitForInit()
  const ids = await seedFormulaScene(page)
  await updateFormulaNumber(page, ids, 1234)
  const { inspector } = await openToken(page, ids.width)
  const value = inspector.getByRole('textbox', { name: 'Value', exact: true })
  await value.press('Home')
  await value.press('ArrowRight')
  await value.press('Shift+ArrowRight')
  await updateFormulaNumber(page, ids, 5678)
  await expect(value).toHaveText('5678')
  await expect(value).toBeFocused()
  await value.press('Meta+z')
  await expect(value).toHaveText('1234')
  await value.press('Meta+Shift+z')
  await expect(value).toHaveText('5678')
  await page.keyboard.insertText('9')
  await expect(value).toHaveText('5978')
  await value.press('Enter')
  expect((await formulaSceneState(page, ids)).width).toBe(5978)
  canvas.assertNoErrors()
})

test('completion inserts a reference and the table shows the result', async ({ page }) => {
  const canvas = new CanvasHelper(page)
  await page.goto('/?test')
  await canvas.waitForInit()
  const ids = await seedFormulaScene(page)
  const { dialog, inspector, row } = await openToken(page, ids.width)
  const value = inspector.getByRole('textbox', { name: 'Value', exact: true })
  const completion = dialog.locator('.cm-tooltip-autocomplete')
  await value.fill('2 * ')
  await value.press('End')
  await value.pressSequentially('ba')
  await expect(completion.getByRole('option', { name: /base/ })).toBeVisible()
  await value.press('Enter')
  await expect(value.locator('[data-reference]')).toHaveCount(1)
  await expect(value).toBeFocused()
  await value.press('Enter')
  await expect.poll(async () => (await formulaSceneState(page, ids)).width).toBe(8)
  await expect(row).toContainText('8px')
  await expect(row).not.toContainText('{')
  await value.fill('{ba')
  await expect(completion.getByRole('option', { name: /base/ })).toBeVisible()
  await value.press('Escape')
  await expect(value).toHaveText('{ba')
  await value.press('Escape')
  await expect(value.locator('[data-reference]')).toHaveCount(1)
  canvas.assertNoErrors()
})

test('completion offers functions where a name goes and inserts the call', async ({ page }) => {
  const canvas = new CanvasHelper(page)
  await page.goto('/?test')
  await canvas.waitForInit()
  const ids = await seedFormulaScene(page)
  const { dialog, inspector } = await openToken(page, ids.width)
  const value = inspector.getByRole('textbox', { name: 'Value', exact: true })
  const completion = dialog.locator('.cm-tooltip-autocomplete')
  await value.fill('2 * ')
  await value.press('End')
  await value.pressSequentially('i')
  await expect(completion.getByRole('option', { name: /^if/ })).toBeVisible()
  await value.press('Enter')
  await expect(value).toHaveText('2 * if(')
  await value.fill('tex')
  await value.press('End')
  await value.pressSequentially('t')
  await expect(completion.getByRole('option', { name: /^text/ })).toBeVisible()
  await value.fill('{tru')
  await value.press('End')
  await value.pressSequentially('t')
  await expect(completion.getByRole('option', { name: /^truthy/ })).toHaveCount(0)
  canvas.assertNoErrors()
})

test('a CSS expression completes token names as var() pills and functions', async ({ page }) => {
  const canvas = new CanvasHelper(page)
  await page.goto('/?test')
  await canvas.waitForInit()
  const ids = await seedFormulaScene(page)
  const { dialog, inspector } = await openToken(page, ids.width)
  await inspector.getByRole('button', { name: 'Add CSS expression' }).click()
  const css = inspector.getByRole('textbox', { name: 'CSS expression', exact: true })
  const completion = dialog.locator('.cm-tooltip-autocomplete')
  await expect(css).toBeFocused()
  await page.keyboard.type('cla')
  await expect(completion.getByRole('option', { name: /^clamp/ })).toBeVisible()
  await page.keyboard.press('Enter')
  await page.keyboard.type('1rem, var(')
  await expect(completion.getByRole('option', { name: /^--base/ })).toBeVisible()
  await expect(completion.getByRole('option', { name: /^clamp/ })).toHaveCount(0)
  await page.keyboard.press('Enter')
  await page.keyboard.type(', 2rem)')
  await expect(css.locator('[data-reference]')).toHaveText('--base')
  await page.keyboard.press('Enter')
  await expect(page.getByTestId('token-output')).toContainText('clamp(1rem, var(--base), 2rem)')

  const error = inspector.getByRole('alert')
  await css.press('Meta+a')
  await page.keyboard.insertText('var(--missing)')
  await expect(css).toHaveAttribute('aria-invalid', 'false')
  await css.press('Enter')
  await expect(error).toHaveText('No token is named --missing')
  await page.keyboard.insertText(' ')
  await expect(error).toHaveCount(0)
  await css.press('Meta+a')
  await page.keyboard.insertText('4 apples')
  await css.press('Enter')
  await expect(error).toHaveText('Not a valid value for width')
  await css.press('Meta+a')
  await page.keyboard.insertText('calc(2 * ')
  await inspector.getByRole('button', { name: 'Use a variable' }).last().click()
  await page.getByRole('option', { name: 'base 4px', exact: true }).click()
  await expect(css).toBeFocused()
  await page.keyboard.insertText(')')
  await css.press('Enter')
  await expect(page.getByTestId('token-output')).toContainText('calc(2 * var(--base))')
  canvas.assertNoErrors()
})

test('long values span lines', async ({ page }) => {
  const canvas = new CanvasHelper(page)
  await page.goto('/?test')
  await canvas.waitForInit()
  const ids = await seedFormulaScene(page)
  const { inspector } = await openToken(page, ids.width)
  const value = inspector.getByRole('textbox', { name: 'Value', exact: true })
  await value.fill('{base} * 2')
  await value.press('End')
  await value.press('Shift+Enter')
  await page.keyboard.insertText('+ {base}')
  await expect(value.locator('.cm-line')).toHaveCount(2)
  await value.press('Enter')
  expect((await formulaSceneState(page, ids)).width).toBe(12)
  canvas.assertNoErrors()
})