| 1 | import Foundation |
| 2 | import Testing |
| 3 | import EquipoShareCore |
| 4 | @testable import EquipoKit |
| 5 | |
| 6 | @Suite struct NotificationJournalTests { |
| 7 | @Test func requestIsDurableBoundAndNeverReplacedOrExtended() throws { |
| 8 | let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) |
| 9 | defer { try? FileManager.default.removeItem(at: directory) } |
| 10 | let journal = ActionJournal(directory: directory) |
| 11 | let now = Date(timeIntervalSince1970: 100000) |
| 12 | let body = Data("original request".utf8) |
| 13 | try journal.save(id: "event-a", binding: "session-a", body: body, now: now) |
| 14 | let restored = ActionJournal(directory: directory) |
| 15 | #expect(try restored.load(id: "event-a", binding: "session-a", now: now) == body) |
| 16 | #expect(try restored.load(id: "event-a", binding: "session-b", now: now) == nil) |
| 17 | #expect(throws: (any Error).self) { try restored.save(id: "event-a", binding: "session-a", body: Data("changed".utf8), now: now) } |
| 18 | try restored.save(id: "event-b", binding: "session-a", body: Data("independent".utf8), now: now) |
| 19 | try restored.save(id: "event-a", binding: "session-a", body: body, now: now.addingTimeInterval(86399)) |
| 20 | #expect(try restored.load(id: "event-a", binding: "session-a", now: now.addingTimeInterval(86400)) == nil) |
| 21 | restored.clear() |
| 22 | #expect(try restored.load(id: "event-b", binding: "session-a", now: now) == nil) |
| 23 | } |
| 24 | @Test func boundedAndCompletedEntriesAreRemoved() throws { |
| 25 | let directory = FileManager.default.temporaryDirectory.appendingPathComponent(UUID().uuidString) |
| 26 | defer { try? FileManager.default.removeItem(at: directory) } |
| 27 | let journal = ActionJournal(directory: directory) |
| 28 | #expect(throws: (any Error).self) { try journal.save(id: "big", binding: "a", body: Data(repeating: 1, count: 64 * 1024 + 1)) } |
| 29 | for n in 0..<32 { try journal.save(id: "event-\(n)", binding: "a", body: Data([1])) } |
| 30 | #expect(throws: (any Error).self) { try journal.save(id: "overflow", binding: "a", body: Data([1])) } |
| 31 | try journal.remove(id: "event-0", binding: "a") |
| 32 | #expect(try ActionJournal(directory: directory).load(id: "event-0", binding: "a") == nil) |
| 33 | try journal.save(id: "replacement", binding: "a", body: Data([2])) |
| 34 | let files = try FileManager.default.contentsOfDirectory(at: directory, includingPropertiesForKeys: nil) |
| 35 | #expect(files.count == 1) |
| 36 | #expect((try FileManager.default.attributesOfItem(atPath: files[0].path)[.posixPermissions] as? NSNumber)?.intValue == 0o600) |
| 37 | } |
| 38 | } |
| 39 | |