/**
* HtmlPreviewFrame tests: the browser / unsandboxed paths keep the exact
* route-src iframe; the desktop sandboxed path fetches the document and its
* local assets from the top frame, frames a sandboxed blob, and revokes it.
* A failed document fetch falls back to the route src.
*/
// @vitest-environment jsdom
import { afterEach, describe, expect, it, vi } from 'vitest'
import { createElement } from 'react'
import { createRoot, type Root } from 'react-dom/client'
import { renderToString } from 'react-dom/server'
import { act } from 'react-dom/test-utils'
import { HtmlPreviewFrame } from '../src/client/HtmlPreviewFrame.tsx'
import { HTML_IFRAME_SANDBOX } from '../src/client/html-preview.ts'
import { setupReactAct } from './test-utils.ts'
setupReactAct()
const SRC = 'dsh-app://app/sidebar/html/s1/p/docs/index.html'
const ORIGIN = 'dsh-app://app/'
/** Resolve after the effect's fetch chain has had a turn. */
async function flush(): Promise {
await new Promise((resolve) => setTimeout(resolve, 0))
}
/** Install a fetch that serves one HTML document plus one image. */
function stubFetch(options: { htmlOk?: boolean } = {}): { calls: string[]; restore: () => void } {
const calls: string[] = []
const original = globalThis.fetch
const fetchMock = async (input: string | URL | Request): Promise => {
const url = typeof input === 'string' ? input : input instanceof URL ? input.href : input.url
calls.push(url)
if (url.endsWith('/index.html')) {
const ok = options.htmlOk !== false
return {
ok,
status: ok ? 200 : 403,
text: async () => '
',
headers: new Headers(),
} as unknown as Response
}
return {
ok: true,
status: 200,
arrayBuffer: async () => Uint8Array.from([1, 2, 3]).buffer,
headers: new Headers({ 'content-type': 'image/png' }),
} as unknown as Response
}
vi.stubGlobal('fetch', fetchMock)
return { calls, restore: () => { globalThis.fetch = original } }
}
let root: Root | undefined
afterEach(() => {
if (root !== undefined) {
act(() => root?.unmount())
root = undefined
}
vi.unstubAllGlobals()
delete (URL as unknown as { createObjectURL?: unknown }).createObjectURL
delete (URL as unknown as { revokeObjectURL?: unknown }).revokeObjectURL
})
describe('HtmlPreviewFrame', () => {
it('renders the route-src iframe on a normal web page', () => {
const html = renderToString(createElement(HtmlPreviewFrame, {
src: '/sidebar/html/s1/p/a/index.html',
title: 'index.html',
className: 'frame',
sandboxed: true,
baseUrl: 'http://localhost/',
}))
const iframe = /