tv-anarchy/Tests/TVAnarchyCoreTests/HelperDeploymentTests.swift
Natalie ca1871f5dd feat(@applications/tv-anarchy): add roku device support
Co-Authored-By: Lilith Autocommit <noreply@atlilith.com>
2026-06-09 21:37:34 -07:00

65 lines
3.5 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)
XCTAssertEqual(HelperDeployment.sha256(body), expected)
XCTAssertEqual(HelperDeployment.vendoredSource(forBin: "/usr/local/bin/black-tv"), script)
// The bin's basename keys the lookup an unknown helper has no expectation.
XCTAssertNil(HelperDeployment.expectedSHA(forBin: "/usr/local/bin/some-other-helper"))
XCTAssertNil(HelperDeployment.vendoredSource(forBin: "/usr/local/bin/some-other-helper"))
}
/// `helperBin` is what the freshness check and the updater key off: the
/// first word of whichever delegated template a host configures.
func testHelperBinFromCommands() {
XCTAssertEqual(CommandsConfig.blackTVDefaults(bin: "/usr/local/bin/black-tv").helperBin,
"/usr/local/bin/black-tv")
XCTAssertEqual(CommandsConfig(stop: ["btv", "stop"]).helperBin, "btv")
XCTAssertNil(CommandsConfig().helperBin)
}
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)
}
}