Repository Public
1import MCCCore2import XCTest3@testable import MCC45@MainActor6final class ServicesTests: XCTestCase {7 private func service(_ id: String, name: String, state: API.ServiceState = .online, manager: API.ServiceManager = .pm2, node: String = "n1",8 managed: Bool = false, gone: Bool = false, ports: [Int] = []) -> API.Service {9 API.Service(id: id, nodeId: node, nodeAlias: node == "n1" ? "M4M64a" : "bhs64", manager: manager, name: name, state: state, ports: ports,10 managed: managed, firstSeenAt: Date(), lastSeenAt: Date(), goneAt: gone ? Date() : nil, freshness: .fresh)11 }1213 func testServiceFilter() {14 let all = [service("a", name: "ka-stats", state: .errored, ports: [8080]), service("b", name: "caddy", manager: .systemd, node: "n2", managed: true),15 service("c", name: "old", gone: true)]16 var f = ServiceFilter()17 XCTAssertEqual(f.apply(all).map(\.id), ["a", "b"], "gone services are hidden by default")18 f.includeGone = true19 XCTAssertEqual(f.apply(all).count, 3)20 XCTAssertEqual(ServiceFilter(manager: .systemd).apply(all).map(\.id), ["b"])21 XCTAssertEqual(ServiceFilter(state: .errored).apply(all).map(\.id), ["a"])22 XCTAssertEqual(ServiceFilter(nodeID: "n2").apply(all).map(\.id), ["b"])23 XCTAssertEqual(ServiceFilter(managed: true).apply(all).map(\.id), ["b"])24 XCTAssertEqual(ServiceFilter(text: "8080").apply(all).map(\.id), ["a"])25 XCTAssertEqual(ServiceFilter(text: "BHS").apply(all).map(\.id), ["b"])26 }2728 func testRowsKeepUnmeasuredValuesOutOfTheWay() {29 let r = ServiceRow(service: service("a", name: "x"))30 XCTAssertEqual(r.memory, -1)31 XCTAssertEqual(r.cpu, -1)32 XCTAssertEqual(r.healthRank, 2)33 XCTAssertEqual(ServiceRow(service: service("e", name: "e", state: .errored)).stateRank, 0, "errored services sort first")34 }3536 func testPublicURL() {37 XCTAssertEqual(PublicURL.normalized(" https://lou-ka.com/ "), "https://lou-ka.com/")38 XCTAssertEqual(PublicURL.normalized(""), "", "empty clears the URL")39 XCTAssertNil(PublicURL.normalized("javascript:alert(1)"))40 XCTAssertNil(PublicURL.normalized("file:///etc/passwd"))41 XCTAssertNil(PublicURL.normalized("lou-ka.com"))42 XCTAssertNotNil(PublicURL.url("http://localhost:8080"))43 XCTAssertNil(PublicURL.url(nil))44 XCTAssertNil(PublicURL.url("ssh://host"))45 }4647 private func type(_ name: String, kinds: [API.ActionTargetKind], available: Bool = true, reason: String? = nil) -> API.ActionType {48 API.ActionType(_type: name, title: name.uppercased(), description: "", criticality: .medium, permission: "actions.run.medium", targetKinds: kinds,49 paramsSchema: try! JSONValue.object([:]).decode(API.ActionType.ParamsSchemaPayload.self), requiresStepUp: false, requiresJustification: false, requiresConfirmation: false, requiresApproval: false,50 timeoutS: 60, lockScope: .service, available: available, unavailableReason: reason)51 }5253 func testCatalogDecidesWhichActionsExist() {54 let types = [type("service.restart", kinds: [.service]), type("deploy.release", kinds: [.service], available: false, reason: "not enabled in v1"),55 type("system.reboot", kinds: [.node])]56 XCTAssertEqual(CatalogActionState.resolve("service.restart", kind: .service, in: types), .enabled(title: "SERVICE.RESTART", criticality: .medium))57 XCTAssertEqual(CatalogActionState.resolve("deploy.release", kind: .service, in: types), .disabled(title: "DEPLOY.RELEASE", reason: "not enabled in v1"))58 XCTAssertEqual(CatalogActionState.resolve("service.stop", kind: .service, in: types), .hidden, "absent from the catalog: no button")59 XCTAssertEqual(CatalogActionState.resolve("system.reboot", kind: .service, in: types), .hidden, "wrong target kind")60 XCTAssertEqual(CatalogActionState.resolve("service.restart", kind: .service, in: nil), .hidden)61 }6263 func testLogSearch() {64 let text = "ERROR one\ninfo two\nerror three\nÉtat: error"65 let r = LogSearch.ranges(of: "error", in: text)66 XCTAssertEqual(r.count, 3)67 XCTAssertEqual((text as NSString).substring(with: r[0]), "ERROR")68 XCTAssertEqual(r[1].location, (text as NSString).range(of: "error three").location)69 XCTAssertTrue(LogSearch.ranges(of: " ", in: text).isEmpty)70 XCTAssertTrue(LogSearch.ranges(of: "absent", in: text).isEmpty)71 // Literal search: regex metacharacters are not interpreted.72 XCTAssertEqual(LogSearch.ranges(of: ".*", in: "a.*b .* c").count, 2)73 XCTAssertEqual(LogSearch.ranges(of: "a", in: String(repeating: "a", count: 50), limit: 10).count, 10)74 XCTAssertEqual(LogSearch.step(from: 2, count: 3, forward: true), 0)75 XCTAssertEqual(LogSearch.step(from: 0, count: 3, forward: false), 2)76 XCTAssertNil(LogSearch.step(from: 0, count: 0, forward: true))77 }7879 func testLogErrorExplanationsAreSpecific() {80 let offline = ServiceLogsView.explain(APIError(status: 422, code: "agent_offline", message: "x"))81 let forbidden = ServiceLogsView.explain(APIError(status: 403, code: "forbidden", message: "x"))82 let other = ServiceLogsView.explain(APIError(status: 500, code: "internal", message: "x"))83 XCTAssertEqual(Set([offline, forbidden, other]).count, 3)84 XCTAssertFalse(offline.hasPrefix("logs."), "missing translation")85 }86}87