package com.sptmobile.endpoint import com.sptmobile.browse.WireEndpoint import com.sptmobile.browse.WireSubnet import org.junit.Assert.assertEquals import org.junit.Assert.assertNull import org.junit.Test class DigestRouterTest { private fun listing( hostNode: String, localIds: Set, subnets: List = emptyList(), ) = DigestRouter.HostListing(hostNode, localIds, subnets) // [unit->REQ-DIGEST-DIRECT-ROUTE] // Most-direct-route (ruling 4): the digest host is the FIRST host in // dial order whose `local` section carries the endpoint — a host that // merely SEES the endpoint in its subnet never serves its digest. @Test fun choose_picks_first_colocated_host_in_dial_order() { val listings = listOf( listing("host-a", localIds = setOf("other")), listing("host-b", localIds = setOf("flynn", "other")), listing("host-c", localIds = setOf("flynn")), ) assertEquals("host-b", DigestRouter.choose("flynn", listings)) } // [unit->REQ-DIGEST-DIRECT-ROUTE] @Test fun choose_is_null_when_no_host_is_colocated() { val seesButNotLocal = listing( "host-a", localIds = emptySet(), subnets = listOf( WireSubnet("SPT_DEV", listOf(WireEndpoint(id = "flynn", node = "n-remote"))) ), ) assertNull(DigestRouter.choose("flynn", listOf(seesButNotLocal))) } // [unit->REQ-ENDPOINT-INSTANCES] // The registry-card fallback carries the endpoint's Instance rows // (ruling 9: one row per node presence) unioned across hosts — same // (endpoint, node) reported by two hosts is ONE instance with both // hosts in `via`. @Test fun registry_card_unions_instances_across_hosts() { val subnetsA = listOf( WireSubnet( "SPT_DEV", listOf( WireEndpoint(id = "flynn", node = "n-1", status = "Active"), WireEndpoint(id = "other", node = "n-1", status = "Active"), ), ) ) val subnetsB = listOf( WireSubnet( "SPT_DEV", listOf( WireEndpoint(id = "flynn", node = "n-1", status = "Active"), WireEndpoint(id = "flynn", node = "n-2", status = "Suspended"), ), ) ) val card = DigestRouter.registryCard( "flynn", listOf( listing("host-a", emptySet(), subnetsA), listing("host-b", emptySet(), subnetsB), ), ) assertEquals(listOf("n-1", "n-2"), card.map { it.node }) assertEquals(listOf("host-a", "host-b"), card[0].via) assertEquals(listOf("host-b"), card[1].via) } }