|
| 1 | +import assert from "node:assert/strict"; |
| 2 | +import test from "node:test"; |
| 3 | + |
| 4 | +import { |
| 5 | + agentBelongsToRelay, |
| 6 | + hasRunningAgentInCommunity, |
| 7 | + normalizeRelayUrlForCompare, |
| 8 | + partitionAgentsByRelay, |
| 9 | +} from "./agentRelayScope.ts"; |
| 10 | + |
| 11 | +const RELAY_A = "ws://relay-a.example.com:3000"; |
| 12 | +const RELAY_B = "wss://relay-b.example.com"; |
| 13 | + |
| 14 | +// ── normalizeRelayUrlForCompare ────────────────────────────────────────────── |
| 15 | +// Must agree with the Rust `normalize_relay_url` (desktop/src-tauri/src/relay.rs) |
| 16 | +// because record pins are stamped by the backend and compared here. These |
| 17 | +// vectors mirror the Rust unit tests. |
| 18 | + |
| 19 | +test("normalize_stripsWhitespaceAndTrailingSlashes", () => { |
| 20 | + assert.equal( |
| 21 | + normalizeRelayUrlForCompare(" wss://relay.example.com// "), |
| 22 | + "wss://relay.example.com", |
| 23 | + ); |
| 24 | +}); |
| 25 | + |
| 26 | +test("normalize_lowercasesSchemeAndAuthority_preservesPathCase", () => { |
| 27 | + assert.equal( |
| 28 | + normalizeRelayUrlForCompare("WSS://Relay.Example.COM:3000/Path"), |
| 29 | + "wss://relay.example.com:3000/Path", |
| 30 | + ); |
| 31 | +}); |
| 32 | + |
| 33 | +test("normalize_schemelessInput_passesThroughTrimmed", () => { |
| 34 | + assert.equal(normalizeRelayUrlForCompare("not-a-url/"), "not-a-url"); |
| 35 | +}); |
| 36 | + |
| 37 | +// ── agentBelongsToRelay ────────────────────────────────────────────────────── |
| 38 | + |
| 39 | +test("belongs_exactMatch", () => { |
| 40 | + assert.equal(agentBelongsToRelay(RELAY_A, RELAY_A), true); |
| 41 | +}); |
| 42 | + |
| 43 | +test("belongs_cosmeticDifferences_stillMatch", () => { |
| 44 | + // Trailing slash and scheme/host case must not split an agent from its |
| 45 | + // community — this is exactly what the shared normalizer exists for. |
| 46 | + assert.equal( |
| 47 | + agentBelongsToRelay("WS://RELAY-A.example.com:3000/", RELAY_A), |
| 48 | + true, |
| 49 | + ); |
| 50 | +}); |
| 51 | + |
| 52 | +test("belongs_differentRelay_doesNotMatch", () => { |
| 53 | + assert.equal(agentBelongsToRelay(RELAY_A, RELAY_B), false); |
| 54 | +}); |
| 55 | + |
| 56 | +test("belongs_blankPin_followsActiveCommunity", () => { |
| 57 | + // Defense-in-depth mirror of the backend's `effective_agent_relay_url` |
| 58 | + // blank fallback: a record that escaped stamping follows the visited |
| 59 | + // community instead of vanishing from every community. |
| 60 | + assert.equal(agentBelongsToRelay("", RELAY_A), true); |
| 61 | + assert.equal(agentBelongsToRelay(" ", RELAY_B), true); |
| 62 | + assert.equal(agentBelongsToRelay(undefined, RELAY_A), true); |
| 63 | +}); |
| 64 | + |
| 65 | +test("belongs_noActiveCommunityRelay_degradesToUnscoped", () => { |
| 66 | + assert.equal(agentBelongsToRelay(RELAY_A, null), true); |
| 67 | + assert.equal(agentBelongsToRelay(RELAY_A, ""), true); |
| 68 | + assert.equal(agentBelongsToRelay(RELAY_A, undefined), true); |
| 69 | +}); |
| 70 | + |
| 71 | +// ── partitionAgentsByRelay: the agent-list filter ──────────────────────────── |
| 72 | + |
| 73 | +test("partition_scopesListToActiveCommunity", () => { |
| 74 | + // Two communities' agents coexist after lazy activation; the list the |
| 75 | + // user sees in community A must contain only A's agents (plus blank-pin |
| 76 | + // strays), with B's surfaced only through the "other communities" count. |
| 77 | + const agents = [ |
| 78 | + { pubkey: "a1", relayUrl: RELAY_A }, |
| 79 | + { pubkey: "b1", relayUrl: RELAY_B }, |
| 80 | + { pubkey: "a2", relayUrl: `${RELAY_A}/` }, |
| 81 | + { pubkey: "stray", relayUrl: "" }, |
| 82 | + ]; |
| 83 | + |
| 84 | + const { inCommunity, other } = partitionAgentsByRelay(agents, RELAY_A); |
| 85 | + |
| 86 | + assert.deepEqual( |
| 87 | + inCommunity.map((agent) => agent.pubkey), |
| 88 | + ["a1", "a2", "stray"], |
| 89 | + ); |
| 90 | + assert.deepEqual( |
| 91 | + other.map((agent) => agent.pubkey), |
| 92 | + ["b1"], |
| 93 | + ); |
| 94 | +}); |
| 95 | + |
| 96 | +test("partition_undefinedAgents_yieldsEmpty", () => { |
| 97 | + const { inCommunity, other } = partitionAgentsByRelay(undefined, RELAY_A); |
| 98 | + assert.deepEqual(inCommunity, []); |
| 99 | + assert.deepEqual(other, []); |
| 100 | +}); |
| 101 | + |
| 102 | +// ── hasRunningAgentInCommunity: the polling gate ───────────────────────────── |
| 103 | + |
| 104 | +test("pollingGate_runningAgentInOtherCommunity_doesNotPoll", () => { |
| 105 | + // A workspace switch leaves the previous community's agents running; they |
| 106 | + // must not keep this community's 5s liveness poll alive. |
| 107 | + const agents = [ |
| 108 | + { relayUrl: RELAY_B, status: "running" }, |
| 109 | + { relayUrl: RELAY_A, status: "stopped" }, |
| 110 | + ]; |
| 111 | + |
| 112 | + assert.equal(hasRunningAgentInCommunity(agents, RELAY_A), false); |
| 113 | +}); |
| 114 | + |
| 115 | +test("pollingGate_runningAgentInThisCommunity_polls", () => { |
| 116 | + const agents = [ |
| 117 | + { relayUrl: RELAY_B, status: "running" }, |
| 118 | + { relayUrl: RELAY_A, status: "running" }, |
| 119 | + ]; |
| 120 | + |
| 121 | + assert.equal(hasRunningAgentInCommunity(agents, RELAY_A), true); |
| 122 | +}); |
| 123 | + |
| 124 | +test("pollingGate_blankPinRunningAgent_polls", () => { |
| 125 | + assert.equal( |
| 126 | + hasRunningAgentInCommunity([{ relayUrl: "", status: "running" }], RELAY_A), |
| 127 | + true, |
| 128 | + ); |
| 129 | +}); |
0 commit comments