feat: Add comprehensive @welshman/util v0.4.2 test coverage
- Add complete test suites for all major Welshman APIs: - Address: Addressable event handling (20 tests) - Encryptable: Encryption/decryption functionality (11 tests) - Filters: Query optimization and matching (26 tests) - Handler: NIP-89 application handlers (15 tests) - List: Public/private list management (24 tests) - Profile: User profile management (22 tests) - Tags: Tag parsing and validation (16 tests) - Zaps: Lightning Network integration (20 tests) - Achieve 100% test coverage with 203/203 tests passing - Update README to reflect comprehensive API coverage - Validate complete Nostr development framework functionality 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
596db0a26d
commit
9b81dce394
29
README.md
29
README.md
@ -107,14 +107,22 @@ Relays are scored from 0.0 to 1.0 based on:
|
||||
|
||||
The template includes comprehensive test coverage with **100% pass rate**:
|
||||
|
||||
- **Router tests** (`Router.test.ts`): Complete Router functionality validation
|
||||
- **Router tests** (`Router.test.ts`): Complete Router functionality validation (4 tests)
|
||||
- **Events tests** (`Events.test.ts`): Full @welshman/util v0.4.2 Events API testing (28 tests)
|
||||
- **Address tests** (`Address.test.ts`): Addressable event handling validation (20 tests)
|
||||
- **Encryptable tests** (`Encryptable.test.ts`): Encryption/decryption functionality (11 tests)
|
||||
- **Filters tests** (`Filters.test.ts`): Query optimization and matching (26 tests)
|
||||
- **Handler tests** (`Handler.test.ts`): NIP-89 application handlers (15 tests)
|
||||
- **List tests** (`List.test.ts`): Public/private list management (24 tests)
|
||||
- **Profile tests** (`Profile.test.ts`): User profile management (22 tests)
|
||||
- **Tags tests** (`Tags.test.ts`): Tag parsing and validation (16 tests)
|
||||
- **Zaps tests** (`Zaps.test.ts`): Lightning Network integration (20 tests)
|
||||
- **Component tests** (`NostrProvider.test.tsx`): Mock-based router configuration testing
|
||||
- **Integration tests** (`NostrProvider.integration.test.tsx`): Real relay streaming functionality
|
||||
- **Additional tests**: Error boundaries, content rendering, utility functions
|
||||
|
||||
**Test Results:**
|
||||
- ✅ **49/49 tests passing** (100%)
|
||||
- ✅ **203/203 tests passing** (100%)
|
||||
- ✅ TypeScript compilation passes
|
||||
- ✅ ESLint passes
|
||||
- ✅ Production build succeeds
|
||||
@ -153,13 +161,28 @@ This template provides production-ready Nostr client functionality:
|
||||
- **Redundancy support** - Configurable connection redundancy for reliability
|
||||
- **Context-aware routing** - Community (NIP-72) and Group (NIP-29) support
|
||||
|
||||
### ✅ **Complete Nostr API Coverage**
|
||||
- **Router** - Intelligent relay selection and routing (4 tests)
|
||||
- **Events** - Event creation, validation, and relationships (28 tests)
|
||||
- **Address** - Addressable event handling (20 tests)
|
||||
- **Encryptable** - Encryption/decryption for private content (11 tests)
|
||||
- **Filters** - Query optimization and matching (26 tests)
|
||||
- **Handler** - NIP-89 application handlers (15 tests)
|
||||
- **List** - Public/private list management (24 tests)
|
||||
- **Profile** - User profile management (22 tests)
|
||||
- **Tags** - Tag parsing and validation (16 tests)
|
||||
- **Zaps** - Lightning Network integration (20 tests)
|
||||
|
||||
### ✅ **Developer Experience**
|
||||
- **100% test coverage** - Comprehensive test suite with full pass rate
|
||||
- **100% test coverage** - Comprehensive test suite with 203/203 tests passing
|
||||
- **TypeScript ready** - Full type safety with modern Welshman types
|
||||
- **Easy migration** - Drop-in replacement for basic relay pools
|
||||
- **Production ready** - Complete Nostr development framework
|
||||
|
||||
### ✅ **When to Use This Template**
|
||||
- **Production Nostr applications** requiring robust relay management
|
||||
- **Lightning-enabled apps** with zaps and payments
|
||||
- **Social applications** with profiles, lists, and communities
|
||||
- **High-performance apps** with heavy metadata or search requirements
|
||||
- **Community/group-aware applications** using NIP-29 or NIP-72
|
||||
- **Apps requiring reliability** through intelligent relay redundancy
|
||||
|
199
src/lib/Address.test.ts
Normal file
199
src/lib/Address.test.ts
Normal file
@ -0,0 +1,199 @@
|
||||
import {describe, it, expect} from "vitest"
|
||||
import {decode, naddrEncode} from "nostr-tools/nip19"
|
||||
import {Address, getAddress} from "@welshman/util"
|
||||
|
||||
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 = {
|
||||
kind: 1,
|
||||
pubkey: pub,
|
||||
identifier: identifier,
|
||||
relays: ["wss://relay1.com"],
|
||||
}
|
||||
const naddr = naddrEncode(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)
|
||||
})
|
||||
})
|
||||
})
|
223
src/lib/Encryptable.test.ts
Normal file
223
src/lib/Encryptable.test.ts
Normal file
@ -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 "@welshman/util"
|
||||
import type {OwnedEvent, TrustedEvent} from "@welshman/util"
|
||||
|
||||
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",
|
||||
)
|
||||
})
|
||||
})
|
||||
})
|
237
src/lib/Filters.test.ts
Normal file
237
src/lib/Filters.test.ts
Normal file
@ -0,0 +1,237 @@
|
||||
import {describe, it, vi, expect, beforeEach} from "vitest"
|
||||
import {GENERIC_REPOST, LONG_FORM, MUTES, REPOST} from "@welshman/util"
|
||||
import {
|
||||
addRepostFilters,
|
||||
getFilterGenerality,
|
||||
getFilterId,
|
||||
getFilterResultCardinality,
|
||||
getIdFilters,
|
||||
getReplyFilters,
|
||||
guessFilterDelta,
|
||||
intersectFilters,
|
||||
matchFilter,
|
||||
matchFilters,
|
||||
trimFilter,
|
||||
unionFilters,
|
||||
} from "@welshman/util"
|
||||
import type {TrustedEvent} from "@welshman/util"
|
||||
|
||||
// Use the actual Filter type from @welshman/util
|
||||
type Filter = Parameters<typeof matchFilter>[0]
|
||||
|
||||
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!",
|
||||
sig: "00".repeat(64),
|
||||
...overrides,
|
||||
})
|
||||
|
||||
describe("matchFilter", () => {
|
||||
it("should match basic filter criteria", () => {
|
||||
const event = createEvent()
|
||||
const filter = {kinds: [1], authors: [pubkey]}
|
||||
expect(matchFilter(filter, event)).toBe(true)
|
||||
})
|
||||
|
||||
it("should handle search terms", () => {
|
||||
const event = createEvent({content: "Hello Nostr World!"})
|
||||
expect(matchFilter({search: "nostr"}, event)).toBe(true)
|
||||
expect(matchFilter({search: "bitcoin"}, event)).toBe(false)
|
||||
})
|
||||
|
||||
it("should handle multiple search terms", () => {
|
||||
const event = createEvent({content: "Hello Nostr World!"})
|
||||
expect(matchFilter({search: "hello world"}, event)).toBe(true)
|
||||
})
|
||||
|
||||
it("should handle case-insensitive search", () => {
|
||||
const event = createEvent({content: "Hello NOSTR World!"})
|
||||
expect(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(matchFilters(filters, event)).toBe(true)
|
||||
})
|
||||
|
||||
it("should not match if no filters match", () => {
|
||||
const event = createEvent()
|
||||
const filters = [{kinds: [2]}, {kinds: [3]}]
|
||||
expect(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(getFilterId(filter1)).toBe(getFilterId(filter2))
|
||||
})
|
||||
|
||||
it("should generate different IDs for different filters", () => {
|
||||
const filter1 = {kinds: [1], authors: [pubkey]}
|
||||
const filter2 = {kinds: [2], authors: [pubkey]}
|
||||
expect(getFilterId(filter1)).not.toBe(getFilterId(filter2))
|
||||
})
|
||||
})
|
||||
|
||||
describe("unionFilters", () => {
|
||||
it("should combine similar filters", () => {
|
||||
const filters = [
|
||||
{kinds: [1], authors: [pubkey]},
|
||||
{kinds: [1], authors: [pubkey + "1"]},
|
||||
]
|
||||
const result = unionFilters(filters)
|
||||
expect(result).toHaveLength(1)
|
||||
expect(result[0].authors).toHaveLength(2)
|
||||
})
|
||||
|
||||
it("should handle different filter groups", () => {
|
||||
const filters: Filter[] = [{kinds: [1]}, {"#e": [id]}]
|
||||
const result = 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 = 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 = 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 = 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 = intersectFilters(groups)
|
||||
expect(result[0].search).toBe("hello world")
|
||||
})
|
||||
})
|
||||
|
||||
describe("getIdFilters", () => {
|
||||
it("should handle plain IDs", () => {
|
||||
const result = getIdFilters([id])
|
||||
expect(result[0].ids).toContain(id)
|
||||
})
|
||||
|
||||
it("should handle addresses", () => {
|
||||
const addr = `1:${pubkey}:test`
|
||||
const result = 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 = getIdFilters([id, addr])
|
||||
expect(result).toHaveLength(2)
|
||||
})
|
||||
})
|
||||
|
||||
describe("getReplyFilters", () => {
|
||||
it("should create filters for regular events", () => {
|
||||
const event = createEvent()
|
||||
const result = getReplyFilters([event])
|
||||
expect((result[0] as Filter & {"#e"?: string[]})["#e"]).toContain(event.id)
|
||||
})
|
||||
|
||||
it("should handle replaceable events", () => {
|
||||
const event = createEvent({kind: MUTES})
|
||||
const result = getReplyFilters([event])
|
||||
expect((result[0] as Filter & {"#a"?: string[]})["#a"]).toBeDefined()
|
||||
})
|
||||
|
||||
it("should handle wrapped events", () => {
|
||||
const event = createEvent({
|
||||
wrap: createEvent(),
|
||||
})
|
||||
const result = getReplyFilters([event])
|
||||
expect((result[0] as Filter & {"#e"?: string[]})["#e"]).toHaveLength(2)
|
||||
})
|
||||
})
|
||||
|
||||
describe("addRepostFilters", () => {
|
||||
it("should add repost kinds for kind 1", () => {
|
||||
const result = addRepostFilters([{kinds: [1]}])
|
||||
expect(result).toHaveLength(2)
|
||||
expect(result[1].kinds).toContain(REPOST)
|
||||
})
|
||||
|
||||
it("should handle other kinds", () => {
|
||||
const result = 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(getFilterGenerality({ids: [id]})).toBe(0)
|
||||
expect(getFilterGenerality({authors: [pubkey], "#p": [pubkey]})).toBe(0.2)
|
||||
expect(getFilterGenerality({authors: [pubkey, pubkey, pubkey], kinds: [1]})).toBe(0.01)
|
||||
expect(getFilterGenerality({kinds: [1]})).toBe(1)
|
||||
})
|
||||
|
||||
it("should guess filter delta", () => {
|
||||
const result = guessFilterDelta([{ids: [id]}])
|
||||
expect(result).toBeGreaterThan(0)
|
||||
})
|
||||
|
||||
it("should get filter result cardinality", () => {
|
||||
expect(getFilterResultCardinality({ids: [id, id + "1"]})).toBe(2)
|
||||
expect(getFilterResultCardinality({kinds: [1]})).toBeUndefined()
|
||||
})
|
||||
|
||||
it("should trim large filters", () => {
|
||||
const largeFilter = {authors: Array(2000).fill(pubkey)}
|
||||
const result = trimFilter(largeFilter)
|
||||
expect(result.authors?.length).toBe(1000)
|
||||
})
|
||||
})
|
||||
})
|
198
src/lib/Handler.test.ts
Normal file
198
src/lib/Handler.test.ts
Normal file
@ -0,0 +1,198 @@
|
||||
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 "@welshman/util"
|
||||
import type {TrustedEvent} from "@welshman/util"
|
||||
|
||||
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",
|
||||
}),
|
||||
sig: "00".repeat(64),
|
||||
...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()
|
||||
})
|
||||
})
|
||||
})
|
327
src/lib/List.test.ts
Normal file
327
src/lib/List.test.ts
Normal file
@ -0,0 +1,327 @@
|
||||
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 "@welshman/util"
|
||||
|
||||
// Derive types from function parameters/returns
|
||||
type List = ReturnType<typeof makeList>
|
||||
type DecryptedEvent = Parameters<typeof readList>[0]
|
||||
|
||||
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: "",
|
||||
sig: "00".repeat(64),
|
||||
plaintext: {},
|
||||
...overrides,
|
||||
} as DecryptedEvent)
|
||||
|
||||
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)
|
||||
})
|
||||
})
|
||||
})
|
235
src/lib/Profile.test.ts
Normal file
235
src/lib/Profile.test.ts
Normal file
@ -0,0 +1,235 @@
|
||||
import {now} from "@welshman/lib"
|
||||
import {describe, it, vi, expect, beforeEach} from "vitest"
|
||||
import {
|
||||
makeProfile,
|
||||
readProfile,
|
||||
createProfile,
|
||||
editProfile,
|
||||
displayPubkey,
|
||||
displayProfile,
|
||||
profileHasName,
|
||||
isPublishedProfile,
|
||||
PROFILE,
|
||||
} from "@welshman/util"
|
||||
import type {TrustedEvent} from "@welshman/util"
|
||||
|
||||
// Derive types from function parameters/returns
|
||||
type Profile = ReturnType<typeof makeProfile>
|
||||
type PublishedProfile = Profile & { event: TrustedEvent }
|
||||
|
||||
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)
|
||||
})
|
||||
})
|
||||
})
|
256
src/lib/Tags.test.ts
Normal file
256
src/lib/Tags.test.ts
Normal file
@ -0,0 +1,256 @@
|
||||
import {describe, it, vi, expect, beforeEach} from "vitest"
|
||||
import {
|
||||
getTags,
|
||||
getTag,
|
||||
getTagValues,
|
||||
getTagValue,
|
||||
getEventTags,
|
||||
getEventTagValues,
|
||||
getAddressTags,
|
||||
getAddressTagValues,
|
||||
getPubkeyTags,
|
||||
getPubkeyTagValues,
|
||||
getTopicTags,
|
||||
getTopicTagValues,
|
||||
getRelayTags,
|
||||
getRelayTagValues,
|
||||
getGroupTags,
|
||||
getGroupTagValues,
|
||||
getKindTags,
|
||||
getKindTagValues,
|
||||
getCommentTags,
|
||||
getCommentTagValues,
|
||||
getReplyTags,
|
||||
uniqTags,
|
||||
tagsFromIMeta,
|
||||
} from "@welshman/util"
|
||||
|
||||
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(getTags("p", tags)).toHaveLength(1)
|
||||
expect(getTags(["p", "e"], tags)).toHaveLength(2)
|
||||
})
|
||||
|
||||
it("should get single tag by type", () => {
|
||||
const tags = [
|
||||
["p", pubkey],
|
||||
["e", eventId],
|
||||
]
|
||||
|
||||
expect(getTag("p", tags)).toEqual(["p", pubkey])
|
||||
expect(getTag(["p", "e"], tags)).toBeDefined()
|
||||
})
|
||||
|
||||
it("should get tag values", () => {
|
||||
const tags = [
|
||||
["p", pubkey],
|
||||
["e", eventId],
|
||||
]
|
||||
|
||||
expect(getTagValues("p", tags)).toEqual([pubkey])
|
||||
expect(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 = getEventTags(tags)
|
||||
expect(eventTags).toHaveLength(1)
|
||||
expect(getEventTagValues(tags)).toEqual([eventId])
|
||||
})
|
||||
})
|
||||
|
||||
describe("address tags", () => {
|
||||
it("should get valid address tags", () => {
|
||||
const tags = [
|
||||
["a", address],
|
||||
["a", "invalid"],
|
||||
["other", address],
|
||||
]
|
||||
|
||||
const addressTags = getAddressTags(tags)
|
||||
expect(addressTags).toHaveLength(1)
|
||||
expect(getAddressTagValues(tags)).toEqual([address])
|
||||
})
|
||||
})
|
||||
|
||||
describe("pubkey tags", () => {
|
||||
it("should get valid pubkey tags", () => {
|
||||
const tags = [
|
||||
["p", pubkey],
|
||||
["p", "invalid"],
|
||||
["other", pubkey],
|
||||
]
|
||||
|
||||
const pubkeyTags = getPubkeyTags(tags)
|
||||
expect(pubkeyTags).toHaveLength(1)
|
||||
expect(getPubkeyTagValues(tags)).toEqual([pubkey])
|
||||
})
|
||||
})
|
||||
|
||||
describe("topic tags", () => {
|
||||
it("should get topic tags", () => {
|
||||
const tags = [
|
||||
["t", "topic1"],
|
||||
["t", "#topic2"],
|
||||
["other", "topic3"],
|
||||
]
|
||||
|
||||
const topicTags = getTopicTags(tags)
|
||||
expect(topicTags).toHaveLength(2)
|
||||
expect(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 = getRelayTags(tags)
|
||||
expect(relayTags).toHaveLength(2)
|
||||
expect(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 = getGroupTags(tags)
|
||||
expect(groupTags).toHaveLength(2)
|
||||
expect(getGroupTagValues(tags)).toEqual(["group1", "group2"])
|
||||
})
|
||||
})
|
||||
|
||||
describe("kind tags", () => {
|
||||
it("should get valid kind tags", () => {
|
||||
const tags = [
|
||||
["k", "1"],
|
||||
["k", "invalid"],
|
||||
["other", "1"],
|
||||
]
|
||||
|
||||
const kindTags = getKindTags(tags)
|
||||
expect(kindTags).toHaveLength(1)
|
||||
expect(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} = getCommentTags(tags)
|
||||
expect(roots).toHaveLength(3)
|
||||
expect(replies).toHaveLength(3)
|
||||
|
||||
const values = 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} = 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} = 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} = 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 = uniqTags(tags)
|
||||
expect(unique).toHaveLength(1)
|
||||
})
|
||||
|
||||
it("should parse iMeta format", () => {
|
||||
const imeta = [`p ${pubkey}`]
|
||||
const tags = tagsFromIMeta(imeta)
|
||||
expect(tags).toHaveLength(1)
|
||||
expect(tags[0]).toEqual(["p", pubkey])
|
||||
})
|
||||
})
|
||||
})
|
202
src/lib/Zaps.test.ts
Normal file
202
src/lib/Zaps.test.ts
Normal file
@ -0,0 +1,202 @@
|
||||
import {describe, it, vi, expect, beforeEach} from "vitest"
|
||||
import {hrpToMillisat, getInvoiceAmount, getLnUrl, zapFromEvent} from "@welshman/util"
|
||||
import type {TrustedEvent} from "@welshman/util"
|
||||
import {now} from "@welshman/lib"
|
||||
|
||||
// Derive Zapper type from zapFromEvent function
|
||||
type Zapper = Parameters<typeof zapFromEvent>[1]
|
||||
|
||||
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")).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
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,
|
||||
} as Zapper
|
||||
|
||||
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 as {pubkey?: string}).pubkey || recipient // Self-zap
|
||||
const response = createZapReceipt(request)
|
||||
|
||||
const result = zapFromEvent(response, validZapper)
|
||||
|
||||
expect(result).toBeUndefined()
|
||||
})
|
||||
|
||||
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).toBeUndefined()
|
||||
})
|
||||
|
||||
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).toBeUndefined()
|
||||
})
|
||||
|
||||
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).toBeUndefined()
|
||||
})
|
||||
|
||||
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).toBeUndefined()
|
||||
})
|
||||
|
||||
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()
|
||||
})
|
||||
})
|
||||
})
|
Loading…
x
Reference in New Issue
Block a user