openpencil/tests/engine/app/chat/history/controller.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

184 lines
5.6 KiB
TypeScript

import 'fake-indexeddb/auto'
import { expect, test } from 'bun:test'
import type { UIMessage } from 'ai'
import { reactive } from 'vue'
import { createConversationHistory } from '@/app/ai/chat/history/controller'
import type { ChatDocumentEditor } from '@/app/ai/chat/history/document'
import { createConversationStore } from '@/app/ai/chat/history/idb'
import { expectDefined } from '#tests/helpers/assert'
function editor(): ChatDocumentEditor {
const id = crypto.randomUUID()
return {
state: { documentName: 'Test document' },
getRecoveryId: () => id,
getSourceIdentity: () => ({ path: null, handle: null }),
getDocumentFilePath: () => null,
getStorageBinding: () => null
}
}
test('adopting recovery in the same editor loads its selection instead of moving old chats', async () => {
const store = createConversationStore()
const active = editor()
const history = createConversationHistory(
{
getEditor: () => active,
ensureChat: async () => null,
profileId: () => null,
resetChat: async () => undefined,
backend: () => 'direct'
},
store
)
await history.initialize()
const original = history.current.value
if (!original) throw new Error('Missing draft')
await history.rename(original.id, 'Existing chat')
const recoveryId = crypto.randomUUID()
const restoredId = crypto.randomUUID()
await store.write({
...original,
id: restoredId,
documentId: `recovery:${recoveryId}`,
title: 'Recovered chat',
titleSource: 'manual'
})
await store.select(`recovery:${recoveryId}`, restoredId)
active.getRecoveryId = () => recoveryId
await history.initialize()
expect(history.current.value?.id).toBe(restoredId)
expect((await store.read(original.id))?.documentId).toBe(original.documentId)
})
function fixture() {
let activeEditor = editor()
let failWrites = false
let resets = 0
const store = createConversationStore()
const chat = reactive({
messages: [] as UIMessage[],
status: 'ready' as const,
stop: async () => undefined
})
const history = createConversationHistory(
{
getEditor: () => activeEditor,
ensureChat: async () => chat,
resetChat: async () => {
resets++
},
profileId: () => null,
backend: () => 'direct'
},
{
...store,
write: async (row) => {
if (failWrites) throw new DOMException('Storage full', 'QuotaExceededError')
await store.write(row)
}
}
)
return {
history,
store,
chat,
setFail: (value: boolean) => {
failWrites = value
},
getResets: () => resets,
switchEditor: () => {
activeEditor = editor()
}
}
}
test('failed flush keeps the current conversation and blocks switching until retry succeeds', async () => {
const f = fixture()
await f.history.ensureChat()
const id = f.history.current.value?.id
f.chat.messages = [
{ id: 'message', role: 'user', parts: [{ type: 'text', text: 'Keep this draft' }] }
]
f.setFail(true)
await expect(f.history.newChat()).rejects.toThrow('Storage full')
expect(f.history.current.value?.id).toBe(id)
expect(f.history.messages.value[0]?.id).toBe('message')
expect(f.history.storageError.value).toBe(true)
f.setFail(false)
await f.history.newChat()
expect(f.history.current.value?.id).not.toBe(id)
expect(f.history.storageError.value).toBe(false)
if (!id) throw new Error('Missing conversation')
expect((await f.store.read(id))?.messages[0]?.message.id).toBe('message')
})
test('queued chat switches finish on the last requested conversation without losing transcripts', async () => {
const f = fixture()
await f.history.initialize()
const first = f.history.current.value?.id
if (first) await f.history.rename(first, 'First')
await f.history.newChat()
const second = f.history.current.value?.id
if (second) await f.history.rename(second, 'Second')
if (!first || !second) throw new Error('Missing conversation')
await Promise.all(
Array.from({ length: 20 }, (_, index) => f.history.open(index % 2 === 0 ? first : second))
)
const current = expectDefined(f.history.current.value, 'current conversation')
expect(current.id).toBe(second)
expect(await f.store.getSelected(current.documentId)).toBe(second)
expect(await f.store.read(first)).not.toBeNull()
expect(f.history.busy.value).toBe(false)
})
test('opening the panel and repeatedly creating drafts does not persist empty chats', async () => {
const f = fixture()
await f.history.initialize()
const documentId = f.history.current.value?.documentId
await f.history.newChat()
await f.history.newChat()
expect(await f.store.list(documentId)).toEqual([])
if (!documentId) throw new Error('Missing document')
expect(await f.store.getSelected(documentId)).toBeNull()
})
test('a pending transport is rejected when the active document changes', async () => {
let activeEditor = editor()
const pending = Promise.withResolvers<{
messages: UIMessage[]
status: 'ready'
stop: () => Promise<void>
}>()
const started = Promise.withResolvers<undefined>()
let stops = 0
const history = createConversationHistory(
{
getEditor: () => activeEditor,
ensureChat: () => {
started.resolve(undefined)
return pending.promise
},
profileId: () => null,
resetChat: async () => undefined,
backend: () => 'direct'
},
createConversationStore()
)
const initializing = history.ensureChat()
await started.promise
activeEditor = editor()
pending.resolve({
messages: [],
status: 'ready',
stop: async () => {
stops++
}
})
expect(await initializing).toBeNull()
expect(stops).toBe(1)
})