/** * ConnectionRegistry — BILATERAL connection materialization (T03.h). * * A connection is live only when BOTH sides have asserted the other. A unilateral * (self-declared) assertion yields no neighbour — the defence against a reader who * fakes a connection to an owner to read that owner's protected documents. */ import { test, expect } from "bun:test"; import { ConnectionRegistry, bilateralConnections } from "../src/connections"; test("a UNILATERAL assertion yields NO neighbour", () => { const reg = new ConnectionRegistry(); reg.assert("mallory", "alice"); // mallory self-declares; alice never asserts back expect([...reg.neighbors("mallory")]).toEqual([]); expect([...reg.neighbors("alice")]).toEqual([]); }); test("a BILATERAL assertion (both sides) materializes the link", () => { const reg = new ConnectionRegistry(); reg.assert("alice", "bob"); reg.assert("bob", "alice"); expect([...reg.neighbors("alice")]).toEqual(["bob"]); expect([...reg.neighbors("bob")]).toEqual(["alice"]); }); test("mixed: only the reciprocated peers surface", () => { const reg = new ConnectionRegistry(); reg.assert("alice", "bob"); // reciprocated below reg.assert("bob", "alice"); reg.assert("alice", "carol"); // NOT reciprocated by carol reg.assert("dave", "alice"); // dave asserts alice, alice never asserts dave expect([...reg.neighbors("alice")].sort()).toEqual(["bob"]); }); test("self-assertion and empty are ignored", () => { const reg = new ConnectionRegistry(); reg.assert("alice", "alice"); reg.assert("", "bob"); reg.assert("alice", ""); expect([...reg.neighbors("alice")]).toEqual([]); }); test("bilateralConnections adapts to the Connections interface", () => { const reg = new ConnectionRegistry(); reg.assertAll([ { from: "alice", to: "bob" }, { from: "bob", to: "alice" }, ]); const conns = bilateralConnections(reg); expect([...conns.neighbors("alice")]).toEqual(["bob"]); expect([...conns.neighbors("carol")]).toEqual([]); }); test("clear() removes all assertions", () => { const reg = new ConnectionRegistry(); reg.assert("alice", "bob"); reg.assert("bob", "alice"); reg.clear(); expect([...reg.neighbors("alice")]).toEqual([]); });