Add relay package
This commit is contained in:
@@ -1,253 +0,0 @@
|
||||
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()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,337 +0,0 @@
|
||||
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],
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user