|
| 1 | +const assert = require('assert'); |
| 2 | +const fs = require('fs'); |
| 3 | +const path = require('path'); |
| 4 | +const { JSDOM } = require('jsdom'); |
| 5 | + |
| 6 | +const htmlPath = path.join(__dirname, '..', 'tools', 'aspect-ratio', 'index.html'); |
| 7 | +let htmlContent = fs.readFileSync(htmlPath, 'utf8'); |
| 8 | + |
| 9 | +// Strip ALL scripts to prevent execution |
| 10 | +htmlContent = htmlContent.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, ''); |
| 11 | + |
| 12 | +const { window } = new JSDOM(htmlContent, { |
| 13 | + url: "http://localhost", |
| 14 | + runScripts: "outside-only" |
| 15 | +}); |
| 16 | + |
| 17 | +let originalHtml = fs.readFileSync(htmlPath, 'utf8'); |
| 18 | +const scriptMatches = Array.from(originalHtml.matchAll(/<script>([\s\S]*?)<\/script>/g)); |
| 19 | +const mainScript = scriptMatches[scriptMatches.length - 1][1]; |
| 20 | + |
| 21 | +window.eval(mainScript); |
| 22 | + |
| 23 | +function testGcd(a, b, expected, description) { |
| 24 | + const result = window.eval(`gcd(${a}, ${b})`); |
| 25 | + try { |
| 26 | + assert.strictEqual(result, expected); |
| 27 | + console.log(`✅ Passed: ${description} (gcd(${a}, ${b}) -> ${expected})`); |
| 28 | + } catch (e) { |
| 29 | + console.error(`❌ Failed: ${description}`); |
| 30 | + console.error(` Input: ${a}, ${b}`); |
| 31 | + console.error(` Expected: ${expected}`); |
| 32 | + console.error(` Actual: ${result}`); |
| 33 | + process.exit(1); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +console.log("Running Aspect Ratio gcd tests..."); |
| 38 | + |
| 39 | +testGcd(48, 18, 6, "Normal numbers"); |
| 40 | +testGcd(1920, 1080, 120, "Common resolution"); |
| 41 | +testGcd(16, 9, 1, "Coprime numbers"); |
| 42 | +testGcd(7, 0, 7, "Zero as second argument"); |
| 43 | +testGcd(0, 7, 7, "Zero as first argument"); |
| 44 | +testGcd(0, 0, 0, "Both arguments zero"); |
| 45 | +testGcd(100, 100, 100, "Same numbers"); |
| 46 | + |
| 47 | +console.log("All Aspect Ratio tests passed!"); |
| 48 | +process.exit(0); |
0 commit comments