openpencil/tests/engine/icons/iconify.test.ts
Danila Poyarkov aa86873dd7
test: typecheck the test suites and fix what that found (#896)
* build: typecheck the test suites

Tests were in no TypeScript program: no tsconfig included tests/** or
packages/*/tests/**, and bun strips types without checking them, so a
fixture could drop a required field and keep passing until something
read it.

@types/bun moves to the root because it was installed per package only,
and #cli-tests/* joins the paths the root config already carries.

* test: fix the type errors the test suites were hiding

Typechecking the tests turned up 1123 errors. Most were ordinary
strictness, but some were real: `NodeChange` bound to Figma's plugin
typings rather than the Kiwi codec in thirteen .fig tests,
materializeInstance was called with six arguments against five so the
blobs and source children were dropped, CanvasKit pixels were written
to a plain object that never reached WASM, and assertions were made
through accessors that do not exist, so they asserted nothing.

Fixtures that had quietly lost a required field now carry it, nullable
results are narrowed through the existing expectDefined helper rather
than assumed, and stand-ins for CanvasKit and the editor go through one
named helper instead of an unexplained cast at each site.

No test was deleted, skipped, or weakened, and no `any`, non-null
assertion, or ts-expect-error was introduced.

* docs: record what typechecking the tests established

Pins the app program's global types with an assertion rather than a
note, since an unpinned types list lets any root @types package decide
which platform src/** is judged against.

The two environment faults that look like code regressions — Vite's
dependency pre-bundle outliving a package rebuild, and heavy .fig
suites failing under load — go to the development docs, where an
explanation belongs.

* fix: align @types/bun and keep node types resolvable when extended

The root manifest declared a newer @types/bun than every package, which
check:monorepo rejects, and pinning the app program's types left them
unresolvable from a config that extends this one out of tree.

* fix: fail the test typecheck when the compiler itself fails

The gate matched diagnostics by substring, so a compiler or config
failure that named no test file printed a pass while having checked
nothing. Diagnostics are now split by whether they name a file: an
unscoped one is the run failing and stops the gate, a test file's is a
finding, and a source file's stays out by design.

Also drops the parameter planComponentConstruction never read, and
makes the inner-shadow verification script exit non-zero when it
renders no image instead of logging and succeeding.

* chore: merge master into tests-typecheck
2026-10-05 12:42:38 +00:00

138 lines
4.6 KiB
TypeScript

import { describe, expect, test, beforeAll } from 'bun:test'
import { fetchIcon, fetchIcons, searchIcons, searchIconsBatch } from '@open-pencil/core'
import { expectDefined } from '#tests/helpers/assert'
let hasNetwork = true
beforeAll(async () => {
try {
await fetch('https://api.iconify.design/mdi.json?icons=home', {
signal: AbortSignal.timeout(3000)
})
} catch {
hasNetwork = false
console.warn('⚠ Skipping iconify tests — no network')
}
})
const networkTest = (name: string, fn: () => Promise<void>, timeout?: number): void =>
test(
name,
async () => {
if (!hasNetwork) return
await fn()
},
timeout
)
describe('fetchIcon', () => {
networkTest('fetches mdi:home', async () => {
const icon = await fetchIcon('mdi:home', 24)
expect(icon.prefix).toBe('mdi')
expect(icon.name).toBe('home')
expect(icon.width).toBe(24)
expect(icon.height).toBe(24)
expect(icon.paths.length).toBeGreaterThan(0)
const path = icon.paths[0]
expect(path.vectorNetwork.vertices.length).toBeGreaterThan(3)
expect(path.vectorNetwork.segments.length).toBeGreaterThan(3)
expect(path.fill).toBe('currentColor')
expect(path.stroke).toBeNull()
})
networkTest('fetches lucide:heart (stroked icon)', async () => {
const icon = await fetchIcon('lucide:heart', 24)
expect(icon.paths.length).toBeGreaterThan(0)
const path = icon.paths[0]
expect(path.vectorNetwork.vertices.length).toBeGreaterThan(3)
expect(path.stroke).toBe('currentColor')
expect(path.strokeWidth).toBe(2)
expect(path.strokeCap).toBe('round')
expect(path.strokeJoin).toBe('round')
})
networkTest('scales to custom size', async () => {
const icon = await fetchIcon('mdi:home', 48)
expect(icon.width).toBe(48)
expect(icon.height).toBe(48)
})
networkTest('caches repeated fetches', async () => {
const a = await fetchIcon('mdi:home', 24)
const b = await fetchIcon('mdi:home', 24)
expect(a).toBe(b)
})
networkTest('throws on invalid icon name format', async () => {
await expect(fetchIcon('invalid')).rejects.toThrow('prefix:name')
})
networkTest('throws on non-existent icon', async () => {
await expect(fetchIcon('mdi:this-icon-definitely-does-not-exist-xyz123')).rejects.toThrow(
'not found'
)
})
networkTest('heroicons:eye — multi-path icon with group attrs', async () => {
const icon = await fetchIcon('heroicons:eye', 24)
expect(icon.paths.length).toBeGreaterThanOrEqual(2)
})
})
describe('fetchIcons (batch)', () => {
networkTest('fetches multiple icons from same prefix in one request', async () => {
const icons = await fetchIcons(['lucide:star', 'lucide:home', 'lucide:search'], 24)
expect(icons.size).toBe(3)
expect(icons.get('lucide:star')).toBeDefined()
expect(icons.get('lucide:home')).toBeDefined()
expect(icons.get('lucide:search')).toBeDefined()
})
networkTest('fetches icons across multiple prefixes', async () => {
const icons = await fetchIcons(['mdi:heart', 'lucide:heart'], 24)
expect(icons.size).toBe(2)
expect(icons.get('mdi:heart')?.prefix).toBe('mdi')
expect(icons.get('lucide:heart')?.prefix).toBe('lucide')
})
networkTest('skips non-existent icons without failing', async () => {
const icons = await fetchIcons(['mdi:home', 'mdi:nonexistent-xyz-999'], 24)
expect(icons.has('mdi:home')).toBe(true)
expect(icons.has('mdi:nonexistent-xyz-999')).toBe(false)
})
networkTest('populates cache for subsequent fetchIcon calls', async () => {
await fetchIcons(['mdi:star'], 24)
const icon = await fetchIcon('mdi:star', 24)
expect(icon.prefix).toBe('mdi')
})
})
describe('searchIcons', () => {
networkTest('searches for heart icons', async () => {
const result = await searchIcons('heart', { limit: 10 })
expect(result.icons.length).toBeGreaterThan(0)
expect(result.total).toBeGreaterThan(0)
expect(result.icons[0]).toContain(':')
})
networkTest('filters by prefix', async () => {
const result = await searchIcons('home', { limit: 5, prefix: 'lucide' })
for (const icon of result.icons) {
expect(icon.startsWith('lucide:')).toBe(true)
}
})
})
describe('searchIconsBatch', () => {
networkTest('searches multiple queries in parallel', async () => {
const results = await searchIconsBatch(['heart', 'arrow'], { limit: 5 })
expect(results.size).toBe(2)
expect(expectDefined(results.get('heart'), 'heart results').icons.length).toBeGreaterThan(0)
expect(expectDefined(results.get('arrow'), 'arrow results').icons.length).toBeGreaterThan(0)
})
})