Files
welshman/packages/domain/__tests__/RoomAddMember.test.ts
T
Jon Staab 22a666d497
tests / tests (push) Failing after 5m17s
Format
2026-06-22 10:23:15 -07:00

86 lines
2.4 KiB
TypeScript

import {describe, it, expect} from "vitest"
import {makeSecret, ROOM_ADD_MEMBER, NOTE, getTagValue} from "@welshman/util"
import type {TrustedEvent} from "@welshman/util"
import {Nip01Signer} from "@welshman/signer"
import {RoomAddMember, RoomAddMemberBuilder} from "../src/kinds/RoomAddMember"
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> = {}): TrustedEvent =>
({
id: "ff".repeat(32),
pubkey,
created_at: 0,
kind: ROOM_ADD_MEMBER,
tags: [],
content: "",
sig: "00".repeat(64),
...overrides,
}) as TrustedEvent
describe("RoomAddMember", () => {
it("reads pubkeys and group", async () => {
const op = await RoomAddMember.fromEvent(
makeEvent({
tags: [
["h", "room1"],
["p", a],
["p", b],
["alt", "x"],
],
}),
)
expect(op.kind).toBe(ROOM_ADD_MEMBER)
expect(op.group()).toBe("room1")
expect(op.pubkeys()).toEqual([a, b])
})
it("round-trips with no duplicated tags", async () => {
const op = await RoomAddMember.fromEvent(
makeEvent({
tags: [
["h", "room1"],
["p", a],
["p", b],
["alt", "x"],
],
}),
)
const tmpl = await op.builder().toTemplate(signer)
expect(tmpl.kind).toBe(ROOM_ADD_MEMBER)
// h round-trips via the base behavior tag.
expect(tmpl.tags.filter(t => t[0] === "h").length).toBe(1)
expect(tmpl.tags.filter(t => t[0] === "p").length).toBe(2)
expect(getTagValue("h", tmpl.tags)).toBe("room1")
expect(tmpl.tags).toContainEqual(["p", a])
expect(tmpl.tags).toContainEqual(["p", b])
// Unknown passthrough tag survives.
expect(tmpl.tags).toContainEqual(["alt", "x"])
})
it("builds from a fresh builder", async () => {
const tmpl = await new RoomAddMemberBuilder()
.setGroup("room2")
.addPubkey(a)
.addPubkey(a) // dedup
.addPubkey(b)
.toTemplate(signer)
expect(tmpl.kind).toBe(ROOM_ADD_MEMBER)
expect(getTagValue("h", tmpl.tags)).toBe("room2")
expect(tmpl.tags.filter(t => t[0] === "p").length).toBe(2)
expect(tmpl.tags).toContainEqual(["p", a])
expect(tmpl.tags).toContainEqual(["p", b])
})
it("throws on the wrong kind", async () => {
await expect(RoomAddMember.fromEvent(makeEvent({kind: NOTE}))).rejects.toThrow()
})
})