* 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
120 lines
3.8 KiB
TypeScript
120 lines
3.8 KiB
TypeScript
import { describe, expect, test } from 'bun:test'
|
|
|
|
import type { LanguageModel } from 'ai'
|
|
|
|
import { SceneGraph } from '@open-pencil/scene-graph'
|
|
|
|
import {
|
|
analyzeAttachedImages,
|
|
designMessageWithImageFindings,
|
|
type ImageAnalysisDependencies
|
|
} from '@/app/ai/attachment/image/analyze'
|
|
import {
|
|
prepareImageAttachment,
|
|
validateImageAttachmentFile
|
|
} from '@/app/ai/attachment/image/prepare'
|
|
import type { PreparedImageAttachment } from '@/app/ai/attachment/image/types'
|
|
import type { EditorStore } from '@/app/editor/session/create'
|
|
|
|
import { asDouble } from '#tests/helpers/doubles'
|
|
|
|
const image: PreparedImageAttachment = {
|
|
data: new Uint8Array([4, 5, 6]),
|
|
blob: new Blob(),
|
|
mediaType: 'image/png',
|
|
originalWidth: 1600,
|
|
originalHeight: 900,
|
|
width: 1280,
|
|
height: 720
|
|
}
|
|
|
|
const secondImage: PreparedImageAttachment = {
|
|
...image,
|
|
data: new Uint8Array([7, 8, 9])
|
|
}
|
|
|
|
describe('image attachment analysis', () => {
|
|
test('rejects unsupported and oversized source files', () => {
|
|
expect(validateImageAttachmentFile(new File(['x'], 'image.gif', { type: 'image/gif' }))).toBe(
|
|
'Choose a PNG, JPEG, or WebP image.'
|
|
)
|
|
expect(
|
|
validateImageAttachmentFile(
|
|
new File([new Uint8Array(20 * 1024 * 1024 + 1)], 'image.png', {
|
|
type: 'image/png'
|
|
})
|
|
)
|
|
).toBe('Images must be 20 MB or smaller.')
|
|
})
|
|
|
|
test('fails with a controlled error without browser image APIs', async () => {
|
|
await expect(
|
|
prepareImageAttachment(new File(['png'], 'image.png', { type: 'image/png' }))
|
|
).rejects.toThrow('Image attachments are unavailable in this environment.')
|
|
})
|
|
|
|
test('sends all bounded images only to Vision and returns text findings', async () => {
|
|
const graph = new SceneGraph()
|
|
const page = graph.getPages()[0]
|
|
const frame = graph.createNode('FRAME', page.id, { width: 2560, height: 1600 })
|
|
const requests: unknown[] = []
|
|
const store = asDouble<EditorStore>({
|
|
graph,
|
|
state: { currentPageId: page.id, selectedIds: new Set([frame.id]) },
|
|
renderExportImage: async () => new Uint8Array([1, 2, 3])
|
|
})
|
|
const dependencies: ImageAnalysisDependencies = {
|
|
createRuntime: async () =>
|
|
({
|
|
kind: 'direct',
|
|
model: {} as LanguageModel,
|
|
role: {
|
|
requestedRole: 'vision',
|
|
profile: { maxOutputTokens: 8000, thinkingLevel: 'low' },
|
|
connection: { providerID: 'openrouter' }
|
|
}
|
|
}) as never,
|
|
inspect: async (options) => {
|
|
requests.push(options)
|
|
return { text: 'Use a tighter grid and stronger heading contrast.' } as never
|
|
}
|
|
}
|
|
|
|
const findings = await analyzeAttachedImages(
|
|
store,
|
|
'Match this layout',
|
|
[image, secondImage],
|
|
dependencies
|
|
)
|
|
|
|
expect(findings).toBe('Use a tighter grid and stronger heading contrast.')
|
|
const request = requests[0] as {
|
|
providerOptions?: unknown
|
|
messages: Array<{ content: Array<{ type: string; data?: Uint8Array }> }>
|
|
}
|
|
expect(request.providerOptions).toEqual({ openrouter: { reasoning: { effort: 'low' } } })
|
|
expect(request.messages[0]?.content.map((part) => part.type)).toEqual([
|
|
'text',
|
|
'file',
|
|
'file',
|
|
'file'
|
|
])
|
|
expect(request.messages[0]?.content[1]?.data).toEqual(image.data)
|
|
expect(request.messages[0]?.content[2]?.data).toEqual(secondImage.data)
|
|
})
|
|
|
|
test('passes textual findings rather than image data to Design', () => {
|
|
const message = designMessageWithImageFindings(
|
|
'Match this layout',
|
|
['first.png', 'second.png'],
|
|
'Use a 12-column grid.'
|
|
)
|
|
|
|
expect(message).toContain('Match this layout')
|
|
expect(message).toContain('first.png')
|
|
expect(message).toContain('second.png')
|
|
expect(message).toContain('Use a 12-column grid.')
|
|
expect(message).not.toContain('base64')
|
|
})
|
|
})
|