import { describe, expect, it, vi } from "vitest"; import { DEFAULT_RELAY_URL, buildRendezvousUrl, bytesToHex, deriveMeshRendezvousSecret, deriveRendezvousId, deriveRendezvousSecret, dialingRendezvousIds, generatePairingRendezvousToken, normalizeRelayUrl, rendezvousSecretFromHex, rendezvousWindowIndex, waitingRendezvousIds, } from "./rendezvous"; const HANDSHAKE_HASH = rendezvousSecretFromHex( "0fbab8cd1782bf4b17d69beb69f5b80b0bf24d93da4924959c76ee701c32e655", ); describe("relay rendezvous contract", () => { it("derives the symmetric cross-client mesh capability vector", () => { const root = Uint8Array.from({ length: 32 }, (_, i) => i); const low = Uint8Array.from({ length: 32 }, (_, i) => i + 32); const high = Uint8Array.from({ length: 32 }, (_, i) => i + 64); const forward = deriveMeshRendezvousSecret(root, low, high); const reverse = deriveMeshRendezvousSecret(root, high, low); expect(bytesToHex(forward)).toBe( "ad0359be7dc8fbb7df19ed027354b5029133a23a6023f43c2b3500b43addae7b", ); expect(reverse).toEqual(forward); }); it("derives the cross-platform secret and hourly id KATs", () => { const secret = deriveRendezvousSecret(HANDSHAKE_HASH); expect(bytesToHex(secret)).toBe( "0803691c8ac7330883c0987e33cea4a171992366ce4b06b88691e84ca689773d", ); expect(deriveRendezvousId(secret, 500_000)).toBe( "9UY3j5AHnwYSApvymtY965su9n60rQOv8QZ2PZzHuKI", ); }); it("uses one-hour windows with mandatory skew tolerance order", () => { const secret = deriveRendezvousSecret(HANDSHAKE_HASH); const now = 500_000 * 3600 + 3599; expect(rendezvousWindowIndex(now)).toBe(500_000); expect(waitingRendezvousIds(secret, now)).toEqual([ deriveRendezvousId(secret, 500_000), deriveRendezvousId(secret, 499_999), ]); expect(dialingRendezvousIds(secret, now)).toEqual([ deriveRendezvousId(secret, 500_000), deriveRendezvousId(secret, 499_999), deriveRendezvousId(secret, 500_001), ]); }); it("generates a fresh 128-bit base64url first-pair token", () => { vi.spyOn(crypto, "getRandomValues").mockImplementation((array) => { (array as Uint8Array).set(Array.from({ length: 16 }, (_, i) => i)); return array; }); expect(generatePairingRendezvousToken()).toBe("AAECAwQFBgcICQoLDA0ODw"); }); it("accepts only credential-free wss relay URLs", () => { expect(normalizeRelayUrl(`${DEFAULT_RELAY_URL}/`)).toBe(DEFAULT_RELAY_URL); expect(normalizeRelayUrl("wss://relay.example.test/base/")).toBe( "wss://relay.example.test/base", ); expect(() => normalizeRelayUrl("ws://relay.example.test")).toThrow(/wss/); expect(() => normalizeRelayUrl("wss://user:pass@relay.example.test")).toThrow(); expect(() => normalizeRelayUrl("wss://relay.example.test?token=x")).toThrow(); }); it("builds the relay path without exposing identity", () => { const id = "AAECAwQFBgcICQoLDA0ODw"; expect(buildRendezvousUrl(DEFAULT_RELAY_URL, id)).toBe( `${DEFAULT_RELAY_URL}/r/${id}`, ); expect(() => buildRendezvousUrl(DEFAULT_RELAY_URL, "short")).toThrow(); }); });