← All repositories

reportage

A life-logging app for iPhone

git clone https://code.inqk.net/reportage.git


Tests/Unit/EventLogTests.swift (2768B)

1import Foundation
2import Testing
3
4@testable import Reportage
5
6@Suite("EventLog")
7struct EventLogTests {
8    private func makeURL() -> URL {
9        URL.temporaryDirectory.appending(path: "event-log-\(UUID().uuidString).json")
10    }
11
12    @Test("Entries are written at once, so a new log on the same file reads them")
13    func persists() {
14        let url = makeURL()
15        defer { try? FileManager.default.removeItem(at: url) }
16        let now = Date()
17        EventLog(fileURL: url).note("Launched", category: "App", now: now)
18        EventLog(fileURL: url).note("No fix", level: .notice, category: "Capture", now: now.addingTimeInterval(1))
19
20        let entries = EventLog(fileURL: url).all(now: now.addingTimeInterval(2))
21        #expect(entries.map(\.message) == ["Launched", "No fix"])
22        #expect(entries.map(\.level) == [.info, .notice])
23        #expect(entries.map(\.category) == ["App", "Capture"])
24    }
25
26    @Test("Entries older than the retention are dropped")
27    func retention() {
28        let log = EventLog(fileURL: nil, retention: 60)
29        let start = Date()
30        log.note("Old", category: "App", now: start)
31        log.note("New", category: "App", now: start.addingTimeInterval(50))
32
33        #expect(log.all(now: start.addingTimeInterval(55)).map(\.message) == ["Old", "New"])
34        #expect(log.all(now: start.addingTimeInterval(70)).map(\.message) == ["New"])
35        #expect(log.all(now: start.addingTimeInterval(200)).isEmpty)
36    }
37
38    @Test("Only the newest entries are kept past the ceiling")
39    func ceiling() {
40        let log = EventLog(fileURL: nil, maxEntries: 3)
41        let now = Date()
42        for i in 1...5 {
43            log.note("\(i)", category: "App", now: now.addingTimeInterval(Double(i)))
44        }
45        #expect(log.all(now: now.addingTimeInterval(6)).map(\.message) == ["3", "4", "5"])
46    }
47
48    @Test("Clearing empties the file too")
49    func clear() {
50        let url = makeURL()
51        defer { try? FileManager.default.removeItem(at: url) }
52        let log = EventLog(fileURL: url)
53        log.note("Launched", category: "App")
54        log.clear()
55
56        #expect(log.all().isEmpty)
57        #expect(EventLog(fileURL: url).all().isEmpty)
58    }
59
60    @Test("The shared text has one line per entry, with its category and level")
61    func text() {
62        let log = EventLog(fileURL: nil)
63        let now = Date()
64        log.note("Launched", category: "App", now: now)
65        log.note("Couldn't get the weather", level: .error, category: "Weather", now: now)
66
67        let lines = log.text(now: now).split(separator: "\n")
68        #expect(lines.count == 2)
69        #expect(lines[0].hasSuffix("[App] info: Launched"))
70        #expect(lines[1].hasSuffix("[Weather] error: Couldn't get the weather"))
71    }
72}