Skip to content

Commit 93da5f1

Browse files
authored
Merge pull request #97 from immineal/test-aspect-ratio-gcd-16982541104038860322
🧪 Add tests for Aspect Ratio Calculator gcd function
2 parents 1df27e2 + ebe2e20 commit 93da5f1

3 files changed

Lines changed: 70 additions & 22 deletions

File tree

package-lock.json

Lines changed: 21 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"dependencies": {
99
"diff": "^7.0.0",
1010
"dompurify": "^3.0.0",
11-
"jsdom": "^29.0.2",
11+
"jsdom": "^29.1.0",
1212
"marked": "^15.0.0",
1313
"tinycolor2": "^1.6.0"
1414
},

tests/test-aspect-ratio.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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

Comments
 (0)