|
| 1 | +import { chromium } from "playwright"; |
| 2 | +import type { Test } from "./testcommon.ts"; |
| 3 | +import { glob } from "node:fs/promises"; |
| 4 | +import path from "node:path"; |
| 5 | +import { fileURLToPath } from "node:url"; |
| 6 | + |
| 7 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 8 | + |
| 9 | +async function discoverTests(): Promise<Test[]> { |
| 10 | + const testFiles = glob("**/*.ts", { |
| 11 | + cwd: path.join(__dirname, "tests"), |
| 12 | + }); |
| 13 | + |
| 14 | + const tests: Test[] = []; |
| 15 | + for await (const file of testFiles) { |
| 16 | + const fullPath = path.join(__dirname, "tests", file); |
| 17 | + const module = await import(fullPath); |
| 18 | + if (module.default) { |
| 19 | + if (Array.isArray(module.default)) { |
| 20 | + tests.push(...module.default); |
| 21 | + } else { |
| 22 | + tests.push(module.default); |
| 23 | + } |
| 24 | + } |
| 25 | + } |
| 26 | + return tests; |
| 27 | +} |
| 28 | + |
| 29 | +async function main() { |
| 30 | + const testFilter = process.argv[2]; |
| 31 | + |
| 32 | + if (!testFilter) { |
| 33 | + console.log("Usage: pnpm inspect <test-name-pattern>"); |
| 34 | + console.log("\nAvailable tests:"); |
| 35 | + const tests = await discoverTests(); |
| 36 | + for (const test of tests) { |
| 37 | + console.log(` - ${test.name}`); |
| 38 | + } |
| 39 | + process.exit(1); |
| 40 | + } |
| 41 | + |
| 42 | + console.log(`🔍 Inspecting tests matching: ${testFilter}\n`); |
| 43 | + |
| 44 | + // Discover and filter tests |
| 45 | + const allTests = await discoverTests(); |
| 46 | + const tests = allTests.filter((t) => t.name.includes(testFilter)); |
| 47 | + |
| 48 | + if (tests.length === 0) { |
| 49 | + console.log(`❌ No tests found matching "${testFilter}"`); |
| 50 | + console.log("\nAvailable tests:"); |
| 51 | + for (const test of allTests) { |
| 52 | + console.log(` - ${test.name}`); |
| 53 | + } |
| 54 | + process.exit(1); |
| 55 | + } |
| 56 | + |
| 57 | + console.log(`📋 Found ${tests.length} matching test(s):`); |
| 58 | + for (const test of tests) { |
| 59 | + console.log(` - ${test.name}`); |
| 60 | + } |
| 61 | + console.log(); |
| 62 | + |
| 63 | + // Start all matching tests |
| 64 | + for (const test of tests) { |
| 65 | + await test.start(); |
| 66 | + console.log( |
| 67 | + `🌐 Test "${test.name}" running at http://localhost:${test.port}/` |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + // Start the harness server |
| 72 | + const { startHarness, PORT: HARNESS_PORT } = await import( |
| 73 | + "./harness/scramjet/index.ts" |
| 74 | + ); |
| 75 | + await startHarness(); |
| 76 | + const harnessUrl = `http://localhost:${HARNESS_PORT}`; |
| 77 | + console.log(`\n📡 Harness running at ${harnessUrl}`); |
| 78 | + |
| 79 | + // Launch browser |
| 80 | + const browser = await chromium.launch({ |
| 81 | + headless: false, |
| 82 | + devtools: true, |
| 83 | + }); |
| 84 | + |
| 85 | + const page = await browser.newPage(); |
| 86 | + |
| 87 | + // Expose test reporting functions |
| 88 | + await page.exposeFunction("__testPass", (message?: string, details?: any) => { |
| 89 | + console.log(`\n✅ Test passed${message ? `: ${message}` : ""}`); |
| 90 | + if (details) console.log(" Details:", details); |
| 91 | + }); |
| 92 | + |
| 93 | + await page.exposeFunction("__testFail", (message?: string, details?: any) => { |
| 94 | + console.log(`\n❌ Test failed${message ? `: ${message}` : ""}`); |
| 95 | + if (details) console.log(" Details:", details); |
| 96 | + }); |
| 97 | + |
| 98 | + page.on("pageerror", (error) => { |
| 99 | + console.log(`\n💥 Page error: ${error.message}`); |
| 100 | + }); |
| 101 | + |
| 102 | + page.on("console", (msg) => { |
| 103 | + const type = msg.type(); |
| 104 | + const prefix = type === "error" ? "❌" : type === "warning" ? "⚠️" : "📝"; |
| 105 | + console.log(`${prefix} [console.${type}] ${msg.text()}`); |
| 106 | + }); |
| 107 | + |
| 108 | + // Navigate to harness |
| 109 | + await page.goto(harnessUrl); |
| 110 | + |
| 111 | + // Wait for harness to be ready |
| 112 | + try { |
| 113 | + await page.waitForFunction( |
| 114 | + () => typeof (window as any).__runwayNavigate === "function", |
| 115 | + { timeout: 30000 } |
| 116 | + ); |
| 117 | + console.log("✅ Harness ready\n"); |
| 118 | + } catch (e) { |
| 119 | + console.error("💥 Harness failed to initialize"); |
| 120 | + await browser.close(); |
| 121 | + process.exit(1); |
| 122 | + } |
| 123 | + |
| 124 | + // Navigate to first test |
| 125 | + const firstTest = tests[0]; |
| 126 | + const testUrl = `http://localhost:${firstTest.port}/`; |
| 127 | + console.log(`🚀 Navigating to test: ${testUrl}`); |
| 128 | + |
| 129 | + await page.evaluate((url) => { |
| 130 | + (window as any).__runwayNavigate(url); |
| 131 | + }, testUrl); |
| 132 | + |
| 133 | + console.log("\n" + "─".repeat(50)); |
| 134 | + console.log("🔍 Browser open for manual inspection"); |
| 135 | + console.log(" Press Ctrl+C to exit\n"); |
| 136 | + |
| 137 | + if (tests.length > 1) { |
| 138 | + console.log("Other test URLs (navigate manually via harness):"); |
| 139 | + for (const test of tests.slice(1)) { |
| 140 | + console.log(` - http://localhost:${test.port}/`); |
| 141 | + } |
| 142 | + console.log(); |
| 143 | + } |
| 144 | + |
| 145 | + // Keep the process running |
| 146 | + await new Promise(() => {}); |
| 147 | +} |
| 148 | + |
| 149 | +main().catch((err) => { |
| 150 | + console.error("Fatal error:", err); |
| 151 | + process.exit(1); |
| 152 | +}); |
0 commit comments