import {describe, it, expect} from "vitest" import {makeSecret, RELAY_ADD_MEMBER, RELAY_REMOVE_MEMBER, NOTE} from "@welshman/util" import type {TrustedEvent} from "@welshman/util" import {Nip01Signer} from "@welshman/signer" import { RelayAddMember, RelayAddMemberBuilder, RelayRemoveMember, RelayRemoveMemberBuilder, } from "../src/kinds/RelayMembershipOp" const signer = new Nip01Signer(makeSecret()) const pubkey = "ee".repeat(32) const a = "aa".repeat(32) const b = "bb".repeat(32) const makeEvent = (overrides: Partial = {}): TrustedEvent => ({ id: "ff".repeat(32), pubkey, created_at: 0, kind: RELAY_ADD_MEMBER, tags: [], content: "", sig: "00".repeat(64), ...overrides, }) as TrustedEvent describe("RelayAddMember", () => { it("reads affected pubkeys, deduped", async () => { const op = await RelayAddMember.fromEvent(makeEvent({tags: [["p", a], ["p", b], ["p", a]]})) expect(op.pubkeys().sort()).toEqual([a, b].sort()) }) it("round-trips with no duplicate p tags and passthrough", async () => { const op = await RelayAddMember.fromEvent( makeEvent({tags: [["p", a], ["p", b], ["alt", "x"]]}), ) const tmpl = await op.builder().toTemplate(signer) expect(tmpl.kind).toBe(RELAY_ADD_MEMBER) expect(tmpl.tags.filter(t => t[0] === "p").length).toBe(2) expect(tmpl.tags).toContainEqual(["p", a]) expect(tmpl.tags).toContainEqual(["alt", "x"]) }) it("builds fresh with the right kind", async () => { const tmpl = await new RelayAddMemberBuilder().addPubkey(a).addPubkey(a).toTemplate(signer) expect(tmpl.kind).toBe(RELAY_ADD_MEMBER) expect(tmpl.tags.filter(t => t[0] === "p").length).toBe(1) expect(tmpl.tags).toContainEqual(["p", a]) }) it("throws on the wrong kind", async () => { await expect(RelayAddMember.fromEvent(makeEvent({kind: NOTE}))).rejects.toThrow() }) }) describe("RelayRemoveMember", () => { it("reads affected pubkeys with the remove kind", async () => { const op = await RelayRemoveMember.fromEvent( makeEvent({kind: RELAY_REMOVE_MEMBER, tags: [["p", a]]}), ) expect(op.kind).toBe(RELAY_REMOVE_MEMBER) expect(op.pubkeys()).toEqual([a]) }) it("builds fresh with the remove kind", async () => { const tmpl = await new RelayRemoveMemberBuilder().addPubkey(a).toTemplate(signer) expect(tmpl.kind).toBe(RELAY_REMOVE_MEMBER) expect(tmpl.tags).toContainEqual(["p", a]) }) it("throws when the add kind is read as a remove", async () => { await expect( RelayRemoveMember.fromEvent(makeEvent({kind: RELAY_ADD_MEMBER})), ).rejects.toThrow() }) })