import { type RawTestLabel, ensureLiteral, isLiteral } from "@allurereport/reader-api"; import type { ReportNode } from "./model.js"; import { CORE, type XmlElement, elements } from "./xml.js"; const JAVA = "https://schemas.opentest4j.org/reporting/java/0.2.0"; const JUNIT = "https://schemas.junit.org/open-test-reporting"; export const nodeType = (node: ReportNode): string | undefined => node.info .filter((info) => info.namespace === CORE && info.name === "metadata") .flatMap((metadata) => elements(metadata, "type", JUNIT)) .at(-1) ?.text.trim(); const sourceDetails = (source: XmlElement): { identity?: string; labels: RawTestLabel[] } => { const labels: RawTestLabel[] = []; let identity: string | undefined; const attributes = source.attributes; if (source.namespace === CORE && isLiteral(source.name, ["directorySource", "fileSource"])) { identity = attributes.path; if (identity !== undefined) { labels.push({ name: "path", value: identity }); } } else if (source.namespace === JAVA) { if (isLiteral(source.name, ["classSource", "methodSource"])) { const className = attributes.className; identity = className; if (className !== undefined) { labels.push({ name: "testClass", value: className }); const dot = className.lastIndexOf("."); if (dot !== -1) { labels.push({ name: "package", value: className.slice(0, dot) }); } } if (source.name === "methodSource" && attributes.methodName !== undefined) { labels.push({ name: "testMethod", value: attributes.methodName }); identity = `${className === undefined ? "" : `${className}.`}${attributes.methodName}(${attributes.methodParameterTypes ?? ""})`; } } else if (source.name === "classpathResourceSource") { identity = attributes.resourceName; if (identity !== undefined) { labels.push({ name: "path", value: identity }); } } else if (source.name === "packageSource") { identity = attributes.name; if (identity !== undefined) { labels.push({ name: "package", value: identity }); } } } if (identity !== undefined) { const position = elements(source, "filePosition", source.namespace)[0]; for (const name of ["line", "column"]) { const value = position?.attributes[name]; if (value !== undefined) { labels.push({ name, value }); identity += `:${value}`; } } } return { identity, labels }; }; export const nodeMetadata = (path: ReportNode[]) => { const labels: RawTestLabel[] = []; let sourceIdentity: string | undefined; for (const node of path) { for (const info of node.info) { if (info.namespace !== CORE) { continue; } if (info.name === "metadata") { for (const tags of elements(info, "tags")) { labels.push(...elements(tags, "tag").map((tag) => ({ name: "tag", value: tag.text }))); } } else if (info.name === "sources") { for (const source of info.children) { const details = sourceDetails(source); labels.push(...details.labels); sourceIdentity = details.identity ?? sourceIdentity; } } } } const seen = new Set(); return { sourceIdentity, labels: labels.filter((label) => { const key = JSON.stringify([label.name, label.value]); if (seen.has(key)) { return false; } seen.add(key); return true; }), }; }; export const infrastructureLabels = (root: XmlElement): RawTestLabel[] => { const coreNames = new Map([ ["hostName", "host"], ["userName", "user"], ["operatingSystem", "os"], ["cpuCores", "cpuCores"], ]); return elements(root, "infrastructure").flatMap((infrastructure) => infrastructure.children.flatMap((field) => { const name = field.namespace === CORE ? coreNames.get(field.name) : field.namespace === JAVA ? ensureLiteral(field.name, ["javaVersion", "fileEncoding", "heapSize"]) : undefined; if (!name) { return []; } const value = field.name === "heapSize" ? field.attributes.max : field.text; return value === undefined ? [] : [{ name, value }]; }), ); }; export const resultDetails = (result: XmlElement | undefined) => { const throwable = result ? elements(result, "throwable", JAVA)[0] : undefined; const trace = throwable?.text || undefined; const reason = result ? elements(result, "reason")[0]?.text : undefined; return { message: reason ?? trace?.split(/\r?\n/, 1)[0] ?? throwable?.attributes.type, trace }; };