53 lines
2.8 KiB
Swift
53 lines
2.8 KiB
Swift
import CryptoKit
|
|
import XCTest
|
|
@testable import TVAnarchyCore
|
|
|
|
/// The Devices tab's "not up to date" badge: a device reports the sha256 of the
|
|
/// helper script it runs; the app hashes the repo's vendored copy. Freshness is
|
|
/// the comparison — no version constants anywhere.
|
|
final class HelperDeploymentTests: XCTestCase {
|
|
func testFreshnessJudgement() {
|
|
// No expectation (unknown helper / no repo checkout) → can't judge.
|
|
XCTAssertNil(HelperDeployment.freshness(expected: nil, reported: "abc"))
|
|
XCTAssertNil(HelperDeployment.freshness(expected: nil, reported: nil))
|
|
// Matching hashes → current.
|
|
XCTAssertEqual(HelperDeployment.freshness(expected: "abc", reported: "abc"), .current)
|
|
// Mismatch → outdated, carrying what the device reported.
|
|
XCTAssertEqual(HelperDeployment.freshness(expected: "abc", reported: "def"),
|
|
.outdated(deployed: "def"))
|
|
// A helper too old to self-report is necessarily outdated.
|
|
XCTAssertEqual(HelperDeployment.freshness(expected: "abc", reported: nil),
|
|
.outdated(deployed: nil))
|
|
}
|
|
|
|
func testExpectedSHAHashesVendoredScript() throws {
|
|
// Point RepoPaths at a throwaway repo via $TV_ANARCHY_REPO.
|
|
let repo = FileManager.default.temporaryDirectory
|
|
.appendingPathComponent("helper-deploy-test-\(UUID().uuidString)")
|
|
let script = repo.appendingPathComponent("mcp/src/blacktv/black-tv.sh")
|
|
try FileManager.default.createDirectory(at: script.deletingLastPathComponent(),
|
|
withIntermediateDirectories: true)
|
|
let body = Data("#!/usr/bin/env bash\necho hi\n".utf8)
|
|
try body.write(to: script)
|
|
setenv("TV_ANARCHY_REPO", repo.path, 1)
|
|
defer {
|
|
unsetenv("TV_ANARCHY_REPO")
|
|
try? FileManager.default.removeItem(at: repo)
|
|
}
|
|
|
|
let expected = SHA256.hash(data: body).map { String(format: "%02x", $0) }.joined()
|
|
XCTAssertEqual(HelperDeployment.expectedSHA(forBin: "/usr/local/bin/black-tv"), expected)
|
|
// The bin's basename keys the lookup — an unknown helper has no expectation.
|
|
XCTAssertNil(HelperDeployment.expectedSHA(forBin: "/usr/local/bin/some-other-helper"))
|
|
}
|
|
|
|
func testHostStatsDecodesWithAndWithoutHelperSha() throws {
|
|
let new = #"{"load1":0.5,"load5":0.4,"load15":0.3,"cores":8,"mpv_cpu":null,"helper_sha":"deadbeef"}"#
|
|
let old = #"{"load1":0.5,"load5":0.4,"load15":0.3,"cores":8,"mpv_cpu":12.5}"#
|
|
let n = try JSONDecoder().decode(HostStats.self, from: Data(new.utf8))
|
|
let o = try JSONDecoder().decode(HostStats.self, from: Data(old.utf8))
|
|
XCTAssertEqual(n.helper_sha, "deadbeef")
|
|
XCTAssertNil(o.helper_sha) // pre-stamp deploy still decodes
|
|
XCTAssertEqual(o.mpv_cpu, 12.5)
|
|
}
|
|
}
|