import XCTest @testable import UCVLZonghengjia final class ClientConfigResponseTests: XCTestCase { func testActualBackendConfigNodeDoesNotRequireProfileOnlyStatus() throws { let data = Data(#"{"config_contract":2,"node":{"slug":"n1","name":"VPN001","region":"东京"},"route_mode":"smart","config":{}}"#.utf8) let response = try JSONDecoder().decode(ClientConfigResponse.self, from: data) XCTAssertEqual(response.node.slug, "n1") XCTAssertEqual(response.node.status, "unknown") } func testDecodesVersionedGatewayContract() throws { let data = Data(#"{"config_contract":2,"gateway":{"host":"vpn.example.invalid","ipv4":"203.0.113.17"},"node":{"slug":"n1","name":"VPN001","region":"东京","status":"online"},"route_mode":"smart","config":{}}"#.utf8) let response = try JSONDecoder().decode(ClientConfigResponse.self, from: data) XCTAssertEqual(response.configContract, 2) XCTAssertEqual(response.gateway?.host, "vpn.example.invalid") XCTAssertEqual(response.gateway?.ipv4, "203.0.113.17") } func testOldBackendPayloadRemainsCompatible() throws { let data = Data(#"{"node":{"slug":"n1","name":"VPN001","region":"东京","status":"online"},"route_mode":"smart","config":{}}"#.utf8) let response = try JSONDecoder().decode(ClientConfigResponse.self, from: data) XCTAssertNil(response.configContract) XCTAssertNil(response.gateway) XCTAssertNil(response.tailscaleEndpoint) XCTAssertNil(response.connectionScope) } func testDecodesOptionalInternalEndpointWithoutChangingPublicConfiguration() throws { let data = Data(#"{"config_contract":2,"connection_scope":"public","tailscale_endpoint":{"contract":1,"node_slug":"n1","address":"100.70.1.2","port":12001},"node":{"slug":"n1","name":"VPN001","region":"东京","status":"online"},"route_mode":"smart","config":{"public":"preserved"}}"#.utf8) let response = try JSONDecoder().decode(ClientConfigResponse.self, from: data) XCTAssertEqual(response.connectionScope, "public") XCTAssertEqual(response.tailscaleEndpoint?.nodeSlug, "n1") XCTAssertEqual(response.tailscaleEndpoint?.address, "100.70.1.2") XCTAssertEqual(response.tailscaleEndpoint?.port, 12001) XCTAssertEqual(response.config["public"], .string("preserved")) } func testMalformedOptionalEndpointDoesNotBreakPublicPayload() throws { for optionalValue in ["null", "true", "[]", "{}", #"{"contract":1,"node_slug":"n1","address":"100.70.1.2","port":"12001"}"#] { let data = Data(""" {"tailscale_endpoint":\(optionalValue),"node":{"slug":"n1","name":"VPN001","region":"东京","status":"online"},"route_mode":"smart","config":{"public":"preserved"}} """.utf8) let response = try JSONDecoder().decode(ClientConfigResponse.self, from: data) XCTAssertNil(response.tailscaleEndpoint) XCTAssertEqual(response.config["public"], .string("preserved")) } } func testMalformedOptionalScopeDisablesInternalEndpointAndPreservesPublicPayload() throws { for scope in ["true", "1", "[]", "{}"] { let data = Data(""" {"config_contract":2,"connection_scope":\(scope),"tailscale_endpoint":{"contract":1,"node_slug":"n1","address":"100.70.1.2","port":12001},"node":{"slug":"n1","name":"VPN001","region":"东京","status":"online"},"route_mode":"smart","config":{"public":"preserved"}} """.utf8) let response = try JSONDecoder().decode(ClientConfigResponse.self, from: data) XCTAssertNil(response.tailscaleEndpoint) XCTAssertEqual(response.config["public"], .string("preserved")) } } }