summaryrefslogtreecommitdiff
path: root/Tests/BubombCoreTests/main.swift
blob: de4bae10edbbe56e3471103807551908107ff96d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import Foundation
import BubombCore

func runTest(suiteName: String, testName: String, block: () async throws -> Void) async {
    TestContext.shared.currentTestFailures = []
    let start = Date()
    var threw = false
    var errorDesc = ""

    do {
        try await block()
    } catch {
        threw = true
        errorDesc = "\(error)"
    }

    let elapsed = Date().timeIntervalSince(start)
    let failures = TestContext.shared.currentTestFailures

    if !threw && failures.isEmpty {
        TestContext.shared.totalPassed += 1
        print("  \u{001B}[32m✓\u{001B}[0m \(testName) (\(String(format: "%.3f", elapsed))s)")
    } else {
        TestContext.shared.totalFailed += 1
        print("  \u{001B}[31m✗\u{001B}[0m \(testName) (\(String(format: "%.3f", elapsed))s)")
        if threw {
            print("    \u{001B}[31mThrew unexpected error: \(errorDesc)\u{001B}[0m")
        }
        for f in failures {
            print("    \u{001B}[31m[\(f.file):\(f.line)] \(f.message)\u{001B}[0m")
        }
    }
}

print("\u{001B}[1m💣 Running Bubomb Test Suite...\u{001B}[0m\n")
let suiteStartTime = Date()

// Suite 1: PayloadValidationTests
print("\u{001B}[33m• PayloadValidationTests\u{001B}[0m")
let val = PayloadValidationTests()
await runTest(suiteName: "PayloadValidationTests", testName: "testEmptyPayloadNameThrows") { val.testEmptyPayloadNameThrows() }
await runTest(suiteName: "PayloadValidationTests", testName: "testEmptyCommandThrows") { val.testEmptyCommandThrows() }
await runTest(suiteName: "PayloadValidationTests", testName: "testValidShellCommandPasses") { try val.testValidShellCommandPasses() }
await runTest(suiteName: "PayloadValidationTests", testName: "testNonExistentShellThrows") { val.testNonExistentShellThrows() }
await runTest(suiteName: "PayloadValidationTests", testName: "testNonExistentWorkingDirectoryThrows") { val.testNonExistentWorkingDirectoryThrows() }
await runTest(suiteName: "PayloadValidationTests", testName: "testInvalidURLThrows") { val.testInvalidURLThrows() }
await runTest(suiteName: "PayloadValidationTests", testName: "testValidURLPasses") { try val.testValidURLPasses() }
await runTest(suiteName: "PayloadValidationTests", testName: "testNonExistentScriptThrows") { val.testNonExistentScriptThrows() }
await runTest(suiteName: "PayloadValidationTests", testName: "testEmptySequenceThrows") { val.testEmptySequenceThrows() }

// Suite 2: SettingsStoreTests
print("\n\u{001B}[33m• SettingsStoreTests\u{001B}[0m")
let storeTests = SettingsStoreTests()
await runTest(suiteName: "SettingsStoreTests", testName: "testInitialSettings") {
    storeTests.setUp()
    storeTests.testInitialSettings()
    storeTests.tearDown()
}
await runTest(suiteName: "SettingsStoreTests", testName: "testSaveAndRetrieveActivePayload") {
    storeTests.setUp()
    storeTests.testSaveAndRetrieveActivePayload()
    storeTests.tearDown()
}
await runTest(suiteName: "SettingsStoreTests", testName: "testExecutionHistoryCap") {
    storeTests.setUp()
    storeTests.testExecutionHistoryCap()
    storeTests.tearDown()
}
await runTest(suiteName: "SettingsStoreTests", testName: "testClearHistory") {
    storeTests.setUp()
    storeTests.testClearHistory()
    storeTests.tearDown()
}
await runTest(suiteName: "SettingsStoreTests", testName: "testResetToDefaults") {
    storeTests.setUp()
    storeTests.testResetToDefaults()
    storeTests.tearDown()
}

// Suite 3: ProcessRunnerTests
print("\n\u{001B}[33m• ProcessRunnerTests\u{001B}[0m")
let procTests = ProcessRunnerTests()
await runTest(suiteName: "ProcessRunnerTests", testName: "testSuccessfulCommandExecution") { try await procTests.testSuccessfulCommandExecution() }
await runTest(suiteName: "ProcessRunnerTests", testName: "testShellCommandExecutorWithArguments") { try await procTests.testShellCommandExecutorWithArguments() }
await runTest(suiteName: "ProcessRunnerTests", testName: "testNonZeroExitStatusThrows") { await procTests.testNonZeroExitStatusThrows() }
await runTest(suiteName: "ProcessRunnerTests", testName: "testEnvironmentVariablesPassedToProcess") { try await procTests.testEnvironmentVariablesPassedToProcess() }
await runTest(suiteName: "ProcessRunnerTests", testName: "testWorkingDirectory") { try await procTests.testWorkingDirectory() }
await runTest(suiteName: "ProcessRunnerTests", testName: "testProcessTimeout") { await procTests.testProcessTimeout() }
await runTest(suiteName: "ProcessRunnerTests", testName: "testProcessCancellation") { await procTests.testProcessCancellation() }

// Suite 4: URLLauncherTests
print("\n\u{001B}[33m• URLLauncherTests\u{001B}[0m")
let urlTests = URLLauncherTests()
await runTest(suiteName: "URLLauncherTests", testName: "testValidURLs") { urlTests.testValidURLs() }
await runTest(suiteName: "URLLauncherTests", testName: "testInvalidURLs") { urlTests.testInvalidURLs() }

// Suite 5: SafetyPolicyTests
print("\n\u{001B}[33m• SafetyPolicyTests\u{001B}[0m")
let safetyTests = SafetyPolicyTests()
await runTest(suiteName: "SafetyPolicyTests", testName: "testDestructiveCommandsDetected") { safetyTests.testDestructiveCommandsDetected() }
await runTest(suiteName: "SafetyPolicyTests", testName: "testSuspiciousCommandsDetected") { safetyTests.testSuspiciousCommandsDetected() }
await runTest(suiteName: "SafetyPolicyTests", testName: "testSafeCommandsPass") { safetyTests.testSafeCommandsPass() }

// Suite 6: SequenceExecutorTests
print("\n\u{001B}[33m• SequenceExecutorTests\u{001B}[0m")
let seqTests = SequenceExecutorTests()
await runTest(suiteName: "SequenceExecutorTests", testName: "testSuccessfulSequentialExecution") { try await seqTests.testSuccessfulSequentialExecution() }
await runTest(suiteName: "SequenceExecutorTests", testName: "testSequenceStopsOnErrorWhenConfigured") { await seqTests.testSequenceStopsOnErrorWhenConfigured() }
await runTest(suiteName: "SequenceExecutorTests", testName: "testSequenceContinuesOnErrorWhenConfigured") { try await seqTests.testSequenceContinuesOnErrorWhenConfigured() }

let totalTime = Date().timeIntervalSince(suiteStartTime)
let passed = TestContext.shared.totalPassed
let failed = TestContext.shared.totalFailed
let total = passed + failed

print("\n" + String(repeating: "-", count: 50))
if failed == 0 {
    print("\u{001B}[32;1m💥 All \(total) tests passed in \(String(format: "%.2f", totalTime))s! BOOM!\u{001B}[0m\n")
    exit(0)
} else {
    print("\u{001B}[31;1m❌ \(failed) of \(total) tests failed in \(String(format: "%.2f", totalTime))s.\u{001B}[0m\n")
    exit(1)
}