Add tests
This commit is contained in:
@@ -0,0 +1,202 @@
|
||||
import {describe, it, expect} from "vitest"
|
||||
import {decode, naddrEncode} from "nostr-tools/nip19"
|
||||
import {Address, getAddress} from "../src/Address"
|
||||
|
||||
describe("Address", () => {
|
||||
const pub = "ee".repeat(32)
|
||||
const identifier = "identifier"
|
||||
|
||||
describe("constructor", () => {
|
||||
it("should create an Address instance with required properties", () => {
|
||||
const address = new Address(1, pub, identifier)
|
||||
|
||||
expect(address.kind).toBe(1)
|
||||
expect(address.pubkey).toBe(pub)
|
||||
expect(address.identifier).toBe(identifier)
|
||||
expect(address.relays).toEqual([])
|
||||
})
|
||||
|
||||
it("should create an Address instance with optional relays", () => {
|
||||
const relays = ["wss://relay1.com", "wss://relay2.com"]
|
||||
const address = new Address(1, pub, identifier, relays)
|
||||
|
||||
expect(address.relays).toEqual(relays)
|
||||
})
|
||||
})
|
||||
|
||||
describe("isAddress", () => {
|
||||
it("should return true for valid address strings", () => {
|
||||
expect(Address.isAddress(`1:${pub}:${identifier}`)).toBe(true)
|
||||
expect(Address.isAddress("30023:abc123:test")).toBe(true)
|
||||
expect(Address.isAddress("0:xyz789:")).toBe(true)
|
||||
})
|
||||
|
||||
it("should return false for invalid address strings", () => {
|
||||
expect(Address.isAddress("invalid")).toBe(false)
|
||||
expect(Address.isAddress(`1:${pub}`)).toBe(false)
|
||||
expect(Address.isAddress(`:${pub}:${identifier}`)).toBe(false)
|
||||
expect(Address.isAddress(`abc:${pub}:${identifier}`)).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("from", () => {
|
||||
it("should create an Address from a valid address string", () => {
|
||||
const address = Address.from(`1:${pub}:${identifier}`)
|
||||
|
||||
expect(address.kind).toBe(1)
|
||||
expect(address.pubkey).toBe(pub)
|
||||
expect(address.identifier).toBe(identifier)
|
||||
})
|
||||
|
||||
it("should handle address strings without identifier", () => {
|
||||
const address = Address.from(`1:${pub}:`)
|
||||
|
||||
expect(address.identifier).toBe("")
|
||||
})
|
||||
|
||||
it("should accept optional relays", () => {
|
||||
const relays = ["wss://relay1.com"]
|
||||
const address = Address.from(`1:${pub}:${identifier}`, relays)
|
||||
|
||||
expect(address.relays).toEqual(relays)
|
||||
})
|
||||
})
|
||||
|
||||
describe("fromNaddr", () => {
|
||||
it("should create an Address from a valid naddr", () => {
|
||||
// Create a valid naddr using nostr-tools encode
|
||||
const data = {
|
||||
type: "naddr",
|
||||
data: {
|
||||
kind: 1,
|
||||
pubkey: pub,
|
||||
identifier: identifier,
|
||||
relays: ["wss://relay1.com"],
|
||||
},
|
||||
}
|
||||
const naddr = naddrEncode(data.data)
|
||||
|
||||
const address = Address.fromNaddr(naddr)
|
||||
|
||||
expect(address.kind).toBe(1)
|
||||
expect(address.pubkey).toBe(pub)
|
||||
expect(address.identifier).toBe(identifier)
|
||||
expect(address.relays).toEqual(["wss://relay1.com"])
|
||||
})
|
||||
|
||||
it("should throw error for invalid naddr", () => {
|
||||
expect(() => Address.fromNaddr("invalid")).toThrow("Invalid naddr invalid")
|
||||
expect(() => Address.fromNaddr("nostr:123")).toThrow("Invalid naddr nostr:123")
|
||||
})
|
||||
})
|
||||
|
||||
describe("fromEvent", () => {
|
||||
it("should create an Address from an event with d tag", () => {
|
||||
const event = {
|
||||
kind: 1,
|
||||
pubkey: pub,
|
||||
tags: [["d", identifier]],
|
||||
}
|
||||
|
||||
const address = Address.fromEvent(event)
|
||||
|
||||
expect(address.kind).toBe(1)
|
||||
expect(address.pubkey).toBe(pub)
|
||||
expect(address.identifier).toBe(identifier)
|
||||
})
|
||||
|
||||
it("should create an Address from an event without d tag", () => {
|
||||
const event = {
|
||||
kind: 1,
|
||||
pubkey: pub,
|
||||
tags: [],
|
||||
}
|
||||
|
||||
const address = Address.fromEvent(event)
|
||||
|
||||
expect(address.identifier).toBe("")
|
||||
})
|
||||
|
||||
it("should accept optional relays", () => {
|
||||
const event = {
|
||||
kind: 1,
|
||||
pubkey: pub,
|
||||
tags: [["d", identifier]],
|
||||
}
|
||||
const relays = ["wss://relay1.com"]
|
||||
|
||||
const address = Address.fromEvent(event, relays)
|
||||
|
||||
expect(address.relays).toEqual(relays)
|
||||
})
|
||||
})
|
||||
|
||||
describe("toString", () => {
|
||||
it("should convert Address to string format", () => {
|
||||
const address = new Address(1, pub, identifier)
|
||||
|
||||
expect(address.toString()).toBe(`1:${pub}:${identifier}`)
|
||||
})
|
||||
|
||||
it("should handle empty identifier", () => {
|
||||
const address = new Address(1, pub, "")
|
||||
|
||||
expect(address.toString()).toBe(`1:${pub}:`)
|
||||
})
|
||||
})
|
||||
|
||||
describe("toNaddr", () => {
|
||||
it("should convert Address to naddr format", () => {
|
||||
const address = new Address(1, pub, identifier, ["wss://relay1.com"])
|
||||
|
||||
const naddr = address.toNaddr()
|
||||
|
||||
// Decode the naddr to verify its contents
|
||||
const decoded = decode(naddr)
|
||||
expect(decoded.type).toBe("naddr")
|
||||
expect(decoded.data.kind).toBe(1)
|
||||
expect(decoded.data.pubkey).toBe(pub)
|
||||
expect(decoded.data.identifier).toBe(identifier)
|
||||
expect(decoded.data.relays).toEqual(["wss://relay1.com"])
|
||||
})
|
||||
})
|
||||
|
||||
describe("getAddress utility", () => {
|
||||
it("should get address string from event", () => {
|
||||
const event = {
|
||||
kind: 1,
|
||||
pubkey: pub,
|
||||
tags: [["d", identifier]],
|
||||
}
|
||||
|
||||
expect(getAddress(event)).toBe(`1:${pub}:${identifier}`)
|
||||
})
|
||||
|
||||
it("should handle event without d tag", () => {
|
||||
const event = {
|
||||
kind: 1,
|
||||
pubkey: pub,
|
||||
tags: [],
|
||||
}
|
||||
|
||||
expect(getAddress(event)).toBe(`1:${pub}:`)
|
||||
})
|
||||
})
|
||||
|
||||
describe("edge cases", () => {
|
||||
it("should handle numeric pubkeys", () => {
|
||||
const address = Address.from("1:123:test")
|
||||
expect(address.pubkey).toBe("123")
|
||||
})
|
||||
|
||||
it("should handle special characters in identifier", () => {
|
||||
const address = Address.from("1:abc:test-123_456")
|
||||
expect(address.identifier).toBe("test-123_456")
|
||||
})
|
||||
|
||||
it("should handle zero kind", () => {
|
||||
const address = Address.from("0:abc:test")
|
||||
expect(address.kind).toBe(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,223 @@
|
||||
import {MUTES} from "@welshman/util"
|
||||
import {now} from "@welshman/lib"
|
||||
import {describe, it, expect, vi, beforeEach} from "vitest"
|
||||
import {Encryptable, asDecryptedEvent} from "../src/Encryptable"
|
||||
import type {OwnedEvent, TrustedEvent} from "../src/Events"
|
||||
|
||||
describe("Encryptable", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
// Mock encryption function
|
||||
const mockEncrypt = vi.fn(async (text: string) => `encrypted:${text}`)
|
||||
|
||||
// Realistic Nostr values
|
||||
const pub = "ee".repeat(32)
|
||||
const currentTime = now()
|
||||
|
||||
describe("constructor", () => {
|
||||
it("should create an instance with minimal event template", () => {
|
||||
const event: Partial<OwnedEvent> = {
|
||||
kind: MUTES,
|
||||
pubkey: pub,
|
||||
created_at: currentTime,
|
||||
}
|
||||
const encryptable = new Encryptable(event, {})
|
||||
|
||||
expect(encryptable.event).toBe(event)
|
||||
expect(encryptable.updates).toEqual({})
|
||||
})
|
||||
|
||||
it("should create an instance with full event template", () => {
|
||||
const event: OwnedEvent = {
|
||||
kind: MUTES,
|
||||
pubkey: pub,
|
||||
created_at: currentTime,
|
||||
content: "original encrypted content",
|
||||
tags: [["p", pub]],
|
||||
}
|
||||
const updates = {
|
||||
content: JSON.stringify({list: ["item1", "item2"]}),
|
||||
tags: [["p", pub, "wss://relay.example.com"]],
|
||||
}
|
||||
const encryptable = new Encryptable(event, updates)
|
||||
|
||||
expect(encryptable.event).toBe(event)
|
||||
expect(encryptable.updates).toBe(updates)
|
||||
})
|
||||
})
|
||||
|
||||
describe("reconcile", () => {
|
||||
it("should encrypt content updates", async () => {
|
||||
const event: Partial<OwnedEvent> = {
|
||||
kind: MUTES,
|
||||
pubkey: pub,
|
||||
created_at: currentTime,
|
||||
}
|
||||
const updates = {
|
||||
content: JSON.stringify({muted: [pub]}),
|
||||
}
|
||||
const encryptable = new Encryptable(event, updates)
|
||||
|
||||
const result = await encryptable.reconcile(mockEncrypt)
|
||||
|
||||
expect(result.content).toBe(`encrypted:${updates.content}`)
|
||||
expect(mockEncrypt).toHaveBeenCalledWith(updates.content)
|
||||
})
|
||||
|
||||
it("should encrypt tag updates", async () => {
|
||||
const event: Partial<OwnedEvent> = {
|
||||
kind: MUTES,
|
||||
pubkey: pub,
|
||||
created_at: currentTime,
|
||||
}
|
||||
const updates = {
|
||||
tags: [["p", pub, "wss://relay.example.com"]],
|
||||
}
|
||||
const encryptable = new Encryptable(event, updates)
|
||||
|
||||
const result = await encryptable.reconcile(mockEncrypt)
|
||||
|
||||
expect(result.tags[0][1]).toBe(`encrypted:${pub}`)
|
||||
expect(mockEncrypt).toHaveBeenCalledWith(pub)
|
||||
})
|
||||
|
||||
it("should handle both content and tag updates", async () => {
|
||||
const event: Partial<OwnedEvent> = {
|
||||
kind: MUTES,
|
||||
pubkey: pub,
|
||||
created_at: currentTime,
|
||||
}
|
||||
const updates = {
|
||||
content: JSON.stringify({muted: [pub]}),
|
||||
tags: [["p", pub, "wss://relay.example.com"]],
|
||||
}
|
||||
const encryptable = new Encryptable(event, updates)
|
||||
|
||||
const result = await encryptable.reconcile(mockEncrypt)
|
||||
|
||||
expect(result.content).toBe(`encrypted:${updates.content}`)
|
||||
expect(result.tags[0][1]).toBe(`encrypted:${pub}`)
|
||||
expect(mockEncrypt).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
|
||||
it("should preserve original content when no updates", async () => {
|
||||
const event: OwnedEvent = {
|
||||
kind: MUTES,
|
||||
pubkey: pub,
|
||||
created_at: currentTime,
|
||||
content: JSON.stringify({originalList: [pub]}),
|
||||
tags: [],
|
||||
}
|
||||
const encryptable = new Encryptable(event, {})
|
||||
|
||||
const result = await encryptable.reconcile(mockEncrypt)
|
||||
|
||||
expect(result.content).toBe(event.content)
|
||||
expect(mockEncrypt).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it("should preserve original tags when no updates", async () => {
|
||||
const event: OwnedEvent = {
|
||||
kind: MUTES,
|
||||
pubkey: pub,
|
||||
created_at: currentTime,
|
||||
content: "",
|
||||
tags: [["p", pub, "wss://relay.example.com"]],
|
||||
}
|
||||
const encryptable = new Encryptable(event, {})
|
||||
|
||||
const result = await encryptable.reconcile(mockEncrypt)
|
||||
|
||||
expect(result.tags).toEqual(event.tags)
|
||||
expect(mockEncrypt).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe("asDecryptedEvent", () => {
|
||||
it("should create a decrypted event with plaintext", () => {
|
||||
const event: TrustedEvent = {
|
||||
id: "ff".repeat(32),
|
||||
sig: "00".repeat(64),
|
||||
kind: MUTES,
|
||||
pubkey: pub,
|
||||
created_at: currentTime,
|
||||
content: "encrypted content",
|
||||
tags: [],
|
||||
}
|
||||
const plaintext = {
|
||||
content: JSON.stringify({muted: [pub]}),
|
||||
tags: [["p", pub, "wss://relay.example.com"]],
|
||||
}
|
||||
|
||||
const result = asDecryptedEvent(event, plaintext)
|
||||
|
||||
expect(result).toEqual({
|
||||
...event,
|
||||
plaintext,
|
||||
})
|
||||
})
|
||||
|
||||
it("should handle empty plaintext", () => {
|
||||
const event: TrustedEvent = {
|
||||
id: "ff".repeat(32),
|
||||
sig: "00".repeat(64),
|
||||
kind: MUTES,
|
||||
pubkey: pub,
|
||||
created_at: currentTime,
|
||||
content: "encrypted content",
|
||||
tags: [],
|
||||
}
|
||||
|
||||
const result = asDecryptedEvent(event)
|
||||
|
||||
expect(result).toEqual({
|
||||
...event,
|
||||
plaintext: {},
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("error handling", () => {
|
||||
it("should handle encryption failures", async () => {
|
||||
const failingEncrypt = async () => {
|
||||
throw new Error("Encryption failed")
|
||||
}
|
||||
const event: Partial<OwnedEvent> = {
|
||||
kind: MUTES,
|
||||
pubkey: pub,
|
||||
created_at: currentTime,
|
||||
}
|
||||
const updates = {
|
||||
content: JSON.stringify({muted: [pub]}),
|
||||
}
|
||||
const encryptable = new Encryptable(event, updates)
|
||||
|
||||
await expect(encryptable.reconcile(failingEncrypt)).rejects.toThrow("Encryption failed")
|
||||
})
|
||||
|
||||
it("should handle partial encryption failures", async () => {
|
||||
let callCount = 0
|
||||
const partialFailingEncrypt = async () => {
|
||||
callCount++
|
||||
if (callCount > 1) throw new Error("Encryption failed")
|
||||
return "encrypted:success"
|
||||
}
|
||||
|
||||
const event: Partial<OwnedEvent> = {
|
||||
kind: MUTES,
|
||||
pubkey: pub,
|
||||
created_at: currentTime,
|
||||
}
|
||||
const updates = {
|
||||
content: JSON.stringify({muted: [pub]}),
|
||||
tags: [["p", pub]],
|
||||
}
|
||||
const encryptable = new Encryptable(event, updates)
|
||||
|
||||
await expect(encryptable.reconcile(partialFailingEncrypt)).rejects.toThrow(
|
||||
"Encryption failed",
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,286 @@
|
||||
import {now} from "@welshman/lib"
|
||||
import {describe, it, expect} from "vitest"
|
||||
import {verifiedSymbol} from "nostr-tools/pure"
|
||||
import * as Events from "../src/Events"
|
||||
import {COMMENT} from "../src/Kinds"
|
||||
|
||||
describe("Events", () => {
|
||||
// Realistic Nostr data
|
||||
const pubkey = "ee".repeat(32)
|
||||
const sig = "ee".repeat(64)
|
||||
const id = "ff".repeat(32)
|
||||
const currentTime = now()
|
||||
|
||||
const createBaseEvent = () => ({
|
||||
kind: 1,
|
||||
content: "Hello Nostr!",
|
||||
tags: [["p", pubkey]],
|
||||
})
|
||||
|
||||
const createStampedEvent = () => ({
|
||||
...createBaseEvent(),
|
||||
created_at: currentTime,
|
||||
})
|
||||
|
||||
const createOwnedEvent = () => ({
|
||||
...createStampedEvent(),
|
||||
pubkey: pubkey,
|
||||
})
|
||||
|
||||
const createHashedEvent = () => ({
|
||||
...createOwnedEvent(),
|
||||
id: id,
|
||||
})
|
||||
|
||||
const createSignedEvent = () => ({
|
||||
...createHashedEvent(),
|
||||
sig: sig,
|
||||
})
|
||||
|
||||
const createCommentEvent = (parentId: string) => ({
|
||||
...createHashedEvent(),
|
||||
kind: COMMENT,
|
||||
tags: [
|
||||
["E", parentId, "", "root"],
|
||||
["P", pubkey],
|
||||
],
|
||||
})
|
||||
|
||||
const createReplyEvent = (parentId: string) => ({
|
||||
...createHashedEvent(),
|
||||
kind: 1,
|
||||
tags: [
|
||||
["e", parentId, "", "root"],
|
||||
["e", parentId, "", "reply"],
|
||||
["p", pubkey, "", "root"],
|
||||
["p", pubkey, "", "reply"],
|
||||
],
|
||||
})
|
||||
|
||||
describe("createEvent", () => {
|
||||
it("should create event with defaults", () => {
|
||||
const event = Events.createEvent(1, {})
|
||||
expect(event.kind).toBe(1)
|
||||
expect(event.content).toBe("")
|
||||
expect(event.tags).toEqual([])
|
||||
expect(event.created_at).toBeLessThanOrEqual(now())
|
||||
})
|
||||
|
||||
it("should create event with provided values", () => {
|
||||
const event = Events.createEvent(1, {
|
||||
content: "Hello Nostr!",
|
||||
tags: [["p", pubkey]],
|
||||
created_at: currentTime,
|
||||
})
|
||||
expect(event).toEqual(createStampedEvent())
|
||||
})
|
||||
})
|
||||
|
||||
describe("type guards", () => {
|
||||
it("should validate EventTemplate", () => {
|
||||
expect(Events.isEventTemplate(createBaseEvent())).toBe(true)
|
||||
expect(Events.isEventTemplate({kind: 1} as Events.EventTemplate)).toBe(false)
|
||||
})
|
||||
|
||||
it("should validate StampedEvent", () => {
|
||||
expect(Events.isStampedEvent(createStampedEvent())).toBe(true)
|
||||
expect(Events.isStampedEvent(createBaseEvent() as Events.StampedEvent)).toBe(false)
|
||||
})
|
||||
|
||||
it("should validate OwnedEvent", () => {
|
||||
expect(Events.isOwnedEvent(createOwnedEvent())).toBe(true)
|
||||
expect(Events.isOwnedEvent(createStampedEvent() as Events.OwnedEvent)).toBe(false)
|
||||
})
|
||||
|
||||
it("should validate HashedEvent", () => {
|
||||
expect(Events.isHashedEvent(createHashedEvent())).toBe(true)
|
||||
expect(Events.isHashedEvent(createOwnedEvent() as Events.HashedEvent)).toBe(false)
|
||||
})
|
||||
|
||||
it("should validate SignedEvent", () => {
|
||||
expect(Events.isSignedEvent(createSignedEvent())).toBe(true)
|
||||
expect(Events.isSignedEvent(createHashedEvent())).toBe(false)
|
||||
})
|
||||
|
||||
it("should validate TrustedEvent", () => {
|
||||
const unwrapped = {
|
||||
...createHashedEvent(),
|
||||
wrap: createSignedEvent(),
|
||||
}
|
||||
expect(Events.isTrustedEvent(createHashedEvent())).toBe(false)
|
||||
expect(Events.isTrustedEvent(createSignedEvent())).toBe(true)
|
||||
expect(Events.isTrustedEvent(unwrapped)).toBe(true)
|
||||
})
|
||||
|
||||
it("should validate UnwrappedEvent", () => {
|
||||
const unwrapped = {
|
||||
...createHashedEvent(),
|
||||
wrap: createSignedEvent(),
|
||||
}
|
||||
expect(Events.isUnwrappedEvent(unwrapped)).toBe(true)
|
||||
expect(Events.isUnwrappedEvent(createHashedEvent())).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("event conversion", () => {
|
||||
it("should convert to EventTemplate", () => {
|
||||
const result = Events.asEventTemplate(createSignedEvent())
|
||||
expect(result).toHaveProperty("kind")
|
||||
expect(result).toHaveProperty("tags")
|
||||
expect(result).toHaveProperty("content")
|
||||
expect(result).not.toHaveProperty("created_at")
|
||||
})
|
||||
|
||||
it("should convert to StampedEvent", () => {
|
||||
const result = Events.asStampedEvent(createSignedEvent())
|
||||
expect(result).toHaveProperty("created_at")
|
||||
expect(result).not.toHaveProperty("pubkey")
|
||||
})
|
||||
|
||||
it("should convert to OwnedEvent", () => {
|
||||
const result = Events.asOwnedEvent(createSignedEvent())
|
||||
expect(result).not.toHaveProperty("sig")
|
||||
expect(result).not.toHaveProperty("id")
|
||||
})
|
||||
|
||||
it("should convert to HashedEvent", () => {
|
||||
const result = Events.asHashedEvent(createSignedEvent())
|
||||
expect(result).not.toHaveProperty("sig")
|
||||
})
|
||||
|
||||
it("should convert to SignedEvent", () => {
|
||||
const trustedEvent = {
|
||||
...createHashedEvent(),
|
||||
sig: sig,
|
||||
wrap: createSignedEvent(),
|
||||
}
|
||||
const result = Events.asSignedEvent(trustedEvent)
|
||||
expect(result).not.toHaveProperty("wrap")
|
||||
expect(result).toHaveProperty("sig")
|
||||
})
|
||||
|
||||
it("should convert to UnwrappedEvent", () => {
|
||||
const trustedEvent = {
|
||||
...createHashedEvent(),
|
||||
sig: sig,
|
||||
wrap: createSignedEvent(),
|
||||
}
|
||||
const result = Events.asUnwrappedEvent(trustedEvent)
|
||||
expect(result).toHaveProperty("wrap")
|
||||
expect(result).not.toHaveProperty("sig")
|
||||
})
|
||||
|
||||
it("should convert to TrustedEvent", () => {
|
||||
const trustedEvent = {
|
||||
...createHashedEvent(),
|
||||
sig: sig,
|
||||
wrap: createSignedEvent(),
|
||||
}
|
||||
const result = Events.asTrustedEvent(trustedEvent)
|
||||
expect(result).toHaveProperty("sig")
|
||||
expect(result).toHaveProperty("wrap")
|
||||
})
|
||||
})
|
||||
|
||||
describe("signature validation", () => {
|
||||
it("should validate signature using verifiedSymbol", () => {
|
||||
let event = createSignedEvent() as Events.SignedEvent
|
||||
event[verifiedSymbol] = true
|
||||
expect(Events.hasValidSignature(event)).toBe(true)
|
||||
|
||||
// Clear verifiedSymbol and use verify the actual signature
|
||||
delete event[verifiedSymbol]
|
||||
// the signature is invalid, but the sig validity is not checked here
|
||||
expect(Events.hasValidSignature(event)).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe("event identifiers", () => {
|
||||
it("should get identifier from d tag", () => {
|
||||
const event = {
|
||||
...createBaseEvent(),
|
||||
tags: [["d", "test-identifier"]],
|
||||
}
|
||||
expect(Events.getIdentifier(event)).toBe("test-identifier")
|
||||
})
|
||||
|
||||
it("should get address for replaceable events", () => {
|
||||
const event = {
|
||||
...createHashedEvent(),
|
||||
kind: 10000, // replaceable kind
|
||||
}
|
||||
expect(Events.getIdOrAddress(event)).toMatch(/^10000:/)
|
||||
})
|
||||
})
|
||||
|
||||
describe("event relationships", () => {
|
||||
it("should identify parent-child relationships", () => {
|
||||
const parent = createHashedEvent()
|
||||
const child = createCommentEvent(parent.id)
|
||||
expect(Events.isChildOf(child, parent)).toBe(true)
|
||||
})
|
||||
|
||||
it("should get parent IDs", () => {
|
||||
const parentId = id
|
||||
const event = createCommentEvent(parentId)
|
||||
expect(Events.getParentIds(event)).toContain(parentId)
|
||||
})
|
||||
|
||||
it("should get parent addresses", () => {
|
||||
const event = {
|
||||
...createCommentEvent(id),
|
||||
tags: [["e", "30023:pubkey:identifier", "", "root"]],
|
||||
}
|
||||
expect(Events.getParentAddrs(event)[0]).toMatch(/^\d+:/)
|
||||
})
|
||||
})
|
||||
|
||||
describe("event type checks", () => {
|
||||
it("should identify ephemeral events", () => {
|
||||
const event = {
|
||||
...createBaseEvent(),
|
||||
kind: 20000, // ephemeral kind
|
||||
}
|
||||
expect(Events.isEphemeral(event)).toBe(true)
|
||||
})
|
||||
|
||||
it("should identify replaceable events", () => {
|
||||
const event = {
|
||||
...createBaseEvent(),
|
||||
kind: 10000, // replaceable kind
|
||||
}
|
||||
expect(Events.isReplaceable(event)).toBe(true)
|
||||
})
|
||||
|
||||
it("should identify parameterized replaceable events", () => {
|
||||
const event = {
|
||||
...createBaseEvent(),
|
||||
kind: 30000, // parameterized replaceable kind
|
||||
}
|
||||
expect(Events.isParameterizedReplaceable(event)).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe("ancestor handling", () => {
|
||||
it("should get ancestors for comments", () => {
|
||||
const parentId = id
|
||||
const event = createCommentEvent(parentId)
|
||||
const ancestors = Events.getAncestors(event)
|
||||
expect(ancestors.roots).toContain(parentId)
|
||||
})
|
||||
|
||||
it("should get ancestors for replies", () => {
|
||||
const parentId = id
|
||||
const event = createReplyEvent(parentId)
|
||||
const ancestors = Events.getAncestors(event)
|
||||
expect(ancestors.roots).toContain(parentId)
|
||||
})
|
||||
|
||||
it("should handle events without ancestors", () => {
|
||||
const event = createBaseEvent()
|
||||
const ancestors = Events.getAncestors(event)
|
||||
expect(ancestors.roots).toEqual([])
|
||||
expect(ancestors.replies).toEqual([])
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,223 @@
|
||||
import {describe, it, vi, expect, beforeEach} from "vitest"
|
||||
import * as Filters from "../src/Filters"
|
||||
|
||||
import type {TrustedEvent} from "../src/Events"
|
||||
import {GENERIC_REPOST, LONG_FORM, MUTES, REPOST} from "@welshman/util"
|
||||
|
||||
describe("Filters", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
const pubkey = "000000789abcdef0000000789abcdef0000000789abcdef0000000789abcdef"
|
||||
const id = "ff".repeat(32)
|
||||
const currentTime = Math.floor(Date.now() / 1000)
|
||||
|
||||
const createEvent = (overrides = {}): TrustedEvent => ({
|
||||
id: id,
|
||||
pubkey: pubkey,
|
||||
created_at: currentTime,
|
||||
kind: 1,
|
||||
tags: [],
|
||||
content: "Hello Nostr!",
|
||||
...overrides,
|
||||
})
|
||||
|
||||
describe("matchFilter", () => {
|
||||
it("should match basic filter criteria", () => {
|
||||
const event = createEvent()
|
||||
const filter = {kinds: [1], authors: [pubkey]}
|
||||
expect(Filters.matchFilter(filter, event)).toBe(true)
|
||||
})
|
||||
|
||||
it("should handle search terms", () => {
|
||||
const event = createEvent({content: "Hello Nostr World!"})
|
||||
expect(Filters.matchFilter({search: "nostr"}, event)).toBe(true)
|
||||
expect(Filters.matchFilter({search: "bitcoin"}, event)).toBe(false)
|
||||
})
|
||||
|
||||
it("should handle multiple search terms", () => {
|
||||
const event = createEvent({content: "Hello Nostr World!"})
|
||||
expect(Filters.matchFilter({search: "hello world"}, event)).toBe(true)
|
||||
})
|
||||
|
||||
it("should handle case-insensitive search", () => {
|
||||
const event = createEvent({content: "Hello NOSTR World!"})
|
||||
expect(Filters.matchFilter({search: "nostr"}, event)).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe("matchFilters", () => {
|
||||
it("should match if any filter matches", () => {
|
||||
const event = createEvent()
|
||||
const filters = [{kinds: [2]}, {kinds: [1], authors: [pubkey]}]
|
||||
expect(Filters.matchFilters(filters, event)).toBe(true)
|
||||
})
|
||||
|
||||
it("should not match if no filters match", () => {
|
||||
const event = createEvent()
|
||||
const filters = [{kinds: [2]}, {kinds: [3]}]
|
||||
expect(Filters.matchFilters(filters, event)).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("getFilterId", () => {
|
||||
it("should generate consistent IDs for equivalent filters", () => {
|
||||
const filter1 = {kinds: [1], authors: [pubkey]}
|
||||
const filter2 = {authors: [pubkey], kinds: [1]}
|
||||
expect(Filters.getFilterId(filter1)).toBe(Filters.getFilterId(filter2))
|
||||
})
|
||||
|
||||
it("should generate different IDs for different filters", () => {
|
||||
const filter1 = {kinds: [1], authors: [pubkey]}
|
||||
const filter2 = {kinds: [2], authors: [pubkey]}
|
||||
expect(Filters.getFilterId(filter1)).not.toBe(Filters.getFilterId(filter2))
|
||||
})
|
||||
})
|
||||
|
||||
describe("unionFilters", () => {
|
||||
it("should combine similar filters", () => {
|
||||
const filters = [
|
||||
{kinds: [1], authors: [pubkey]},
|
||||
{kinds: [1], authors: [pubkey + "1"]},
|
||||
]
|
||||
const result = Filters.unionFilters(filters)
|
||||
expect(result).toHaveLength(1)
|
||||
expect(result[0].authors).toHaveLength(2)
|
||||
})
|
||||
|
||||
it("should handle different filter groups", () => {
|
||||
const filters = [{kinds: [1]}, {"#e": [id]}]
|
||||
const result = Filters.unionFilters(filters)
|
||||
expect(result).toHaveLength(2)
|
||||
})
|
||||
|
||||
it("should preserve limit, since, until, and search", () => {
|
||||
const filters = [
|
||||
{kinds: [1], limit: 10, since: 1000, until: 2000, search: "test"},
|
||||
{kinds: [1], limit: 10, since: 1000, until: 2000, search: "test"},
|
||||
]
|
||||
const result = Filters.unionFilters(filters)
|
||||
expect(result).toHaveLength(1)
|
||||
expect(result[0]).toMatchObject({limit: 10, since: 1000, until: 2000, search: "test"})
|
||||
})
|
||||
})
|
||||
|
||||
describe("intersectFilters", () => {
|
||||
it("should combine filter groups", () => {
|
||||
const groups = [[{kinds: [1]}], [{authors: [pubkey]}]]
|
||||
const result = Filters.intersectFilters(groups)
|
||||
expect(result).toHaveLength(1)
|
||||
expect(result[0]).toMatchObject({
|
||||
kinds: [1],
|
||||
authors: [pubkey],
|
||||
})
|
||||
})
|
||||
|
||||
it("should handle since, until, and limit", () => {
|
||||
const groups = [
|
||||
[{since: 1000, until: 2000, limit: 10}],
|
||||
[{since: 1500, until: 1800, limit: 20}],
|
||||
]
|
||||
const result = Filters.intersectFilters(groups)
|
||||
expect(result[0]).toMatchObject({
|
||||
since: 1500, // Max of since
|
||||
until: 1800, // Min of until
|
||||
limit: 20, // Max of limit
|
||||
})
|
||||
})
|
||||
|
||||
it("should combine search terms", () => {
|
||||
const groups = [[{search: "hello"}], [{search: "world"}]]
|
||||
const result = Filters.intersectFilters(groups)
|
||||
expect(result[0].search).toBe("hello world")
|
||||
})
|
||||
})
|
||||
|
||||
describe("getIdFilters", () => {
|
||||
it("should handle plain IDs", () => {
|
||||
const result = Filters.getIdFilters([id])
|
||||
expect(result[0].ids).toContain(id)
|
||||
})
|
||||
|
||||
it("should handle addresses", () => {
|
||||
const addr = `1:${pubkey}:test`
|
||||
const result = Filters.getIdFilters([addr])
|
||||
expect(result[0]).toMatchObject({
|
||||
kinds: [1],
|
||||
authors: [pubkey],
|
||||
"#d": ["test"],
|
||||
})
|
||||
})
|
||||
|
||||
it("should handle mixed IDs and addresses", () => {
|
||||
const addr = `1:${pubkey}:test`
|
||||
const result = Filters.getIdFilters([id, addr])
|
||||
expect(result).toHaveLength(2)
|
||||
})
|
||||
})
|
||||
|
||||
describe("getReplyFilters", () => {
|
||||
it("should create filters for regular events", () => {
|
||||
const event = createEvent()
|
||||
const result = Filters.getReplyFilters([event])
|
||||
expect((result[0] as any)["#e"]).toContain(event.id)
|
||||
})
|
||||
|
||||
it("should handle replaceable events", () => {
|
||||
const event = createEvent({kind: MUTES})
|
||||
const result = Filters.getReplyFilters([event])
|
||||
expect((result[0] as any)["#a"]).toBeDefined()
|
||||
})
|
||||
|
||||
it("should handle wrapped events", () => {
|
||||
const event = createEvent({
|
||||
wrap: createEvent(),
|
||||
})
|
||||
const result = Filters.getReplyFilters([event])
|
||||
expect((result[0] as any)["#e"]).toHaveLength(2)
|
||||
})
|
||||
})
|
||||
|
||||
describe("addRepostFilters", () => {
|
||||
it("should add repost kinds for kind 1", () => {
|
||||
const result = Filters.addRepostFilters([{kinds: [1]}])
|
||||
expect(result).toHaveLength(2)
|
||||
expect(result[1].kinds).toContain(REPOST)
|
||||
})
|
||||
|
||||
it("should handle other kinds", () => {
|
||||
const result = Filters.addRepostFilters([{kinds: [LONG_FORM]}])
|
||||
expect(result[1].kinds).toContain(GENERIC_REPOST)
|
||||
expect(result[1].kinds).not.toContain(REPOST)
|
||||
expect(result[1]["#k"]).toContain(LONG_FORM.toString())
|
||||
})
|
||||
})
|
||||
|
||||
describe("filter utilities", () => {
|
||||
it("should calculate filter generality", () => {
|
||||
expect(Filters.getFilterGenerality({ids: [id]})).toBe(0)
|
||||
expect(Filters.getFilterGenerality({authors: [pubkey], "#p": [pubkey]})).toBe(0.2)
|
||||
expect(Filters.getFilterGenerality({authors: [pubkey, pubkey, pubkey], kinds: [1]})).toBe(
|
||||
0.01,
|
||||
)
|
||||
expect(Filters.getFilterGenerality({kinds: [1]})).toBe(1)
|
||||
})
|
||||
|
||||
it("should guess filter delta", () => {
|
||||
const result = Filters.guessFilterDelta([{ids: [id]}])
|
||||
expect(result).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
it("should get filter result cardinality", () => {
|
||||
expect(Filters.getFilterResultCardinality({ids: [id, id + "1"]})).toBe(2)
|
||||
expect(Filters.getFilterResultCardinality({kinds: [1]})).toBeNull()
|
||||
})
|
||||
|
||||
it("should trim large filters", () => {
|
||||
const largeFilter = {authors: Array(2000).fill(pubkey)}
|
||||
const result = Filters.trimFilter(largeFilter)
|
||||
expect(result.authors?.length).toBe(1000)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,197 @@
|
||||
import {now} from "@welshman/lib"
|
||||
import {HANDLER_INFORMATION} from "@welshman/util"
|
||||
import {describe, it, vi, expect, beforeEach} from "vitest"
|
||||
import {readHandlers, getHandlerKey, displayHandler, getHandlerAddress} from "../src/Handler"
|
||||
import type {TrustedEvent} from "../src/Events"
|
||||
|
||||
describe("Handler", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
const pubkey = "ee".repeat(32)
|
||||
const id = "ff".repeat(32)
|
||||
const currentTime = now()
|
||||
|
||||
const createHandlerEvent = (overrides = {}): TrustedEvent => ({
|
||||
id: id,
|
||||
pubkey: pubkey,
|
||||
created_at: currentTime,
|
||||
kind: HANDLER_INFORMATION,
|
||||
tags: [
|
||||
["d", "test-handler"],
|
||||
["k", "30023"],
|
||||
["k", "30024"],
|
||||
],
|
||||
content: JSON.stringify({
|
||||
name: "Test Handler",
|
||||
image: "https://example.com/image.jpg",
|
||||
about: "Test handler description",
|
||||
website: "https://example.com",
|
||||
lud16: "user@domain.com",
|
||||
nip05: "user@domain.com",
|
||||
}),
|
||||
...overrides,
|
||||
})
|
||||
|
||||
describe("readHandlers", () => {
|
||||
it("should parse valid handler event with full metadata", () => {
|
||||
const event = createHandlerEvent()
|
||||
const handlers = readHandlers(event)
|
||||
|
||||
expect(handlers).toHaveLength(2) // Two k tags
|
||||
expect(handlers[0]).toMatchObject({
|
||||
kind: 30023,
|
||||
identifier: "test-handler",
|
||||
name: "Test Handler",
|
||||
image: "https://example.com/image.jpg",
|
||||
about: "Test handler description",
|
||||
website: "https://example.com",
|
||||
lud16: "user@domain.com",
|
||||
nip05: "user@domain.com",
|
||||
})
|
||||
})
|
||||
|
||||
it("should handle display_name and picture alternatives", () => {
|
||||
const event = createHandlerEvent({
|
||||
content: JSON.stringify({
|
||||
display_name: "Test Handler",
|
||||
picture: "https://example.com/image.jpg",
|
||||
about: "Test description",
|
||||
}),
|
||||
})
|
||||
const handlers = readHandlers(event)
|
||||
|
||||
expect(handlers[0].name).toBe("Test Handler")
|
||||
expect(handlers[0].image).toBe("https://example.com/image.jpg")
|
||||
})
|
||||
|
||||
it("should return empty array if name is missing", () => {
|
||||
const event = createHandlerEvent({
|
||||
content: JSON.stringify({
|
||||
image: "https://example.com/image.jpg",
|
||||
about: "Test description",
|
||||
}),
|
||||
})
|
||||
const handlers = readHandlers(event)
|
||||
|
||||
expect(handlers).toEqual([])
|
||||
})
|
||||
|
||||
it("should return empty array if image is missing", () => {
|
||||
const event = createHandlerEvent({
|
||||
content: JSON.stringify({
|
||||
name: "Test Handler",
|
||||
about: "Test description",
|
||||
}),
|
||||
})
|
||||
const handlers = readHandlers(event)
|
||||
|
||||
expect(handlers).toEqual([])
|
||||
})
|
||||
|
||||
it("should handle invalid JSON content", () => {
|
||||
const event = createHandlerEvent({
|
||||
content: "invalid json",
|
||||
})
|
||||
const handlers = readHandlers(event)
|
||||
|
||||
expect(handlers).toEqual([])
|
||||
})
|
||||
|
||||
it("should handle empty content", () => {
|
||||
const event = createHandlerEvent({
|
||||
content: "",
|
||||
})
|
||||
const handlers = readHandlers(event)
|
||||
|
||||
expect(handlers).toEqual([])
|
||||
})
|
||||
|
||||
it("should handle missing optional fields", () => {
|
||||
const event = createHandlerEvent({
|
||||
content: JSON.stringify({
|
||||
name: "Test Handler",
|
||||
image: "https://example.com/image.jpg",
|
||||
}),
|
||||
})
|
||||
const handlers = readHandlers(event)
|
||||
|
||||
expect(handlers[0]).toMatchObject({
|
||||
name: "Test Handler",
|
||||
image: "https://example.com/image.jpg",
|
||||
about: "",
|
||||
website: "",
|
||||
lud16: "",
|
||||
nip05: "",
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("getHandlerKey", () => {
|
||||
it("should generate correct handler key", () => {
|
||||
const event = createHandlerEvent()
|
||||
const handler = readHandlers(event)[0]
|
||||
const key = getHandlerKey(handler)
|
||||
|
||||
expect(key).toBe(`30023:31990:${pubkey}:test-handler`)
|
||||
})
|
||||
})
|
||||
|
||||
describe("displayHandler", () => {
|
||||
it("should return handler name when available", () => {
|
||||
const event = createHandlerEvent()
|
||||
const handler = readHandlers(event)[0]
|
||||
|
||||
expect(displayHandler(handler)).toBe("Test Handler")
|
||||
})
|
||||
|
||||
it("should return fallback when handler is undefined", () => {
|
||||
expect(displayHandler(undefined, "Fallback")).toBe("Fallback")
|
||||
})
|
||||
|
||||
it("should return empty string when no fallback provided", () => {
|
||||
expect(displayHandler(undefined)).toBe("")
|
||||
})
|
||||
})
|
||||
|
||||
describe("getHandlerAddress", () => {
|
||||
it("should return web-tagged address if available", () => {
|
||||
const event = createHandlerEvent({
|
||||
tags: [
|
||||
["a", "30023:pubkey1:test", "relay1", "web"],
|
||||
["a", "30024:pubkey2:test", "relay2"],
|
||||
],
|
||||
})
|
||||
|
||||
expect(getHandlerAddress(event)).toBe("30023:pubkey1:test")
|
||||
})
|
||||
|
||||
it("should return first address if no web tag", () => {
|
||||
const event = createHandlerEvent({
|
||||
tags: [
|
||||
["a", "30023:pubkey1:test", "relay1"],
|
||||
["a", "30024:pubkey2:test", "relay2"],
|
||||
],
|
||||
})
|
||||
|
||||
expect(getHandlerAddress(event)).toBe("30023:pubkey1:test")
|
||||
})
|
||||
|
||||
it("should return undefined if no address tags", () => {
|
||||
const event = createHandlerEvent({
|
||||
tags: [["d", "test-handler"]],
|
||||
})
|
||||
|
||||
expect(getHandlerAddress(event)).toBeUndefined()
|
||||
})
|
||||
|
||||
it("should handle empty tags array", () => {
|
||||
const event = createHandlerEvent({
|
||||
tags: [],
|
||||
})
|
||||
|
||||
expect(getHandlerAddress(event)).toBeUndefined()
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,324 @@
|
||||
import {now} from "@welshman/lib"
|
||||
import {MUTES} from "@welshman/util"
|
||||
import {describe, it, vi, expect, beforeEach} from "vitest"
|
||||
import {
|
||||
makeList,
|
||||
readList,
|
||||
getListTags,
|
||||
removeFromList,
|
||||
removeFromListByPredicate,
|
||||
addToListPublicly,
|
||||
addToListPrivately,
|
||||
} from "../src/List"
|
||||
import type {DecryptedEvent} from "../src/Encryptable"
|
||||
import type {List} from "../src/List"
|
||||
|
||||
describe("List", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
const pubkey = "ee".repeat(32)
|
||||
const validEventId = "ff".repeat(32)
|
||||
const address = `30023:${pubkey}:test`
|
||||
const currentTime = now()
|
||||
|
||||
const createDecryptedEvent = (overrides = {}): DecryptedEvent => ({
|
||||
id: validEventId,
|
||||
pubkey: pubkey,
|
||||
created_at: currentTime,
|
||||
kind: MUTES,
|
||||
tags: [],
|
||||
content: "",
|
||||
plaintext: {},
|
||||
...overrides,
|
||||
})
|
||||
|
||||
describe("makeList", () => {
|
||||
it("should create a list with defaults", () => {
|
||||
const list = makeList({kind: MUTES})
|
||||
expect(list).toEqual({
|
||||
kind: MUTES,
|
||||
publicTags: [],
|
||||
privateTags: [],
|
||||
})
|
||||
})
|
||||
|
||||
it("should preserve existing tags", () => {
|
||||
const list = makeList({
|
||||
kind: MUTES,
|
||||
publicTags: [["p", pubkey]],
|
||||
privateTags: [["e", validEventId]],
|
||||
})
|
||||
expect(list.publicTags).toHaveLength(1)
|
||||
expect(list.privateTags).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe("readList", () => {
|
||||
it("should parse valid public tags", () => {
|
||||
const event = createDecryptedEvent({
|
||||
tags: [
|
||||
["p", pubkey],
|
||||
["e", validEventId],
|
||||
["a", address],
|
||||
["t", "test"],
|
||||
["r", "wss://relay.example.com"],
|
||||
["relay", "wss://relay.example.com"],
|
||||
["unknown", "value"],
|
||||
],
|
||||
})
|
||||
const list = readList(event)
|
||||
expect(list.publicTags).toHaveLength(7)
|
||||
})
|
||||
|
||||
it("should not parse invalid public tags", () => {
|
||||
const event = createDecryptedEvent({
|
||||
tags: [
|
||||
["p", "invalid-pubkey"],
|
||||
["e", "invalid-event-id"],
|
||||
["a", "invalid-address"],
|
||||
["t", ""],
|
||||
["r", "invalid-url"],
|
||||
["relay", "invalid-url"],
|
||||
],
|
||||
})
|
||||
const list = readList(event)
|
||||
expect(list.publicTags).toHaveLength(0)
|
||||
})
|
||||
|
||||
it("should parse valid private tags", () => {
|
||||
const event = createDecryptedEvent({
|
||||
plaintext: {
|
||||
content: JSON.stringify([
|
||||
["p", pubkey],
|
||||
["e", validEventId],
|
||||
]),
|
||||
},
|
||||
})
|
||||
const list = readList(event)
|
||||
expect(list.privateTags).toHaveLength(2)
|
||||
})
|
||||
|
||||
it("should not parse invalid private tags", () => {
|
||||
const event = createDecryptedEvent({
|
||||
plaintext: {
|
||||
content: JSON.stringify([
|
||||
["p", "invalid-pubkey"],
|
||||
["e", "invalid-event-id"],
|
||||
]),
|
||||
},
|
||||
})
|
||||
const list = readList(event)
|
||||
expect(list.privateTags).toHaveLength(0)
|
||||
})
|
||||
|
||||
it("should filter invalid tags", () => {
|
||||
const event = createDecryptedEvent({
|
||||
tags: [
|
||||
["p", "invalid-pubkey"],
|
||||
["e", "invalid-event-id"],
|
||||
["a", "invalid-address"],
|
||||
["t", ""],
|
||||
["r", "invalid-url"],
|
||||
["relay", "invalid-url"],
|
||||
],
|
||||
})
|
||||
const list = readList(event)
|
||||
expect(list.publicTags).toHaveLength(0)
|
||||
})
|
||||
|
||||
it("should handle invalid JSON in private content", () => {
|
||||
const event = createDecryptedEvent({
|
||||
plaintext: {content: "invalid-json"},
|
||||
})
|
||||
const list = readList(event)
|
||||
expect(list.privateTags).toEqual([])
|
||||
})
|
||||
|
||||
it("should handle non-array private content", () => {
|
||||
const event = createDecryptedEvent({
|
||||
plaintext: {content: JSON.stringify({not: "an-array"})},
|
||||
})
|
||||
const list = readList(event)
|
||||
expect(list.privateTags).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe("getListTags", () => {
|
||||
it("should combine public and private tags", () => {
|
||||
const list: List = {
|
||||
kind: MUTES,
|
||||
publicTags: [["p", pubkey]],
|
||||
privateTags: [["e", validEventId]],
|
||||
}
|
||||
const tags = getListTags(list)
|
||||
expect(tags).toHaveLength(2)
|
||||
})
|
||||
|
||||
it("should handle undefined list", () => {
|
||||
expect(getListTags(undefined)).toEqual([])
|
||||
})
|
||||
})
|
||||
|
||||
describe("removeFromList", () => {
|
||||
it("should remove matching public tags", () => {
|
||||
const list: List = {
|
||||
kind: MUTES,
|
||||
publicTags: [["p", pubkey]],
|
||||
privateTags: [],
|
||||
event: createDecryptedEvent(),
|
||||
}
|
||||
const result = removeFromList(list, pubkey)
|
||||
expect(result.event.tags).toHaveLength(0)
|
||||
})
|
||||
|
||||
it("should remove matching private tags", () => {
|
||||
const list: List = {
|
||||
kind: MUTES,
|
||||
publicTags: [],
|
||||
privateTags: [["p", pubkey]],
|
||||
event: createDecryptedEvent(),
|
||||
}
|
||||
const result = removeFromList(list, pubkey)
|
||||
const plaintext = JSON.parse(result.updates.content || "[]")
|
||||
expect(plaintext).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("removeFromListByPredicate", () => {
|
||||
it("should remove tags matching predicate", () => {
|
||||
const list: List = {
|
||||
kind: MUTES,
|
||||
publicTags: [
|
||||
["p", pubkey],
|
||||
["e", validEventId],
|
||||
],
|
||||
privateTags: [["p", pubkey]],
|
||||
event: createDecryptedEvent(),
|
||||
}
|
||||
const result = removeFromListByPredicate(list, tag => tag[0] === "p")
|
||||
expect(result.event.tags).toHaveLength(1)
|
||||
const plaintext = JSON.parse(result.updates.content || "[]")
|
||||
expect(plaintext).toHaveLength(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe("addToListPublicly", () => {
|
||||
it("should add tags to public list", () => {
|
||||
const list: List = {
|
||||
kind: MUTES,
|
||||
publicTags: [],
|
||||
privateTags: [],
|
||||
event: createDecryptedEvent(),
|
||||
}
|
||||
const result = addToListPublicly(list, ["p", pubkey])
|
||||
expect(result.event.tags).toHaveLength(1)
|
||||
expect(result.updates).toEqual({})
|
||||
})
|
||||
|
||||
it("should deduplicate tags", () => {
|
||||
const list: List = {
|
||||
kind: MUTES,
|
||||
publicTags: [["p", pubkey]],
|
||||
privateTags: [],
|
||||
event: createDecryptedEvent(),
|
||||
}
|
||||
const result = addToListPublicly(list, ["p", pubkey])
|
||||
expect(result.event.tags).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe("addToListPrivately", () => {
|
||||
it("should add tags to private list", () => {
|
||||
const list: List = {
|
||||
kind: MUTES,
|
||||
publicTags: [],
|
||||
privateTags: [],
|
||||
event: createDecryptedEvent(),
|
||||
}
|
||||
const result = addToListPrivately(list, ["p", pubkey])
|
||||
const plaintext = JSON.parse(result.updates.content || "[]")
|
||||
expect(plaintext).toHaveLength(1)
|
||||
})
|
||||
|
||||
it("should deduplicate private tags", () => {
|
||||
const list: List = {
|
||||
kind: MUTES,
|
||||
publicTags: [],
|
||||
privateTags: [["p", pubkey]],
|
||||
event: createDecryptedEvent(),
|
||||
}
|
||||
const result = addToListPrivately(list, ["p", pubkey])
|
||||
const plaintext = JSON.parse(result.updates.content || "[]")
|
||||
expect(plaintext).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
|
||||
describe("tag validation", () => {
|
||||
it("should validate pubkey tags", () => {
|
||||
const event = createDecryptedEvent({
|
||||
tags: [
|
||||
["p", pubkey],
|
||||
["p", "invalid"],
|
||||
],
|
||||
})
|
||||
const list = readList(event)
|
||||
expect(list.publicTags).toHaveLength(1)
|
||||
})
|
||||
|
||||
it("should validate event tags", () => {
|
||||
const event = createDecryptedEvent({
|
||||
tags: [
|
||||
["e", validEventId],
|
||||
["e", "invalid"],
|
||||
],
|
||||
})
|
||||
const list = readList(event)
|
||||
expect(list.publicTags).toHaveLength(1)
|
||||
})
|
||||
|
||||
it("should validate address tags", () => {
|
||||
const event = createDecryptedEvent({
|
||||
tags: [
|
||||
["a", address],
|
||||
["a", "invalid"],
|
||||
],
|
||||
})
|
||||
const list = readList(event)
|
||||
expect(list.publicTags).toHaveLength(1)
|
||||
})
|
||||
|
||||
it("should validate topic tags", () => {
|
||||
const event = createDecryptedEvent({
|
||||
tags: [
|
||||
["t", "valid-topic"],
|
||||
["t", ""],
|
||||
],
|
||||
})
|
||||
const list = readList(event)
|
||||
expect(list.publicTags).toHaveLength(1)
|
||||
})
|
||||
|
||||
it("should validate relay tags", () => {
|
||||
const event = createDecryptedEvent({
|
||||
tags: [
|
||||
["r", "wss://relay.example.com"],
|
||||
["r", "invalid"],
|
||||
["relay", "wss://relay.example.com"],
|
||||
["relay", "invalid"],
|
||||
],
|
||||
})
|
||||
const list = readList(event)
|
||||
expect(list.publicTags).toHaveLength(2)
|
||||
})
|
||||
|
||||
it("should accept unknown tag types", () => {
|
||||
const event = createDecryptedEvent({
|
||||
tags: [["unknown", "value"]],
|
||||
})
|
||||
const list = readList(event)
|
||||
expect(list.publicTags).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,232 @@
|
||||
import {now} from "@welshman/lib"
|
||||
import {describe, it, vi, expect, beforeEach} from "vitest"
|
||||
import {
|
||||
makeProfile,
|
||||
readProfile,
|
||||
createProfile,
|
||||
editProfile,
|
||||
displayPubkey,
|
||||
displayProfile,
|
||||
profileHasName,
|
||||
isPublishedProfile,
|
||||
} from "../src/Profile"
|
||||
import {PROFILE} from "../src/Kinds"
|
||||
import type {TrustedEvent} from "../src/Events"
|
||||
import type {Profile, PublishedProfile} from "../src/Profile"
|
||||
|
||||
describe("Profile", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
// Realistic Nostr data
|
||||
const pubkey = "ee".repeat(32)
|
||||
const id = "ff".repeat(32)
|
||||
const sig = "00".repeat(64)
|
||||
const currentTime = now()
|
||||
|
||||
const createEvent = (overrides = {}): TrustedEvent => ({
|
||||
id: id,
|
||||
pubkey: pubkey,
|
||||
created_at: currentTime,
|
||||
kind: PROFILE,
|
||||
tags: [],
|
||||
content: "",
|
||||
sig: sig,
|
||||
...overrides,
|
||||
})
|
||||
|
||||
describe("makeProfile", () => {
|
||||
it("should create empty profile", () => {
|
||||
const profile = makeProfile()
|
||||
expect(profile).toEqual({})
|
||||
})
|
||||
|
||||
it("should handle lud06 lightning address", () => {
|
||||
const profile = makeProfile({
|
||||
lud06:
|
||||
"lnurl1dp68gurn8ghj7um9wfmxjcm99e3k7mf0v9cxj0m385ekvcenxc6r2c35xvukxefcv5mkvv34x5ekzd3ev56nyd3hxqurzepexejxxepnxscrvwfnv9nxzcn9xq6xyefhvgcxxcmyxymnserxfq5fns",
|
||||
})
|
||||
expect(profile.lnurl).toBeDefined()
|
||||
})
|
||||
|
||||
it("should handle lud16 lightning address", () => {
|
||||
const profile = makeProfile({
|
||||
lud16: "user@domain.com",
|
||||
})
|
||||
expect(profile.lnurl).toBeDefined()
|
||||
})
|
||||
|
||||
it("should preserve other profile fields", () => {
|
||||
const profile = makeProfile({
|
||||
name: "Test User",
|
||||
about: "Test Bio",
|
||||
picture: "https://example.com/pic.jpg",
|
||||
})
|
||||
expect(profile.name).toBe("Test User")
|
||||
expect(profile.about).toBe("Test Bio")
|
||||
expect(profile.picture).toBe("https://example.com/pic.jpg")
|
||||
})
|
||||
})
|
||||
|
||||
describe("readProfile", () => {
|
||||
it("should parse valid profile content", () => {
|
||||
const event = createEvent({
|
||||
content: JSON.stringify({
|
||||
name: "Test User",
|
||||
about: "Test Bio",
|
||||
picture: "https://example.com/pic.jpg",
|
||||
lud16: "user@domain.com",
|
||||
}),
|
||||
})
|
||||
const profile = readProfile(event)
|
||||
|
||||
expect(profile.name).toBe("Test User")
|
||||
expect(profile.about).toBe("Test Bio")
|
||||
expect(profile.picture).toBe("https://example.com/pic.jpg")
|
||||
expect(profile.lnurl).toBeDefined()
|
||||
expect(profile.event).toBe(event)
|
||||
})
|
||||
|
||||
it("should handle invalid JSON content", () => {
|
||||
const event = createEvent({
|
||||
content: "invalid json",
|
||||
})
|
||||
const profile = readProfile(event)
|
||||
|
||||
expect(profile.event).toBe(event)
|
||||
expect(Object.keys(profile)).not.toContain("name")
|
||||
})
|
||||
|
||||
it("should handle empty content", () => {
|
||||
const event = createEvent({
|
||||
content: "",
|
||||
})
|
||||
const profile = readProfile(event)
|
||||
|
||||
expect(profile.event).toBe(event)
|
||||
expect(Object.keys(profile)).not.toContain("name")
|
||||
})
|
||||
})
|
||||
|
||||
describe("createProfile", () => {
|
||||
it("should create profile event template", () => {
|
||||
const profile: Profile = {
|
||||
name: "Test User",
|
||||
about: "Test Bio",
|
||||
picture: "https://example.com/pic.jpg",
|
||||
lud16: "user@domain.com",
|
||||
}
|
||||
const result = createProfile(profile)
|
||||
|
||||
expect(result.kind).toBe(PROFILE)
|
||||
expect(JSON.parse(result.content)).toMatchObject({
|
||||
name: "Test User",
|
||||
about: "Test Bio",
|
||||
picture: "https://example.com/pic.jpg",
|
||||
lud16: "user@domain.com",
|
||||
})
|
||||
})
|
||||
|
||||
it("should exclude event field from content", () => {
|
||||
const profile: Profile = {
|
||||
name: "Test User",
|
||||
event: createEvent(),
|
||||
}
|
||||
const result = createProfile(profile)
|
||||
const content = JSON.parse(result.content)
|
||||
|
||||
expect(content).not.toHaveProperty("event")
|
||||
expect(content).toHaveProperty("name")
|
||||
})
|
||||
})
|
||||
|
||||
describe("editProfile", () => {
|
||||
it("should create edit event template with existing tags", () => {
|
||||
const profile: PublishedProfile = {
|
||||
name: "Test User",
|
||||
event: createEvent({
|
||||
tags: [["p", pubkey]],
|
||||
}),
|
||||
}
|
||||
const result = editProfile(profile)
|
||||
|
||||
expect(result.kind).toBe(PROFILE)
|
||||
expect(result.tags).toEqual([["p", pubkey]])
|
||||
expect(JSON.parse(result.content)).toMatchObject({
|
||||
name: "Test User",
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("displayPubkey", () => {
|
||||
it("should format pubkey correctly", () => {
|
||||
const display = displayPubkey(pubkey)
|
||||
|
||||
expect(display.length).toBe(14) // 8 + 1 + 5 characters
|
||||
})
|
||||
})
|
||||
|
||||
describe("displayProfile", () => {
|
||||
it("should display name if available", () => {
|
||||
const profile: Profile = {name: "Test User"}
|
||||
expect(displayProfile(profile)).toBe("Test User")
|
||||
})
|
||||
|
||||
it("should display display_name if name not available", () => {
|
||||
const profile: Profile = {display_name: "Test Display"}
|
||||
expect(displayProfile(profile)).toBe("Test Display")
|
||||
})
|
||||
|
||||
it("should display pubkey if no names available", () => {
|
||||
const profile: Profile = {event: createEvent()}
|
||||
expect(displayProfile(profile)).toMatch(/^npub1/)
|
||||
})
|
||||
|
||||
it("should display fallback if no profile", () => {
|
||||
expect(displayProfile(undefined, "Fallback")).toBe("Fallback")
|
||||
})
|
||||
|
||||
it("should truncate long names", () => {
|
||||
const longName = "a".repeat(100) + " " + "b".repeat(100)
|
||||
const profile: Profile = {name: longName}
|
||||
// ellipsize split at space and adds ellipsis to the end of the first part
|
||||
expect(displayProfile(profile).length).toBeLessThanOrEqual(103)
|
||||
})
|
||||
})
|
||||
|
||||
describe("profileHasName", () => {
|
||||
it("should return true if profile has name", () => {
|
||||
expect(profileHasName({name: "Test"})).toBe(true)
|
||||
})
|
||||
|
||||
it("should return true if profile has display_name", () => {
|
||||
expect(profileHasName({display_name: "Test"})).toBe(true)
|
||||
})
|
||||
|
||||
it("should return false if profile has no names", () => {
|
||||
expect(profileHasName({})).toBe(false)
|
||||
})
|
||||
|
||||
it("should return false if profile is undefined", () => {
|
||||
expect(profileHasName(undefined)).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("isPublishedProfile", () => {
|
||||
it("should return true for published profile", () => {
|
||||
const profile: PublishedProfile = {
|
||||
name: "Test",
|
||||
event: createEvent(),
|
||||
}
|
||||
expect(isPublishedProfile(profile)).toBe(true)
|
||||
})
|
||||
|
||||
it("should return false for unpublished profile", () => {
|
||||
const profile: Profile = {
|
||||
name: "Test",
|
||||
}
|
||||
expect(isPublishedProfile(profile)).toBe(false)
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,253 @@
|
||||
import {now} from "@welshman/lib"
|
||||
import {describe, it, expect, beforeEach, vi, afterEach} from "vitest"
|
||||
import {
|
||||
Relay,
|
||||
normalizeRelayUrl,
|
||||
isRelayUrl,
|
||||
isOnionUrl,
|
||||
isLocalUrl,
|
||||
isIPAddress,
|
||||
isShareableRelayUrl,
|
||||
displayRelayUrl,
|
||||
displayRelayProfile,
|
||||
} from "../src/Relay"
|
||||
import {Repository} from "../src/Repository"
|
||||
import type {TrustedEvent} from "../src/Events"
|
||||
|
||||
describe("Relay", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
vi.useFakeTimers()
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
vi.useRealTimers()
|
||||
})
|
||||
|
||||
// Realistic Nostr data
|
||||
const pubkey = "ee".repeat(32)
|
||||
const id = "ff".repeat(32)
|
||||
const sig = "00".repeat(64)
|
||||
const currentTime = now()
|
||||
const onionUrl = "abcdefghijklmnopqrstuvwxyz234567abcdefghijklmnopqrstuvwx.onion"
|
||||
|
||||
const createEvent = (overrides = {}): TrustedEvent => ({
|
||||
id: id,
|
||||
pubkey: pubkey,
|
||||
created_at: currentTime,
|
||||
kind: 1,
|
||||
tags: [],
|
||||
content: "Hello Nostr!",
|
||||
sig: sig,
|
||||
...overrides,
|
||||
})
|
||||
|
||||
describe("URL utilities", () => {
|
||||
describe("isRelayUrl", () => {
|
||||
it("should validate proper relay URLs", () => {
|
||||
expect(isRelayUrl("wss://relay.example.com")).toBe(true)
|
||||
expect(isRelayUrl("ws://relay.example.com")).toBe(true)
|
||||
expect(isRelayUrl("relay.example.com")).toBe(true)
|
||||
})
|
||||
|
||||
it("should reject invalid URLs", () => {
|
||||
expect(isRelayUrl("http://relay.example.com")).toBe(false)
|
||||
expect(isRelayUrl("not-a-url")).toBe(false)
|
||||
expect(isRelayUrl("ws:\\example.com\\path\\to\\file.ext")).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("isOnionUrl", () => {
|
||||
it("should validate onion URLs", () => {
|
||||
expect(isOnionUrl(onionUrl)).toBe(true)
|
||||
})
|
||||
|
||||
it("should reject non-onion URLs", () => {
|
||||
expect(isOnionUrl("wss://relay.example.com")).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("isLocalUrl", () => {
|
||||
it("should validate local URLs", () => {
|
||||
expect(isLocalUrl("wss://relay.local")).toBe(true)
|
||||
expect(isLocalUrl("ws://localhost:8080")).toBe(true)
|
||||
})
|
||||
|
||||
it("should reject non-local URLs", () => {
|
||||
expect(isLocalUrl("wss://relay.example.com")).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("isIPAddress", () => {
|
||||
it("should validate IP addresses", () => {
|
||||
expect(isIPAddress("wss://192.168.1.1")).toBe(true)
|
||||
})
|
||||
|
||||
it("should reject domains", () => {
|
||||
expect(isIPAddress("wss://relay.example.com")).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("isShareableRelayUrl", () => {
|
||||
it("should validate shareable URLs", () => {
|
||||
expect(isShareableRelayUrl("wss://relay.example.com")).toBe(true)
|
||||
})
|
||||
|
||||
it("should reject local URLs", () => {
|
||||
expect(isShareableRelayUrl("wss://relay.local")).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe("normalizeRelayUrl", () => {
|
||||
it("should normalize URLs consistently", () => {
|
||||
expect(normalizeRelayUrl("relay.example.com")).toBe("wss://relay.example.com/")
|
||||
expect(normalizeRelayUrl("wss://RELAY.EXAMPLE.COM")).toBe("wss://relay.example.com/")
|
||||
})
|
||||
|
||||
it("should handle onion URLs", () => {
|
||||
expect(normalizeRelayUrl(onionUrl)).toBe(`ws://${onionUrl}/`)
|
||||
})
|
||||
})
|
||||
|
||||
describe("displayRelayUrl", () => {
|
||||
it("should format URLs for display", () => {
|
||||
expect(displayRelayUrl("wss://relay.example.com/")).toBe("relay.example.com")
|
||||
})
|
||||
})
|
||||
|
||||
describe("displayRelayProfile", () => {
|
||||
it("should display profile name when available", () => {
|
||||
const profile = {url: "wss://relay.example.com", name: "Test Relay"}
|
||||
expect(displayRelayProfile(profile)).toBe("Test Relay")
|
||||
})
|
||||
|
||||
it("should use fallback when no name", () => {
|
||||
const profile = {url: "wss://relay.example.com"}
|
||||
expect(displayRelayProfile(profile, "Fallback")).toBe("Fallback")
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("Relay class", () => {
|
||||
let relay: Relay
|
||||
let repository: Repository<TrustedEvent>
|
||||
|
||||
beforeEach(() => {
|
||||
repository = new Repository<TrustedEvent>()
|
||||
relay = new Relay(repository)
|
||||
})
|
||||
|
||||
describe("EVENT handling", () => {
|
||||
it("should publish events to repository", async () => {
|
||||
const event = createEvent()
|
||||
const publishSpy = vi.spyOn(repository, "publish")
|
||||
|
||||
relay.send("EVENT", event)
|
||||
|
||||
expect(publishSpy).toHaveBeenCalledWith(event)
|
||||
|
||||
// Should emit OK
|
||||
const okHandler = vi.fn()
|
||||
relay.on("OK", okHandler)
|
||||
|
||||
// Wait for async operations
|
||||
await vi.runAllTimersAsync()
|
||||
|
||||
expect(okHandler).toHaveBeenCalledWith(event.id, true, "")
|
||||
})
|
||||
|
||||
it("should notify matching subscribers", async () => {
|
||||
const event = createEvent()
|
||||
const subId = "test-sub"
|
||||
const filter = {kinds: [1]}
|
||||
|
||||
relay.send("REQ", subId, filter)
|
||||
|
||||
const eventHandler = vi.fn()
|
||||
relay.on("EVENT", eventHandler)
|
||||
|
||||
relay.send("EVENT", event)
|
||||
|
||||
await vi.runAllTimersAsync()
|
||||
|
||||
expect(eventHandler).toHaveBeenCalledWith(subId, event)
|
||||
})
|
||||
|
||||
it("should not notify for deleted events", async () => {
|
||||
const event = createEvent()
|
||||
repository.removeEvent(event.id)
|
||||
|
||||
const eventHandler = vi.fn()
|
||||
relay.on("EVENT", eventHandler)
|
||||
|
||||
relay.send("EVENT", event)
|
||||
|
||||
await vi.runAllTimersAsync()
|
||||
|
||||
expect(eventHandler).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
|
||||
describe("REQ handling", () => {
|
||||
it("should handle subscription requests", async () => {
|
||||
const event = createEvent()
|
||||
repository.publish(event)
|
||||
|
||||
const subId = "test-sub"
|
||||
const filter = {kinds: [1]}
|
||||
|
||||
const eventHandler = vi.fn()
|
||||
const eoseHandler = vi.fn()
|
||||
|
||||
relay.on("EVENT", eventHandler)
|
||||
relay.on("EOSE", eoseHandler)
|
||||
|
||||
relay.send("REQ", subId, filter)
|
||||
|
||||
await vi.runAllTimersAsync()
|
||||
|
||||
expect(eventHandler).toHaveBeenCalledWith(subId, event)
|
||||
expect(eoseHandler).toHaveBeenCalledWith(subId)
|
||||
})
|
||||
|
||||
it("should handle multiple filters", async () => {
|
||||
const event1 = createEvent({kind: 1})
|
||||
const event2 = createEvent({kind: 2, id: "ee".repeat(31)})
|
||||
repository.publish(event1)
|
||||
repository.publish(event2)
|
||||
|
||||
const subId = "test-sub"
|
||||
const filters = [{kinds: [1]}, {kinds: [2]}]
|
||||
|
||||
const eventHandler = vi.fn()
|
||||
relay.on("EVENT", eventHandler)
|
||||
|
||||
relay.send("REQ", subId, ...filters)
|
||||
|
||||
await vi.runAllTimersAsync()
|
||||
|
||||
expect(eventHandler).toHaveBeenCalledTimes(2)
|
||||
})
|
||||
})
|
||||
|
||||
describe("CLOSE handling", () => {
|
||||
it("should close subscriptions", async () => {
|
||||
const subId = "test-sub"
|
||||
relay.send("REQ", subId, {kinds: [1]})
|
||||
relay.send("CLOSE", subId)
|
||||
|
||||
await vi.runAllTimersAsync()
|
||||
|
||||
const event = createEvent()
|
||||
const eventHandler = vi.fn()
|
||||
relay.on("EVENT", eventHandler)
|
||||
|
||||
relay.send("EVENT", event)
|
||||
|
||||
await vi.runAllTimersAsync()
|
||||
|
||||
expect(eventHandler).not.toHaveBeenCalled()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,337 @@
|
||||
import {now} from "@welshman/lib"
|
||||
import {getAddress} from "@welshman/util"
|
||||
import {describe, it, vi, expect, beforeEach} from "vitest"
|
||||
import {Repository} from "../src/Repository"
|
||||
import type {TrustedEvent} from "../src/Events"
|
||||
import {DELETE, MUTES} from "../src/Kinds"
|
||||
|
||||
describe("Repository", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
// Realistic Nostr data
|
||||
const pubkey = "ee".repeat(32)
|
||||
const id = "ff".repeat(32)
|
||||
const sig = "00".repeat(64)
|
||||
const currentTime = now()
|
||||
|
||||
const createEvent = (overrides = {}): TrustedEvent => ({
|
||||
id: id,
|
||||
pubkey: pubkey,
|
||||
created_at: currentTime,
|
||||
kind: 1,
|
||||
tags: [],
|
||||
content: "Hello Nostr!",
|
||||
sig: sig,
|
||||
...overrides,
|
||||
})
|
||||
|
||||
describe("basic operations", () => {
|
||||
let repo: Repository
|
||||
|
||||
beforeEach(() => {
|
||||
repo = new Repository()
|
||||
})
|
||||
|
||||
it("should publish and retrieve events", () => {
|
||||
const event = createEvent()
|
||||
expect(repo.publish(event)).toBe(true)
|
||||
expect(repo.getEvent(event.id)).toEqual(event)
|
||||
})
|
||||
|
||||
it("should not publish invalid events", () => {
|
||||
const invalidEvent = {} as TrustedEvent
|
||||
const result = repo.publish(invalidEvent)
|
||||
expect(result).toBe(false)
|
||||
})
|
||||
|
||||
it("should handle duplicate events", () => {
|
||||
const event = createEvent()
|
||||
expect(repo.publish(event)).toBe(true)
|
||||
expect(repo.publish(event)).toBe(false)
|
||||
})
|
||||
|
||||
it("should check if events exist", () => {
|
||||
const event = createEvent()
|
||||
repo.publish(event)
|
||||
expect(repo.hasEvent(event)).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe("replaceable events", () => {
|
||||
let repo: Repository
|
||||
|
||||
beforeEach(() => {
|
||||
repo = new Repository()
|
||||
})
|
||||
|
||||
it("should handle replaceable events", () => {
|
||||
const event1 = createEvent({kind: MUTES, created_at: currentTime - 100})
|
||||
const event2 = createEvent({kind: MUTES, created_at: currentTime, id: "ee".repeat(32)})
|
||||
|
||||
const address1 = getAddress(event1)
|
||||
const address2 = getAddress(event2)
|
||||
|
||||
repo.publish(event1)
|
||||
repo.publish(event2)
|
||||
|
||||
expect(repo.getEvent(event1.id)).toEqual(event1)
|
||||
expect(repo.getEvent(address1)).toEqual(event2)
|
||||
expect(repo.getEvent(event2.id)).toEqual(event2)
|
||||
expect(repo.getEvent(address2)).toEqual(event2)
|
||||
|
||||
const event3 = createEvent({kind: MUTES, created_at: currentTime - 50, id: "dd".repeat(32)})
|
||||
|
||||
repo.publish(event3)
|
||||
|
||||
expect(repo.getEvent(event3.id)).toBeUndefined()
|
||||
})
|
||||
|
||||
it("should not replace with older events", () => {
|
||||
const event1 = createEvent({kind: MUTES, created_at: currentTime})
|
||||
const event2 = createEvent({kind: MUTES, created_at: currentTime - 100})
|
||||
|
||||
repo.publish(event1)
|
||||
repo.publish(event2)
|
||||
|
||||
expect(repo.getEvent(event1.id)).toEqual(event1)
|
||||
})
|
||||
})
|
||||
|
||||
describe("delete events", () => {
|
||||
let repo: Repository
|
||||
|
||||
beforeEach(() => {
|
||||
repo = new Repository()
|
||||
})
|
||||
|
||||
it("should handle delete events", () => {
|
||||
const event = createEvent()
|
||||
const deleteEvent = createEvent({
|
||||
id: "ee".repeat(32),
|
||||
kind: DELETE,
|
||||
tags: [["e", event.id]],
|
||||
created_at: currentTime + 100,
|
||||
})
|
||||
|
||||
repo.publish(event)
|
||||
repo.publish(deleteEvent)
|
||||
|
||||
expect(repo.isDeleted(event)).toBe(true)
|
||||
})
|
||||
|
||||
it("should handle delete by address", () => {
|
||||
const event = createEvent({kind: MUTES})
|
||||
const deleteEvent = createEvent({
|
||||
id: "ee".repeat(32),
|
||||
kind: DELETE,
|
||||
tags: [["a", `10000:${event.pubkey}:`]],
|
||||
created_at: currentTime + 100,
|
||||
})
|
||||
|
||||
repo.publish(event)
|
||||
repo.publish(deleteEvent)
|
||||
|
||||
expect(repo.isDeletedByAddress(event)).toBe(true)
|
||||
})
|
||||
})
|
||||
|
||||
describe("query operations", () => {
|
||||
let repo: Repository
|
||||
|
||||
beforeEach(() => {
|
||||
repo = new Repository()
|
||||
})
|
||||
|
||||
it("should throw on invalid queries", () => {
|
||||
expect(() => repo.query([{limit: 10}], {shouldSort: false})).toThrow()
|
||||
})
|
||||
|
||||
it("should query by ids", () => {
|
||||
const event = createEvent()
|
||||
repo.publish(event)
|
||||
|
||||
const results = repo.query([{ids: [event.id]}])
|
||||
expect(results).toContain(event)
|
||||
})
|
||||
|
||||
it("should query by authors", () => {
|
||||
const event = createEvent()
|
||||
repo.publish(event)
|
||||
|
||||
const results = repo.query([{authors: [event.pubkey]}])
|
||||
expect(results).toContain(event)
|
||||
})
|
||||
|
||||
it("should query by kinds", () => {
|
||||
const event = createEvent({kind: 1})
|
||||
repo.publish(event)
|
||||
|
||||
const results = repo.query([{kinds: [1]}])
|
||||
expect(results).toContain(event)
|
||||
})
|
||||
|
||||
it("should query by tags", () => {
|
||||
const event = createEvent({tags: [["p", pubkey]]})
|
||||
repo.publish(event)
|
||||
|
||||
const results = repo.query([{"#p": [pubkey]}])
|
||||
expect(results).toContain(event)
|
||||
})
|
||||
|
||||
it("should query by time range", () => {
|
||||
const event = createEvent()
|
||||
repo.publish(event)
|
||||
|
||||
const results = repo.query([
|
||||
{
|
||||
since: currentTime - 3600,
|
||||
until: currentTime + 3600,
|
||||
},
|
||||
])
|
||||
expect(results).toContain(event)
|
||||
})
|
||||
|
||||
it("should handle multiple filters", () => {
|
||||
const event = createEvent({kind: 1})
|
||||
repo.publish(event)
|
||||
|
||||
const results = repo.query([{kinds: [1]}, {authors: [event.pubkey]}])
|
||||
expect(results).toHaveLength(1)
|
||||
expect(results).toContain(event)
|
||||
})
|
||||
|
||||
it("should respect limit parameter", () => {
|
||||
const events = [
|
||||
createEvent({id: id + "1", created_at: currentTime}),
|
||||
createEvent({id: id + "2", created_at: currentTime - 100}),
|
||||
]
|
||||
|
||||
events.forEach(e => repo.publish(e))
|
||||
|
||||
const results = repo.query([{limit: 1}])
|
||||
expect(results).toHaveLength(1)
|
||||
expect(results[0]).toEqual(events[0]) // Most recent event
|
||||
})
|
||||
|
||||
it("should not return deleted events", () => {
|
||||
const event = createEvent()
|
||||
const deleteEvent = createEvent({
|
||||
id: "ee".repeat(32),
|
||||
kind: DELETE,
|
||||
tags: [["e", event.id]],
|
||||
created_at: currentTime + 100,
|
||||
})
|
||||
|
||||
repo.publish(event)
|
||||
repo.publish(deleteEvent)
|
||||
|
||||
const results = repo.query([{kinds: [1]}])
|
||||
expect(results).not.toContain(event)
|
||||
})
|
||||
})
|
||||
|
||||
describe("dump and load", () => {
|
||||
let repo: Repository
|
||||
|
||||
beforeEach(() => {
|
||||
repo = new Repository()
|
||||
})
|
||||
|
||||
it("should dump all events", () => {
|
||||
const event = createEvent()
|
||||
repo.publish(event)
|
||||
|
||||
const dumped = repo.dump()
|
||||
expect(dumped).toContain(event)
|
||||
})
|
||||
|
||||
it("should load events", () => {
|
||||
const event = createEvent()
|
||||
repo.load([event])
|
||||
|
||||
expect(repo.getEvent(event.id)).toEqual(event)
|
||||
})
|
||||
|
||||
it("should handle chunked loading", () => {
|
||||
const events = Array.from({length: 1500}, (_, i) => createEvent({id: id.slice(0, -1) + i}))
|
||||
|
||||
repo.load(events, 500)
|
||||
expect(repo.dump()).toHaveLength(1500)
|
||||
})
|
||||
|
||||
it("should emit update events", () => {
|
||||
const event = createEvent()
|
||||
const updateHandler = vi.fn()
|
||||
|
||||
repo.on("update", updateHandler)
|
||||
repo.load([event])
|
||||
|
||||
expect(updateHandler).toHaveBeenCalledWith({
|
||||
added: [event],
|
||||
removed: new Set(),
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("wrapped events", () => {
|
||||
let repo: Repository
|
||||
|
||||
beforeEach(() => {
|
||||
repo = new Repository()
|
||||
})
|
||||
|
||||
it("should handle wrapped events", () => {
|
||||
const wrapped = createEvent()
|
||||
const event = createEvent({
|
||||
wrap: wrapped,
|
||||
})
|
||||
|
||||
repo.publish(event)
|
||||
expect(repo.eventsByWrap.get(wrapped.id)).toEqual(event)
|
||||
})
|
||||
})
|
||||
|
||||
describe("event removal", () => {
|
||||
let repo: Repository
|
||||
|
||||
beforeEach(() => {
|
||||
repo = new Repository()
|
||||
})
|
||||
|
||||
it("should remove events", () => {
|
||||
const event = createEvent()
|
||||
repo.publish(event)
|
||||
repo.removeEvent(event.id)
|
||||
|
||||
expect(repo.getEvent(event.id)).toBeUndefined()
|
||||
})
|
||||
|
||||
it("should remove wrapped events", () => {
|
||||
const wrapped = createEvent()
|
||||
const event = createEvent({
|
||||
wrap: wrapped,
|
||||
})
|
||||
|
||||
repo.publish(event)
|
||||
repo.removeEvent(event.id)
|
||||
|
||||
expect(repo.eventsByWrap.get(wrapped.id)).toBeUndefined()
|
||||
})
|
||||
|
||||
it("should emit update on removal", () => {
|
||||
const event = createEvent()
|
||||
const updateHandler = vi.fn()
|
||||
|
||||
repo.on("update", updateHandler)
|
||||
repo.publish(event)
|
||||
repo.removeEvent(event.id)
|
||||
|
||||
expect(updateHandler).toHaveBeenLastCalledWith({
|
||||
added: [],
|
||||
removed: [event.id],
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,232 @@
|
||||
import {describe, it, vi, expect, beforeEach} from "vitest"
|
||||
import * as Tags from "../src/Tags"
|
||||
|
||||
describe("Tags", () => {
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
const pubkey = "ee".repeat(32)
|
||||
const eventId = "ff".repeat(32)
|
||||
const address = `30023:${pubkey}:test`
|
||||
|
||||
describe("basic tag operations", () => {
|
||||
it("should get tags by type", () => {
|
||||
const tags = [
|
||||
["p", pubkey],
|
||||
["e", eventId],
|
||||
["t", "test"],
|
||||
]
|
||||
|
||||
expect(Tags.getTags("p", tags)).toHaveLength(1)
|
||||
expect(Tags.getTags(["p", "e"], tags)).toHaveLength(2)
|
||||
})
|
||||
|
||||
it("should get single tag by type", () => {
|
||||
const tags = [
|
||||
["p", pubkey],
|
||||
["e", eventId],
|
||||
]
|
||||
|
||||
expect(Tags.getTag("p", tags)).toEqual(["p", pubkey])
|
||||
expect(Tags.getTag(["p", "e"], tags)).toBeDefined()
|
||||
})
|
||||
|
||||
it("should get tag values", () => {
|
||||
const tags = [
|
||||
["p", pubkey],
|
||||
["e", eventId],
|
||||
]
|
||||
|
||||
expect(Tags.getTagValues("p", tags)).toEqual([pubkey])
|
||||
expect(Tags.getTagValue("p", tags)).toBe(pubkey)
|
||||
})
|
||||
})
|
||||
|
||||
describe("specific tag types", () => {
|
||||
describe("event tags", () => {
|
||||
it("should get valid event tags", () => {
|
||||
const tags = [
|
||||
["e", eventId],
|
||||
["e", "invalid"],
|
||||
["other", eventId],
|
||||
]
|
||||
|
||||
const eventTags = Tags.getEventTags(tags)
|
||||
expect(eventTags).toHaveLength(1)
|
||||
expect(Tags.getEventTagValues(tags)).toEqual([eventId])
|
||||
})
|
||||
})
|
||||
|
||||
describe("address tags", () => {
|
||||
it("should get valid address tags", () => {
|
||||
const tags = [
|
||||
["a", address],
|
||||
["a", "invalid"],
|
||||
["other", address],
|
||||
]
|
||||
|
||||
const addressTags = Tags.getAddressTags(tags)
|
||||
expect(addressTags).toHaveLength(1)
|
||||
expect(Tags.getAddressTagValues(tags)).toEqual([address])
|
||||
})
|
||||
})
|
||||
|
||||
describe("pubkey tags", () => {
|
||||
it("should get valid pubkey tags", () => {
|
||||
const tags = [
|
||||
["p", pubkey],
|
||||
["p", "invalid"],
|
||||
["other", pubkey],
|
||||
]
|
||||
|
||||
const pubkeyTags = Tags.getPubkeyTags(tags)
|
||||
expect(pubkeyTags).toHaveLength(1)
|
||||
expect(Tags.getPubkeyTagValues(tags)).toEqual([pubkey])
|
||||
})
|
||||
})
|
||||
|
||||
describe("topic tags", () => {
|
||||
it("should get topic tags", () => {
|
||||
const tags = [
|
||||
["t", "topic1"],
|
||||
["t", "#topic2"],
|
||||
["other", "topic3"],
|
||||
]
|
||||
|
||||
const topicTags = Tags.getTopicTags(tags)
|
||||
expect(topicTags).toHaveLength(2)
|
||||
expect(Tags.getTopicTagValues(tags)).toEqual(["topic1", "topic2"])
|
||||
})
|
||||
})
|
||||
|
||||
describe("relay tags", () => {
|
||||
it("should get valid relay tags", () => {
|
||||
const tags = [
|
||||
["r", "wss://relay.example.com"],
|
||||
["relay", "wss://relay2.example.com"],
|
||||
["r", "invalid"],
|
||||
["other", "wss://relay.example.com"],
|
||||
]
|
||||
|
||||
const relayTags = Tags.getRelayTags(tags)
|
||||
expect(relayTags).toHaveLength(2)
|
||||
expect(Tags.getRelayTagValues(tags)).toEqual([
|
||||
"wss://relay.example.com",
|
||||
"wss://relay2.example.com",
|
||||
])
|
||||
})
|
||||
})
|
||||
|
||||
describe("group tags", () => {
|
||||
it("should get valid group tags", () => {
|
||||
const tags = [
|
||||
["h", "group1", "wss://relay.example.com"],
|
||||
["group", "group2", "wss://relay.example.com"],
|
||||
["h", "invalid"],
|
||||
["other", "group3", "wss://relay.example.com"],
|
||||
]
|
||||
|
||||
const groupTags = Tags.getGroupTags(tags)
|
||||
expect(groupTags).toHaveLength(2)
|
||||
expect(Tags.getGroupTagValues(tags)).toEqual(["group1", "group2"])
|
||||
})
|
||||
})
|
||||
|
||||
describe("kind tags", () => {
|
||||
it("should get valid kind tags", () => {
|
||||
const tags = [
|
||||
["k", "1"],
|
||||
["k", "invalid"],
|
||||
["other", "1"],
|
||||
]
|
||||
|
||||
const kindTags = Tags.getKindTags(tags)
|
||||
expect(kindTags).toHaveLength(1)
|
||||
expect(Tags.getKindTagValues(tags)).toEqual([1])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("comment and reply tags", () => {
|
||||
describe("comment tags", () => {
|
||||
it("should separate root and reply tags", () => {
|
||||
const tags = [
|
||||
["E", eventId],
|
||||
["e", eventId],
|
||||
["P", pubkey],
|
||||
["p", pubkey],
|
||||
["K", "1"],
|
||||
["k", "1"],
|
||||
]
|
||||
|
||||
const {roots, replies} = Tags.getCommentTags(tags)
|
||||
expect(roots).toHaveLength(3)
|
||||
expect(replies).toHaveLength(3)
|
||||
|
||||
const values = Tags.getCommentTagValues(tags)
|
||||
expect(values.roots).toContain(eventId)
|
||||
expect(values.replies).toContain(eventId)
|
||||
})
|
||||
})
|
||||
|
||||
describe("reply tags", () => {
|
||||
it("should handle root replies", () => {
|
||||
const tags = [
|
||||
["e", eventId, "", "root"],
|
||||
["e", eventId, "", "reply"],
|
||||
["q", eventId],
|
||||
]
|
||||
|
||||
const {roots, replies, mentions} = Tags.getReplyTags(tags)
|
||||
expect(roots).toHaveLength(1)
|
||||
expect(replies).toHaveLength(1)
|
||||
expect(mentions).toHaveLength(1)
|
||||
})
|
||||
|
||||
it("should handle implicit positions", () => {
|
||||
const tags = [
|
||||
["e", eventId],
|
||||
["e", eventId],
|
||||
["e", eventId],
|
||||
]
|
||||
|
||||
const {roots, replies, mentions} = Tags.getReplyTags(tags)
|
||||
expect(roots).toHaveLength(1)
|
||||
expect(replies).toHaveLength(1)
|
||||
expect(mentions).toHaveLength(1)
|
||||
})
|
||||
|
||||
it("should handle address tags", () => {
|
||||
const tags = [
|
||||
["a", address, "", "root"],
|
||||
["a", address, "", "reply"],
|
||||
]
|
||||
|
||||
const {roots, replies} = Tags.getReplyTags(tags)
|
||||
expect(roots).toHaveLength(1)
|
||||
expect(replies).toHaveLength(1)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe("tag utilities", () => {
|
||||
it("should deduplicate tags", () => {
|
||||
const tags = [
|
||||
["p", pubkey],
|
||||
["p", pubkey],
|
||||
["p", pubkey, "extra"],
|
||||
]
|
||||
|
||||
const unique = Tags.uniqTags(tags)
|
||||
expect(unique).toHaveLength(1)
|
||||
})
|
||||
|
||||
it("should parse iMeta format", () => {
|
||||
const imeta = [`p ${pubkey}`]
|
||||
const tags = Tags.tagsFromIMeta(imeta)
|
||||
expect(tags).toHaveLength(1)
|
||||
expect(tags[0]).toEqual(["p", pubkey])
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,199 @@
|
||||
import {describe, it, vi, expect, beforeEach} from "vitest"
|
||||
import {hrpToMillisat, getInvoiceAmount, getLnUrl, zapFromEvent, Zapper} from "../src/Zaps"
|
||||
import type {TrustedEvent} from "../src/Events"
|
||||
import {now} from "@welshman/lib"
|
||||
|
||||
describe("Zaps", () => {
|
||||
const recipient = "dd".repeat(32)
|
||||
const zapper = "ee".repeat(32)
|
||||
// nostrPubkey is the pubkey the ln server will use to sign zap receipt events
|
||||
const nostrPubkey = "ff".repeat(32)
|
||||
const currentTime = now()
|
||||
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
})
|
||||
|
||||
describe("hrpToMillisat", () => {
|
||||
it("should convert basic amounts", () => {
|
||||
expect(hrpToMillisat("100")).toBe(BigInt(10000000000000))
|
||||
})
|
||||
|
||||
it("should handle milli amounts", () => {
|
||||
expect(hrpToMillisat("100m")).toBe(BigInt(10000000000))
|
||||
})
|
||||
|
||||
it("should handle micro amounts", () => {
|
||||
expect(hrpToMillisat("100u")).toBe(BigInt(10000000))
|
||||
})
|
||||
|
||||
it("should handle nano amounts", () => {
|
||||
expect(hrpToMillisat("100n")).toBe(BigInt(10000))
|
||||
})
|
||||
|
||||
it("should handle pico amounts", () => {
|
||||
expect(hrpToMillisat("100p")).toBe(BigInt(10))
|
||||
})
|
||||
|
||||
it("should throw on invalid multiplier", () => {
|
||||
expect(() => hrpToMillisat("100x")).toThrow("Not a valid multiplier for the amount")
|
||||
})
|
||||
|
||||
it("should throw on invalid amount", () => {
|
||||
expect(() => hrpToMillisat("ppp")).toThrow("Not a valid human readable amount")
|
||||
})
|
||||
|
||||
it("should throw on amount outside valid range", () => {
|
||||
expect(() => hrpToMillisat("2100000000000000001")).toThrow("Amount is outside of valid range")
|
||||
})
|
||||
})
|
||||
|
||||
describe("getInvoiceAmount", () => {
|
||||
it("should extract amount from bolt11 invoice", () => {
|
||||
const bolt11 = "lnbc100n1..." // Simplified for test
|
||||
expect(getInvoiceAmount(bolt11)).toBe(10000)
|
||||
})
|
||||
})
|
||||
|
||||
describe("getLnUrl", () => {
|
||||
it("should handle lnurl1 addresses", () => {
|
||||
const lnurl =
|
||||
"lnurl1dp68gurn8ghj7um9wfmxjcm99e3k7mf0v9cxj0m385ekvcenxc6r2c35xvukxefcv5mkvv34x5ekzd3ev56nyd3hxqurzepexejxxepnxscrvwfnv9nxzcn9xq6xyefhvgcxxcmyxymnserxfq5fns"
|
||||
expect(getLnUrl(lnurl)).toBe(lnurl)
|
||||
})
|
||||
|
||||
it("should encode regular URLs", () => {
|
||||
const url = "https://example.com/.well-known/lnurlp/test"
|
||||
const result = getLnUrl(url)
|
||||
expect(result?.startsWith("lnurl1")).toBe(true)
|
||||
})
|
||||
|
||||
it("should handle lud16 addresses", () => {
|
||||
const address = "user@domain.com"
|
||||
const result = getLnUrl(address)
|
||||
expect(result?.startsWith("lnurl1")).toBe(true)
|
||||
})
|
||||
|
||||
it("should return null for invalid input", () => {
|
||||
expect(getLnUrl("invalid")).toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe("zapFromEvent", () => {
|
||||
const createZapRequest = (): TrustedEvent => ({
|
||||
id: "ff".repeat(32),
|
||||
sig: "00".repeat(64),
|
||||
kind: 9734,
|
||||
pubkey: zapper,
|
||||
created_at: currentTime,
|
||||
content: "",
|
||||
tags: [
|
||||
["amount", "100000"],
|
||||
["lnurl", "lnurl1..."],
|
||||
["p", recipient],
|
||||
],
|
||||
})
|
||||
|
||||
const createZapReceipt = (request: TrustedEvent): TrustedEvent => ({
|
||||
id: "aa".repeat(32),
|
||||
sig: "11".repeat(64),
|
||||
kind: 9735,
|
||||
pubkey: nostrPubkey,
|
||||
created_at: currentTime + 60,
|
||||
content: "",
|
||||
tags: [
|
||||
["bolt11", "lnbc1000n1..."],
|
||||
["description", JSON.stringify(request)],
|
||||
["p", recipient],
|
||||
["P", zapper],
|
||||
],
|
||||
})
|
||||
|
||||
const validZapper: Zapper = {
|
||||
lnurl: "lnurl1...",
|
||||
pubkey: recipient,
|
||||
nostrPubkey: nostrPubkey,
|
||||
callback: "https://example.com/callback",
|
||||
minSendable: 1000,
|
||||
maxSendable: 100000000,
|
||||
allowsNostr: true,
|
||||
}
|
||||
|
||||
it("should validate a legitimate zap", () => {
|
||||
const request = createZapRequest()
|
||||
const response = createZapReceipt(request)
|
||||
|
||||
const result = zapFromEvent(response, validZapper)
|
||||
|
||||
expect(result).toBeTruthy()
|
||||
expect(result?.request).toEqual(request)
|
||||
expect(result?.response).toEqual(response)
|
||||
expect(result?.invoiceAmount).toBe(100000)
|
||||
})
|
||||
|
||||
it("should reject self-zaps", () => {
|
||||
const request = createZapRequest()
|
||||
request.pubkey = validZapper.pubkey! // Self-zap
|
||||
const response = createZapReceipt(request)
|
||||
|
||||
const result = zapFromEvent(response, validZapper)
|
||||
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
|
||||
it("should reject amount mismatch", () => {
|
||||
const request = createZapRequest()
|
||||
const response = createZapReceipt(request)
|
||||
response.tags = response.tags.map(tag =>
|
||||
tag[0] === "bolt11" ? ["bolt11", "lnbc200n1..."] : tag,
|
||||
)
|
||||
|
||||
const result = zapFromEvent(response, validZapper)
|
||||
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
|
||||
it("should reject incorrect zapper pubkey", () => {
|
||||
const request = createZapRequest()
|
||||
const response = createZapReceipt(request)
|
||||
response.pubkey = "deadbeef".repeat(8) // Not the ln server pubkey
|
||||
|
||||
const result = zapFromEvent(response, validZapper)
|
||||
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
|
||||
it("should reject incorrect lnurl", () => {
|
||||
const request = createZapRequest()
|
||||
request.tags = request.tags.map(tag =>
|
||||
tag[0] === "lnurl" ? ["lnurl", "different_lnurl"] : tag,
|
||||
)
|
||||
const response = createZapReceipt(request)
|
||||
|
||||
const result = zapFromEvent(response, validZapper)
|
||||
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
|
||||
it("should handle invalid description JSON", () => {
|
||||
const response = createZapReceipt(createZapRequest())
|
||||
response.tags = response.tags.map(tag =>
|
||||
tag[0] === "description" ? ["description", "invalid json"] : tag,
|
||||
)
|
||||
|
||||
const result = zapFromEvent(response, validZapper)
|
||||
|
||||
expect(result).toBeNull()
|
||||
})
|
||||
|
||||
it("should accept zap when recipient is zapper", () => {
|
||||
const request = createZapRequest()
|
||||
const response = createZapReceipt(request)
|
||||
response.pubkey = recipient // Recipient is zapper
|
||||
|
||||
const result = zapFromEvent(response, validZapper)
|
||||
|
||||
expect(result).toBeTruthy()
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user