|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import fs from "node:fs"; |
| 3 | +import path from "node:path"; |
| 4 | +import { execFileSync } from "node:child_process"; |
| 5 | +import { fileURLToPath, pathToFileURL } from "node:url"; |
| 6 | + |
| 7 | +const __filename = fileURLToPath(import.meta.url); |
| 8 | +const __dirname = path.dirname(__filename); |
| 9 | +const repoRoot = path.resolve(__dirname, "..", ".."); |
| 10 | +const toolsRoot = path.join(repoRoot, "tools"); |
| 11 | +const resultsPath = path.join(repoRoot, "tmp", "v2-error-logging-results.json"); |
| 12 | + |
| 13 | +const TOOLS = [ |
| 14 | + "asset-browser-v2", |
| 15 | + "palette-manager-v2", |
| 16 | + "svg-asset-studio-v2", |
| 17 | + "tilemap-studio-v2", |
| 18 | + "vector-map-editor-v2" |
| 19 | +]; |
| 20 | + |
| 21 | +const VALID_TYPES = new Set(["EMPTY", "INVALID", "RUNTIME"]); |
| 22 | + |
| 23 | +function readText(filePath) { |
| 24 | + return fs.readFileSync(filePath, "utf8"); |
| 25 | +} |
| 26 | + |
| 27 | +function checkJsSyntax(jsPath) { |
| 28 | + try { |
| 29 | + execFileSync(process.execPath, ["--check", jsPath], { |
| 30 | + cwd: repoRoot, |
| 31 | + stdio: ["ignore", "pipe", "pipe"] |
| 32 | + }); |
| 33 | + return { syntaxValid: true, syntaxError: "" }; |
| 34 | + } catch (error) { |
| 35 | + return { |
| 36 | + syntaxValid: false, |
| 37 | + syntaxError: (error?.stderr || error?.stdout || error?.message || "").toString().trim() |
| 38 | + }; |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +function validateLogEntry(logEntry, expectedTool, expectedType) { |
| 43 | + const issues = []; |
| 44 | + if (!logEntry || typeof logEntry !== "object" || Array.isArray(logEntry)) { |
| 45 | + issues.push("Log entry must be an object."); |
| 46 | + return issues; |
| 47 | + } |
| 48 | + if (logEntry.tool !== expectedTool) { |
| 49 | + issues.push(`Expected tool "${expectedTool}", got "${typeof logEntry.tool === "string" ? logEntry.tool : "missing"}".`); |
| 50 | + } |
| 51 | + if (logEntry.type !== expectedType) { |
| 52 | + issues.push(`Expected type "${expectedType}", got "${typeof logEntry.type === "string" ? logEntry.type : "missing"}".`); |
| 53 | + } |
| 54 | + if (!VALID_TYPES.has(logEntry.type)) { |
| 55 | + issues.push(`Unexpected type value "${typeof logEntry.type === "string" ? logEntry.type : "missing"}".`); |
| 56 | + } |
| 57 | + if (typeof logEntry.message !== "string" || !logEntry.message.trim()) { |
| 58 | + issues.push("Log message must be a non-empty string."); |
| 59 | + } |
| 60 | + if (!Object.prototype.hasOwnProperty.call(logEntry, "details")) { |
| 61 | + issues.push("Log details field is missing."); |
| 62 | + } else if (!logEntry.details || typeof logEntry.details !== "object" || Array.isArray(logEntry.details)) { |
| 63 | + issues.push("Log details must be an object."); |
| 64 | + } |
| 65 | + return issues; |
| 66 | +} |
| 67 | + |
| 68 | +function simulateStructuredLogs(toolId) { |
| 69 | + const capturedLogs = []; |
| 70 | + const writeLog = (logEntry) => capturedLogs.push(logEntry); |
| 71 | + const emitStructuredLog = (type, message, details) => { |
| 72 | + writeLog({ |
| 73 | + tool: toolId, |
| 74 | + type, |
| 75 | + message, |
| 76 | + details: details && typeof details === "object" ? details : {} |
| 77 | + }); |
| 78 | + }; |
| 79 | + |
| 80 | + emitStructuredLog("EMPTY", "No hostContextId was provided for this tool.", { hostContextId: "" }); |
| 81 | + emitStructuredLog("INVALID", "Session payload is invalid for this tool.", { hostContextId: `${toolId}-invalid` }); |
| 82 | + try { |
| 83 | + throw new Error("runtime-test-injection"); |
| 84 | + } catch (error) { |
| 85 | + emitStructuredLog("RUNTIME", `Unable to read session context: ${error instanceof Error ? error.message : "unknown error"}`, { hostContextId: `${toolId}-runtime` }); |
| 86 | + } |
| 87 | + |
| 88 | + return capturedLogs; |
| 89 | +} |
| 90 | + |
| 91 | +function validateTool(toolId) { |
| 92 | + const jsPath = path.join(toolsRoot, toolId, "index.js"); |
| 93 | + const jsExists = fs.existsSync(jsPath); |
| 94 | + const jsText = jsExists ? readText(jsPath) : ""; |
| 95 | + const { syntaxValid, syntaxError } = checkJsSyntax(jsPath); |
| 96 | + const failures = []; |
| 97 | + |
| 98 | + const hasStructuredLoggerMethod = jsText.includes("logStructuredError(type, message, details)"); |
| 99 | + const hasObjectLogShape = jsText.includes("console.error({") && |
| 100 | + jsText.includes(`tool: "${toolId}"`) && |
| 101 | + jsText.includes("type,") && |
| 102 | + jsText.includes("message,") && |
| 103 | + jsText.includes("details:"); |
| 104 | + const hasEmptyTrigger = jsText.includes('this.logStructuredError("EMPTY",'); |
| 105 | + const hasInvalidTrigger = jsText.includes('this.logStructuredError("INVALID",'); |
| 106 | + const hasRuntimeTrigger = jsText.includes('this.logStructuredError("RUNTIME",'); |
| 107 | + |
| 108 | + if (!jsExists) failures.push("Missing tool index.js."); |
| 109 | + if (!syntaxValid) failures.push("Tool index.js failed syntax check."); |
| 110 | + if (!hasStructuredLoggerMethod) failures.push("Missing logStructuredError(type, message, details) method."); |
| 111 | + if (!hasObjectLogShape) failures.push("Structured log object shape is missing or inconsistent."); |
| 112 | + if (!hasEmptyTrigger) failures.push("Missing EMPTY structured log trigger."); |
| 113 | + if (!hasInvalidTrigger) failures.push("Missing INVALID structured log trigger."); |
| 114 | + if (!hasRuntimeTrigger) failures.push("Missing RUNTIME structured log trigger."); |
| 115 | + |
| 116 | + const simulatedLogs = simulateStructuredLogs(toolId); |
| 117 | + if (simulatedLogs.length !== 3) { |
| 118 | + failures.push(`Expected 3 simulated logs, got ${simulatedLogs.length}.`); |
| 119 | + } |
| 120 | + |
| 121 | + const emptyIssues = validateLogEntry(simulatedLogs[0], toolId, "EMPTY"); |
| 122 | + const invalidIssues = validateLogEntry(simulatedLogs[1], toolId, "INVALID"); |
| 123 | + const runtimeIssues = validateLogEntry(simulatedLogs[2], toolId, "RUNTIME"); |
| 124 | + emptyIssues.forEach((entry) => failures.push(`EMPTY log: ${entry}`)); |
| 125 | + invalidIssues.forEach((entry) => failures.push(`INVALID log: ${entry}`)); |
| 126 | + runtimeIssues.forEach((entry) => failures.push(`RUNTIME log: ${entry}`)); |
| 127 | + |
| 128 | + return { |
| 129 | + tool: toolId, |
| 130 | + jsPath: path.relative(repoRoot, jsPath).replace(/\\/g, "/"), |
| 131 | + jsExists, |
| 132 | + syntaxValid, |
| 133 | + syntaxError, |
| 134 | + hasStructuredLoggerMethod, |
| 135 | + hasObjectLogShape, |
| 136 | + hasEmptyTrigger, |
| 137 | + hasInvalidTrigger, |
| 138 | + hasRuntimeTrigger, |
| 139 | + simulatedLogs, |
| 140 | + failures |
| 141 | + }; |
| 142 | +} |
| 143 | + |
| 144 | +export function run() { |
| 145 | + const rows = TOOLS.map(validateTool); |
| 146 | + const failures = rows.flatMap((row) => row.failures.map((entry) => `${row.tool}: ${entry}`)); |
| 147 | + |
| 148 | + fs.mkdirSync(path.dirname(resultsPath), { recursive: true }); |
| 149 | + fs.writeFileSync(resultsPath, `${JSON.stringify({ |
| 150 | + generatedAt: new Date().toISOString(), |
| 151 | + toolCount: rows.length, |
| 152 | + failures, |
| 153 | + rows |
| 154 | + }, null, 2)}\n`, "utf8"); |
| 155 | + |
| 156 | + console.log(`v2 error logging results: ${resultsPath}`); |
| 157 | + assert.equal(failures.length, 0, `V2 error logging failures: ${failures.join(" | ")}`); |
| 158 | + return { toolCount: rows.length, failures, rows }; |
| 159 | +} |
| 160 | + |
| 161 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 162 | + try { |
| 163 | + const summary = run(); |
| 164 | + console.log(JSON.stringify(summary, null, 2)); |
| 165 | + } catch (error) { |
| 166 | + console.error(error); |
| 167 | + process.exitCode = 1; |
| 168 | + } |
| 169 | +} |
0 commit comments