Fix some tests

This commit is contained in:
Jon Staab
2025-04-08 10:17:30 -07:00
parent 1f7101daee
commit 74b20da8fb
11 changed files with 144 additions and 376 deletions
+2 -2
View File
@@ -85,7 +85,7 @@ describe("collection", () => {
})
await col.loadItem("1")
expect(mockLoad).toHaveBeenCalledWith("1")
expect(mockLoad).toHaveBeenCalledWith("1", [])
})
it("should handle concurrent loading of the same item", async () => {
@@ -212,7 +212,7 @@ describe("collection", () => {
})
col.deriveItem("1")
expect(mockLoad).toHaveBeenCalledWith("1")
expect(mockLoad).toHaveBeenCalledWith("1", [])
})
})
-200
View File
@@ -1,200 +0,0 @@
import {describe, it, expect, vi, beforeEach, afterEach} from "vitest"
import {writable, get} from "svelte/store"
import {Repository} from "@welshman/relay"
import {Tracker} from "@welshman/net"
import {
initStorage,
clearStorage,
storageAdapters,
dead,
getAll,
bulkPut,
bulkDelete,
} from "../src/storage"
describe("storage", () => {
const DB_NAME = "test-db"
const DB_VERSION = 1
beforeEach(async () => {
vi.clearAllMocks()
dead.set(false)
vi.useFakeTimers()
})
afterEach(async () => {
vi.useRealTimers()
await clearStorage()
// Clean up the test database
await new Promise((resolve, reject) => {
const req = indexedDB.deleteDatabase(DB_NAME)
req.onsuccess = () => resolve(undefined)
req.onerror = () => reject(req.error)
})
})
describe("basic operations", () => {
it("should initialize storage and store items", async () => {
const store = writable<{id: string; value: string}[]>([])
const adapters = {
items: storageAdapters.fromCollectionStore("id", store),
}
initStorage(DB_NAME, DB_VERSION, adapters)
await vi.runAllTimersAsync()
store.set([
{id: "1", value: "test1"},
{id: "2", value: "test2"},
])
const itemsPromise = getAll("items")
await vi.runAllTimersAsync()
const items = await itemsPromise
expect(items).toHaveLength(2)
expect(items).toContainEqual({id: "1", value: "test1"})
expect(items).toContainEqual({id: "2", value: "test2"})
})
it("should update items when store changes", async () => {
const store = writable<{id: string; value: string}[]>([])
const adapters = {
items: storageAdapters.fromCollectionStore("id", store),
}
initStorage(DB_NAME, DB_VERSION, adapters)
await vi.runAllTimersAsync()
// init storage with the first item
store.set([{id: "1", value: "test1"}])
store.update(items => [...items, {id: "2", value: "test2"}])
const itemsPromise = getAll("items")
await vi.runAllTimersAsync()
const items = await itemsPromise
expect(items).toHaveLength(2)
expect(items).toContainEqual({id: "2", value: "test2"})
})
it("should remove items when deleted from store", async () => {
const store = writable<{id: string; value: string}[]>()
const adapters = {
items: storageAdapters.fromCollectionStore("id", store),
}
initStorage(DB_NAME, DB_VERSION, adapters)
await vi.runAllTimersAsync()
store.set([
{id: "1", value: "test1"},
{id: "2", value: "test2"},
])
store.update(items => items.filter(item => item.id !== "1"))
await vi.runAllTimersAsync()
const itemsPromise = getAll("items")
await vi.runAllTimersAsync()
const items = await itemsPromise
await vi.runAllTimersAsync()
expect(items).toHaveLength(1)
expect(items[0]).toEqual({id: "2", value: "test2"})
})
})
describe("storage adapters", () => {
it("should handle repository adapter", async () => {
const repository = new Repository()
const adapters = {
events: storageAdapters.fromRepository(repository),
}
initStorage(DB_NAME, DB_VERSION, adapters)
await vi.runAllTimersAsync()
const event = {
id: "test-id",
pubkey: "test-pubkey",
kind: 1,
created_at: 123,
content: "test",
tags: [],
}
repository.publish(event)
const eventsPromise = getAll("events")
await vi.runAllTimersAsync()
const events = await eventsPromise
expect(events).toContainEqual(event)
})
it("should handle tracker adapter", async () => {
const tracker = new Tracker()
const adapters = {
relays: storageAdapters.fromTracker(tracker),
}
initStorage(DB_NAME, DB_VERSION, adapters)
await vi.runAllTimersAsync()
tracker.track("event1", "relay1")
tracker.track("event1", "relay2")
const relaysPromise = getAll("relays")
await vi.runAllTimersAsync()
const relays = await relaysPromise
expect(relays).toContainEqual({
key: "event1",
value: ["relay1", "relay2"],
})
})
})
describe("error handling", () => {
it("should handle initialization errors", async () => {
const badAdapter = {
keyPath: undefined,
store: writable([]),
options: {},
}
const rejectPromise = initStorage(DB_NAME, DB_VERSION, {bad: badAdapter})
await vi.runAllTimersAsync()
// we can initialize storage with an undefined keypath
expect(rejectPromise).to.not.rejects
})
it("should prevent multiple initializations", async () => {
const adapters = {
test: {
keyPath: "id",
store: writable([]),
options: {},
},
}
initStorage(DB_NAME, DB_VERSION, adapters)
await vi.runAllTimersAsync()
await expect(initStorage(DB_NAME, DB_VERSION, adapters)).rejects.toThrow(
"Db initialized multiple times",
)
})
})
})
+11 -12
View File
@@ -15,7 +15,7 @@ import {
prepEvent,
publishThunk,
publishThunks,
thunkWorker,
thunkQueue,
walkThunks,
} from "../src/thunk"
@@ -34,12 +34,13 @@ describe("thunk", () => {
addSession({method: 'nip01', secret, pubkey})
})
afterEach(() => {
afterEach(async () => {
thunkQueue.stop()
thunkQueue.clear()
await vi.runAllTimersAsync()
vi.useRealTimers()
vi.clearAllMocks()
thunkWorker.clear()
thunkWorker.pause()
thunkWorker.resume()
thunkQueue.start()
dropSession(pubkey)
})
@@ -110,23 +111,21 @@ describe("thunk", () => {
it("should update status during publishing", async () => {
const send = vi.fn()
const track = vi.spyOn(tracker, 'track')
const statuses: Map<string, any> = new Map<string, any>()
const thunk = makeThunk(mockRequest)
let status = {}
// Subscribe to status updates
thunk.status.subscribe(status => {
for (const [key, value] of Object.entries(status)) {
statuses.set(key, value)
}
thunk.status.subscribe(_status => {
status = _status
})
// Start the publish process
thunkWorker.push(thunk)
thunkQueue.push(thunk)
// Wait for initial async operations
await vi.runAllTimersAsync()
expect(statuses.get(LOCAL_RELAY_URL)).toEqual({
expect(status[LOCAL_RELAY_URL]).toEqual({
status: PublishStatus.Success,
message: "",
})
+1 -1
View File
@@ -151,7 +151,7 @@ describe("getAdapter", () => {
const adapter = getAdapter(url, { getAdapter: getCustomAdapter })
expect(getCustomAdapter).toHaveBeenCalledWith(url, { getAdapter: getCustomAdapter })
expect(getCustomAdapter).toHaveBeenCalledWith(url, expect.objectContaining({ getAdapter: getCustomAdapter }))
expect(adapter).toBe(customAdapter)
})
})
+61 -104
View File
@@ -2,18 +2,18 @@ import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"
import { Socket, SocketStatus, SocketEvent } from "../src/socket"
import { makeEvent, CLIENT_AUTH } from "@welshman/util"
import { Nip01Signer } from "@welshman/signer"
import { AuthState, AuthStatus, AuthStateEvent, AuthManager, makeAuthEvent } from "../src/auth"
import { AuthState, AuthStatus, AuthStateEvent, makeAuthEvent } from "../src/auth"
import EventEmitter from "events"
import { RelayMessage } from "../src/message"
vi.mock('isomorphic-ws', () => {
const WebSocket = vi.fn(function () {
const WebSocket = vi.fn(function (this: any) {
setTimeout(() => this.onopen())
})
WebSocket.prototype.send = vi.fn()
WebSocket.prototype.close = vi.fn(function () {
WebSocket.prototype.close = vi.fn(function (this: any) {
this.onclose()
})
@@ -22,183 +22,140 @@ vi.mock('isomorphic-ws', () => {
describe('auth', () => {
let socket: Socket
let authManager: AuthManager
let sign = vi.fn(Nip01Signer.ephemeral().sign)
beforeEach(() => {
socket = new Socket('wss://test.relay')
authManager = new AuthManager(socket, { sign })
})
afterEach(() => {
vi.clearAllMocks()
socket.cleanup()
authManager.cleanup()
})
describe("AuthState", () => {
it("should initialize with None status", () => {
expect(authManager.state.status).toBe(AuthStatus.None)
expect(socket.auth.status).toBe(AuthStatus.None)
})
it("should handle AUTH message from relay", () => {
const message: RelayMessage = ["AUTH", "challenge123"]
socket.emit(SocketEvent.Receive, message)
expect(authManager.state.challenge).toBe("challenge123")
expect(authManager.state.status).toBe(AuthStatus.Requested)
expect(socket.auth.challenge).toBe("challenge123")
expect(socket.auth.status).toBe(AuthStatus.Requested)
})
it("should handle successful OK message", () => {
authManager.state.request = "request123"
socket.auth.request = "request123"
const message: RelayMessage = ["OK", "request123", true, "success"]
socket.emit(SocketEvent.Receive, message)
expect(authManager.state.status).toBe(AuthStatus.Ok)
expect(authManager.state.details).toBe("success")
expect(socket.auth.status).toBe(AuthStatus.Ok)
expect(socket.auth.details).toBe("success")
})
it("should handle failed OK message", () => {
authManager.state.request = "request123"
socket.auth.request = "request123"
const message: RelayMessage = ["OK", "request123", false, "forbidden"]
socket.emit(SocketEvent.Receive, message)
expect(authManager.state.status).toBe(AuthStatus.Forbidden)
expect(authManager.state.details).toBe("forbidden")
expect(socket.auth.status).toBe(AuthStatus.Forbidden)
expect(socket.auth.details).toBe("forbidden")
})
it("should ignore OK messages for different requests", () => {
authManager.state.request = "request123"
socket.auth.request = "request123"
const message: RelayMessage = ["OK", "different-request", true, "success"]
socket.emit(SocketEvent.Receive, message)
expect(authManager.state.status).toBe(AuthStatus.None)
expect(socket.auth.status).toBe(AuthStatus.None)
})
it("should handle client AUTH message", () => {
const message: RelayMessage = ["AUTH", { id: "123", kind: CLIENT_AUTH }]
socket.emit(SocketEvent.Sending, message)
expect(authManager.state.status).toBe(AuthStatus.PendingResponse)
expect(socket.auth.status).toBe(AuthStatus.PendingResponse)
})
it("should reset state on socket close", () => {
authManager.state.challenge = "challenge123"
authManager.state.request = "request123"
authManager.state.details = "details"
authManager.state.status = AuthStatus.PendingResponse
socket.auth.challenge = "challenge123"
socket.auth.request = "request123"
socket.auth.details = "details"
socket.auth.status = AuthStatus.PendingResponse
socket.emit(SocketEvent.Status, SocketStatus.Closed)
expect(authManager.state.challenge).toBeUndefined()
expect(authManager.state.request).toBeUndefined()
expect(authManager.state.details).toBeUndefined()
expect(authManager.state.status).toBe(AuthStatus.None)
expect(socket.auth.challenge).toBeUndefined()
expect(socket.auth.request).toBeUndefined()
expect(socket.auth.details).toBeUndefined()
expect(socket.auth.status).toBe(AuthStatus.None)
})
it("should emit status changes", () => {
const statusSpy = vi.fn()
authManager.state.on(AuthStateEvent.Status, statusSpy)
socket.auth.on(AuthStateEvent.Status, statusSpy)
authManager.state.setStatus(AuthStatus.Requested)
socket.auth.setStatus(AuthStatus.Requested)
expect(statusSpy).toHaveBeenCalledWith(AuthStatus.Requested)
})
it("should cleanup properly", () => {
const removeListenersSpy = vi.spyOn(authManager.state, "removeAllListeners")
authManager.state.cleanup()
const removeListenersSpy = vi.spyOn(socket.auth, "removeAllListeners")
socket.auth.cleanup()
expect(removeListenersSpy).toHaveBeenCalled()
})
})
describe("AuthManager", () => {
it("should create AuthState instance", () => {
expect(authManager.state).toBeInstanceOf(AuthState)
describe("authenticate", () => {
it("should throw an error when there is no challenge", async () => {
const sign = vi.fn()
await expect(socket.auth.authenticate(sign)).rejects.toThrow(
"Attempted to authenticate with no challenge"
)
})
it("should respond automatically when eager is true", () => {
const respondSpy = vi.spyOn(AuthManager.prototype, "respond")
const eagerManager = new AuthManager(socket, { sign, eager: true })
it("should throw an error when status is not requested", async () => {
const sign = vi.fn()
socket.emit(SocketEvent.Receive, ["AUTH", "challenge123"])
socket.auth.challenge = "challenge123"
socket.auth.status = AuthStatus.PendingResponse
expect(respondSpy).toHaveBeenCalled()
await expect(socket.auth.authenticate(sign)).rejects.toThrow(
"Attempted to authenticate when auth is already auth:status:pending_response"
)
})
it("should not respond automatically when eager is false", () => {
const respondSpy = vi.spyOn(AuthManager.prototype, "respond")
socket.emit(SocketEvent.Receive, ["AUTH", "challenge123"])
it("should update status when signature fails", async () => {
const sign = vi.fn()
expect(respondSpy).not.toHaveBeenCalled()
socket.auth.challenge = "challenge123"
socket.auth.status = AuthStatus.Requested
await socket.auth.authenticate(sign)
expect(socket.auth.status).toBe(AuthStatus.DeniedSignature)
})
describe("respond", () => {
it("should throw error if no challenge", async () => {
await expect(authManager.respond()).rejects.toThrow("Attempted to authenticate with no challenge")
})
it("should send AUTH message", async () => {
const sendSpy = vi.spyOn(socket, 'send')
let event
it("should throw error if status is not Requested", async () => {
authManager.state.challenge = "challenge123"
authManager.state.status = AuthStatus.PendingSignature
socket.auth.challenge = "challenge123"
socket.auth.status = AuthStatus.Requested
await expect(authManager.respond()).rejects.toThrow("Attempted to authenticate when auth is already auth:status:pending_signature")
})
const sign = async e => {
event = await Nip01Signer.ephemeral().sign(e)
it("should handle successful sign", async () => {
const sendSpy = vi.spyOn(socket, 'send')
return event
}
authManager.state.challenge = "challenge123"
authManager.state.status = AuthStatus.Requested
const signedEvent = { id: "signed-event-id", kind: CLIENT_AUTH }
sign.mockResolvedValue(signedEvent)
await socket.auth.authenticate(sign)
await authManager.respond()
expect(authManager.state.request).toBe("signed-event-id")
expect(sendSpy).toHaveBeenCalledWith(["AUTH", signedEvent])
})
it("should handle denied signature", async () => {
const sendSpy = vi.spyOn(socket, 'send')
authManager.state.challenge = "challenge123"
authManager.state.status = AuthStatus.Requested
sign.mockResolvedValue(null)
await authManager.respond()
expect(authManager.state.status).toBe(AuthStatus.DeniedSignature)
expect(sendSpy).not.toHaveBeenCalled()
})
})
describe("attempt", () => {
it("should attempt to open socket", async () => {
const attemptToOpenSpy = vi.spyOn(socket, 'attemptToOpen')
await authManager.attempt()
expect(attemptToOpenSpy).toHaveBeenCalled()
})
it("should wait for challenge", async () => {
const waitForChallengeSpy = vi.spyOn(authManager, "waitForChallenge")
await authManager.attempt()
expect(waitForChallengeSpy).toHaveBeenCalled()
})
it("should respond if challenge received", async () => {
const respondSpy = vi.spyOn(authManager, "respond")
authManager.state.challenge = "challenge123"
authManager.state.status = AuthStatus.Requested
await authManager.attempt()
expect(respondSpy).toHaveBeenCalled()
})
it("should wait for resolution", async () => {
const waitForResolutionSpy = vi.spyOn(authManager, "waitForResolution")
await authManager.attempt()
expect(waitForResolutionSpy).toHaveBeenCalled()
})
expect(socket.auth.request).toStrictEqual(event.id)
expect(sendSpy).toHaveBeenCalledWith(["AUTH", event])
})
})
})
+27 -23
View File
@@ -42,24 +42,24 @@ describe('policy', () => {
describe("socketPolicyAuthBuffer", () => {
it("should buffer messages when not authenticated", () => {
const cleanup = socketPolicyAuthBuffer(socket)
const removeSpy = vi.spyOn(socket._sendQueue, 'remove')
const sendSpy = vi.spyOn(socket, 'send')
socket.emit(SocketEvent.Receive, ["AUTH", "challenge"])
// Regular event should be buffered
const event: ClientMessage = ["EVENT", { id: "123"}]
socket.send(event)
expect(removeSpy).toHaveBeenCalledWith(event)
expect(sendSpy).toHaveBeenCalledWith(event)
// Auth event should not be buffered
const authEvent: ClientMessage = ["AUTH", { id: "456" }]
socket.send(authEvent)
expect(removeSpy).not.toHaveBeenCalledWith(authEvent)
expect(sendSpy).toHaveBeenCalledWith(authEvent)
// Auth join event should not be buffered
const joinEvent: ClientMessage = ["EVENT", { id: "789", kind: AUTH_JOIN }]
socket.send(joinEvent)
expect(removeSpy).not.toHaveBeenCalledWith(joinEvent)
expect(sendSpy).toHaveBeenCalledWith(joinEvent)
cleanup()
})
@@ -88,7 +88,7 @@ describe('policy', () => {
it("should handle CLOSE messages properly", () => {
const cleanup = socketPolicyAuthBuffer(socket)
const removeSpy = vi.spyOn(socket._sendQueue, 'remove')
const sendSpy = vi.spyOn(socket, 'send')
socket.emit(SocketEvent.Receive, ["AUTH", "challenge"])
@@ -100,55 +100,59 @@ describe('policy', () => {
const close: ClientMessage = ["CLOSE", "123"]
socket.send(close)
// Both messages should be removed
expect(removeSpy).toHaveBeenCalledWith(req)
expect(removeSpy).toHaveBeenCalledWith(close)
// Both messages should be sent
expect(sendSpy).toHaveBeenCalledWith(req)
expect(sendSpy).toHaveBeenCalledWith(close)
cleanup()
})
it("should retry events once when auth-required", () => {
const cleanup = socketPolicyAuthBuffer(socket)
const sendSpy = vi.spyOn(socket, 'send')
const recvQueueRemoveSpy = vi.spyOn(socket._recvQueue, 'remove')
// Send an event
const event: ClientMessage = ["EVENT", { id: "123", kind: 1, content: "", tags: [], pubkey: "", sig: "" }]
socket.emit(SocketEvent.Send, event)
// Receive auth-required rejection
socket.emit(SocketEvent.Receive, ["OK", "123", false, "auth-required: need to auth first"])
const authReqMsg: RelayMessage = ["OK", "123", false, "auth-required: need to auth first"]
socket.emit(SocketEvent.Receiving, authReqMsg)
// Should retry the event
expect(sendSpy).toHaveBeenCalledWith(event)
// Should remove the auth-required message
expect(recvQueueRemoveSpy).toHaveBeenCalledWith(authReqMsg)
// Receive another auth-required rejection
socket.emit(SocketEvent.Receive, ["OK", "123", false, "auth-required: need to auth first"])
const authReqMsg2: RelayMessage = ["OK", "123", false, "auth-required: need to auth first"]
socket.emit(SocketEvent.Receiving, authReqMsg2)
// Should not retry again
expect(sendSpy).toHaveBeenCalledTimes(1)
// Should remove the second auth-required message too
expect(recvQueueRemoveSpy).toHaveBeenCalledWith(authReqMsg2)
cleanup()
})
it("should retry REQ once when auth-required", () => {
const cleanup = socketPolicyAuthBuffer(socket)
const sendSpy = vi.spyOn(socket, 'send')
const recvQueueRemoveSpy = vi.spyOn(socket._recvQueue, 'remove')
// Send a REQ
const req: ClientMessage = ["REQ", "123", { kinds: [1] }]
socket.emit(SocketEvent.Send, req)
// Receive auth-required rejection via CLOSED
socket.emit(SocketEvent.Receive, ["CLOSED", "123", "auth-required: need to auth first"])
// Receive auth-required rejection
const authReqMsg: RelayMessage = ["OK", "123", false, "auth-required: need to auth first"]
socket.emit(SocketEvent.Receiving, authReqMsg)
// Should retry the request
expect(sendSpy).toHaveBeenCalledWith(req)
// Should remove the auth-required message
expect(recvQueueRemoveSpy).toHaveBeenCalledWith(authReqMsg)
// Receive another auth-required rejection
socket.emit(SocketEvent.Receive, ["CLOSED", "123", "auth-required: need to auth first"])
const authReqMsg2: RelayMessage = ["OK", "123", false, "auth-required: need to auth first"]
socket.emit(SocketEvent.Receiving, authReqMsg2)
// Should not retry again
expect(sendSpy).toHaveBeenCalledTimes(1)
// Should remove the second auth-required message too
expect(recvQueueRemoveSpy).toHaveBeenCalledWith(authReqMsg2)
cleanup()
})
+34 -26
View File
@@ -1,12 +1,11 @@
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"
import { Nip01Signer } from '@welshman/signer'
import { LOCAL_RELAY_URL, makeEvent } from '@welshman/util'
import { ClientMessageType, RelayMessage } from "../src/message"
import { AdapterContext, AbstractAdapter, AdapterEvent, MockAdapter } from "../src/adapter"
import { makeEvent } from '@welshman/util'
import { ClientMessageType } from "../src/message"
import { MockAdapter } from "../src/adapter"
import { SingleRequest, MultiRequest, RequestEvent } from "../src/request"
import { Tracker } from "../src/tracker"
describe("Unireq", () => {
describe("SingleRequest", () => {
beforeEach(() => {
vi.useFakeTimers()
})
@@ -22,7 +21,6 @@ describe("Unireq", () => {
relay: 'whatever',
filters: [{kinds: [1]}],
context: {getAdapter: () => adapter},
autoClose: true,
})
const duplicateSpy = vi.fn()
@@ -41,19 +39,21 @@ describe("Unireq", () => {
await vi.runAllTimersAsync()
expect(sendSpy).toHaveBeenCalledWith([ClientMessageType.Req, expect.any(String), {kinds: [1]}])
const id = Array.from(req._ids)[0]
expect(sendSpy).toHaveBeenCalledWith([ClientMessageType.Req, id, {kinds: [1]}])
const signer = Nip01Signer.ephemeral()
const event1 = await signer.sign(makeEvent(1))
const event2 = await signer.sign(makeEvent(7))
const event3 = makeEvent(1)
adapter.receive(["EVENT", expect.any(String), event1])
adapter.receive(["EVENT", expect.any(String), event2])
adapter.receive(["EVENT", expect.any(String), event1])
adapter.receive(["EVENT", expect.any(String), event3])
adapter.receive(["EVENT", id, event1])
adapter.receive(["EVENT", id, event2])
adapter.receive(["EVENT", id, event1])
adapter.receive(["EVENT", id, event3])
await vi.runAllTimers()
await vi.runAllTimersAsync()
expect(duplicateSpy).toHaveBeenCalledWith(event1)
expect(filteredSpy).toHaveBeenCalledWith(event2)
@@ -61,14 +61,17 @@ describe("Unireq", () => {
expect(eventSpy).toHaveBeenCalledWith(event1)
expect(eoseSpy).toHaveBeenCalledTimes(0)
adapter.receive(["EOSE", expect.any(String)])
adapter.receive(["EOSE", id])
expect(eoseSpy).toHaveBeenCalledTimes(1)
req.close()
expect(closeSpy).toHaveBeenCalledTimes(1)
})
})
describe("Multireq", () => {
describe("MultiRequest", () => {
beforeEach(() => {
vi.useFakeTimers()
})
@@ -83,7 +86,6 @@ describe("Multireq", () => {
const send2Spy = vi.fn()
const adapter2 = new MockAdapter('2', send2Spy)
const req = new MultiRequest({
autoClose: true,
relays: ['1', '2'],
filters: [{kinds: [1]}],
context: {
@@ -105,10 +107,13 @@ describe("Multireq", () => {
req.on(RequestEvent.Eose, eoseSpy)
req.on(RequestEvent.Close, closeSpy)
await vi.runAllTimers()
await vi.runAllTimersAsync()
expect(send1Spy).toHaveBeenCalledWith([ClientMessageType.Req, expect.any(String), {kinds: [1]}])
expect(send2Spy).toHaveBeenCalledWith([ClientMessageType.Req, expect.any(String), {kinds: [1]}])
const id1 = Array.from(req._children[0]._ids)[0]
const id2 = Array.from(req._children[1]._ids)[0]
expect(send1Spy).toHaveBeenCalledWith([ClientMessageType.Req, id1, {kinds: [1]}])
expect(send2Spy).toHaveBeenCalledWith([ClientMessageType.Req, id2, {kinds: [1]}])
const signer = Nip01Signer.ephemeral()
const event1 = await signer.sign(makeEvent(1))
@@ -116,13 +121,13 @@ describe("Multireq", () => {
const event3 = makeEvent(1)
const event4 = await signer.sign(makeEvent(1))
adapter1.receive(["EVENT", expect.any(String), event1])
adapter1.receive(["EVENT", expect.any(String), event2])
adapter1.receive(["EVENT", expect.any(String), event3])
adapter2.receive(["EVENT", expect.any(String), event1])
adapter2.receive(["EVENT", expect.any(String), event4])
adapter1.receive(["EVENT", id1, event1])
adapter1.receive(["EVENT", id1, event2])
adapter1.receive(["EVENT", id1, event3])
adapter2.receive(["EVENT", id2, event1])
adapter2.receive(["EVENT", id2, event4])
await vi.runAllTimers()
await vi.runAllTimersAsync()
expect(duplicateSpy).toHaveBeenCalledWith(event1, '2')
expect(filteredSpy).toHaveBeenCalledWith(event2, '1')
@@ -130,10 +135,13 @@ describe("Multireq", () => {
expect(eventSpy).toHaveBeenCalledWith(event1, '1')
expect(eoseSpy).toHaveBeenCalledTimes(0)
adapter1.receive(["EOSE", expect.any(String)])
adapter2.receive(["EOSE", expect.any(String)])
adapter1.receive(["EOSE", id1])
adapter2.receive(["EOSE", id2])
expect(eoseSpy).toHaveBeenCalledTimes(2)
req.close()
expect(closeSpy).toHaveBeenCalledTimes(1)
})
})
+2 -2
View File
@@ -56,7 +56,7 @@ describe("Socket", () => {
expect(() => socket.open()).toThrow("Attempted to open a websocket that has not been closed")
})
it("should emit invalid status on invalid URL", () => {
it("should emit error status on invalid URL", () => {
const statusSpy = vi.fn()
socket.on(SocketEvent.Status, statusSpy)
@@ -66,7 +66,7 @@ describe("Socket", () => {
socket.open()
expect(statusSpy).toHaveBeenCalledWith(SocketStatus.Invalid, "wss://test.relay")
expect(statusSpy).toHaveBeenCalledWith(SocketStatus.Error, "wss://test.relay")
})
})
+4 -4
View File
@@ -186,12 +186,12 @@ describe("Events", () => {
it("should validate signature using verifiedSymbol", () => {
let event = createSignedEvent() as Events.SignedEvent
event[verifiedSymbol] = true
expect(Events.hasValidSignature(event)).toBe(true)
expect(Events.verifyEvent(event)).toBe(true)
// Clear verifiedSymbol and use verify the actual signature
// Clear verifiedSymbol and verify the actual signature
delete event[verifiedSymbol]
// the signature is invalid, but the sig validity is not checked here
expect(Events.hasValidSignature(event)).toBe(true)
// the signature is invalid, so verifyEvent should return false
expect(Events.verifyEvent(event)).toBe(false)
})
})
+1 -1
View File
@@ -211,7 +211,7 @@ describe("Filters", () => {
it("should get filter result cardinality", () => {
expect(Filters.getFilterResultCardinality({ids: [id, id + "1"]})).toBe(2)
expect(Filters.getFilterResultCardinality({kinds: [1]})).toBeNull()
expect(Filters.getFilterResultCardinality({kinds: [1]})).toBeUndefined()
})
it("should trim large filters", () => {
+1 -1
View File
@@ -72,7 +72,7 @@ export const verifyEvent = (() => {
})
}
return (event: TrustedEvent) => event.sig && verify(event as SignedEvent)
return (event: TrustedEvent) => event.sig && (event[verifiedSymbol] || verify(event as SignedEvent))
})()
export const isEventTemplate = (e: EventTemplate): e is EventTemplate =>