-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_tool.js
More file actions
177 lines (160 loc) · 8 KB
/
Copy pathtest_tool.js
File metadata and controls
177 lines (160 loc) · 8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import { validateHtmlContent, validateCssContent } from "./dist/w3c-validator.js";
import { auditSeoMetadata, validateSchemaMarkup, checkBrokenLinks } from "./dist/seo-auditor.js";
import { captureScreenshots } from "./dist/screenshot.js";
import * as fs from "fs/promises";
import * as path from "path";
const args = process.argv.slice(2);
const command = args[0];
const target = args[1];
async function run() {
if (!command || !target) {
console.log("Usage: node test_tool.js <html|css|seo|links|schema|report|screenshot> <file_path_or_url>");
process.exit(0);
}
const resolvedPath = path.resolve(target);
try {
switch (command) {
case "html": {
const content = await fs.readFile(resolvedPath, "utf-8");
const results = await validateHtmlContent(content);
console.log("HTML Validation Results:", JSON.stringify(results, null, 2));
break;
}
case "css": {
const content = await fs.readFile(resolvedPath, "utf-8");
const results = await validateCssContent(content);
console.log("CSS Validation Results:", JSON.stringify(results, null, 2));
break;
}
case "seo": {
const content = await fs.readFile(resolvedPath, "utf-8");
const results = auditSeoMetadata(content);
console.log("SEO Audit Results:", JSON.stringify(results, null, 2));
break;
}
case "schema": {
const content = await fs.readFile(resolvedPath, "utf-8");
const results = validateSchemaMarkup(content);
console.log("Schema Audit Results:", JSON.stringify(results, null, 2));
break;
}
case "links": {
const content = await fs.readFile(resolvedPath, "utf-8");
const results = await checkBrokenLinks(content);
console.log("Broken Links Results:", JSON.stringify(results, null, 2));
break;
}
case "report": {
const htmlContent = await fs.readFile(resolvedPath, "utf-8");
const htmlErrors = await validateHtmlContent(htmlContent);
const seoIssues = auditSeoMetadata(htmlContent);
const schemaIssues = validateSchemaMarkup(htmlContent);
const linkStatuses = await checkBrokenLinks(htmlContent);
const htmlErrCount = htmlErrors.filter(e => e.type === "error").length;
const htmlWarnCount = htmlErrors.filter(e => e.type !== "error").length;
const seoErrCount = seoIssues.filter(i => i.severity === "error").length;
const seoWarnCount = seoIssues.filter(i => i.severity !== "error").length;
const schemaErrCount = schemaIssues.filter(i => i.severity === "error").length;
const brokenLinkCount = linkStatuses.filter(l => !l.ok).length;
// Calculate Scores (PageSpeed style: Base 100)
let htmlScore = 100 - (htmlErrCount * 15) - (htmlWarnCount * 2);
let seoScore = 100 - (seoErrCount * 15) - (seoWarnCount * 4) - (schemaErrCount * 15);
let linkScore = linkStatuses.length > 0 ? (100 - (brokenLinkCount * 25)) : 100;
// Clamp to [0, 100]
htmlScore = Math.max(0, Math.min(100, htmlScore));
seoScore = Math.max(0, Math.min(100, seoScore));
linkScore = Math.max(0, Math.min(100, linkScore));
const getCircle = (score) => {
if (score >= 90) return "🟢";
if (score >= 50) return "🟠";
return "🔴";
};
const overallScore = Math.round((htmlScore + seoScore + linkScore) / 3);
const report = [
`# 📋 Web Validation & SEO Audit Report — ${getCircle(overallScore)} **${overallScore}**/100`,
`*Generated for: \`${path.basename(target)}\`*`,
``,
`## ⚡ Page Health Scores (PageSpeed Inspired)`,
``,
`| Score Card | Status | Score |`,
`| :--- | :---: | :---: |`,
`| **W3C HTML Validation** | ${getCircle(htmlScore)} ${htmlScore >= 90 ? "Excellent" : (htmlScore >= 50 ? "Needs Work" : "Poor")} | **${htmlScore}** / 100 |`,
`| **SEO & Accessibility** | ${getCircle(seoScore)} ${seoScore >= 90 ? "Optimized" : (seoScore >= 50 ? "Warnings" : "Poor")} | **${seoScore}** / 100 |`,
`| **Links Integrity** | ${linkStatuses.length > 0 ? `${getCircle(linkScore)} ${linkScore >= 90 ? "All Good" : "Broken Links"}` : "ℹ️ No Links"} | ${linkStatuses.length > 0 ? `**${linkScore}** / 100` : "N/A"} |`,
``,
`---`,
``,
`## 📊 Summary Overview`,
`| Audit Category | Status | Details |`,
`| :--- | :---: | :--- |`,
`| **W3C HTML Validation** | ${htmlErrCount > 0 ? "❌ Failed" : "✅ Passed"} | ${htmlErrCount} Errors, ${htmlWarnCount} Warnings |`,
`| **Technical SEO & Accessibility** | ${seoErrCount > 0 ? "❌ Critical Issues" : (seoWarnCount > 0 ? "⚠️ Warnings" : "✅ Optimized")} | ${seoErrCount} Errors, ${seoWarnCount} Warnings |`,
`| **JSON-LD Schema Verification** | ${schemaErrCount > 0 ? "❌ Invalid" : "✅ Valid"} | ${schemaErrCount} Syntax Errors |`,
`| **Broken Link Check** | ${brokenLinkCount > 0 ? "❌ Broken Links Found" : "✅ All Links OK"} | ${brokenLinkCount} Dead Links, ${linkStatuses.length} Total Links Checked |`,
``,
`---`,
``,
`## 🔴 HTML Syntax & Compliance Issues (${htmlErrors.length})`,
];
if (htmlErrors.length === 0) {
report.push("*No HTML syntax or markup validation errors found! Excellent job.*");
} else {
report.push("| Line | Col | Severity | Message | Extract |");
report.push("| :---: | :---: | :--- | :--- | :--- |");
for (const err of htmlErrors) {
const extract = err.extract ? `\`${err.extract.replace(/\n/g, " ").trim()}\`` : "N/A";
report.push(`| ${err.lastLine || "N/A"} | ${err.lastColumn || "N/A"} | ${err.type === "error" ? "🔴 Error" : "⚠️ Warning"} | ${err.message} | ${extract} |`);
}
}
report.push(
``,
`---`,
``,
`## 🔍 Technical SEO & Accessibility Issues (${seoIssues.length + schemaIssues.length})`
);
const allSeo = [...seoIssues, ...schemaIssues];
if (allSeo.length === 0) {
report.push("*No technical SEO or schema issues found! Page is search-engine ready.*");
} else {
report.push("| Category | Severity | Message | Element Snippet |");
report.push("| :--- | :--- | :--- | :--- |");
for (const issue of allSeo) {
const severityLabel = issue.severity === "error" ? "🔴 Error" : (issue.severity === "warning" ? "⚠️ Warning" : "ℹ️ Info");
const snippet = issue.element ? `\`${issue.element.trim()}\`` : "N/A";
report.push(`| ${issue.category} | ${severityLabel} | ${issue.message} | ${snippet} |`);
}
}
report.push(
``,
`---`,
``,
`## 🔗 Link Health Check (${linkStatuses.length} links checked)`
);
if (linkStatuses.length === 0) {
report.push("*No hyperlinks found in the document.*");
} else {
report.push("| Link URL | Status Code | Health | Details |");
report.push("| :--- | :---: | :---: | :--- |");
for (const link of linkStatuses) {
report.push(`| [${link.url}](${link.url}) | ${link.status} | ${link.ok ? "✅ Healthy" : "❌ Broken"} | ${link.message || "Accessible"} |`);
}
}
console.log(report.join("\n"));
break;
}
case "screenshot": {
const outputDir = path.resolve("./screenshots-test");
console.log(`Starting viewport screenshots generation for "${target}" to folder "${outputDir}"...`);
const results = await captureScreenshots(target, outputDir);
console.log("Screenshot Capture Complete! Results:");
console.log(JSON.stringify(results, null, 2));
break;
}
default:
console.log(`Unknown command: ${command}`);
}
} catch (error) {
console.error("Error executing validation:", error.message);
}
}
run();