SPB Git

Repository Public License

ModelTests.swift 2.9 KB 62 lines swift

1// ORIX Secure2// File: Packages/ORIXSecureModel/Tests/ORIXSecureModelTests/ModelTests.swift3// Description: Tests of the shared result vocabulary: measurement semantics, finding ordering, Codable round trips.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import Foundation8import Testing9@testable import ORIXSecureModel1011@Suite struct ModelTests {12    @Test func measurementKeepsUnavailabilityExplicit() {13        let cycles: Reading<Int> = .unavailable(.notExposedByiOS, "No public API")14        #expect(cycles.value == nil)15        #expect(cycles.unavailability?.kind == .notExposedByiOS)16        #expect(!cycles.isMeasured)17        let level: Reading<Double> = .measured(0.42)18        #expect(level.value == 0.42)19        #expect(level.map { Int($0 * 100) } == .measured(42))20        #expect(cycles.map { $0 + 1 }.unavailability?.detail == "No public API")21    }2223    @Test func measurementRoundTripsThroughJSON() throws {24        let values: [Reading<Int>] = [.measured(7), .unavailable(.simulator, "Simulator")]25        let data = try JSONEncoder().encode(values)26        let decoded = try JSONDecoder().decode([Reading<Int>].self, from: data)27        #expect(decoded == values)28    }2930    private func finding(_ id: String, _ state: AssessmentState, _ severity: Severity = .none) -> Finding {31        Finding(id: id, category: .device, title: id, state: state, severity: severity, confidence: .measured, verification: .publicAPI, explanation: "")32    }3334    @Test func prioritizedPutsAttentionFirstThenSeverity() {35        let list = [finding("a", .verified), finding("b", .attention, .low), finding("c", .userConfirmationRequired), finding("d", .attention, .high)]36        #expect(list.prioritized().map(\.id) == ["d", "b", "c", "a"])37        #expect(list.count(.attention) == 2)38        #expect(list.highestSeverity == .high)39        #expect([finding("x", .verified, .high)].highestSeverity == Severity.none)40    }4142    @Test func reportDocumentRoundTrips() throws {43        let report = ReportDocument(kind: .securityAssessment, title: "T", summary: "S", sections: [44            .init(title: "One", paragraphs: ["p"], facts: [.init("k", "v")], findings: [finding("a", .attention, .moderate)], bullets: ["b"])45        ], unavailable: ["Battery cycle count"])46        let encoder = JSONEncoder()47        encoder.dateEncodingStrategy = .iso860148        let decoder = JSONDecoder()49        decoder.dateDecodingStrategy = .iso860150        let data = try encoder.encode(report)51        let decoded = try decoder.decode(ReportDocument.self, from: data)52        #expect(decoded.title == "T")53        #expect(decoded.allFindings.first?.severity == .moderate)54        #expect(decoded.unavailable == ["Battery cycle count"])55    }5657    @Test func englishIsTheSourceLanguageWithoutACatalog() {58        #expect(AssessmentState.userConfirmationRequired.title == "User confirmation required")59        #expect(L("Checked \(3) items") == "Checked 3 items")60    }61}62
Files main
Menu

Theme