krz/octosentry

macOS menu bar app to monitor GitHub security alerts

clone: git clone https://gitbay.org/krz/octosentry.git

main: octosentryTests/AlertFilterTests.swift · raw

 1//
 2//  AlertFilterTests.swift
 3//  octosentryTests
 4//
 5
 6import Foundation
 7import Testing
 8@testable import octosentry
 9
10struct AlertFilterTests {
11
12    private let dependabotHello = TestEvents.event(id: "a", source: .dependabot, repo: "octocat/hello-world")
13    private let codeScanningHello = TestEvents.event(id: "b", source: .codeScanning, repo: "octocat/hello-world")
14    private let secretScanningSpoon = TestEvents.event(id: "c", source: .secretScanning, repo: "octocat/spoon-knife")
15
16    private var allEvents: [SecurityEvent] {
17        [dependabotHello, codeScanningHello, secretScanningSpoon]
18    }
19
20    @Test func emptyFilterIsInactiveAndMatchesEverything() {
21        let filter = AlertFilter()
22
23        #expect(filter.isActive == false)
24        #expect(filter.apply(to: allEvents).map(\.id) == ["a", "b", "c"])
25    }
26
27    @Test func filtersBySource() {
28        var filter = AlertFilter()
29        filter.sources = [.dependabot]
30
31        #expect(filter.isActive)
32        #expect(filter.apply(to: allEvents).map(\.id) == ["a"])
33    }
34
35    @Test func sourceFilterUnionsSelectedSources() {
36        var filter = AlertFilter()
37        filter.sources = [.dependabot, .secretScanning]
38
39        #expect(filter.apply(to: allEvents).map(\.id) == ["a", "c"])
40    }
41
42    @Test func filtersByRepo() {
43        var filter = AlertFilter()
44        filter.repos = ["octocat/spoon-knife"]
45
46        #expect(filter.apply(to: allEvents).map(\.id) == ["c"])
47    }
48
49    // Source and repo intersect: an event has to satisfy both.
50    @Test func sourceAndRepoAreCombinedWithAnd() {
51        var filter = AlertFilter()
52        filter.sources = [.dependabot]
53        filter.repos = ["octocat/spoon-knife"]
54
55        #expect(filter.apply(to: allEvents).isEmpty)
56
57        filter.repos = ["octocat/hello-world"]
58        #expect(filter.apply(to: allEvents).map(\.id) == ["a"])
59    }
60
61    @Test func showingHiddenCountsAsAnActiveFilter() {
62        var filter = AlertFilter()
63        #expect(filter.isActive == false)
64
65        filter.showsHidden = true
66        #expect(filter.isActive)
67        // It reveals rows rather than removing them, so nothing is filtered out.
68        #expect(filter.apply(to: allEvents).map(\.id) == ["a", "b", "c"])
69    }
70
71    @Test func filterPreservesInputOrder() {
72        var filter = AlertFilter()
73        filter.repos = ["octocat/hello-world"]
74
75        #expect(filter.apply(to: allEvents.reversed()).map(\.id) == ["b", "a"])
76    }
77}