AI-native design editor. Open-source Figma alternative.
ci: shard engine tests for PR checks
Danila Poyarkov4b75212c0ce3
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.ymlindex 06ebda4d4..23bc3ba1f 100644--- a/.github/workflows/ci.yml+++ b/.github/workflows/ci.yml@@ -13,7 +13,7 @@ concurrency:cancel-in-progress: truejobs:- check:+ quality:runs-on: ubuntu-lateststeps:- uses: actions/checkout@v4@@ -25,13 +25,31 @@ jobs:- name: Format checkrun: bun run format:check- - name: Lint & typecheck+ - name: Lint, typecheck, architecture, package, and tooling checksrun: bun run check- - name: Unit tests- run: bun test tests/engine/+ unit-tests:+ runs-on: ubuntu-latest+ strategy:+ fail-fast: true+ matrix:+ group: [app, dom, editor, fig, render, scene, vue]+ name: Unit tests (${{ matrix.group }})+ steps:+ - uses: actions/checkout@v4++ - uses: ./.github/actions/setup-bun+ with:+ lfs: 'true'++ - name: Run quick unit test shard+ shell: bash+ run: |+ mapfile -t test_files < <(bun tools/unit-tests/src/list.ts "${{ matrix.group }}")+ if [ "${#test_files[@]}" -eq 0 ]; then+ echo "No tests found for shard ${{ matrix.group }}"+ exit 0+ fi+ bun test "${test_files[@]}"env:BUN_HEAVY_TESTS: 'false'-- - name: Copy-paste detection- run: bun run test:dupesdiff --git a/.github/workflows/heavy-tests.yml b/.github/workflows/heavy-tests.ymlindex 75704c1e0..7ddcaec4b 100644--- a/.github/workflows/heavy-tests.yml+++ b/.github/workflows/heavy-tests.yml@@ -2,13 +2,15 @@ name: Heavy testson:workflow_dispatch:+ schedule:+ - cron: '17 3 * * *'concurrency:group: heavy-tests-${{ github.ref }}cancel-in-progress: truejobs:- fig-roundtrip:+ fixture-heavy-unit-tests:runs-on: ubuntu-latesttimeout-minutes: 30steps:@@ -18,7 +20,14 @@ jobs:with:lfs: 'true'- - name: Heavy fig roundtrip tests- run: bun test --timeout 180000 tests/engine/fig-roundtrip.test.ts+ - name: Heavy fixture unit tests+ shell: bash+ run: |+ mapfile -t test_files < <(bun tools/unit-tests/src/list.ts all --heavy-only)+ if [ "${#test_files[@]}" -eq 0 ]; then+ echo "No heavy unit tests found"+ exit 0+ fi+ bun test --timeout 180000 "${test_files[@]}"env:BUN_HEAVY_TESTS: 'true'diff --git a/package.json b/package.jsonindex f8a5fb92c..17932629f 100644--- a/package.json+++ b/package.json@@ -26,6 +26,8 @@"test:figma": "playwright test --project=figma","figma:debug": "open -a Figma --args --remote-debugging-port=9222","test:unit": "bun test ./tests/engine",+ "test:unit:quick": "bash -lc 'mapfile -t test_files < <(bun tools/unit-tests/src/list.ts all); bun test \"${test_files[@]}\"'",+ "test:unit:heavy": "bash -lc 'mapfile -t test_files < <(bun tools/unit-tests/src/list.ts all --heavy-only); bun test --timeout 180000 \"${test_files[@]}\"'","test:coverage": "bun test --coverage ./tests/engine","test:type-shapes": "bun tools/type-shapes/src/index.ts","test:tools": "bun tools/test.ts",diff --git a/tools/unit-tests/package.json b/tools/unit-tests/package.jsonnew file mode 100644index 000000000..672a61158--- /dev/null+++ b/tools/unit-tests/package.json@@ -0,0 +1,8 @@+{+ "name": "@open-pencil/tool-unit-tests",+ "private": true,+ "type": "module",+ "scripts": {+ "test": "bun test tests"+ }+}diff --git a/tools/unit-tests/src/list.ts b/tools/unit-tests/src/list.tsnew file mode 100644index 000000000..34801ad5f--- /dev/null+++ b/tools/unit-tests/src/list.ts@@ -0,0 +1,19 @@+#!/usr/bin/env bun++import { listHeavyUnitTests, listUnitTests, type UnitTestGroup, unitTestGroupNames } from './shards'++const args = new Set(process.argv.slice(2))+const groupArg = process.argv.slice(2).find((arg) => !arg.startsWith('--')) ?? 'all'+const groupNames = unitTestGroupNames()++if (!groupNames.includes(groupArg as UnitTestGroup)) {+ throw new Error(`Unknown unit test group: ${groupArg}. Expected one of: ${groupNames.join(', ')}`)+}++const group = groupArg as UnitTestGroup+const files = args.has('--heavy-only')+ ? await listHeavyUnitTests(group)+ : await listUnitTests(group, { includeHeavy: args.has('--include-heavy') })++process.stdout.write(files.join('\n'))+if (files.length > 0) process.stdout.write('\n')diff --git a/tools/unit-tests/src/shards.ts b/tools/unit-tests/src/shards.tsnew file mode 100644index 000000000..5f09e5706--- /dev/null+++ b/tools/unit-tests/src/shards.ts@@ -0,0 +1,98 @@+import { readdir } from 'node:fs/promises'+import { dirname, isAbsolute, join, relative, resolve, sep } from 'node:path'+import { fileURLToPath } from 'node:url'++const REPO_ROOT = resolve(dirname(fileURLToPath(import.meta.url)), '../../..')++export const UNIT_TEST_GROUPS = {+ app: ['tests/engine/acp', 'tests/engine/app', 'tests/engine/cli', 'tests/engine/tauri'],+ dom: ['tests/engine/dom-css', 'tests/engine/color', 'tests/engine/icons', 'tests/engine/pen'],+ editor: [+ 'tests/engine/clipboard',+ 'tests/engine/editor',+ 'tests/engine/hit-test',+ 'tests/engine/snap'+ ],+ fig: ['tests/engine/figma', 'tests/engine/io', 'tests/engine/kiwi'],+ render: ['tests/engine/geometry', 'tests/engine/layout', 'tests/engine/render'],+ scene: [+ 'tests/engine/lint',+ 'tests/engine/random',+ 'tests/engine/scene-graph',+ 'tests/engine/text'+ ],+ vue: [+ 'tests/engine/mcp',+ 'tests/engine/profiler',+ 'tests/engine/tools',+ 'tests/engine/vector',+ 'tests/engine/vue'+ ]+} as const++export type UnitTestGroup = keyof typeof UNIT_TEST_GROUPS | 'all'++export const HEAVY_UNIT_TEST_PATTERNS = [+ 'tests/engine/clipboard/fixtures/',+ 'tests/engine/io/fig/heavy/',+ 'tests/engine/io/fig/roundtrip/exhaustive.test.ts',+ 'tests/engine/io/fig/roundtrip/glyph-blob.test.ts',+ 'tests/engine/io/fig/roundtrip/variables.test.ts',+ 'tests/engine/io/fig/export/text.test.ts',+ 'tests/engine/io/fig/export/worker.test.ts',+ 'tests/engine/io/fig/import/group-reclassify.test.ts',+ 'tests/engine/layout/auto-layout/text/measurement.test.ts'+] as const++export function unitTestGroupNames(): UnitTestGroup[] {+ return [...Object.keys(UNIT_TEST_GROUPS), 'all'] as UnitTestGroup[]+}++export function pathsForUnitTestGroup(group: UnitTestGroup): string[] {+ if (group === 'all') return Object.values(UNIT_TEST_GROUPS).flat()+ return [...UNIT_TEST_GROUPS[group]]+}++export function isHeavyUnitTest(path: string): boolean {+ const normalized = normalizePath(path)+ return HEAVY_UNIT_TEST_PATTERNS.some(+ (pattern) => normalized.startsWith(pattern) || normalized === pattern+ )+}++export async function listUnitTests(+ group: UnitTestGroup,+ options: { includeHeavy?: boolean } = {}+): Promise<string[]> {+ const files = await listTestFiles(pathsForUnitTestGroup(group))+ return options.includeHeavy ? files : files.filter((file) => !isHeavyUnitTest(file))+}++export async function listHeavyUnitTests(group: UnitTestGroup = 'all'): Promise<string[]> {+ const files = await listTestFiles(pathsForUnitTestGroup(group))+ return files.filter(isHeavyUnitTest)+}++async function listTestFiles(paths: string[]): Promise<string[]> {+ const files = await Promise.all(paths.map((path) => listTestFilesInPath(path)))+ return [...new Set(files.flat())].sort()+}++async function listTestFilesInPath(path: string): Promise<string[]> {+ const absolutePath = resolve(REPO_ROOT, path)+ const entries = await readdir(absolutePath, { withFileTypes: true })+ const files = await Promise.all(+ entries.map(async (entry) => {+ const childPath = join(path, entry.name)+ if (entry.isDirectory()) return listTestFilesInPath(childPath)+ if (entry.isFile() && entry.name.endsWith('.test.ts')) return [normalizePath(childPath)]+ return []+ })+ )+ return files.flat()+}++function normalizePath(path: string): string {+ if (!isAbsolute(path)) return path.split(sep).join('/')+ return relative(REPO_ROOT, path).split(sep).join('/') || path.split(sep).join('/')+}diff --git a/tools/unit-tests/tests/shards.test.ts b/tools/unit-tests/tests/shards.test.tsnew file mode 100644index 000000000..385f449e5--- /dev/null+++ b/tools/unit-tests/tests/shards.test.ts@@ -0,0 +1,34 @@+import { expect, test } from 'bun:test'++import {+ isHeavyUnitTest,+ listHeavyUnitTests,+ listUnitTests,+ pathsForUnitTestGroup,+ unitTestGroupNames+} from '../src/shards'++test('unit test groups cover all declared shards', () => {+ expect(unitTestGroupNames()).toContain('all')+ expect(pathsForUnitTestGroup('dom')).toContain('tests/engine/dom-css')+ expect(pathsForUnitTestGroup('all')).toContain('tests/engine/io')+})++test('heavy unit test matcher excludes fixture-heavy tests', () => {+ expect(isHeavyUnitTest('tests/engine/io/fig/heavy/fixtures.test.ts')).toBe(true)+ expect(isHeavyUnitTest('tests/engine/io/fig/roundtrip/glyph-blob.test.ts')).toBe(true)+ expect(isHeavyUnitTest('tests/engine/dom-css/runtime.test.ts')).toBe(false)+})++test('quick unit test listing excludes heavy tests', async () => {+ const quickFiles = await listUnitTests('all')+ expect(quickFiles).toContain('tests/engine/dom-css/runtime.test.ts')+ expect(quickFiles).not.toContain('tests/engine/io/fig/heavy/fixtures.test.ts')+ expect(quickFiles).not.toContain('tests/engine/io/fig/roundtrip/glyph-blob.test.ts')+})++test('heavy unit test listing contains only heavy tests', async () => {+ const heavyFiles = await listHeavyUnitTests()+ expect(heavyFiles).toContain('tests/engine/io/fig/heavy/fixtures.test.ts')+ expect(heavyFiles.every(isHeavyUnitTest)).toBe(true)+})