Tests/Unit/LogScrubberTests.swift (3470B)
1import Foundation
2import Testing
3
4@testable import Crossmate
5
6@Suite("Log scrubber")
7@MainActor
8struct LogScrubberTests {
9 @Test("UUIDs truncate to an eight-character prefix")
10 func uuids() {
11 let scrubbed = LogScrubber.scrub(
12 "engagement: reconnect backstop cancelled for 0A5207B4-CB66-45A5-A054-27861594B612"
13 )
14 #expect(scrubbed == "engagement: reconnect backstop cancelled for 0A5207B4…")
15 }
16
17 @Test("Ping record names keep their structure but lose full IDs")
18 func pingRecordNames() {
19 let scrubbed = LogScrubber.scrub(
20 "ping(invite): already-handled record "
21 + "ping-C05349C4-FEAE-49F7-86F7-03F34656B302"
22 + "-_f838827eeca157aeafb6901073c18676"
23 + "-02b1f8d0a782450ea5e4f4ca20ae7a48-1781217475858"
24 )
25 #expect(
26 scrubbed
27 == "ping(invite): already-handled record ping-C05349C4…-_f838827e…-02b1f8d0…-1781217475858"
28 )
29 }
30
31 @Test("User record names keep underscore plus eight hex; sentinels pass through")
32 func userRecordNames() {
33 let scrubbed = LogScrubber.scrub(
34 #"share=["_1960c4811a7e1a7210c2031998319301", "__defaultOwner__"]"#
35 )
36 #expect(scrubbed == #"share=["_1960c481…", "__defaultOwner__"]"#)
37 }
38
39 @Test("Share URL tokens truncate while the link stays recognisable")
40 func shareURLs() {
41 let scrubbed = LogScrubber.scrub(
42 "share link created for 0A5207B4-CB66-45A5-A054-27861594B612: "
43 + "https://www.icloud.com/share/0aBcDeFgHiJkLmNoPqRs#Monday_Puzzle"
44 )
45 #expect(
46 scrubbed
47 == "share link created for 0A5207B4…: https://www.icloud.com/share/0aBcDeFg…#Monday_Puzzle"
48 )
49 }
50
51 @Test("Full-length hex tokens collapse to a single stub")
52 func longHexTokens() {
53 let token = String(repeating: "c769792fd840", count: 6)
54 let scrubbed = LogScrubber.scrub("token=\(token)")
55 #expect(scrubbed == "token=c769792f…")
56 }
57
58 @Test("Prose, subscription IDs, and decimal timestamps pass through")
59 func passthrough() {
60 let messages = [
61 "remote notification: sub=CKSyncEngineDatabaseSubscription-Private",
62 "shared zone discovery: nothing new (server=23, known=23)",
63 "freshen game list foreground: private skipped (cooldown, last 7s ago)",
64 ]
65 for message in messages {
66 #expect(LogScrubber.scrub(message) == message)
67 }
68 }
69
70 @Test("EventLog scrubs at append time")
71 func eventLogAppliesScrubbing() {
72 let log = EventLog()
73 log.note("engagement: ending for 0A5207B4-CB66-45A5-A054-27861594B612")
74 #expect(log.entries.last?.message == "engagement: ending for 0A5207B4…")
75 }
76
77 @Test("SyncMonitor scrubs the stored error description")
78 func syncMonitorScrubsErrorDescription() {
79 let monitor = SyncMonitor(log: EventLog())
80 monitor.recordError(
81 "shared game/moves catch-up",
82 NSError(
83 domain: "CKErrorDomain",
84 code: 26,
85 userInfo: [
86 NSLocalizedDescriptionKey:
87 "Zone game-0A5207B4-CB66-45A5-A054-27861594B612 does not exist"
88 ]
89 )
90 )
91 #expect(monitor.lastErrorDescription == "Zone game-0A5207B4… does not exist")
92 }
93}