openpencil/src/app/browser-bridge.ts
Danila Poyarkov bbf122a5c5 chore(tests): route store access through browser bridge
- Expose window.openPencil.getStore() instead of a direct store property
- Update E2E helpers and specs to use the bridge getter
- Add lint coverage preventing direct window.openPencil.store access
2026-05-06 12:19:39 +03:00

49 lines
1.2 KiB
TypeScript

import type { ChatTransport, UIMessage } from 'ai'
import type { EditorStore } from '@/app/editor/session/create'
export interface OpenPencilTestHooks {
writeCount?: () => number
mockHandle?: FileSystemFileHandle
savedOpen?: Window['open']
}
export interface OpenPencilWindowAPI {
getStore?: () => EditorStore
setChatTransport?: (factory: () => ChatTransport<UIMessage>) => void
openFile?: (path: string) => Promise<void>
test?: OpenPencilTestHooks
}
declare global {
interface Window {
openPencil?: OpenPencilWindowAPI
}
}
let activeStore: EditorStore | null = null
function windowApi(): OpenPencilWindowAPI {
window.openPencil ??= {}
window.openPencil.getStore ??= () => {
if (!activeStore) throw new Error('OpenPencil store not initialized')
return activeStore
}
return window.openPencil
}
export function setOpenPencilStore(store: EditorStore) {
activeStore = store
windowApi()
}
export function exposeChatTransportOverride(
setChatTransport: (factory: () => ChatTransport<UIMessage>) => void
) {
windowApi().setChatTransport = setChatTransport
}
export function setOpenPencilOpenFileHandler(openFile: (path: string) => Promise<void>) {
windowApi().openFile = openFile
}