1import Foundation
2
3/// One published run: the folder it sits in (the commit's short hash) and its numbers.
4public struct HistoryRun: Sendable, Equatable {
5 public let folder: String
6 public let totals: RunTotals
7
833 public init(folder: String, totals: RunTotals) {
933 self.folder = folder
1033 self.totals = totals
1133 }
12}
13
14public enum HistoryPageError: Error, Equatable {
15 case noRuns
16}
17
18/// Coverage history above the published runs (DESIGN.md D7), newest first.
19public struct HistoryPage: Sendable {
20 /// The stylesheet's name; it sits beside the page.
21 public static let stylesheetPath = SiteStylesheet.fileName
22
23 /// - Parameters:
24 /// - title: the project's name, shown as given.
25 /// - runs: in any order; each run's report is `<folder>/index.html` beside the page.
26 /// - Throws: `HistoryPageError.noRuns` when `runs` is empty, since there is nothing
27 /// to display.
2816 public init(title: String, runs: [HistoryRun]) throws {
2916 guard !runs.isEmpty else { throw HistoryPageError.noRuns }
3015 self.title = title
3120 self.newestFirst = runs.sorted {
3220 ($0.totals.date, $1.folder) > ($1.totals.date, $0.folder)
3320 }
3415 }
35
36 private let title: String
37 private let newestFirst: [HistoryRun]
38
39 /// The newest run, including unavailable runs, for the site root redirect.
400 public var newestRunFolder: String { newestFirst[0].folder }
41
4215 public var html: String {
4315 let latest = newestFirst[0]
4415 let header = SiteHeader(
4515 title: title, currentTab: .history, hasTestsPage: latest.totals.hasTestsPage == true,
4615 rootPath: String(Self.reportLink(latest).dropLast(IndexPage.sitePath.count)),
4715 run: ReportHeader(historyPath: "../index.html"))
4828 let branches = newestFirst.contains { $0.totals.branches != nil }
4928 let conditions = newestFirst.contains { $0.totals.conditions != nil }
5028 let trend = CoverageTrend(runs: newestFirst.map(\.totals))
5115 return
5215 """
5315 <!doctype html>
5415 <html lang="en">
5515 <head>
5615 <meta charset="utf-8">
5715 <meta name="viewport" content="width=device-width, initial-scale=1">
5815 <title>History · \(escapingHTML(title))</title>
5915 <link rel="stylesheet" href="\(Self.stylesheetPath)">
6015 </head>
6115 <body class="history-page">
6215 <header class="page-header">
6315 \(header.html)
6415 <p class="totals">\(groupingDigits(newestFirst.count)) \
6515 \(newestFirst.count == 1 ? "commit" : "commits"), newest first</p>
6615 </header>
6715 <main>
6815 <section class="history-chart" aria-label="Coverage trend">
6915 \(trend.chart)
7015 <p class="trend-legend">Lines\(branches ? " · Branches" : "")\(conditions ? " · Conditions" : "") · Red marks: no data\(trend.scaleText.map { " · Scale \($0)" } ?? "")</p>
7115 </section>
7215 <table class="history">
7315 <thead><tr><th>Commit</th><th>Subject</th><th>Lines</th>\(branches ? "<th>Branches</th>" : "")\(conditions ? "<th>Conditions</th>" : "")<th class="date">Date</th></tr></thead>
7415 <tbody>
7515 \(rows(branches: branches, conditions: conditions))
7615 </tbody>
7715 </table>
7815 </main>
7915 </body>
8015 </html>
8115
8215 """
8315 }
84
85 /// Relative to the page, with the folder's name encoded so any name stays one link.
8643 private static func reportLink(_ run: HistoryRun) -> String {
8743 let folder = run.folder.addingPercentEncoding(withAllowedCharacters: folderCharacters)
8843 return "\(folder ?? run.folder)/\(IndexPage.sitePath)"
8943 }
90
91 /// Unreserved characters only: `#`, `?`, `%` and spaces in a name must not end the path.
92 private static let folderCharacters = CharacterSet(
93 charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-._~")
94
9515 private func rows(branches: Bool, conditions: Bool) -> String {
9628 newestFirst.enumerated().map { index, run in
9728 let previous = newestFirst.dropFirst(index + 1).first { $0.totals.unavailable == nil }
9828 return rowMarkup(
9928 run, previous: previous?.totals, branches: branches, conditions: conditions)
10028 }.joined(separator: "\n")
10115 }
102
103 private func rowMarkup(
104 _ run: HistoryRun, previous: RunTotals?, branches: Bool, conditions: Bool
10528 ) -> String {
10628 let totals = run.totals
10728 let subject = escapingHTML(totals.subject ?? "")
10828 var cells =
10928 #"<td class="commit"><a href="\#(escapingHTML(Self.reportLink(run)))">"#
11028 + "\(escapingHTML(run.folder))</a></td>"
11128 + #"<td class="subject">\#(subject)</td>"#
11228 if let reason = totals.unavailable {
1133 cells +=
1143 #"<td class="lines"><span class="unavailable">\#(escapingHTML(reason))</span></td>"#
11525 } else {
11625 cells +=
11725 #"<td class="lines">\#(Self.metricMarkup(totals.lines, before: previous?.lines))</td>"#
11825 }
11928 if branches {
1201 cells +=
1211 #"<td class="branches">\#(Self.metricMarkup(totals.unavailable == nil ? totals.branches : nil, before: previous?.branches))</td>"#
1221 }
12328 if conditions {
1241 cells +=
1251 #"<td class="conditions">\#(Self.metricMarkup(totals.unavailable == nil ? totals.conditions : nil, before: previous?.conditions))</td>"#
1261 }
12728 return "<tr>\(cells)" + #"<td class="date">\#(Self.dateMarkup(totals.date))</td></tr>"#
12828 }
129
130 private static func metricMarkup(_ count: RunTotals.Lines?, before: RunTotals.Lines?) -> String
13127 {
13227 guard let count else { return "—" }
13327 let summary = LineSummary(count: count.total, covered: count.covered)
13427 var text =
13527 #"<span class="percent \#(summary.tier.rawValue)">\#(summary.percentText)</span>"#
13627 if let before {
13711 let previous = LineSummary(count: before.total, covered: before.covered)
13811 let change = CoverageChange.points(summary.percent - previous.percent)
13911 text += #" <span class="change \#(change.styleName)">\#(change.text)</span>"#
14011 }
14127 return text
14227 }
143
144 /// The machine-readable date with a short text beside it, `2026-12-01 08:30 UTC`.
14528 private static func dateMarkup(_ date: Date) -> String {
14628 let iso = date.formatted(.iso8601)
14728 let shown = iso.prefix(shownDateLength).replacingOccurrences(of: "T", with: " ")
14828 return #"<time datetime="\#(iso)">\#(shown) UTC</time>"#
14928 }
150
151 /// `2026-12-01T08:30`: up to the minute.
152 private static let shownDateLength = 16
153}