openpencil/tests/engine/app/document/io/browser.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

84 lines
2.8 KiB
TypeScript

import { afterEach, describe, expect, mock, test } from 'bun:test'
import { readBoundedBody } from '@/app/document/io/browser'
import { openBrowserFileFromURL } from '@/app/shell/menu/files'
import { fetchStub } from '#tests/helpers/fetch'
const MIB = 1024 * 1024
const originalFetch = globalThis.fetch
function streamed(chunks: number[]): Response {
return new Response(
new ReadableStream<Uint8Array>({
start(controller) {
for (const size of chunks) controller.enqueue(new Uint8Array(size))
controller.close()
}
})
)
}
afterEach(() => {
globalThis.fetch = originalFetch
})
describe('readBoundedBody', () => {
test('buffers a body that stays under the cap', async () => {
const onExceeded = mock(() => undefined)
const bytes = await readBoundedBody(streamed([20, 10]), MIB, { onExceeded })
expect(bytes.byteLength).toBe(30)
expect(onExceeded).not.toHaveBeenCalled()
})
test('aborts the request as soon as the cap is exceeded', async () => {
const onExceeded = mock(() => undefined)
// The third chunk pushes it over: the read stops there instead of draining the
// body, and the notice names the cap the user has to stay under.
const body = streamed([MIB / 2, MIB / 2, 1, 64 * MIB])
await expect(readBoundedBody(body, MIB, { onExceeded })).rejects.toThrow('exceeds 1 MiB')
expect(onExceeded).toHaveBeenCalledTimes(1)
})
test('accepts a body of exactly the cap and refuses one byte more', async () => {
const onExceeded = mock(() => undefined)
const exact = await readBoundedBody(streamed([MIB / 2, MIB / 2]), MIB, { onExceeded })
expect(exact.byteLength).toBe(MIB)
expect(onExceeded).not.toHaveBeenCalled()
await expect(readBoundedBody(streamed([MIB, 1]), MIB, { onExceeded })).rejects.toThrow(
'exceeds'
)
expect(onExceeded).toHaveBeenCalledTimes(1)
})
test('counts the bytes that arrive, not a Content-Length claim', async () => {
const response = streamed([MIB, MIB])
response.headers.set('Content-Length', '1')
const onExceeded = mock(() => undefined)
await expect(readBoundedBody(response, MIB, { onExceeded })).rejects.toThrow('exceeds')
expect(onExceeded).toHaveBeenCalledTimes(1)
})
test('lets a caller name its own oversized-body error', async () => {
await expect(
readBoundedBody(streamed([MIB, 1]), MIB, { sizeError: 'Vectorized SVG is too large' })
).rejects.toThrow('Vectorized SVG is too large')
})
})
describe('openBrowserFileFromURL', () => {
test('refuses a response with no body instead of opening an empty document', async () => {
globalThis.fetch = fetchStub(async () => new Response(null, { status: 200 }))
await expect(openBrowserFileFromURL(new URL('https://example.com/design.pen'))).rejects.toThrow(
'carried no body'
)
})
})