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
+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")
})
})