rob-revisited/ide/ui/js/tests-model.js

128 lines
5.2 KiB
JavaScript

// The Tests window's logic without the page (tests-model.test.mjs): statuses of tree nodes from the results, the
// side-by-side diff of a failed comparison, the test methods of a PHP / JavaScript file for the editor's gutter.
/** the worst status first: what a group of tests shows */
export const ORDER = ['error', 'failed', 'running', 'queued', 'skipped', 'passed'];
/** The status of a group from its children's (null: never run). */
export function worst(statuses) {
let best = null;
for (const s of statuses) {
if (!s) continue;
if (best === null || ORDER.indexOf(s) < ORDER.indexOf(best)) best = s;
}
return best;
}
/** passed / failed / skipped / running counts and total duration (ms) of results */
export function counts(results) {
const c = { passed: 0, failed: 0, skipped: 0, running: 0, total: 0, ms: 0 };
for (const r of results) {
c.total++;
c.ms += r.duration || 0;
if (r.status === 'failed' || r.status === 'error') c.failed++;
else if (r.status === 'skipped') c.skipped++;
else if (r.status === 'running') c.running++;
else c.passed++;
}
return c;
}
/**
* Lines of expected and actual side by side: [{left, right, kind}] with kind same | changed | removed | added (a longest
* common subsequence of the lines; above 1500 lines a plain pairing).
*/
export function sideBySide(expected, actual) {
const a = String(expected ?? '').split('\n');
const b = String(actual ?? '').split('\n');
const rows = [];
if (a.length * b.length > 1500 * 1500) {
for (let i = 0; i < Math.max(a.length, b.length); i++) rows.push({ left: a[i] ?? null, right: b[i] ?? null, kind: a[i] === b[i] ? 'same' : 'changed' });
return rows;
}
const n = a.length, m = b.length;
const lcs = Array.from({ length: n + 1 }, () => new Int32Array(m + 1));
for (let i = n - 1; i >= 0; i--) for (let j = m - 1; j >= 0; j--) lcs[i][j] = a[i] === b[j] ? lcs[i + 1][j + 1] + 1 : Math.max(lcs[i + 1][j], lcs[i][j + 1]);
let i = 0, j = 0;
let removed = [], added = [];
const flush = () => {
// a block of removed and added lines face each other (changed), the rest alone
const k = Math.max(removed.length, added.length);
for (let x = 0; x < k; x++) {
const l = removed[x], r = added[x];
rows.push({ left: l ?? null, right: r ?? null, kind: l !== undefined && r !== undefined ? 'changed' : l !== undefined ? 'removed' : 'added' });
}
removed = []; added = [];
};
while (i < n || j < m) {
if (i < n && j < m && a[i] === b[j]) { flush(); rows.push({ left: a[i], right: b[j], kind: 'same' }); i++; j++; }
else if (j < m && (i >= n || lcs[i][j + 1] >= lcs[i + 1][j])) { added.push(b[j]); j++; }
else { removed.push(a[i]); i++; }
}
flush();
return rows;
}
const NAMESPACE = /^\s*namespace\s+([\w\\]+)\s*;/;
const CLASS = /^\s*(?:(?:final|abstract|readonly)\s+)*class\s+(\w+)\s+extends\s+\\?[\w\\]+/;
const METHOD = /^\s*public\s+function\s+(\w+)\s*\(/;
const MARKER = /#\[(?:\\?PHPUnit\\Framework\\Attributes\\)?Test\b|@test\b/;
/**
* The test classes and methods of a PHP file's text with their line: [{line, id, kind: 'class'|'method', name}]. A method
* is a test when it starts with "test" or has #[Test] / @test in the lines above it (its attributes, its doc comment).
*/
export function phpTestMarks(text) {
const lines = String(text).split('\n');
const out = [];
let ns = '';
let cls = null;
let abstract = false;
lines.forEach((line, i) => {
const n = NAMESPACE.exec(line);
if (n) { ns = n[1]; return; }
const c = CLASS.exec(line);
if (c) {
abstract = /\babstract\b/.test(line);
cls = (ns ? ns + '\\' : '') + c[1];
if (!abstract) out.push({ line: i + 1, id: cls, kind: 'class', name: c[1] });
return;
}
const m = METHOD.exec(line);
if (m && cls && !abstract && !/\bstatic\b/.test(line)) {
let marked = m[1].startsWith('test');
for (let k = i - 1; !marked && k >= Math.max(0, i - 12); k--) {
if (MARKER.test(lines[k])) marked = true;
if (/[;}{]\s*$/.test(lines[k]) && !/^\s*(?:\*|\/\*\*|#\[)/.test(lines[k])) break;
}
if (marked) out.push({ line: i + 1, id: `${cls}::${m[1]}`, kind: 'method', name: m[1] });
}
});
return out;
}
/** "a > b > c" style names of the tests of a scope's files: {path: [{line, id, name}]} (from tests.discover) */
export function fileIndex(scopes) {
const out = new Map();
for (const scope of scopes || []) {
for (const suite of scope.suites || []) {
for (const f of suite.files || []) out.set(f.path, { scope, file: f });
}
}
return out;
}
/** a duration in ms as text: 12 ms, 1.4 s, 2 min 3 s */
export function duration(ms) {
if (ms === null || ms === undefined) return '';
if (ms < 1000) return `${Math.round(ms)} ms`;
if (ms < 60000) return `${(ms / 1000).toFixed(ms < 10000 ? 1 : 0)} s`;
return `${Math.floor(ms / 60000)} min ${Math.round((ms % 60000) / 1000)} s`;
}
/** project paths with a line in a text (a stack): `tests/X.php:12`, `file:///w/a.test.mjs:5:3` */
export function locations(text) {
const out = [];
for (const m of String(text || '').matchAll(/(?:file:\/\/)?((?:\/?[\w.@-]+\/)*[\w.@-]+\.(?:php|m?js|cjs|ts|tsx|jsx)):(\d+)/g)) out.push({ path: m[1], line: +m[2], text: m[0], index: m.index });
return out;
}