openpencil/tests/e2e/variables/import.spec.ts
Danila Poyarkov 7ee621ec41
feat: import and export W3C design tokens (#964)
* feat(core): read and write W3C design tokens

Variables export as DTCG 2025.10 token files, one per collection mode in the shape Figma imports, with text and effect styles as typography and shadow tokens and a resolver document that combines them. OpenPencil's own token fields ride in a dev.openpencil extension, so an export reads back as it was.

Import reads plain mode files, resolver documents, and Tokens Studio themes, plans what it would add, update, or skip against the document without changing it, and applies the plan as one undo step: values merge into variables by name, nothing is deleted, and an updated style updates the layers that use it.

* feat: import and export design tokens from the variables dialog

The stylesheet menu exports the document's variables and styles as a zip of W3C design tokens, and Import design tokens reads token files, folders, or a zip into a dialog that shows where each collection and mode goes, what would be added, updated, or skipped and why, before applying it as one undo step.

* feat: import and export design tokens from tools and the CLI

openpencil tokens --format dtcg --out <folder> writes the token files, design_to_tokens takes the same format, and import_design_tokens imports token files into a document for AI, MCP, and openpencil tool call. The editor and these paths share one apply step behind a target interface: the editor makes each change undoable, a headless document takes them directly.

Re-importing an export changes nothing it does not need to: values that match are left alone, layers that use an updated style are changed only outside instances, where they follow their component, and collections that share a name keep their own files apart.

* perf(core): index token lookups when planning an import

Resolving each reference scanned every token and variable, which made importing a large design system quadratic. A resolver now also finds files picked without their folders by name.

* docs: document design token import and export

* test: type the design token result in the tokens command test

* fix: lay out the design token import dialog as a mapping list

Each collection and mode sits beside the picker for where it goes, in one bordered list, so the pickers no longer read as headings. Skipped tokens use AppCollapsible, file problems AppAlert, and the counts move to the footer.

* fix: keep durations, variable metadata, and style edits through design token import

A duration Figma's format writes in seconds goes back into a variable that holds milliseconds. Tokens without OpenPencil's extension leave an updated variable's unit, scopes, and code syntax as they are. A style token whose value was edited in another tool takes that edit instead of the stale exact fields its extension still carries. Aliases to a repeated collection name resolve within one collection, a collection named Styles no longer collides with the resolver's styles set, and the dialog plans again when the document changes and right before applying. The editor imports the apply step directly rather than the whole format barrel.

* fix: keep effect order and explicit alias targets in design token import

Edited shadows take the places of the shadows they replace, so blurs keep their positions. An alias whose Figma data names a collection that is left out or absent is missing rather than bound to the same path in another collection. The resolver reserves the Styles set name only when there is a styles file.

* perf(cli): print token stylesheets without loading every page

A CSS or Tailwind stylesheet reads only variables, which load with the document, so tokens skips populating and laying out every page as variables already does. On an 85 MB design kit this takes the command from about seven minutes to seconds.
2026-10-08 16:53:46 +00:00

59 lines
2 KiB
TypeScript

import { expect, test, useEditorSetup } from '#tests/e2e/fixtures'
const editor = useEditorSetup()
const theme = (background: string) =>
JSON.stringify({
color: {
$type: 'color',
background: { $value: background },
surface: { $value: '{color.background}' }
},
space: { md: { $type: 'dimension', $value: { value: 1, unit: 'rem' } } }
})
test('design token files import as a collection with a mode per file', async () => {
await editor.page
.getByRole('region', { name: 'Variables' })
.getByRole('button', { name: 'Open variables' })
.click()
const dialog = editor.page.getByTestId('variables-dialog')
await expect(dialog).toBeVisible()
const chooser = editor.page.waitForEvent('filechooser')
await dialog.getByTestId('variables-import-tokens').click()
await (
await chooser
).setFiles([
{
name: 'Light.tokens.json',
mimeType: 'application/json',
buffer: Buffer.from(theme('#ffffff'))
},
{
name: 'Dark.tokens.json',
mimeType: 'application/json',
buffer: Buffer.from(theme('#09090b'))
}
])
const importDialog = editor.page.getByTestId('variables-import-dialog')
await expect(importDialog.getByTestId('variables-import-summary')).toHaveText(
'3 to add · 0 to update · 0 skipped'
)
await importDialog.getByTestId('variables-import-confirm').click()
await expect(importDialog).toBeHidden()
await expect(dialog.getByTestId('variable-row')).toHaveCount(3)
const stylesheet = dialog.getByTestId('token-output')
await expect(stylesheet).toContainText('--spacing-md: 1rem')
// Each file is a mode: the second one's value goes under its condition.
await expect(stylesheet).toContainText(/="dark"\][^}]*--color-background: #09090b/i)
// The whole import is one step on the document's history.
await dialog.getByTestId('variable-row').first().click()
await editor.page.keyboard.press('ControlOrMeta+KeyZ')
await expect(dialog.getByTestId('variable-row')).toHaveCount(0)
editor.canvas.assertNoErrors()
})