| 1 | import XCTest |
| 2 | @testable import LChatCore |
| 3 | |
| 4 | // 🤖 The perf ledger's pure halves: JSONL parsing (lenient — a torn line from |
| 5 | // a crash mid-append must never sink the export), the Settings summary line |
| 6 | // (pluralisation is a shipping requirement), and the export's diagnostic |
| 7 | // byte cap. |
| 8 | final class PerfTests: XCTestCase { |
| 9 | func testEventRoundtripAndLenientParse() throws { |
| 10 | let event = PerfEvent(t: 1_753_000_000_000, kind: "slow", name: "feed.openThread", |
| 11 | ms: 1812.5, detail: "214 messages") |
| 12 | let line = String(data: try JSONEncoder().encode(event), encoding: .utf8)! |
| 13 | let jsonl = line + "\n{\"torn\":tru\n" + line + "\nnot json at all\n" |
| 14 | let parsed = PerfEvent.parse(jsonl: jsonl) |
| 15 | XCTAssertEqual(parsed, [event, event]) |
| 16 | } |
| 17 | |
| 18 | func testSummaryEmpty() { |
| 19 | XCTAssertNil(PerfEvent.summary([])) |
| 20 | // Boot/metric/note events alone aren't "cuts" — nothing to say. |
| 21 | XCTAssertNil(PerfEvent.summary([PerfEvent(t: 0, kind: "boot", name: "boot.firstFrame", ms: 500)])) |
| 22 | } |
| 23 | |
| 24 | func testSummarySingularAndPlural() { |
| 25 | let oneEach = [ |
| 26 | PerfEvent(t: 0, kind: "hang", name: "main.hang", ms: 900), |
| 27 | PerfEvent(t: 1, kind: "slow", name: "api.get", ms: 1500), |
| 28 | ] |
| 29 | XCTAssertEqual(PerfEvent.summary(oneEach), "1 hang · 1 slow moment · worst api.get 1.5s") |
| 30 | |
| 31 | let several = [ |
| 32 | PerfEvent(t: 0, kind: "hang", name: "main.hang", ms: 900), |
| 33 | PerfEvent(t: 1, kind: "hang", name: "main.hang", ms: 450), |
| 34 | PerfEvent(t: 2, kind: "slow", name: "api.get", ms: 1500), |
| 35 | PerfEvent(t: 3, kind: "slow", name: "feed.openThread", ms: 2400), |
| 36 | ] |
| 37 | XCTAssertEqual(PerfEvent.summary(several), "2 hangs · 2 slow moments · worst feed.openThread 2.4s") |
| 38 | } |
| 39 | |
| 40 | func testSummaryHangsOnly() { |
| 41 | let hangs = [PerfEvent(t: 0, kind: "hang", name: "main.hang", ms: 600)] |
| 42 | XCTAssertEqual(PerfEvent.summary(hangs), "1 hang") |
| 43 | } |
| 44 | |
| 45 | func testSummaryIncludesHitches() { |
| 46 | let one = [PerfEvent(t: 0, kind: "hitch", name: "frame.hitch", ms: 180)] |
| 47 | XCTAssertEqual(PerfEvent.summary(one), "1 hitch") |
| 48 | let mixed = [ |
| 49 | PerfEvent(t: 0, kind: "hang", name: "main.hang", ms: 600), |
| 50 | PerfEvent(t: 1, kind: "hitch", name: "frame.hitch", ms: 180), |
| 51 | PerfEvent(t: 2, kind: "hitch", name: "frame.hitch", ms: 250), |
| 52 | ] |
| 53 | XCTAssertEqual(PerfEvent.summary(mixed), "1 hang · 2 hitches") |
| 54 | } |
| 55 | |
| 56 | func testCapDiagnosticsKeepsNewestUnderBudget() { |
| 57 | let a = String(repeating: "a", count: 100) |
| 58 | let b = String(repeating: "b", count: 100) |
| 59 | let c = String(repeating: "c", count: 100) |
| 60 | // Newest first in, newest kept: the cap stops at the first payload |
| 61 | // that would overflow, never skipping ahead to a smaller older one. |
| 62 | XCTAssertEqual(PerfExport.capDiagnostics([a, b, c], maxBytes: 250), [a, b]) |
| 63 | XCTAssertEqual(PerfExport.capDiagnostics([a, b, c], maxBytes: 1000), [a, b, c]) |
| 64 | XCTAssertEqual(PerfExport.capDiagnostics([a], maxBytes: 50), []) |
| 65 | XCTAssertEqual(PerfExport.capDiagnostics([], maxBytes: 1000), []) |
| 66 | } |
| 67 | } |
| 68 | |