* 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
203 lines
6.6 KiB
TypeScript
203 lines
6.6 KiB
TypeScript
import { afterEach, describe, expect, test, vi } from 'bun:test'
|
|
|
|
import { defineComponent, h, type ComponentPublicInstance } from 'vue'
|
|
|
|
import {
|
|
useDocumentWorkspace,
|
|
type DocumentWorkspaceItem,
|
|
type DocumentWorkspaceSource
|
|
} from '@open-pencil/vue'
|
|
|
|
import { createTestRenderer, hostNode } from '#tests/helpers/vue/renderer'
|
|
|
|
type Deferred<Value> = {
|
|
promise: Promise<Value>
|
|
resolve(value: Value): void
|
|
}
|
|
|
|
function deferred<Value>(): Deferred<Value> {
|
|
let resolvePromise: ((value: Value) => void) | null = null
|
|
const promise = new Promise<Value>((resolve) => {
|
|
resolvePromise = resolve
|
|
})
|
|
return {
|
|
promise,
|
|
resolve(value) {
|
|
resolvePromise?.(value)
|
|
}
|
|
}
|
|
}
|
|
|
|
const renderer = createTestRenderer()
|
|
|
|
type Workspace = ReturnType<typeof useDocumentWorkspace<DocumentWorkspaceItem>>
|
|
|
|
type WorkspaceHolder = { current: Workspace | null }
|
|
|
|
async function flushTasks(): Promise<void> {
|
|
await new Promise<void>((resolve) => {
|
|
setTimeout(resolve, 0)
|
|
})
|
|
}
|
|
|
|
function mountWorkspace(
|
|
source: DocumentWorkspaceSource<DocumentWorkspaceItem>,
|
|
options: { previewConcurrency?: number } = {}
|
|
): { workspace: Workspace; unmount(): void } {
|
|
const holder: WorkspaceHolder = { current: null }
|
|
const component = defineComponent({
|
|
setup() {
|
|
holder.current = useDocumentWorkspace({
|
|
source,
|
|
previewConcurrency: options.previewConcurrency,
|
|
refreshOnFocus: false,
|
|
refreshOnReconnect: false
|
|
})
|
|
return () => h('div')
|
|
}
|
|
})
|
|
const app = renderer.createApp(component)
|
|
app.mount(hostNode()) as ComponentPublicInstance
|
|
const workspace = holder.current
|
|
if (workspace == null) throw new Error('Workspace composable did not initialize')
|
|
return { workspace, unmount: () => app.unmount() }
|
|
}
|
|
|
|
afterEach(() => {
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
describe('useDocumentWorkspace', () => {
|
|
test('shares an in-flight refresh and responds to source events', async () => {
|
|
const firstRefresh = deferred<DocumentWorkspaceItem[]>()
|
|
const source: { listener: (() => void) | null } = { listener: null }
|
|
const refresh = vi
|
|
.fn<() => Promise<DocumentWorkspaceItem[]>>()
|
|
.mockImplementationOnce(() => firstRefresh.promise)
|
|
.mockResolvedValueOnce([{ id: 'second', name: 'Second', updatedAt: '2026-08-10' }])
|
|
const mounted = mountWorkspace({
|
|
refresh,
|
|
loadPreview: async () => null,
|
|
subscribe(listener) {
|
|
source.listener = listener
|
|
return () => {
|
|
source.listener = null
|
|
}
|
|
}
|
|
})
|
|
|
|
const inFlightA = mounted.workspace.refresh()
|
|
const inFlightB = mounted.workspace.refresh()
|
|
expect(inFlightA).toBe(inFlightB)
|
|
expect(refresh).toHaveBeenCalledTimes(1)
|
|
|
|
source.listener?.()
|
|
source.listener?.()
|
|
firstRefresh.resolve([{ id: 'first', name: 'First', updatedAt: '2026-08-09' }])
|
|
await inFlightA
|
|
await flushTasks()
|
|
expect(refresh).toHaveBeenCalledTimes(2)
|
|
expect(mounted.workspace.documents.value.map(({ id }) => id)).toEqual(['second'])
|
|
|
|
mounted.unmount()
|
|
expect(source.listener).toBeNull()
|
|
})
|
|
|
|
test('invalidates an in-flight preview when the document changes', async () => {
|
|
const previewLoad = deferred<Uint8Array | null>()
|
|
const freshPreviewLoad = deferred<Uint8Array | null>()
|
|
const refresh = vi
|
|
.fn<() => Promise<DocumentWorkspaceItem[]>>()
|
|
.mockResolvedValueOnce([{ id: 'one', name: 'One', updatedAt: 'first' }])
|
|
.mockResolvedValueOnce([{ id: 'one', name: 'One', updatedAt: 'second' }])
|
|
const loadPreview = vi
|
|
.fn<() => Promise<Uint8Array | null>>()
|
|
.mockImplementationOnce(() => previewLoad.promise)
|
|
.mockImplementationOnce(() => freshPreviewLoad.promise)
|
|
const createObjectURL = vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:preview')
|
|
const mounted = mountWorkspace({ refresh, loadPreview })
|
|
await flushTasks()
|
|
|
|
mounted.workspace.loadPreview('one')
|
|
expect(loadPreview).toHaveBeenCalledTimes(1)
|
|
await mounted.workspace.refresh()
|
|
previewLoad.resolve(new Uint8Array([1]))
|
|
await previewLoad.promise
|
|
await flushTasks()
|
|
|
|
expect(createObjectURL).not.toHaveBeenCalled()
|
|
expect(loadPreview).toHaveBeenCalledTimes(2)
|
|
freshPreviewLoad.resolve(new Uint8Array([2]))
|
|
await freshPreviewLoad.promise
|
|
await flushTasks()
|
|
expect(createObjectURL).toHaveBeenCalledTimes(1)
|
|
mounted.unmount()
|
|
})
|
|
|
|
test('surfaces preview failures to consumers', async () => {
|
|
const failure = new Error('preview failed')
|
|
const onPreviewError = vi.fn()
|
|
const holder: WorkspaceHolder = { current: null }
|
|
const component = defineComponent({
|
|
setup() {
|
|
holder.current = useDocumentWorkspace({
|
|
source: {
|
|
refresh: async () => [],
|
|
loadPreview: async () => {
|
|
throw failure
|
|
}
|
|
},
|
|
refreshOnFocus: false,
|
|
refreshOnReconnect: false,
|
|
onPreviewError
|
|
})
|
|
return () => h('div')
|
|
}
|
|
})
|
|
const app = renderer.createApp(component)
|
|
app.mount(hostNode())
|
|
holder.current?.loadPreview('one')
|
|
await flushTasks()
|
|
|
|
expect(holder.current?.previewErrors.value.one).toBe(failure)
|
|
expect(onPreviewError).toHaveBeenCalledWith('one', failure)
|
|
app.unmount()
|
|
})
|
|
|
|
test('deduplicates previews, limits concurrency, and revokes URLs on unmount', async () => {
|
|
const loads = new Map<string, Deferred<Uint8Array | null>>()
|
|
const loadPreview = vi.fn((id: string) => {
|
|
const load = deferred<Uint8Array | null>()
|
|
loads.set(id, load)
|
|
return load.promise
|
|
})
|
|
const createObjectURL = vi.spyOn(URL, 'createObjectURL').mockReturnValue('blob:preview')
|
|
const revokeObjectURL = vi.spyOn(URL, 'revokeObjectURL').mockImplementation(() => undefined)
|
|
const mounted = mountWorkspace(
|
|
{ refresh: async () => [], loadPreview },
|
|
{ previewConcurrency: 2 }
|
|
)
|
|
|
|
mounted.workspace.loadPreview('one')
|
|
mounted.workspace.loadPreview('one')
|
|
mounted.workspace.loadPreview('two')
|
|
mounted.workspace.loadPreview('three')
|
|
expect(loadPreview.mock.calls.map((call) => call[0])).toEqual(['one', 'two'])
|
|
|
|
loads.get('one')?.resolve(new Uint8Array([1]))
|
|
await loads.get('one')?.promise
|
|
await flushTasks()
|
|
expect(loadPreview.mock.calls.map((call) => call[0])).toEqual(['one', 'two', 'three'])
|
|
expect(createObjectURL).toHaveBeenCalledTimes(1)
|
|
|
|
mounted.unmount()
|
|
expect(revokeObjectURL).toHaveBeenCalledWith('blob:preview')
|
|
|
|
loads.get('two')?.resolve(new Uint8Array([2]))
|
|
loads.get('three')?.resolve(new Uint8Array([3]))
|
|
await Promise.all([loads.get('two')?.promise, loads.get('three')?.promise])
|
|
await flushTasks()
|
|
expect(createObjectURL).toHaveBeenCalledTimes(1)
|
|
})
|
|
})
|