|
| 1 | +import fs from 'node:fs/promises' |
| 2 | +import path from 'node:path' |
| 3 | +import { fileURLToPath } from 'node:url' |
| 4 | +import type { Har as HarFile } from 'har-format' |
| 5 | +import { parseHTML } from 'linkedom' |
| 6 | +import { PAGES } from './har-index' |
| 7 | + |
| 8 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)) |
| 9 | + |
| 10 | +export interface TestDOMGlobals { |
| 11 | + Document: typeof Document |
| 12 | + DocumentFragment: typeof DocumentFragment |
| 13 | + document: Document |
| 14 | + Element: typeof Element |
| 15 | + HTMLDivElement: typeof HTMLDivElement |
| 16 | + HTMLElement: typeof HTMLElement |
| 17 | + HTMLMetaElement: typeof HTMLMetaElement |
| 18 | + HTMLTextAreaElement: typeof HTMLTextAreaElement |
| 19 | + location: Location |
| 20 | + Node: typeof Node |
| 21 | + Text: typeof Text |
| 22 | + window: Window |
| 23 | +} |
| 24 | + |
| 25 | +export interface TestDOMContext { |
| 26 | + cleanup: () => void |
| 27 | + document: Document |
| 28 | + window: Window |
| 29 | + url: string |
| 30 | +} |
| 31 | + |
| 32 | +let currentDOMInstance: any = null |
| 33 | +let originalGlobals: Partial<TestDOMGlobals> = {} |
| 34 | + |
| 35 | +export async function loadHtmlFromHar(key: keyof typeof PAGES): Promise<string> { |
| 36 | + const url = PAGES[key] |
| 37 | + const harPath = path.join(__dirname, 'har', `${key}.har`) |
| 38 | + const harContent = await fs.readFile(harPath, 'utf-8') |
| 39 | + const harData: HarFile = JSON.parse(harContent) |
| 40 | + const mainEntry = harData.log.entries.find((entry) => entry.request.url === url) |
| 41 | + |
| 42 | + if (!mainEntry) { |
| 43 | + throw new Error(`No entry found for URL: ${url} in HAR file: ${harPath}`) |
| 44 | + } |
| 45 | + |
| 46 | + return mainEntry.response.content.text || '' |
| 47 | +} |
| 48 | + |
| 49 | +export function createDOMFromHar(html: string, url: string): TestDOMGlobals { |
| 50 | + const dom = parseHTML(html) |
| 51 | + |
| 52 | + return { |
| 53 | + Document: dom.Document, |
| 54 | + DocumentFragment: dom.DocumentFragment, |
| 55 | + document: dom.document, |
| 56 | + Element: dom.Element, |
| 57 | + HTMLDivElement: dom.HTMLDivElement, |
| 58 | + HTMLElement: dom.HTMLElement, |
| 59 | + HTMLMetaElement: dom.HTMLMetaElement, |
| 60 | + HTMLTextAreaElement: dom.HTMLTextAreaElement, |
| 61 | + location: new URL(url) as any, |
| 62 | + Node: dom.Node, |
| 63 | + Text: dom.Text, |
| 64 | + window: { |
| 65 | + ...dom.window, |
| 66 | + location: new URL(url), |
| 67 | + } as any, |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +export function setupDOMFromHar(domGlobals: TestDOMGlobals): void { |
| 72 | + // Store original globals for cleanup |
| 73 | + originalGlobals = { |
| 74 | + Document: (globalThis as any).Document, |
| 75 | + DocumentFragment: (globalThis as any).DocumentFragment, |
| 76 | + document: (globalThis as any).document, |
| 77 | + Element: (globalThis as any).Element, |
| 78 | + HTMLDivElement: (globalThis as any).HTMLDivElement, |
| 79 | + HTMLElement: (globalThis as any).HTMLElement, |
| 80 | + HTMLMetaElement: (globalThis as any).HTMLMetaElement, |
| 81 | + HTMLTextAreaElement: (globalThis as any).HTMLTextAreaElement, |
| 82 | + location: (globalThis as any).location, |
| 83 | + Node: (globalThis as any).Node, |
| 84 | + Text: (globalThis as any).Text, |
| 85 | + window: (globalThis as any).window, |
| 86 | + } |
| 87 | + |
| 88 | + // Set new globals |
| 89 | + Object.assign(globalThis, domGlobals) |
| 90 | + currentDOMInstance = domGlobals |
| 91 | +} |
| 92 | + |
| 93 | +export function cleanupDOM(): void { |
| 94 | + if (currentDOMInstance) { |
| 95 | + // Reset globals to original values |
| 96 | + Object.assign(globalThis, originalGlobals) |
| 97 | + |
| 98 | + // Clear references |
| 99 | + currentDOMInstance = null |
| 100 | + originalGlobals = {} |
| 101 | + } |
| 102 | +} |
| 103 | + |
| 104 | +export async function setupHarDOM(key: keyof typeof PAGES): Promise<TestDOMGlobals> { |
| 105 | + const html = await loadHtmlFromHar(key) |
| 106 | + const url = PAGES[key] |
| 107 | + const domGlobals = createDOMFromHar(html, url) |
| 108 | + setupDOMFromHar(domGlobals) |
| 109 | + return domGlobals |
| 110 | +} |
0 commit comments