import { describe, expect, it } from 'vitest'; import { hasTweaksTemplate, hasUrlModeBridge, htmlNeedsFocusGuard, htmlNeedsPoweredPreview, htmlNeedsSandboxShim, parseForceInline, shouldUrlLoadHtmlPreview, } from '../../src/components/file-viewer-render-mode'; describe('shouldUrlLoadHtmlPreview', () => { const base = { mode: 'preview' as const, isDeck: false, commentMode: false, forceInline: false }; it('URL-loads a plain HTML preview by default', () => { expect(shouldUrlLoadHtmlPreview(base)).toBe(true); }); it('falls back to srcDoc when the file is a deck (deck bridge required)', () => { expect(shouldUrlLoadHtmlPreview({ ...base, isDeck: true })).toBe(false); }); it('falls back to srcDoc when comment mode is active without a URL bridge', () => { expect(shouldUrlLoadHtmlPreview({ ...base, commentMode: true })).toBe(false); }); it('keeps URL-load when comment mode is active and the artifact owns the bridge', () => { expect(shouldUrlLoadHtmlPreview({ ...base, commentMode: true, urlModeBridge: true })).toBe(true); }); it('keeps URL-load when comment mode is active and the raw route injects the comment bridge', () => { expect(shouldUrlLoadHtmlPreview({ ...base, commentMode: true, urlCommentBridge: true })).toBe(true); }); it('falls back to srcDoc when direct edit mode is active without an artifact-owned bridge', () => { expect(shouldUrlLoadHtmlPreview({ ...base, editMode: true })).toBe(false); }); it('falls back to srcDoc when direct edit mode is active even if the artifact owns a URL bridge', () => { expect(shouldUrlLoadHtmlPreview({ ...base, editMode: true, urlModeBridge: true })).toBe(false); }); it('falls back to srcDoc when inspect mode is active (selection bridge required)', () => { expect(shouldUrlLoadHtmlPreview({ ...base, inspectMode: true })).toBe(false); }); it('falls back to srcDoc when draw mode is active without a URL snapshot bridge', () => { expect(shouldUrlLoadHtmlPreview({ ...base, drawMode: true })).toBe(false); }); it('keeps URL-load when draw mode is active and the raw route injected the snapshot bridge', () => { expect(shouldUrlLoadHtmlPreview({ ...base, drawMode: true, urlSnapshotBridge: true })).toBe(true); }); it('falls back to srcDoc when the artifact ships the class based tweaks template', () => { // Without this, a plain `.tw-panel` artifact would URL load on first // open, skip the tweaks bridge entirely, and leave the toolbar toggle // disabled (no `od:tweaks-available` ever fires). expect(shouldUrlLoadHtmlPreview({ ...base, tweaksBridge: true })).toBe(false); }); it('falls back to srcDoc when the user opts in via forceInline', () => { expect(shouldUrlLoadHtmlPreview({ ...base, forceInline: true })).toBe(false); }); it('falls back to srcDoc when the HTML source needs a focus guard', () => { expect(shouldUrlLoadHtmlPreview({ ...base, needsFocusGuard: true })).toBe(false); }); it('falls back to srcDoc when the source references project files by site-root path', () => { // URL-load serves the document untouched, so `/reference-assets/main.css` // resolves against the app origin root and 404s; only the srcDoc pipeline // rewrites confirmed root-relative refs into resolvable URLs. expect(shouldUrlLoadHtmlPreview({ ...base, projectRootAssetRefs: true })).toBe(false); }); it('does not URL-load while the source-code tab is active', () => { expect(shouldUrlLoadHtmlPreview({ ...base, mode: 'source' })).toBe(false); }); it('treats any disqualifying flag as sufficient on its own', () => { expect(shouldUrlLoadHtmlPreview({ ...base, isDeck: true, commentMode: true })).toBe(false); expect(shouldUrlLoadHtmlPreview({ ...base, isDeck: true, forceInline: true })).toBe(false); expect(shouldUrlLoadHtmlPreview({ ...base, commentMode: true, forceInline: true })).toBe(false); expect(shouldUrlLoadHtmlPreview({ ...base, tweaksBridge: true, forceInline: true })).toBe(false); expect(shouldUrlLoadHtmlPreview({ ...base, commentMode: true, urlModeBridge: true, inspectMode: true })).toBe(false); expect(shouldUrlLoadHtmlPreview({ ...base, drawMode: true, urlSnapshotBridge: true, inspectMode: true })).toBe(false); }); }); describe('hasTweaksTemplate', () => { it('matches a plain `.tw-panel` artifact', () => { const source = '
'; expect(hasTweaksTemplate(source)).toBe(true); }); it('matches the `.tw-hidden` toggle class even without an explicit `.tw-panel`', () => { // Defensive: the template ships both selectors and either one signals a // tweaks-template artifact that needs the bridge. const source = ''; expect(hasTweaksTemplate(source)).toBe(true); }); it('does not match unrelated identifiers that merely contain `tw`', () => { expect(hasTweaksTemplate('Use od-direct-edit.js for editing
')).toBe(false); expect(hasUrlModeBridge('')).toBe(false); }); it('ignores unrelated script URLs', () => { expect(hasUrlModeBridge('')).toBe(false); expect(hasUrlModeBridge(null)).toBe(false); }); }); describe('parseForceInline', () => { it('returns false when the parameter is absent', () => { expect(parseForceInline('')).toBe(false); expect(parseForceInline('?other=1')).toBe(false); expect(parseForceInline(null)).toBe(false); expect(parseForceInline(undefined)).toBe(false); }); it('returns true for the documented opt-in values', () => { expect(parseForceInline('?forceInline=1')).toBe(true); expect(parseForceInline('?forceInline=true')).toBe(true); expect(parseForceInline('?forceInline=TRUE')).toBe(true); expect(parseForceInline('?forceInline=yes')).toBe(true); expect(parseForceInline('?forceInline=on')).toBe(true); }); it('returns false for explicit opt-out values and unrelated strings', () => { expect(parseForceInline('?forceInline=0')).toBe(false); expect(parseForceInline('?forceInline=false')).toBe(false); expect(parseForceInline('?forceInline=no')).toBe(false); expect(parseForceInline('?forceInline=off')).toBe(false); expect(parseForceInline('?forceInline=banana')).toBe(false); }); it('treats an empty value as absent (defensive: ?forceInline= shows up as "")', () => { expect(parseForceInline('?forceInline=')).toBe(false); }); it('accepts a pre-built URLSearchParams', () => { const params = new URLSearchParams('forceInline=1&other=foo'); expect(parseForceInline(params)).toBe(true); }); it('survives surrounding whitespace in the value', () => { const params = new URLSearchParams(); params.set('forceInline', ' 1 '); expect(parseForceInline(params)).toBe(true); }); }); describe('htmlNeedsSandboxShim', () => { it('returns false for plain static HTML', () => { expect(htmlNeedsSandboxShim('
')).toBe(false);
// `` similarly does not run JavaScript.
expect(htmlNeedsSandboxShim('')).toBe(false);
// Text content mentioning `script src=` (e.g. a docs page) must not trigger.
expect(htmlNeedsSandboxShim('Use <script src="app.js">
Our web worker culture is great
')).toBe(false); expect(htmlNeedsPoweredPreview('A workshop about 3D printing
')).toBe(false); }); });