59 lines
2.5 KiB
Swift
59 lines
2.5 KiB
Swift
import XCTest
|
|
|
|
@testable import MultiSimAdmin
|
|
|
|
final class DeviceServiceTests: XCTestCase {
|
|
func testLoadsAndFlattensWhitelistedReadSections() async throws {
|
|
let store = InstanceSessionStore()
|
|
let client = DeviceTestClient()
|
|
let service = DeviceService(client: client, sessions: store)
|
|
let instance = Self.instance()
|
|
await store.set(instance.id, origin: instance.origin, cookie: "simadmin_session=token")
|
|
|
|
let snapshot = try await service.snapshot(instance)
|
|
|
|
XCTAssertEqual(client.routes, [.device, .sim, .network, .stats, .statsCpu, .connectivity])
|
|
XCTAssertEqual(snapshot.sections[0].values["model"], "SIMBox 8")
|
|
XCTAssertEqual(snapshot.sections[0].values["online"], "是")
|
|
XCTAssertEqual(snapshot.sections[1].values["phone_numbers"], "13800138000")
|
|
XCTAssertEqual(snapshot.sections[2].values["operator_name"], "中国移动")
|
|
XCTAssertEqual(snapshot.sections[3].values["uptime_uptime_seconds"], "86400")
|
|
XCTAssertEqual(snapshot.sections[4].values["core_count"], "4")
|
|
XCTAssertEqual(snapshot.sections[5].values["state"], "connected")
|
|
}
|
|
|
|
private static func instance() -> Instance {
|
|
Instance(
|
|
id: UUID(),
|
|
name: "Device Node",
|
|
origin: URL(string: "http://192.168.1.20:3000")!,
|
|
configRevision: 1,
|
|
createdAt: Date(),
|
|
updatedAt: Date()
|
|
)
|
|
}
|
|
}
|
|
|
|
private final class DeviceTestClient: SimAdminClientProtocol, @unchecked Sendable {
|
|
var routes: [UpstreamRoute] = []
|
|
|
|
func send(_ route: UpstreamRoute, baseURL: URL, cookie: String?) async throws -> UpstreamResponse {
|
|
routes.append(route)
|
|
guard cookie == "simadmin_session=token" else {
|
|
throw UpstreamError.unauthorized
|
|
}
|
|
let payload: [UpstreamRoute: String] = [
|
|
.device: #"{"data":{"model":"SIMBox 8","online":true}}"#,
|
|
.sim: #"{"data":{"phone_numbers":["13800138000"],"iccid":"123"}}"#,
|
|
.network: #"{"data":{"operator_name":"中国移动","registration_status":"registered"}}"#,
|
|
.stats: #"{"data":{"uptime":{"uptime_seconds":86400}}}"#,
|
|
.statsCpu: #"{"data":{"core_count":4,"model_name":"ARM CPU"}}"#,
|
|
.connectivity: #"{"data":{"state":"connected","latency_ms":21}}"#
|
|
]
|
|
guard let json = payload[route] else {
|
|
throw UpstreamError.notSupported
|
|
}
|
|
return UpstreamResponse(status: 200, data: Data(json.utf8))
|
|
}
|
|
}
|