const fs = require('fs'); const path = require('path'); function summarizeResults(results) { const lines = []; if (results.assertions && Array.isArray(results.assertions)) { lines.push(`- Assertions:`); for (const a of results.assertions) { lines.push(` - ${a.name}: ${a.ok ? 'PASS' : 'FAIL'} - ${a.detail || ''}`); } } if (results.console && Array.isArray(results.console) && results.console.length) { const errors = results.console.filter(c => c.type === 'error' || c.type === 'warning'); if (errors.length) { lines.push(`- Console issues:`); for (const e of errors) { lines.push(` - [${e.type}] ${e.text}`); } } } if (results.error) lines.push(`- Error: ${results.error}`); return lines.join('\n'); } function appendLog(runName, resultJsonPath, results, artifactUrl) { try { const logPath = path.resolve(__dirname, 'LOG.md'); const now = new Date().toISOString(); const summary = summarizeResults(results); const screenshot = results.screenshot ? path.relative(path.dirname(logPath), results.screenshot) : ''; const status = (results.assertions && Array.isArray(results.assertions) && results.assertions.every(a => a.ok)) ? 'PASS' : (results.error ? 'ERROR' : 'UNKNOWN'); const entry = [ `## ${now} — ${runName}`, `- Status: ${status}`, `- Result JSON: ${resultJsonPath}`, artifactUrl ? `- Artifact: ${artifactUrl}` : '', screenshot ? `- Screenshot: ${screenshot}` : '', summary ? summary : '- No assertions or console issues recorded', '', ].filter(Boolean).join('\n'); fs.appendFileSync(logPath, entry + '\n'); return true; } catch (err) { try { console.warn('Failed to append log', err); } catch(e){} return false; } } // publishArtifact: runs publish_artifact.js and returns the printed path/URL or null function publishArtifact(resultJsonPath, label) { try { const pub = require('child_process').execFileSync; const script = path.resolve(__dirname, 'publish_artifact.js'); const out = pub(process.execPath, [script, resultJsonPath, label], { encoding: 'utf8' }); return out.trim(); } catch (err) { console.warn('publishArtifact failed', err && err.message ? err.message : err); return null; } } module.exports = { appendLog, publishArtifact };