openpencil/tests/engine/library/routed-catalog.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

97 lines
3.4 KiB
TypeScript

import { describe, expect, test } from 'bun:test'
import 'fake-indexeddb/auto'
import { SceneGraph } from '@open-pencil/scene-graph'
import type { LibraryObjectStore } from '@/app/integrations/storage'
import { LocalLibraryCatalog } from '@/app/libraries/catalog/local'
import { RoutedLibraryCatalog } from '@/app/libraries/catalog/routed'
import { StorageLibraryCatalog } from '@/app/libraries/catalog/storage'
class MemoryObjects implements LibraryObjectStore {
readonly values = new Map<string, Uint8Array>()
fail = false
async getObject(key: string) {
if (this.fail) throw new Error('offline')
return this.values.get(key) ?? null
}
async putObject(key: string, bytes: Uint8Array) {
if (this.fail) throw new Error('offline')
this.values.set(key, new Uint8Array(bytes))
}
async listObjects(prefix: string) {
if (this.fail) throw new Error('offline')
return [...this.values]
.filter(([key]) => key.startsWith(prefix))
.map(([key, value]) => ({ key, size: value.byteLength, etag: null }))
}
}
type CatalogDatabaseName = NonNullable<ConstructorParameters<typeof LocalLibraryCatalog>[0]>
/**
* `LocalLibraryCatalog` infers its database-name parameter from the app's single default name, so
* the parameter reads as that one literal even though any name opens a database. Each case needs
* its own, so the widening happens here once, with the reason written down.
*/
function isolatedCatalog(): LocalLibraryCatalog {
const databaseName: string = `library-cache-${crypto.randomUUID()}`
return new LocalLibraryCatalog(databaseName as CatalogDatabaseName)
}
function graph() {
const result = new SceneGraph()
result.createNode('COMPONENT', result.getPages()[0].id, {
name: 'Button',
componentKey: 'button'
})
return result
}
describe('routed library catalog', () => {
test('caches remote revisions for offline retrieval', async () => {
const objects = new MemoryObjects()
const remote = new StorageLibraryCatalog(objects)
const local = isolatedCatalog()
const routed = new RoutedLibraryCatalog(local)
routed.useStorage(remote)
const published = await routed.publishRevision({
libraryId: 'design-system',
name: 'Design system',
graph: graph()
})
objects.fail = true
expect(
(await routed.getRevision('design-system', published.manifest.revisionId)).manifest
).toEqual(published.manifest)
expect(await routed.listLibraries()).toMatchObject([
{ libraryId: 'design-system', latestRevisionId: published.manifest.revisionId }
])
})
test('does not replace corrupted remote revisions with cached data', async () => {
const objects = new MemoryObjects()
const remote = new StorageLibraryCatalog(objects)
const local = isolatedCatalog()
const routed = new RoutedLibraryCatalog(local)
routed.useStorage(remote)
const published = await routed.publishRevision({
libraryId: 'design-system',
name: 'Design system',
graph: graph()
})
const key = `open-pencil/libraries/design-system/revisions/${published.manifest.revisionId}.json`
const bytes = objects.values.get(key)
if (!bytes) throw new Error('Expected remote revision')
objects.values.set(
key,
new TextEncoder().encode(new TextDecoder().decode(bytes).replace('Button', 'Corrupted'))
)
await expect(
routed.getRevision('design-system', published.manifest.revisionId)
).rejects.toThrow('hash mismatch')
})
})