Re-work test mocks for net2

This commit is contained in:
Jon Staab
2025-03-27 17:46:37 -07:00
parent d6a66a473c
commit f75783ea0a
10 changed files with 449 additions and 1553 deletions
+1 -1
View File
@@ -1,6 +1,6 @@
--ignore-dir=docs
--ignore-dir=dist
--ignore-dir=build
--ignore-dir=__tests__
# --ignore-dir=__tests__
--ignore-dir=.svelte-kit
--ignore-file=match:yarn.lock
-1092
View File
File diff suppressed because it is too large Load Diff
+37 -40
View File
@@ -1,36 +1,35 @@
import { sleep } from "@welshman/lib"
import WebSocket from "isomorphic-ws"
import WebSocket from 'isomorphic-ws'
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"
import { Socket, SocketStatus, SocketEventType } from "../src/socket"
import { ClientMessage, RelayMessage } from "../src/message"
vi.mock("isomorphic-ws")
vi.mock('isomorphic-ws', () => {
const WebSocket = vi.fn(function () {
setTimeout(() => this.onopen())
})
WebSocket.prototype.send = vi.fn()
WebSocket.prototype.close = vi.fn(function () {
this.onclose()
})
return { default: WebSocket }
})
describe("Socket", () => {
let socket: Socket
let mockWs: any
beforeEach(() => {
vi.useFakeTimers()
vi.clearAllMocks()
mockWs = {
close: vi.fn(),
send: vi.fn(),
onopen: null,
onclose: null,
onerror: null,
onmessage: null,
}
vi.mocked(WebSocket).mockImplementation(() => mockWs)
socket = new Socket("wss://test.relay")
})
afterEach(() => {
socket.cleanup()
vi.clearAllMocks()
vi.useRealTimers()
socket.cleanup()
})
it("should initialize with correct url", () => {
@@ -44,16 +43,10 @@ describe("Socket", () => {
socket.open()
expect(WebSocket).toHaveBeenCalledWith("wss://test.relay")
expect(socket._ws).toBeDefined()
expect(statusSpy).toHaveBeenCalledWith(SocketStatus.Opening, "wss://test.relay")
})
it("should emit open status when connection opens", () => {
const statusSpy = vi.fn()
socket.on(SocketEventType.Status, statusSpy)
socket.open()
mockWs.onopen()
vi.runAllTimers()
expect(statusSpy).toHaveBeenCalledWith(SocketStatus.Open, "wss://test.relay")
})
@@ -83,10 +76,12 @@ describe("Socket", () => {
socket.on(SocketEventType.Status, statusSpy)
socket.open()
socket.close()
mockWs.onclose()
expect(mockWs.close).toHaveBeenCalled()
const ws = socket._ws
socket.close()
expect(ws.close).toHaveBeenCalled()
expect(statusSpy).toHaveBeenCalledWith(SocketStatus.Closed, "wss://test.relay")
})
})
@@ -96,26 +91,25 @@ describe("Socket", () => {
const enqueueSpy = vi.fn()
socket.on(SocketEventType.Enqueue, enqueueSpy)
const message: ClientMessage = ["EVENT", { id: "123", kind: 1, content: "", tags: [], pubkey: "", sig: "" }]
const message: ClientMessage = ["EVENT", { id: "123", kind: 1 }]
socket.send(message)
expect(enqueueSpy).toHaveBeenCalledWith(message, "wss://test.relay")
})
it("should send queued messages when socket is open", async () => {
it("should send messages when socket is open", async () => {
const sendSpy = vi.fn()
socket.on(SocketEventType.Send, sendSpy)
socket.open()
mockWs.onopen()
socket._ws.onopen()
const message: ClientMessage = ["EVENT", { id: "123", kind: 1, content: "", tags: [], pubkey: "", sig: "" }]
const message: ClientMessage = ["EVENT", { id: "123", kind: 1 }]
socket.send(message)
// Allow task queue to process
await vi.runAllTimers()
expect(mockWs.send).toHaveBeenCalledWith(JSON.stringify(message))
expect(socket._ws.send).toHaveBeenCalledWith(JSON.stringify(message))
expect(sendSpy).toHaveBeenCalledWith(message, "wss://test.relay")
})
})
@@ -126,8 +120,8 @@ describe("Socket", () => {
socket.on(SocketEventType.Receive, receiveSpy)
socket.open()
const message: RelayMessage = ["EVENT", "123", { id: "123", kind: 1, content: "", tags: [], pubkey: "", sig: "" }]
mockWs.onmessage({ data: JSON.stringify(message) })
const message: RelayMessage = ["EVENT", "123", { id: "123", kind: 1 }]
socket._ws.onmessage({ data: JSON.stringify(message) })
// Allow task queue to process
await vi.runAllTimers()
@@ -140,7 +134,7 @@ describe("Socket", () => {
socket.on(SocketEventType.Error, errorSpy)
socket.open()
mockWs.onmessage({ data: "invalid json" })
socket._ws.onmessage({ data: "invalid json" })
expect(errorSpy).toHaveBeenCalledWith("Invalid message received", "wss://test.relay")
})
@@ -150,7 +144,7 @@ describe("Socket", () => {
socket.on(SocketEventType.Error, errorSpy)
socket.open()
mockWs.onmessage({ data: JSON.stringify({ not: "an array" }) })
socket._ws.onmessage({ data: JSON.stringify({ not: "an array" }) })
expect(errorSpy).toHaveBeenCalledWith("Invalid message received", "wss://test.relay")
})
@@ -159,9 +153,12 @@ describe("Socket", () => {
describe("cleanup", () => {
it("should close socket and clear queues", () => {
socket.open()
const ws = socket._ws
socket.cleanup()
expect(mockWs.close).toHaveBeenCalled()
expect(ws.close).toHaveBeenCalled()
expect(socket.listenerCount(SocketEventType.Send)).toBe(0)
})
})
@@ -172,7 +169,7 @@ describe("Socket", () => {
socket.on(SocketEventType.Status, statusSpy)
socket.open()
mockWs.onerror()
socket._ws.onerror()
expect(statusSpy).toHaveBeenCalledWith(SocketStatus.Error, "wss://test.relay")
})
+23 -28
View File
@@ -6,39 +6,35 @@ import { Pool } from "../src/pool"
import { ClientMessage, RelayMessage } from "../src/message"
import EventEmitter from "events"
vi.mock("@welshman/lib", () => ({
on: (target: any, eventName: string, callback: Function) => {
target.on(eventName, callback)
return () => target.off(eventName, callback)
},
call: (fn: Function) => fn()
}))
vi.mock('isomorphic-ws', () => {
const WebSocket = vi.fn(function () {
setTimeout(() => this.onopen())
})
vi.mock("../src/socket")
vi.mock("@welshman/util", () => ({
Relay: vi.fn(() => new EventEmitter()),
LOCAL_RELAY_URL: "local://welshman.relay/",
isRelayUrl: vi.fn((url) => url.startsWith("wss://"))
}))
WebSocket.prototype.send = vi.fn()
WebSocket.prototype.close = vi.fn(function () {
this.onclose()
})
return { default: WebSocket }
})
describe("SocketAdapter", () => {
let socket: Socket
let adapter: SocketAdapter
beforeEach(() => {
const mockSocket = new EventEmitter()
Object.assign(mockSocket, {
url: "wss://test.relay",
send: vi.fn(),
removeAllListeners: vi.fn()
})
socket = mockSocket as unknown as Socket
vi.useFakeTimers()
socket = new Socket('wss://test.relay')
adapter = new SocketAdapter(socket)
})
afterEach(() => {
adapter.cleanup()
vi.clearAllMocks()
vi.useRealTimers()
socket.cleanup()
adapter.cleanup()
})
it("should initialize with correct socket", () => {
@@ -51,17 +47,18 @@ describe("SocketAdapter", () => {
const receiveSpy = vi.fn()
adapter.on(AdapterEventType.Receive, receiveSpy)
const message: RelayMessage = ["EVENT", "123", { id: "123", kind: 1, content: "", tags: [], pubkey: "", sig: "" }]
const message: RelayMessage = ["EVENT", "123", { id: "123", kind: 1 }]
socket.emit(SocketEventType.Receive, message, "wss://test.relay")
expect(receiveSpy).toHaveBeenCalledWith(message, "wss://test.relay")
})
it("should send messages to socket", () => {
const message: ClientMessage = ["EVENT", { id: "123", kind: 1, content: "", tags: [], pubkey: "", sig: "" }]
const sendSpy = vi.spyOn(socket, 'send')
const message: ClientMessage = ["EVENT", { id: "123", kind: 1 }]
adapter.send(message)
expect(socket.send).toHaveBeenCalledWith(message)
expect(sendSpy).toHaveBeenCalledWith(message)
})
it("should cleanup properly", () => {
@@ -100,14 +97,14 @@ describe("LocalAdapter", () => {
const receiveSpy = vi.fn()
adapter.on(AdapterEventType.Receive, receiveSpy)
const message: RelayMessage = ["EVENT", "123", { id: "123", kind: 1, content: "", tags: [], pubkey: "", sig: "" }]
const message: RelayMessage = ["EVENT", "123", { id: "123", kind: 1 }]
relay.emit("*", ...message)
expect(receiveSpy).toHaveBeenCalledWith(message, LOCAL_RELAY_URL)
})
it("should send messages to relay", () => {
const message: ClientMessage = ["EVENT", { id: "123", kind: 1, content: "", tags: [], pubkey: "", sig: "" }]
const message: ClientMessage = ["EVENT", { id: "123", kind: 1 }]
adapter.send(message)
expect(relay.send).toHaveBeenCalledWith("EVENT", message[1])
@@ -147,7 +144,6 @@ describe("getAdapter", () => {
})
it("should throw error for invalid relay URL", () => {
vi.mocked(isRelayUrl).mockReturnValue(false)
expect(() => getAdapter("invalid-url", {})).toThrow("Invalid relay url invalid-url")
})
@@ -158,7 +154,6 @@ describe("getAdapter", () => {
it("should throw error for remote relay URL without pool context", () => {
const url = "wss://test.relay"
vi.mocked(isRelayUrl).mockReturnValue(true)
expect(() => getAdapter(url, {})).toThrow(`Unable to get socket for ${url}`)
})
+156 -195
View File
@@ -1,242 +1,203 @@
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"
import { Socket, SocketStatus, SocketEventType } from "../src/socket"
import { makeEvent, CLIENT_AUTH } from "@welshman/util"
import { Nip01Signer } from "@welshman/signer"
import { AuthState, AuthStatus, AuthStateEventType, AuthManager, makeAuthEvent } from "../src/auth"
import EventEmitter from "events"
import { RelayMessage } from "../src/message"
// Mock dependencies
vi.mock("@welshman/lib", () => ({
on: (target: any, eventName: string, callback: Function) => {
target.on(eventName, callback)
return () => target.off(eventName, callback)
},
call: (fn: Function) => fn(),
sleep: vi.fn()
}))
vi.mock("@welshman/util", () => ({
makeEvent: vi.fn((kind, opts) => ({
kind,
id: "test-event-id",
...opts
})),
CLIENT_AUTH: 24242
}))
describe("AuthState", () => {
let socket: Socket & EventEmitter
let authState: AuthState
beforeEach(() => {
const mockSocket = new EventEmitter()
Object.assign(mockSocket, {
url: "wss://test.relay",
send: vi.fn(),
removeAllListeners: vi.fn()
})
socket = mockSocket as unknown as Socket
authState = new AuthState(socket)
vi.mock('isomorphic-ws', () => {
const WebSocket = vi.fn(function () {
setTimeout(() => this.onopen())
})
afterEach(() => {
authState.cleanup()
vi.clearAllMocks()
WebSocket.prototype.send = vi.fn()
WebSocket.prototype.close = vi.fn(function () {
this.onclose()
})
it("should initialize with None status", () => {
expect(authState.status).toBe(AuthStatus.None)
})
it("should handle AUTH message from relay", () => {
const message: RelayMessage = ["AUTH", "challenge123"]
socket.emit(SocketEventType.Receive, message)
expect(authState.challenge).toBe("challenge123")
expect(authState.status).toBe(AuthStatus.Requested)
})
it("should handle successful OK message", () => {
authState.request = "request123"
const message: RelayMessage = ["OK", "request123", true, "success"]
socket.emit(SocketEventType.Receive, message)
expect(authState.status).toBe(AuthStatus.Ok)
expect(authState.details).toBe("success")
})
it("should handle failed OK message", () => {
authState.request = "request123"
const message: RelayMessage = ["OK", "request123", false, "forbidden"]
socket.emit(SocketEventType.Receive, message)
expect(authState.status).toBe(AuthStatus.Forbidden)
expect(authState.details).toBe("forbidden")
})
it("should ignore OK messages for different requests", () => {
authState.request = "request123"
const message: RelayMessage = ["OK", "different-request", true, "success"]
socket.emit(SocketEventType.Receive, message)
expect(authState.status).toBe(AuthStatus.None)
})
it("should handle client AUTH message", () => {
const message: RelayMessage = ["AUTH", { id: "123", kind: CLIENT_AUTH }]
socket.emit(SocketEventType.Enqueue, message)
expect(authState.status).toBe(AuthStatus.PendingResponse)
})
it("should reset state on socket close", () => {
authState.challenge = "challenge123"
authState.request = "request123"
authState.details = "details"
authState.status = AuthStatus.PendingResponse
socket.emit(SocketEventType.Status, SocketStatus.Closed)
expect(authState.challenge).toBeUndefined()
expect(authState.request).toBeUndefined()
expect(authState.details).toBeUndefined()
expect(authState.status).toBe(AuthStatus.None)
})
it("should emit status changes", () => {
const statusSpy = vi.fn()
authState.on(AuthStateEventType.Status, statusSpy)
authState.setStatus(AuthStatus.Requested)
expect(statusSpy).toHaveBeenCalledWith(AuthStatus.Requested)
})
it("should cleanup properly", () => {
const removeListenersSpy = vi.spyOn(authState, "removeAllListeners")
authState.cleanup()
expect(removeListenersSpy).toHaveBeenCalled()
})
return { default: WebSocket }
})
describe("AuthManager", () => {
let socket: Socket & EventEmitter
let manager: AuthManager
let signFn: jest.Mock
describe('auth', () => {
let socket: Socket
let authManager: AuthManager
let sign = vi.fn(Nip01Signer.ephemeral().sign)
beforeEach(() => {
const mockSocket = new EventEmitter()
Object.assign(mockSocket, {
url: "wss://test.relay",
send: vi.fn(),
removeAllListeners: vi.fn(),
attemptToOpen: vi.fn()
})
socket = mockSocket as unknown as Socket & EventEmitter
signFn = vi.fn()
manager = new AuthManager(socket, { sign: signFn })
socket = new Socket('wss://test.relay')
authManager = new AuthManager(socket, { sign })
})
afterEach(() => {
manager.cleanup()
vi.clearAllMocks()
socket.cleanup()
authManager.cleanup()
})
it("should create AuthState instance", () => {
expect(manager.state).toBeInstanceOf(AuthState)
})
it("should respond automatically when eager is true", () => {
const respondSpy = vi.spyOn(AuthManager.prototype, "respond")
const eagerManager = new AuthManager(socket, { sign: signFn, eager: true })
socket.emit(SocketEventType.Receive, ["AUTH", "challenge123"])
expect(respondSpy).toHaveBeenCalled()
})
it("should not respond automatically when eager is false", () => {
const respondSpy = vi.spyOn(AuthManager.prototype, "respond")
socket.emit(SocketEventType.Receive, ["AUTH", "challenge123"])
expect(respondSpy).not.toHaveBeenCalled()
})
describe("respond", () => {
it("should throw error if no challenge", async () => {
await expect(manager.respond()).rejects.toThrow("Attempted to authenticate with no challenge")
describe("AuthState", () => {
it("should initialize with None status", () => {
expect(authManager.state.status).toBe(AuthStatus.None)
})
it("should throw error if status is not Requested", async () => {
manager.state.challenge = "challenge123"
manager.state.status = AuthStatus.PendingSignature
it("should handle AUTH message from relay", () => {
const message: RelayMessage = ["AUTH", "challenge123"]
socket.emit(SocketEventType.Receive, message)
await expect(manager.respond()).rejects.toThrow("Attempted to authenticate when auth is already auth:status:pending_signature")
expect(authManager.state.challenge).toBe("challenge123")
expect(authManager.state.status).toBe(AuthStatus.Requested)
})
it("should handle successful sign", async () => {
manager.state.challenge = "challenge123"
manager.state.status = AuthStatus.Requested
const signedEvent = { id: "signed-event-id", kind: CLIENT_AUTH }
signFn.mockResolvedValue(signedEvent)
it("should handle successful OK message", () => {
authManager.state.request = "request123"
const message: RelayMessage = ["OK", "request123", true, "success"]
socket.emit(SocketEventType.Receive, message)
await manager.respond()
expect(manager.state.request).toBe("signed-event-id")
expect(socket.send).toHaveBeenCalledWith(["AUTH", signedEvent])
expect(authManager.state.status).toBe(AuthStatus.Ok)
expect(authManager.state.details).toBe("success")
})
it("should handle denied signature", async () => {
manager.state.challenge = "challenge123"
manager.state.status = AuthStatus.Requested
signFn.mockResolvedValue(null)
it("should handle failed OK message", () => {
authManager.state.request = "request123"
const message: RelayMessage = ["OK", "request123", false, "forbidden"]
socket.emit(SocketEventType.Receive, message)
await manager.respond()
expect(authManager.state.status).toBe(AuthStatus.Forbidden)
expect(authManager.state.details).toBe("forbidden")
})
expect(manager.state.status).toBe(AuthStatus.DeniedSignature)
expect(socket.send).not.toHaveBeenCalled()
it("should ignore OK messages for different requests", () => {
authManager.state.request = "request123"
const message: RelayMessage = ["OK", "different-request", true, "success"]
socket.emit(SocketEventType.Receive, message)
expect(authManager.state.status).toBe(AuthStatus.None)
})
it("should handle client AUTH message", () => {
const message: RelayMessage = ["AUTH", { id: "123", kind: CLIENT_AUTH }]
socket.emit(SocketEventType.Enqueue, message)
expect(authManager.state.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.emit(SocketEventType.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)
})
it("should emit status changes", () => {
const statusSpy = vi.fn()
authManager.state.on(AuthStateEventType.Status, statusSpy)
authManager.state.setStatus(AuthStatus.Requested)
expect(statusSpy).toHaveBeenCalledWith(AuthStatus.Requested)
})
it("should cleanup properly", () => {
const removeListenersSpy = vi.spyOn(authManager.state, "removeAllListeners")
authManager.state.cleanup()
expect(removeListenersSpy).toHaveBeenCalled()
})
})
describe("attempt", () => {
it("should attempt to open socket", async () => {
await manager.attempt()
expect(socket.attemptToOpen).toHaveBeenCalled()
describe("AuthManager", () => {
it("should create AuthState instance", () => {
expect(authManager.state).toBeInstanceOf(AuthState)
})
it("should wait for challenge", async () => {
const waitForChallengeSpy = vi.spyOn(manager, "waitForChallenge")
await manager.attempt()
expect(waitForChallengeSpy).toHaveBeenCalled()
})
it("should respond automatically when eager is true", () => {
const respondSpy = vi.spyOn(AuthManager.prototype, "respond")
const eagerManager = new AuthManager(socket, { sign, eager: true })
socket.emit(SocketEventType.Receive, ["AUTH", "challenge123"])
it("should respond if challenge received", async () => {
const respondSpy = vi.spyOn(manager, "respond")
manager.state.challenge = "challenge123"
manager.state.status = AuthStatus.Requested
await manager.attempt()
expect(respondSpy).toHaveBeenCalled()
})
it("should wait for resolution", async () => {
const waitForResolutionSpy = vi.spyOn(manager, "waitForResolution")
await manager.attempt()
expect(waitForResolutionSpy).toHaveBeenCalled()
it("should not respond automatically when eager is false", () => {
const respondSpy = vi.spyOn(AuthManager.prototype, "respond")
socket.emit(SocketEventType.Receive, ["AUTH", "challenge123"])
expect(respondSpy).not.toHaveBeenCalled()
})
})
describe("makeAuthEvent", () => {
it("should create auth event with correct tags", () => {
const url = "wss://test.relay"
const challenge = "challenge123"
describe("respond", () => {
it("should throw error if no challenge", async () => {
await expect(authManager.respond()).rejects.toThrow("Attempted to authenticate with no challenge")
})
makeAuthEvent(url, challenge)
it("should throw error if status is not Requested", async () => {
authManager.state.challenge = "challenge123"
authManager.state.status = AuthStatus.PendingSignature
expect(makeEvent).toHaveBeenCalledWith(CLIENT_AUTH, {
tags: [
["relay", url],
["challenge", challenge]
]
await expect(authManager.respond()).rejects.toThrow("Attempted to authenticate when auth is already auth:status:pending_signature")
})
it("should handle successful sign", async () => {
const sendSpy = vi.spyOn(socket, 'send')
authManager.state.challenge = "challenge123"
authManager.state.status = AuthStatus.Requested
const signedEvent = { id: "signed-event-id", kind: CLIENT_AUTH }
sign.mockResolvedValue(signedEvent)
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()
})
})
})
+167 -34
View File
@@ -1,4 +1,3 @@
import WebSocket from "isomorphic-ws"
import { AUTH_JOIN } from "@welshman/util"
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"
import { Socket, SocketStatus, SocketEventType } from "../src/socket"
@@ -13,50 +12,33 @@ import {
} from "../src/policy"
import { ClientMessage, RelayMessage } from "../src/message"
// Hoist mock definition to top level
const mockWs = vi.hoisted(() => ({
close: vi.fn(),
send: vi.fn(),
onopen: vi.fn(),
onclose: null,
onerror: null,
onmessage: null,
}))
// Mock the WebSocket module
vi.mock('isomorphic-ws', () => ({
default: mockWs
}))
describe('policy', () => {
let socket: Socket
let mockWs: any
beforeEach(() => {
vi.useFakeTimers()
vi.clearAllMocks()
mockWs = {
close: vi.fn(),
send: vi.fn(),
onopen: vi.fn(),
onclose: null,
onerror: null,
onmessage: null,
}
vi.mock('@/store', () => ({default: mockWs}))
socket = new Socket("wss://test.relay")
})
afterEach(() => {
socket.cleanup()
vi.useRealTimers()
})
describe("socketPolicySendWhenOpen", () => {
it("should send when open", async () => {
const cleanup = socketPolicySendWhenOpen(socket)
const stopSpy = vi.spyOn(socket._sendQueue, 'stop')
const startSpy = vi.spyOn(socket._sendQueue, 'start')
socket.emit(SocketEventType.Status, SocketStatus.Opening, socket.url)
expect(stopSpy).toHaveBeenCalled()
expect(startSpy).not.toHaveBeenCalled()
socket.emit(SocketEventType.Status, SocketStatus.Open, socket.url)
expect(startSpy).toHaveBeenCalled()
cleanup()
})
vi.clearAllMocks()
})
describe("socketPolicyDeferOnAuth", () => {
@@ -127,4 +109,155 @@ describe('policy', () => {
cleanup()
})
})
describe("socketPolicyRetryAuthRequired", () => {
it("should retry events once when auth-required", () => {
const cleanup = socketPolicyRetryAuthRequired(socket)
const sendSpy = vi.spyOn(socket, 'send')
// Send an event
const event: ClientMessage = ["EVENT", { id: "123", kind: 1, content: "", tags: [], pubkey: "", sig: "" }]
socket.emit(SocketEventType.Send, event)
// Receive auth-required rejection
socket.emit(SocketEventType.Receive, ["OK", "123", false, "auth-required: need to auth first"])
// Should retry the event
expect(sendSpy).toHaveBeenCalledWith(event)
// Receive another auth-required rejection
socket.emit(SocketEventType.Receive, ["OK", "123", false, "auth-required: need to auth first"])
// Should not retry again
expect(sendSpy).toHaveBeenCalledTimes(1)
cleanup()
})
it("should retry REQ once when auth-required", () => {
const cleanup = socketPolicyRetryAuthRequired(socket)
const sendSpy = vi.spyOn(socket, 'send')
// Send a REQ
const req: ClientMessage = ["REQ", "123", { kinds: [1] }]
socket.emit(SocketEventType.Send, req)
// Receive auth-required rejection via CLOSED
socket.emit(SocketEventType.Receive, ["CLOSED", "123", "auth-required: need to auth first"])
// Should retry the request
expect(sendSpy).toHaveBeenCalledWith(req)
// Receive another auth-required rejection
socket.emit(SocketEventType.Receive, ["CLOSED", "123", "auth-required: need to auth first"])
// Should not retry again
expect(sendSpy).toHaveBeenCalledTimes(1)
cleanup()
})
it("should not retry AUTH_JOIN events", () => {
const cleanup = socketPolicyRetryAuthRequired(socket)
const sendSpy = vi.spyOn(socket, 'send')
// Send an AUTH_JOIN event
const event: ClientMessage = ["EVENT", { id: "123", kind: AUTH_JOIN, content: "", tags: [], pubkey: "", sig: "" }]
socket.emit(SocketEventType.Send, event)
// Receive auth-required rejection
socket.emit(SocketEventType.Receive, ["OK", "123", false, "auth-required: need to auth first"])
// Should not retry AUTH_JOIN events
expect(sendSpy).not.toHaveBeenCalled()
cleanup()
})
it("should clear pending messages on successful response", () => {
const cleanup = socketPolicyRetryAuthRequired(socket)
const sendSpy = vi.spyOn(socket, 'send')
// Send an event
const event: ClientMessage = ["EVENT", { id: "123", kind: 1, content: "", tags: [], pubkey: "", sig: "" }]
socket.emit(SocketEventType.Send, event)
// Receive successful response
socket.emit(SocketEventType.Receive, ["OK", "123", true, ""])
// Receive auth-required rejection (should not trigger retry since message was cleared)
socket.emit(SocketEventType.Receive, ["OK", "123", false, "auth-required: need to auth first"])
// Should not retry
expect(sendSpy).not.toHaveBeenCalled()
cleanup()
})
})
describe("socketPolicyConnectOnSend", () => {
it("should open socket on send when closed", () => {
const cleanup = socketPolicyConnectOnSend(socket)
const openSpy = vi.spyOn(socket, 'open')
// Socket starts closed
socket.emit(SocketEventType.Status, SocketStatus.Closed)
// Send a message
const event: ClientMessage = ["EVENT", { id: "123", kind: 1 }]
socket.emit(SocketEventType.Enqueue, event)
// Should open the socket
expect(openSpy).toHaveBeenCalled()
cleanup()
})
it("should not open socket if already open", () => {
const cleanup = socketPolicyConnectOnSend(socket)
const openSpy = vi.spyOn(socket, 'open')
// Socket is open
socket.emit(SocketEventType.Status, SocketStatus.Open)
// Send a message
const event: ClientMessage = ["EVENT", { id: "123", kind: 1 }]
socket.emit(SocketEventType.Enqueue, event)
// Should not try to open the socket
expect(openSpy).not.toHaveBeenCalled()
cleanup()
})
it("should not open socket if there was a recent error", () => {
const cleanup = socketPolicyConnectOnSend(socket)
const openSpy = vi.spyOn(socket, 'open')
// Socket has an error
socket.emit(SocketEventType.Status, SocketStatus.Error)
socket.emit(SocketEventType.Status, SocketStatus.Closed)
// Send a message
const event: ClientMessage = ["EVENT", { id: "123", kind: 1 }]
socket.emit(SocketEventType.Enqueue, event)
// Should not try to open the socket due to recent error
expect(openSpy).not.toHaveBeenCalled()
// Advance time past the error timeout
vi.advanceTimersByTime(31000)
// Send another message
socket.emit(SocketEventType.Enqueue, event)
// Now it should try to open
expect(openSpy).toHaveBeenCalled()
cleanup()
})
})
describe("socketPolicyCloseOnTimeout", () => {
})
})
+28 -140
View File
@@ -3,188 +3,80 @@ import { Socket } from "../src/socket"
import { Pool, makeSocket } from "../src/pool"
import { normalizeRelayUrl } from "@welshman/util"
// Mock dependencies
vi.mock("@welshman/lib", () => ({
remove: vi.fn((item, array) => array.filter(x => x !== item)),
on: vi.fn((target, event, callback) => {
if (target.on) {
target.on(event, callback)
}
return () => {
if (target.off) {
target.off(event, callback)
}
}
}),
call: vi.fn(fn => fn())
}))
vi.mock("@welshman/util", () => ({
normalizeRelayUrl: vi.fn(url => url)
}))
vi.mock("../src/socket", async (importOriginal) => {
const original = await importOriginal()
return {
...original,
Socket: vi.fn().mockImplementation((url) => ({
url,
cleanup: vi.fn(),
_sendQueue: {
start: vi.fn(),
stop: vi.fn()
},
on: vi.fn(),
off: vi.fn()
})),
}
})
describe("makeSocket", () => {
let mockSocket: any
beforeEach(() => {
mockSocket = {
url: "wss://test.relay",
cleanup: vi.fn(),
_sendQueue: {
start: vi.fn(),
stop: vi.fn()
},
on: vi.fn(),
off: vi.fn()
}
vi.mocked(Socket).mockReturnValue(mockSocket)
vi.mock('isomorphic-ws', () => {
const WebSocket = vi.fn(function () {
setTimeout(() => this.onopen())
})
afterEach(() => {
vi.clearAllMocks()
WebSocket.prototype.send = vi.fn()
WebSocket.prototype.close = vi.fn(function () {
this.onclose()
})
it("should create socket with url", () => {
const socket = makeSocket("wss://test.relay", [])
expect(Socket).toHaveBeenCalledWith("wss://test.relay")
})
it("should apply custom policies", () => {
const customPolicy = vi.fn(() => () => {})
const socket = makeSocket("wss://test.relay", [customPolicy])
expect(customPolicy).toHaveBeenCalledWith(mockSocket)
})
return { default: WebSocket }
})
describe("Pool", () => {
let pool: Pool
let customMakeSocket: jest.Mock
beforeEach(() => {
customMakeSocket = vi.fn()
pool = new Pool({ makeSocket: customMakeSocket })
pool = new Pool()
})
afterEach(() => {
vi.clearAllMocks()
})
describe("initialization", () => {
it("should initialize with empty data map", () => {
expect(pool._data.size).toBe(0)
})
it("should initialize with empty subscriptions", () => {
expect(pool._subs).toEqual([])
})
})
describe("has", () => {
it("should return false for non-existent socket", () => {
expect(pool.has("wss://test.relay")).toBe(false)
})
it("should return true for existing socket", () => {
const mockSocket = { url: "wss://test.relay" }
customMakeSocket.mockReturnValue(mockSocket)
pool.get("wss://test.relay")
it("should return true for existing socket, normalizing the url", () => {
pool.get("wss://test.relay/")
expect(pool.has("wss://test.relay")).toBe(true)
})
})
describe("makeSocket", () => {
it("should use custom makeSocket if provided", () => {
const mockSocket = { url: "wss://test.relay" }
customMakeSocket.mockReturnValue(mockSocket)
const result = pool.makeSocket("wss://test.relay")
expect(customMakeSocket).toHaveBeenCalledWith("wss://test.relay")
expect(result).toBe(mockSocket)
})
it("should use default makeSocket if none provided", () => {
pool = new Pool({})
const socket = pool.makeSocket("wss://test.relay")
expect(Socket).toHaveBeenCalledWith("wss://test.relay")
})
})
describe("get", () => {
it("should normalize relay URL", () => {
const mockSocket = { url: "wss://test.relay" }
customMakeSocket.mockReturnValue(mockSocket)
pool.get("wss://test.relay")
expect(normalizeRelayUrl).toHaveBeenCalledWith("wss://test.relay")
})
it("should create new socket if none exists", () => {
const mockSocket = { url: "wss://test.relay" }
customMakeSocket.mockReturnValue(mockSocket)
it("should create new socket if none exists, normalizing the relay url", () => {
const socket = pool.get("wss://test.relay")
expect(customMakeSocket).toHaveBeenCalledWith("wss://test.relay")
expect(socket).toBe(mockSocket)
expect(socket.url).toEqual("wss://test.relay/")
})
it("should return existing socket if it exists", () => {
const mockSocket = { url: "wss://test.relay" }
customMakeSocket.mockReturnValue(mockSocket)
const firstSocket = pool.get("wss://test.relay")
const secondSocket = pool.get("wss://test.relay")
expect(customMakeSocket).toHaveBeenCalledTimes(1)
expect(firstSocket).toBe(secondSocket)
})
})
describe("subscribe", () => {
it("should notify subscribers of new sockets", () => {
const sub1 = vi.fn()
const sub2 = vi.fn()
const mockSocket = { url: "wss://test.relay" }
customMakeSocket.mockReturnValue(mockSocket)
pool.subscribe(sub1)
pool.subscribe(sub2)
pool.get("wss://test.relay")
expect(sub1).toHaveBeenCalledWith(mockSocket)
expect(sub2).toHaveBeenCalledWith(mockSocket)
expect(sub1).toHaveBeenCalledTimes(1)
expect(sub2).toHaveBeenCalledTimes(1)
})
it("should not notify subscribers for existing sockets", () => {
const mockSocket = { url: "wss://test.relay" }
customMakeSocket.mockReturnValue(mockSocket)
pool.get("wss://test.relay")
const sub = vi.fn()
pool.subscribe(sub)
pool.get("wss://test.relay")
expect(sub).not.toHaveBeenCalled()
})
})
describe("subscribe", () => {
it("should add subscription", () => {
const sub = vi.fn()
pool.subscribe(sub)
@@ -194,9 +86,9 @@ describe("Pool", () => {
it("should return unsubscribe function", () => {
const sub = vi.fn()
const unsubscribe = pool.subscribe(sub)
unsubscribe()
expect(pool._subs).not.toContain(sub)
})
})
@@ -204,13 +96,12 @@ describe("Pool", () => {
describe("remove", () => {
it("should remove and cleanup existing socket", () => {
const mockSocket = { url: "wss://test.relay", cleanup: vi.fn() }
customMakeSocket.mockReturnValue(mockSocket)
pool.get("wss://test.relay")
pool.remove("wss://test.relay")
pool._data.set(mockSocket.url, mockSocket)
pool.remove(mockSocket.url)
expect(mockSocket.cleanup).toHaveBeenCalled()
expect(pool._data.has("wss://test.relay")).toBe(false)
expect(pool._data.has(mockSocket.url)).toBe(false)
})
it("should do nothing for non-existent socket", () => {
@@ -223,10 +114,11 @@ describe("Pool", () => {
it("should remove all sockets", () => {
const urls = ["wss://test1.relay", "wss://test2.relay"]
const mockSockets = urls.map(url => ({ url, cleanup: vi.fn() }))
let socketIndex = 0
customMakeSocket.mockImplementation(() => mockSockets[socketIndex++])
urls.forEach(url => pool.get(url))
for (const mockSocket of mockSockets) {
pool._data.set(mockSocket.url, mockSocket)
}
pool.clear()
expect(pool._data.size).toBe(0)
@@ -234,9 +126,5 @@ describe("Pool", () => {
expect(socket.cleanup).toHaveBeenCalled()
})
})
it("should do nothing on empty pool", () => {
expect(() => pool.clear()).not.toThrow()
})
})
})
+29 -20
View File
@@ -14,21 +14,11 @@ import {
import {Socket, SocketStatus, SocketEventType} from "./socket.js"
import {AuthState, AuthStatus, AuthStateEventType} from "./auth.js"
// Pause sending messages when the socket isn't open
export const socketPolicySendWhenOpen = (socket: Socket) => {
const unsubscribers = [
on(socket, SocketEventType.Status, (newStatus: SocketStatus) => {
if (newStatus === SocketStatus.Open) {
socket._sendQueue.start()
} else {
socket._sendQueue.stop()
}
}),
]
return () => unsubscribers.forEach(call)
}
/**
* Defers sending messages when a challenge has been presented and not answered yet
* @param socket - a Socket object
* @return a cleanup function
*/
export const socketPolicyDeferOnAuth = (socket: Socket) => {
const buffer: ClientMessage[] = []
const authState = new AuthState(socket)
@@ -78,6 +68,11 @@ export const socketPolicyDeferOnAuth = (socket: Socket) => {
}
}
/**
* Re-enqueues event/req messages once if rejected due to auth-required
* @param socket - a Socket object
* @return a cleanup function
*/
export const socketPolicyRetryAuthRequired = (socket: Socket) => {
const retried = new Set<string>()
const pending = new Map<string, ClientMessage>()
@@ -132,6 +127,11 @@ export const socketPolicyRetryAuthRequired = (socket: Socket) => {
return () => unsubscribers.forEach(call)
}
/**
* Auto-connects a closed socket when a message is sent unless there was a recent error
* @param socket - a Socket object
* @return a cleanup function
*/
export const socketPolicyConnectOnSend = (socket: Socket) => {
let lastError = 0
let currentStatus = SocketStatus.Closed
@@ -146,9 +146,9 @@ export const socketPolicyConnectOnSend = (socket: Socket) => {
// Keep track of the current status
currentStatus = newStatus
}),
on(socket, SocketEventType.Send, (message: ClientMessage) => {
on(socket, SocketEventType.Enqueue, (message: ClientMessage) => {
// When a new message is sent, make sure the socket is open (unless there was a recent error)
if (currentStatus === SocketStatus.Closed && now() - lastError < ago(30)) {
if (currentStatus === SocketStatus.Closed && lastError < ago(30)) {
socket.open()
}
}),
@@ -157,8 +157,13 @@ export const socketPolicyConnectOnSend = (socket: Socket) => {
return () => unsubscribers.forEach(call)
}
/**
* Auto-closes a socket after 30 seconds of inactivity
* @param socket - a Socket object
* @return a cleanup function
*/
export const socketPolicyCloseOnTimeout = (socket: Socket) => {
let lastActivity = 0
let lastActivity = now()
const unsubscribers = [
on(socket, SocketEventType.Send, (message: ClientMessage) => {
@@ -170,7 +175,7 @@ export const socketPolicyCloseOnTimeout = (socket: Socket) => {
]
const interval = setInterval(() => {
if (lastActivity < ago(30)) {
if (socket.status === SocketStatus.Open && lastActivity < ago(30)) {
socket.close()
}
}, 3000)
@@ -181,6 +186,11 @@ export const socketPolicyCloseOnTimeout = (socket: Socket) => {
}
}
/**
* Automatically re-opens a socket if there are active requests or publishes
* @param socket - a Socket object
* @return a cleanup function
*/
export const socketPolicyReopenActive = (socket: Socket) => {
const pending = new Map<string, ClientMessage>()
@@ -226,7 +236,6 @@ export const socketPolicyReopenActive = (socket: Socket) => {
}
export const defaultSocketPolicies = [
socketPolicySendWhenOpen,
socketPolicyDeferOnAuth,
socketPolicyRetryAuthRequired,
socketPolicyConnectOnSend,
+2 -2
View File
@@ -23,10 +23,10 @@ export class Pool {
_data = new Map<string, Socket>()
_subs: PoolSubscription[] = []
constructor(readonly options: PoolOptions) {}
constructor(readonly options: PoolOptions = {}) {}
has(url: string) {
return this._data.has(url)
return this._data.has(normalizeRelayUrl(url))
}
makeSocket(url: string) {
+6 -1
View File
@@ -62,15 +62,20 @@ export class Socket extends (EventEmitter as new () => TypedEmitter<SocketEvents
this._ws = new WebSocket(this.url)
this.emit(SocketEventType.Status, SocketStatus.Opening, this.url)
this._ws.onopen = () => this.emit(SocketEventType.Status, SocketStatus.Open, this.url)
this._ws.onopen = () => {
this.emit(SocketEventType.Status, SocketStatus.Open, this.url)
this._sendQueue.start()
}
this._ws.onerror = () => {
this.emit(SocketEventType.Status, SocketStatus.Error, this.url)
this._sendQueue.stop()
this._ws = undefined
}
this._ws.onclose = () => {
this.emit(SocketEventType.Status, SocketStatus.Closed, this.url)
this._sendQueue.stop()
this._ws = undefined
}