File size: 2,925 Bytes
fc93158
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import Foundation
import Testing
@testable import OpenClaw

private struct SystemRunCommandContractFixture: Decodable {
    let cases: [SystemRunCommandContractCase]
}

private struct SystemRunCommandContractCase: Decodable {
    let name: String
    let command: [String]
    let rawCommand: String?
    let expected: SystemRunCommandContractExpected
}

private struct SystemRunCommandContractExpected: Decodable {
    let valid: Bool
    let displayCommand: String?
    let errorContains: String?
}

struct ExecSystemRunCommandValidatorTests {
    @Test func `matches shared system run command contract fixture`() throws {
        for entry in try Self.loadContractCases() {
            let result = ExecSystemRunCommandValidator.resolve(command: entry.command, rawCommand: entry.rawCommand)

            if !entry.expected.valid {
                switch result {
                case let .ok(resolved):
                    Issue
                        .record("\(entry.name): expected invalid result, got displayCommand=\(resolved.displayCommand)")
                case let .invalid(message):
                    if let expected = entry.expected.errorContains {
                        #expect(
                            message.contains(expected),
                            "\(entry.name): expected error containing \(expected), got \(message)")
                    }
                }
                continue
            }

            switch result {
            case let .ok(resolved):
                #expect(
                    resolved.displayCommand == entry.expected.displayCommand,
                    "\(entry.name): unexpected display command")
            case let .invalid(message):
                Issue.record("\(entry.name): unexpected invalid result: \(message)")
            }
        }
    }

    private static func loadContractCases() throws -> [SystemRunCommandContractCase] {
        let fixtureURL = try self.findContractFixtureURL()
        let data = try Data(contentsOf: fixtureURL)
        let decoded = try JSONDecoder().decode(SystemRunCommandContractFixture.self, from: data)
        return decoded.cases
    }

    private static func findContractFixtureURL() throws -> URL {
        var cursor = URL(fileURLWithPath: #filePath).deletingLastPathComponent()
        for _ in 0..<8 {
            let candidate = cursor
                .appendingPathComponent("test")
                .appendingPathComponent("fixtures")
                .appendingPathComponent("system-run-command-contract.json")
            if FileManager.default.fileExists(atPath: candidate.path) {
                return candidate
            }
            cursor.deleteLastPathComponent()
        }
        throw NSError(
            domain: "ExecSystemRunCommandValidatorTests",
            code: 1,
            userInfo: [NSLocalizedDescriptionKey: "missing shared system-run command contract fixture"])
    }
}