|
| 1 | +import fs from 'node:fs' |
| 2 | +import path from 'node:path' |
| 3 | +import { afterAll, beforeAll, describe, expect, it } from 'vitest' |
| 4 | + |
| 5 | +import { |
| 6 | + cleanupJBrowse, |
| 7 | + createJBrowsePage, |
| 8 | + launchBrowser, |
| 9 | + setupJBrowse, |
| 10 | + startJBrowseServer, |
| 11 | + stopServer, |
| 12 | + waitForJBrowseLoad, |
| 13 | + waitForTrackLoad, |
| 14 | +} from './setup' |
| 15 | + |
| 16 | +import type { ChildProcess } from 'node:child_process' |
| 17 | +import type { Browser, Page } from 'puppeteer' |
| 18 | + |
| 19 | +const JBROWSE_VERSION = process.env.TEST_JBROWSE_VERSION || 'nightly' |
| 20 | +const SCREENSHOT_DIR = path.join('test-screenshots', JBROWSE_VERSION) |
| 21 | + |
| 22 | +fs.mkdirSync(SCREENSHOT_DIR, { recursive: true }) |
| 23 | + |
| 24 | +function screenshot(name: string) { |
| 25 | + return path.join(SCREENSHOT_DIR, `${name}.png`) |
| 26 | +} |
| 27 | + |
| 28 | +describe('MafViewer Plugin E2E', () => { |
| 29 | + let server: ChildProcess | undefined |
| 30 | + let browser: Browser | undefined |
| 31 | + let page: Page | undefined |
| 32 | + const pluginErrors: string[] = [] |
| 33 | + |
| 34 | + beforeAll(async () => { |
| 35 | + setupJBrowse() |
| 36 | + server = await startJBrowseServer() |
| 37 | + browser = await launchBrowser() |
| 38 | + page = await createJBrowsePage(browser) |
| 39 | + |
| 40 | + page.on('console', msg => { |
| 41 | + const text = msg.text() |
| 42 | + if ( |
| 43 | + msg.type() === 'error' && |
| 44 | + (text.includes('plugin') || text.includes('Plugin')) |
| 45 | + ) { |
| 46 | + pluginErrors.push(text) |
| 47 | + } |
| 48 | + }) |
| 49 | + |
| 50 | + await waitForJBrowseLoad(page) |
| 51 | + await waitForTrackLoad(page) |
| 52 | + }, 180_000) |
| 53 | + |
| 54 | + afterAll(async () => { |
| 55 | + if (browser) { |
| 56 | + await browser.close() |
| 57 | + } |
| 58 | + if (server) { |
| 59 | + await stopServer(server) |
| 60 | + } |
| 61 | + await cleanupJBrowse() |
| 62 | + }) |
| 63 | + |
| 64 | + it('should load JBrowse without errors', async () => { |
| 65 | + expect(page).toBeDefined() |
| 66 | + const root = await page!.$('#root') |
| 67 | + expect(root).not.toBeNull() |
| 68 | + await page!.screenshot({ path: screenshot('jbrowse-loaded') }) |
| 69 | + }, 30_000) |
| 70 | + |
| 71 | + it('should load the MafViewer plugin without errors', async () => { |
| 72 | + expect(page).toBeDefined() |
| 73 | + |
| 74 | + if (pluginErrors.length > 0) { |
| 75 | + console.log('Plugin errors:', pluginErrors) |
| 76 | + } |
| 77 | + expect(pluginErrors).toHaveLength(0) |
| 78 | + |
| 79 | + const pluginLoaded = await page!.evaluate(() => { |
| 80 | + // @ts-expect-error JBrowse global |
| 81 | + const session = window.__jbrowse_session |
| 82 | + if (session) { |
| 83 | + const plugins = session.jbrowse?.plugins || [] |
| 84 | + return plugins.some( |
| 85 | + (p: { name: string }) => |
| 86 | + p.name === 'MafViewer' || |
| 87 | + p.name === 'MafViewerPlugin' || |
| 88 | + p.name === 'jbrowse-plugin-mafviewer', |
| 89 | + ) |
| 90 | + } |
| 91 | + const scripts = Array.from(document.querySelectorAll('script')) |
| 92 | + return scripts.some(s => s.src?.includes('mafviewer')) |
| 93 | + }) |
| 94 | + |
| 95 | + console.log(`Plugin loaded: ${pluginLoaded}`) |
| 96 | + expect(pluginLoaded).toBe(true) |
| 97 | + }, 30_000) |
| 98 | + |
| 99 | + it('should render MAF track without crashing', async () => { |
| 100 | + expect(page).toBeDefined() |
| 101 | + await new Promise(r => setTimeout(r, 5000)) |
| 102 | + await page!.screenshot({ path: screenshot('maf-track-rendered') }) |
| 103 | + |
| 104 | + const canvasCount = await page!.$$eval('canvas', els => els.length) |
| 105 | + console.log(`Canvas elements found: ${canvasCount}`) |
| 106 | + expect(canvasCount).toBeGreaterThan(0) |
| 107 | + }, 60_000) |
| 108 | + |
| 109 | + it('should match image snapshot', async () => { |
| 110 | + expect(page).toBeDefined() |
| 111 | + await new Promise(r => setTimeout(r, 2000)) |
| 112 | + |
| 113 | + // Hide the header bar which contains a timestamp |
| 114 | + await page!.evaluate(() => { |
| 115 | + const header = document.querySelector('header') |
| 116 | + if (header) { |
| 117 | + ;(header as HTMLElement).style.display = 'none' |
| 118 | + } |
| 119 | + }) |
| 120 | + |
| 121 | + const image = await page!.screenshot() |
| 122 | + expect(image).toMatchImageSnapshot() |
| 123 | + |
| 124 | + // Restore header |
| 125 | + await page!.evaluate(() => { |
| 126 | + const header = document.querySelector('header') |
| 127 | + if (header) { |
| 128 | + ;(header as HTMLElement).style.display = '' |
| 129 | + } |
| 130 | + }) |
| 131 | + }, 30_000) |
| 132 | +}) |
0 commit comments