Files
apes-sidebar/tests/mermaid-frame-asset.spec.ts
T
OCILY d7925a87d9 fix(mermaid): separate diagram errors from frame failures and validate the frame protocol
Review findings on #1040:

- A bad fence no longer poisons the document. `renderMermaidDiagram` used to
  treat every `renderInFrame` rejection as a frame failure, so a mermaid
  syntax error (a normal `mermaid-error` answer) was retried and then flipped
  `frameUnavailable` — every later diagram failed until the page reloaded.
  Frame answers are now classified: `MermaidDiagramError` is final for that
  one diagram (its block shows the message + source), while
  `MermaidFrameError` (load/handshake/timeout/protocol) gets the single retry
  and then the page-level fast-fail breaker.
- Both directions of postMessage are validated. Parent side binds
  `event.source` to the frame's `contentWindow` plus an opaque-origin
  allowlist and validates id/svg/message; the inlined frame script binds
  `event.source` to `window.parent` and validates id/code/theme/length before
  rendering.
- Visibility gating now flushes on return: the 80ms inline-HTML pass remembers
  a skipped pass and replays it on `visibilitychange`, and the preview handle
  also pauses/resumes on document visibility (not just IntersectionObserver).
- Tests: new `tests/mermaid-frame.spec.ts` covers the error classes
  (diagram error does not break later renders; transport retry; breaker after
  two failures) and the message validator (wrong window/origin, malformed
  payloads); the asset spec pins the frame-side sender/payload checks.
2026-10-28 20:20:50 +08:00

40 lines
2.0 KiB
TypeScript

/**
* The generated mermaid frame asset must stay a single self-contained HTML
* document: the preview loads it into an opaque-origin sandboxed iframe, which
* can fetch no subresources, and a raw `</script` inside either inlined script
* would close the element early and corrupt the frame.
*/
import { describe, expect, it } from 'vitest'
import { MERMAID_FRAME_HTML } from '../src/client/mermaid-frame-asset.generated.ts'
describe('mermaid frame asset', () => {
it('is a self-contained HTML document with the frame protocol', () => {
expect(MERMAID_FRAME_HTML.startsWith('<!doctype html>')).toBe(true)
expect(MERMAID_FRAME_HTML).toContain('window.__dshMermaidFrame')
expect(MERMAID_FRAME_HTML).toContain('mermaid-frame-ready')
expect(MERMAID_FRAME_HTML).toContain('dsh-frame-mermaid-')
})
it('inlines exactly two script bodies (mermaid UMD + frame protocol)', () => {
// Every `</script` inside the inlined sources is escaped as `<\/script`,
// so the document carries exactly the two structural closing tags.
expect(MERMAID_FRAME_HTML.match(/<\/script>/gi)?.length).toBe(2)
})
it('carries the strict mermaid configuration used by the preview', () => {
expect(MERMAID_FRAME_HTML).toContain("securityLevel: 'strict'")
expect(MERMAID_FRAME_HTML).toContain('htmlLabels: false')
expect(MERMAID_FRAME_HTML).toContain('suppressErrorRendering: true')
})
it('validates the sender and the payload on the frame side', () => {
// The opaque-origin sandbox cannot compare origins, so the frame binds
// requests to its parent window and validates every field it consumes.
expect(MERMAID_FRAME_HTML).toContain('event.source !== window.parent')
expect(MERMAID_FRAME_HTML).toContain("typeof data.id !== 'number'")
expect(MERMAID_FRAME_HTML).toContain("typeof data.code !== 'string'")
expect(MERMAID_FRAME_HTML).toContain('data.code.length > MAX_SOURCE')
expect(MERMAID_FRAME_HTML).toContain("data.theme === 'dark' ? 'dark' : 'default'")
})
})