1import Foundation
2
3/// The run's test results: failures first, open, with their messages, then every
4/// suite folded, listing its cases. Readable with scripts off; it has none.
5public struct TestsPage: Sendable {
6 /// Beside the index, which links it from its headline.
7 public static let sitePath = "tests.html"
8
9 private let run: ReportHeader
10 private let title: String
11 private let results: TestResults
12
13 /// - Parameters:
14 /// - title: the site's name, shown as given in the page's title.
15 /// - run: shared commit, subject and optional History destination.
1614 public init(title: String, results: TestResults, run: ReportHeader = ReportHeader()) {
1714 self.run = run
1814 self.title = title
1914 self.results = results
2014 }
21
2214 public var html: String {
2314 """
2414 <!doctype html>
2514 <html lang="en">
2614 <head>
2714 <meta charset="utf-8">
2814 <meta name="viewport" content="width=device-width, initial-scale=1">
2914 <title>Tests · \(escapingHTML(title))</title>
3014 <link rel="stylesheet" href="\(SiteStylesheet.fileName)">
3114 </head>
3214 <body class="tests-page">
3314 <header class="page-header">
3414 \(SiteHeader(title: title, currentTab: .tests, hasTestsPage: true, rootPath: "", run: run).html)
3514 \(headlineMarkup)
3614 </header>
3714 <main>
3814 \(failuresMarkup)\(suitesMarkup)
3914 </main>
4014 </body>
4114 </html>
4214
4314 """
4414 }
45
4614 private var headlineMarkup: String {
4714 let counts = results.counts
4814 return #"<p class="totals">"#
4914 + "\(counts.totalText): \(groupingDigits(counts.passed)) passed, "
5014 + "\(groupingDigits(counts.failed)) failed, \(groupingDigits(counts.skipped)) skipped</p>"
5114 }
52
53 /// Empty when nothing failed.
5414 private var failuresMarkup: String {
5518 let failures = results.cases.enumerated().compactMap { index, testCase -> String? in
5618 guard case .failed(let message, let details) = testCase.outcome else { return nil }
575 let detailsMarkup = details.isEmpty ? "" : "<pre>\(escapingHTML(details))</pre>"
585 return #"<article class="failure" id="\#(Self.failureID(at: index))"><h3>"#
595 + "\(escapingHTML(testCase.suite)) · \(escapingHTML(testCase.name))</h3>"
605 + "<p>\(escapingHTML(message))</p>\(detailsMarkup)</article>"
6118 }
6214 guard !failures.isEmpty else { return "" }
635 return #"<section class="failures"><h2>Failures</h2>"# + failures.joined() + "</section>\n"
6414 }
65
66 /// One folded block per suite, in the order suites first appear; a suite with a
67 /// failure starts open.
6814 private var suitesMarkup: String {
6914 var order: [String] = []
7014 var casesBySuite: [String: [(index: Int, testCase: TestResults.TestCase)]] = [:]
7118 for (index, testCase) in results.cases.enumerated() {
7218 if casesBySuite[testCase.suite] == nil {
7313 order.append(testCase.suite)
7413 }
7518 casesBySuite[testCase.suite, default: []].append((index, testCase))
7618 }
7714 return order.map { suite in
7813 let cases = casesBySuite[suite] ?? []
7918 let hasFailure = cases.contains {
8018 if case .failed = $0.testCase.outcome { true } else { false }
8118 }
8213 return #"<details class="suite""# + (hasFailure ? " open" : "")
8313 + "><summary>\(escapingHTML(suite)) <span class=\"count\">\(cases.count)</span>"
8418 + "</summary><ul>" + cases.map(Self.caseMarkup).joined() + "</ul></details>"
8513 }.joined(separator: "\n")
8614 }
87
88 /// Names a failure by its position among all cases, so the list entry can link to it.
8910 private static func failureID(at index: Int) -> String {
9010 "failure-\(index)"
9110 }
92
9318 private static func caseMarkup(index: Int, testCase: TestResults.TestCase) -> String {
9418 let name = escapingHTML(testCase.name)
9518 switch testCase.outcome {
9618 case .passed:
9711 return #"<li class="passed">\#(name)\#(timeMarkup(testCase.seconds))</li>"#
9818 case .failed:
995 let link = ##"<a href="#\##(failureID(at: index))">\##(name)</a>"##
1005 return #"<li class="failed">\#(link)\#(timeMarkup(testCase.seconds))</li>"#
10118 case .skipped(let message):
1022 let reason =
1032 message.isEmpty ? "" : #" <span class="reason">\#(escapingHTML(message))</span>"#
1042 return #"<li class="skipped">\#(name)\#(reason)</li>"#
10518 }
10618 }
107
108 /// Under a second in whole milliseconds, longer in seconds with two decimals.
10916 private static func timeMarkup(_ seconds: Double?) -> String {
11016 guard let seconds else { return "" }
11111 let text =
11211 seconds < 1
11311 ? "\(Int((seconds * millisecondsPerSecond).rounded())) ms"
11411 : String(format: "%.2f s", seconds)
11511 return #" <span class="time">\#(text)</span>"#
11616 }
117
118 private static let millisecondsPerSecond = 1000.0
119}