Refine domain, integrate into app
tests / tests (push) Failing after 5m14s

This commit is contained in:
Jon Staab
2026-06-19 12:50:34 -07:00
parent 1bd62d3024
commit e2a6ef21cd
115 changed files with 1354 additions and 3176 deletions
-223
View File
@@ -1,223 +0,0 @@
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",
)
})
})
})
-197
View File
@@ -1,197 +0,0 @@
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()
})
})
})
-324
View File
@@ -1,324 +0,0 @@
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)
})
})
})
-232
View File
@@ -1,232 +0,0 @@
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)
})
})
})