macOS: test gateway version normalization

This commit is contained in:
ImLukeF
2026-03-28 12:05:27 +11:00
parent 8545cbd358
commit 6c9126ec19
2 changed files with 19 additions and 8 deletions

View File

@@ -291,6 +291,17 @@ enum GatewayEnvironment {
// MARK: - Internals
/// Exposed for tests so CLI version output normalization stays local to gateway checks.
static func normalizeGatewayVersionOutput(_ raw: String?) -> String? {
guard var normalized = raw?.trimmingCharacters(in: .whitespacesAndNewlines), !normalized.isEmpty else {
return nil
}
if normalized.lowercased().hasPrefix("openclaw ") {
normalized = String(normalized.dropFirst("openclaw ".count))
}
return normalized
}
private static func readGatewayVersion(binary: String) -> Semver? {
let start = Date()
let process = Process()
@@ -317,14 +328,8 @@ enum GatewayEnvironment {
bin=\(binary, privacy: .public)
""")
}
var raw = String(data: data, encoding: .utf8)?
.trimmingCharacters(in: .whitespacesAndNewlines)
// `openclaw --version` outputs "OpenClaw 2026.x.y"; strip the product prefix
// so Semver.parse receives only the version token.
if let r = raw, r.lowercased().hasPrefix("openclaw ") {
raw = String(r.dropFirst("openclaw ".count))
}
return Semver.parse(raw)
let raw = String(data: data, encoding: .utf8)
return Semver.parse(self.normalizeGatewayVersionOutput(raw))
} catch {
let elapsedMs = Int(Date().timeIntervalSince(start) * 1000)
self.logger.error(

View File

@@ -24,6 +24,12 @@ struct GatewayEnvironmentTests {
#expect(Semver.parse("OpenClaw 2026.3.23-1") == nil)
}
@Test func `gateway version output strips product prefix before parsing`() {
let normalized = GatewayEnvironment.normalizeGatewayVersionOutput(" OpenClaw 2026.3.23-1 \n")
#expect(normalized == "2026.3.23-1")
#expect(Semver.parse(normalized) == Semver(major: 2026, minor: 3, patch: 23))
}
@Test func `semver compatibility requires same major and not older`() {
let required = Semver(major: 2, minor: 1, patch: 0)
#expect(Semver(major: 2, minor: 1, patch: 0).compatible(with: required))