Re-work test mocks for net2
This commit is contained in:
@@ -1,6 +1,6 @@
|
|||||||
--ignore-dir=docs
|
--ignore-dir=docs
|
||||||
--ignore-dir=dist
|
--ignore-dir=dist
|
||||||
--ignore-dir=build
|
--ignore-dir=build
|
||||||
--ignore-dir=__tests__
|
# --ignore-dir=__tests__
|
||||||
--ignore-dir=.svelte-kit
|
--ignore-dir=.svelte-kit
|
||||||
--ignore-file=match:yarn.lock
|
--ignore-file=match:yarn.lock
|
||||||
|
|||||||
Generated
-1092
File diff suppressed because it is too large
Load Diff
@@ -1,36 +1,35 @@
|
|||||||
import { sleep } from "@welshman/lib"
|
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 { afterEach, beforeEach, describe, expect, it, vi } from "vitest"
|
||||||
import { Socket, SocketStatus, SocketEventType } from "../src/socket"
|
import { Socket, SocketStatus, SocketEventType } from "../src/socket"
|
||||||
import { ClientMessage, RelayMessage } from "../src/message"
|
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", () => {
|
describe("Socket", () => {
|
||||||
let socket: Socket
|
let socket: Socket
|
||||||
let mockWs: any
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.useFakeTimers()
|
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")
|
socket = new Socket("wss://test.relay")
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
socket.cleanup()
|
vi.clearAllMocks()
|
||||||
vi.useRealTimers()
|
vi.useRealTimers()
|
||||||
|
socket.cleanup()
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should initialize with correct url", () => {
|
it("should initialize with correct url", () => {
|
||||||
@@ -44,16 +43,10 @@ describe("Socket", () => {
|
|||||||
|
|
||||||
socket.open()
|
socket.open()
|
||||||
|
|
||||||
expect(WebSocket).toHaveBeenCalledWith("wss://test.relay")
|
expect(socket._ws).toBeDefined()
|
||||||
expect(statusSpy).toHaveBeenCalledWith(SocketStatus.Opening, "wss://test.relay")
|
expect(statusSpy).toHaveBeenCalledWith(SocketStatus.Opening, "wss://test.relay")
|
||||||
})
|
|
||||||
|
|
||||||
it("should emit open status when connection opens", () => {
|
vi.runAllTimers()
|
||||||
const statusSpy = vi.fn()
|
|
||||||
socket.on(SocketEventType.Status, statusSpy)
|
|
||||||
|
|
||||||
socket.open()
|
|
||||||
mockWs.onopen()
|
|
||||||
|
|
||||||
expect(statusSpy).toHaveBeenCalledWith(SocketStatus.Open, "wss://test.relay")
|
expect(statusSpy).toHaveBeenCalledWith(SocketStatus.Open, "wss://test.relay")
|
||||||
})
|
})
|
||||||
@@ -83,10 +76,12 @@ describe("Socket", () => {
|
|||||||
socket.on(SocketEventType.Status, statusSpy)
|
socket.on(SocketEventType.Status, statusSpy)
|
||||||
|
|
||||||
socket.open()
|
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")
|
expect(statusSpy).toHaveBeenCalledWith(SocketStatus.Closed, "wss://test.relay")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -96,26 +91,25 @@ describe("Socket", () => {
|
|||||||
const enqueueSpy = vi.fn()
|
const enqueueSpy = vi.fn()
|
||||||
socket.on(SocketEventType.Enqueue, enqueueSpy)
|
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)
|
socket.send(message)
|
||||||
|
|
||||||
expect(enqueueSpy).toHaveBeenCalledWith(message, "wss://test.relay")
|
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()
|
const sendSpy = vi.fn()
|
||||||
socket.on(SocketEventType.Send, sendSpy)
|
socket.on(SocketEventType.Send, sendSpy)
|
||||||
|
|
||||||
socket.open()
|
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)
|
socket.send(message)
|
||||||
|
|
||||||
// Allow task queue to process
|
|
||||||
await vi.runAllTimers()
|
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")
|
expect(sendSpy).toHaveBeenCalledWith(message, "wss://test.relay")
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -126,8 +120,8 @@ describe("Socket", () => {
|
|||||||
socket.on(SocketEventType.Receive, receiveSpy)
|
socket.on(SocketEventType.Receive, receiveSpy)
|
||||||
|
|
||||||
socket.open()
|
socket.open()
|
||||||
const message: RelayMessage = ["EVENT", "123", { id: "123", kind: 1, content: "", tags: [], pubkey: "", sig: "" }]
|
const message: RelayMessage = ["EVENT", "123", { id: "123", kind: 1 }]
|
||||||
mockWs.onmessage({ data: JSON.stringify(message) })
|
socket._ws.onmessage({ data: JSON.stringify(message) })
|
||||||
|
|
||||||
// Allow task queue to process
|
// Allow task queue to process
|
||||||
await vi.runAllTimers()
|
await vi.runAllTimers()
|
||||||
@@ -140,7 +134,7 @@ describe("Socket", () => {
|
|||||||
socket.on(SocketEventType.Error, errorSpy)
|
socket.on(SocketEventType.Error, errorSpy)
|
||||||
|
|
||||||
socket.open()
|
socket.open()
|
||||||
mockWs.onmessage({ data: "invalid json" })
|
socket._ws.onmessage({ data: "invalid json" })
|
||||||
|
|
||||||
expect(errorSpy).toHaveBeenCalledWith("Invalid message received", "wss://test.relay")
|
expect(errorSpy).toHaveBeenCalledWith("Invalid message received", "wss://test.relay")
|
||||||
})
|
})
|
||||||
@@ -150,7 +144,7 @@ describe("Socket", () => {
|
|||||||
socket.on(SocketEventType.Error, errorSpy)
|
socket.on(SocketEventType.Error, errorSpy)
|
||||||
|
|
||||||
socket.open()
|
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")
|
expect(errorSpy).toHaveBeenCalledWith("Invalid message received", "wss://test.relay")
|
||||||
})
|
})
|
||||||
@@ -159,9 +153,12 @@ describe("Socket", () => {
|
|||||||
describe("cleanup", () => {
|
describe("cleanup", () => {
|
||||||
it("should close socket and clear queues", () => {
|
it("should close socket and clear queues", () => {
|
||||||
socket.open()
|
socket.open()
|
||||||
|
|
||||||
|
const ws = socket._ws
|
||||||
|
|
||||||
socket.cleanup()
|
socket.cleanup()
|
||||||
|
|
||||||
expect(mockWs.close).toHaveBeenCalled()
|
expect(ws.close).toHaveBeenCalled()
|
||||||
expect(socket.listenerCount(SocketEventType.Send)).toBe(0)
|
expect(socket.listenerCount(SocketEventType.Send)).toBe(0)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -172,7 +169,7 @@ describe("Socket", () => {
|
|||||||
socket.on(SocketEventType.Status, statusSpy)
|
socket.on(SocketEventType.Status, statusSpy)
|
||||||
|
|
||||||
socket.open()
|
socket.open()
|
||||||
mockWs.onerror()
|
socket._ws.onerror()
|
||||||
|
|
||||||
expect(statusSpy).toHaveBeenCalledWith(SocketStatus.Error, "wss://test.relay")
|
expect(statusSpy).toHaveBeenCalledWith(SocketStatus.Error, "wss://test.relay")
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -6,39 +6,35 @@ import { Pool } from "../src/pool"
|
|||||||
import { ClientMessage, RelayMessage } from "../src/message"
|
import { ClientMessage, RelayMessage } from "../src/message"
|
||||||
import EventEmitter from "events"
|
import EventEmitter from "events"
|
||||||
|
|
||||||
vi.mock("@welshman/lib", () => ({
|
vi.mock('isomorphic-ws', () => {
|
||||||
on: (target: any, eventName: string, callback: Function) => {
|
const WebSocket = vi.fn(function () {
|
||||||
target.on(eventName, callback)
|
setTimeout(() => this.onopen())
|
||||||
return () => target.off(eventName, callback)
|
})
|
||||||
},
|
|
||||||
call: (fn: Function) => fn()
|
|
||||||
}))
|
|
||||||
|
|
||||||
vi.mock("../src/socket")
|
WebSocket.prototype.send = vi.fn()
|
||||||
vi.mock("@welshman/util", () => ({
|
|
||||||
Relay: vi.fn(() => new EventEmitter()),
|
WebSocket.prototype.close = vi.fn(function () {
|
||||||
LOCAL_RELAY_URL: "local://welshman.relay/",
|
this.onclose()
|
||||||
isRelayUrl: vi.fn((url) => url.startsWith("wss://"))
|
})
|
||||||
}))
|
|
||||||
|
return { default: WebSocket }
|
||||||
|
})
|
||||||
|
|
||||||
describe("SocketAdapter", () => {
|
describe("SocketAdapter", () => {
|
||||||
let socket: Socket
|
let socket: Socket
|
||||||
let adapter: SocketAdapter
|
let adapter: SocketAdapter
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
const mockSocket = new EventEmitter()
|
vi.useFakeTimers()
|
||||||
Object.assign(mockSocket, {
|
socket = new Socket('wss://test.relay')
|
||||||
url: "wss://test.relay",
|
|
||||||
send: vi.fn(),
|
|
||||||
removeAllListeners: vi.fn()
|
|
||||||
})
|
|
||||||
socket = mockSocket as unknown as Socket
|
|
||||||
adapter = new SocketAdapter(socket)
|
adapter = new SocketAdapter(socket)
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
adapter.cleanup()
|
|
||||||
vi.clearAllMocks()
|
vi.clearAllMocks()
|
||||||
|
vi.useRealTimers()
|
||||||
|
socket.cleanup()
|
||||||
|
adapter.cleanup()
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should initialize with correct socket", () => {
|
it("should initialize with correct socket", () => {
|
||||||
@@ -51,17 +47,18 @@ describe("SocketAdapter", () => {
|
|||||||
const receiveSpy = vi.fn()
|
const receiveSpy = vi.fn()
|
||||||
adapter.on(AdapterEventType.Receive, receiveSpy)
|
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")
|
socket.emit(SocketEventType.Receive, message, "wss://test.relay")
|
||||||
|
|
||||||
expect(receiveSpy).toHaveBeenCalledWith(message, "wss://test.relay")
|
expect(receiveSpy).toHaveBeenCalledWith(message, "wss://test.relay")
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should send messages to socket", () => {
|
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)
|
adapter.send(message)
|
||||||
|
|
||||||
expect(socket.send).toHaveBeenCalledWith(message)
|
expect(sendSpy).toHaveBeenCalledWith(message)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should cleanup properly", () => {
|
it("should cleanup properly", () => {
|
||||||
@@ -100,14 +97,14 @@ describe("LocalAdapter", () => {
|
|||||||
const receiveSpy = vi.fn()
|
const receiveSpy = vi.fn()
|
||||||
adapter.on(AdapterEventType.Receive, receiveSpy)
|
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)
|
relay.emit("*", ...message)
|
||||||
|
|
||||||
expect(receiveSpy).toHaveBeenCalledWith(message, LOCAL_RELAY_URL)
|
expect(receiveSpy).toHaveBeenCalledWith(message, LOCAL_RELAY_URL)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should send messages to relay", () => {
|
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)
|
adapter.send(message)
|
||||||
|
|
||||||
expect(relay.send).toHaveBeenCalledWith("EVENT", message[1])
|
expect(relay.send).toHaveBeenCalledWith("EVENT", message[1])
|
||||||
@@ -147,7 +144,6 @@ describe("getAdapter", () => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
it("should throw error for invalid relay URL", () => {
|
it("should throw error for invalid relay URL", () => {
|
||||||
vi.mocked(isRelayUrl).mockReturnValue(false)
|
|
||||||
expect(() => getAdapter("invalid-url", {})).toThrow("Invalid relay url invalid-url")
|
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", () => {
|
it("should throw error for remote relay URL without pool context", () => {
|
||||||
const url = "wss://test.relay"
|
const url = "wss://test.relay"
|
||||||
vi.mocked(isRelayUrl).mockReturnValue(true)
|
|
||||||
expect(() => getAdapter(url, {})).toThrow(`Unable to get socket for ${url}`)
|
expect(() => getAdapter(url, {})).toThrow(`Unable to get socket for ${url}`)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -1,242 +1,203 @@
|
|||||||
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"
|
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"
|
||||||
import { Socket, SocketStatus, SocketEventType } from "../src/socket"
|
import { Socket, SocketStatus, SocketEventType } from "../src/socket"
|
||||||
import { makeEvent, CLIENT_AUTH } from "@welshman/util"
|
import { makeEvent, CLIENT_AUTH } from "@welshman/util"
|
||||||
|
import { Nip01Signer } from "@welshman/signer"
|
||||||
import { AuthState, AuthStatus, AuthStateEventType, AuthManager, makeAuthEvent } from "../src/auth"
|
import { AuthState, AuthStatus, AuthStateEventType, AuthManager, makeAuthEvent } from "../src/auth"
|
||||||
import EventEmitter from "events"
|
import EventEmitter from "events"
|
||||||
import { RelayMessage } from "../src/message"
|
import { RelayMessage } from "../src/message"
|
||||||
|
|
||||||
// Mock dependencies
|
vi.mock('isomorphic-ws', () => {
|
||||||
vi.mock("@welshman/lib", () => ({
|
const WebSocket = vi.fn(function () {
|
||||||
on: (target: any, eventName: string, callback: Function) => {
|
setTimeout(() => this.onopen())
|
||||||
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)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
WebSocket.prototype.send = vi.fn()
|
||||||
authState.cleanup()
|
|
||||||
vi.clearAllMocks()
|
WebSocket.prototype.close = vi.fn(function () {
|
||||||
|
this.onclose()
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should initialize with None status", () => {
|
return { default: WebSocket }
|
||||||
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()
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("AuthManager", () => {
|
describe('auth', () => {
|
||||||
let socket: Socket & EventEmitter
|
let socket: Socket
|
||||||
let manager: AuthManager
|
let authManager: AuthManager
|
||||||
let signFn: jest.Mock
|
let sign = vi.fn(Nip01Signer.ephemeral().sign)
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
const mockSocket = new EventEmitter()
|
socket = new Socket('wss://test.relay')
|
||||||
Object.assign(mockSocket, {
|
authManager = new AuthManager(socket, { sign })
|
||||||
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 })
|
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
manager.cleanup()
|
|
||||||
vi.clearAllMocks()
|
vi.clearAllMocks()
|
||||||
|
socket.cleanup()
|
||||||
|
authManager.cleanup()
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should create AuthState instance", () => {
|
describe("AuthState", () => {
|
||||||
expect(manager.state).toBeInstanceOf(AuthState)
|
it("should initialize with None status", () => {
|
||||||
})
|
expect(authManager.state.status).toBe(AuthStatus.None)
|
||||||
|
|
||||||
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")
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should throw error if status is not Requested", async () => {
|
it("should handle AUTH message from relay", () => {
|
||||||
manager.state.challenge = "challenge123"
|
const message: RelayMessage = ["AUTH", "challenge123"]
|
||||||
manager.state.status = AuthStatus.PendingSignature
|
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 () => {
|
it("should handle successful OK message", () => {
|
||||||
manager.state.challenge = "challenge123"
|
authManager.state.request = "request123"
|
||||||
manager.state.status = AuthStatus.Requested
|
const message: RelayMessage = ["OK", "request123", true, "success"]
|
||||||
const signedEvent = { id: "signed-event-id", kind: CLIENT_AUTH }
|
socket.emit(SocketEventType.Receive, message)
|
||||||
signFn.mockResolvedValue(signedEvent)
|
|
||||||
|
|
||||||
await manager.respond()
|
expect(authManager.state.status).toBe(AuthStatus.Ok)
|
||||||
|
expect(authManager.state.details).toBe("success")
|
||||||
expect(manager.state.request).toBe("signed-event-id")
|
|
||||||
expect(socket.send).toHaveBeenCalledWith(["AUTH", signedEvent])
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should handle denied signature", async () => {
|
it("should handle failed OK message", () => {
|
||||||
manager.state.challenge = "challenge123"
|
authManager.state.request = "request123"
|
||||||
manager.state.status = AuthStatus.Requested
|
const message: RelayMessage = ["OK", "request123", false, "forbidden"]
|
||||||
signFn.mockResolvedValue(null)
|
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)
|
it("should ignore OK messages for different requests", () => {
|
||||||
expect(socket.send).not.toHaveBeenCalled()
|
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", () => {
|
describe("AuthManager", () => {
|
||||||
it("should attempt to open socket", async () => {
|
it("should create AuthState instance", () => {
|
||||||
await manager.attempt()
|
expect(authManager.state).toBeInstanceOf(AuthState)
|
||||||
expect(socket.attemptToOpen).toHaveBeenCalled()
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should wait for challenge", async () => {
|
it("should respond automatically when eager is true", () => {
|
||||||
const waitForChallengeSpy = vi.spyOn(manager, "waitForChallenge")
|
const respondSpy = vi.spyOn(AuthManager.prototype, "respond")
|
||||||
await manager.attempt()
|
const eagerManager = new AuthManager(socket, { sign, eager: true })
|
||||||
expect(waitForChallengeSpy).toHaveBeenCalled()
|
|
||||||
})
|
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()
|
expect(respondSpy).toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should wait for resolution", async () => {
|
it("should not respond automatically when eager is false", () => {
|
||||||
const waitForResolutionSpy = vi.spyOn(manager, "waitForResolution")
|
const respondSpy = vi.spyOn(AuthManager.prototype, "respond")
|
||||||
await manager.attempt()
|
socket.emit(SocketEventType.Receive, ["AUTH", "challenge123"])
|
||||||
expect(waitForResolutionSpy).toHaveBeenCalled()
|
|
||||||
|
expect(respondSpy).not.toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
|
||||||
describe("makeAuthEvent", () => {
|
describe("respond", () => {
|
||||||
it("should create auth event with correct tags", () => {
|
it("should throw error if no challenge", async () => {
|
||||||
const url = "wss://test.relay"
|
await expect(authManager.respond()).rejects.toThrow("Attempted to authenticate with no challenge")
|
||||||
const challenge = "challenge123"
|
})
|
||||||
|
|
||||||
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, {
|
await expect(authManager.respond()).rejects.toThrow("Attempted to authenticate when auth is already auth:status:pending_signature")
|
||||||
tags: [
|
})
|
||||||
["relay", url],
|
|
||||||
["challenge", challenge]
|
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()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
import WebSocket from "isomorphic-ws"
|
|
||||||
import { AUTH_JOIN } from "@welshman/util"
|
import { AUTH_JOIN } from "@welshman/util"
|
||||||
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"
|
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"
|
||||||
import { Socket, SocketStatus, SocketEventType } from "../src/socket"
|
import { Socket, SocketStatus, SocketEventType } from "../src/socket"
|
||||||
@@ -13,50 +12,33 @@ import {
|
|||||||
} from "../src/policy"
|
} from "../src/policy"
|
||||||
import { ClientMessage, RelayMessage } from "../src/message"
|
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', () => {
|
describe('policy', () => {
|
||||||
let socket: Socket
|
let socket: Socket
|
||||||
let mockWs: any
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
vi.useFakeTimers()
|
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")
|
socket = new Socket("wss://test.relay")
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
socket.cleanup()
|
socket.cleanup()
|
||||||
vi.useRealTimers()
|
vi.useRealTimers()
|
||||||
})
|
vi.clearAllMocks()
|
||||||
|
|
||||||
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()
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("socketPolicyDeferOnAuth", () => {
|
describe("socketPolicyDeferOnAuth", () => {
|
||||||
@@ -127,4 +109,155 @@ describe('policy', () => {
|
|||||||
cleanup()
|
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", () => {
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -3,188 +3,80 @@ import { Socket } from "../src/socket"
|
|||||||
import { Pool, makeSocket } from "../src/pool"
|
import { Pool, makeSocket } from "../src/pool"
|
||||||
import { normalizeRelayUrl } from "@welshman/util"
|
import { normalizeRelayUrl } from "@welshman/util"
|
||||||
|
|
||||||
// Mock dependencies
|
vi.mock('isomorphic-ws', () => {
|
||||||
vi.mock("@welshman/lib", () => ({
|
const WebSocket = vi.fn(function () {
|
||||||
remove: vi.fn((item, array) => array.filter(x => x !== item)),
|
setTimeout(() => this.onopen())
|
||||||
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)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
WebSocket.prototype.send = vi.fn()
|
||||||
vi.clearAllMocks()
|
|
||||||
|
WebSocket.prototype.close = vi.fn(function () {
|
||||||
|
this.onclose()
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should create socket with url", () => {
|
return { default: WebSocket }
|
||||||
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)
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe("Pool", () => {
|
describe("Pool", () => {
|
||||||
let pool: Pool
|
let pool: Pool
|
||||||
let customMakeSocket: jest.Mock
|
|
||||||
|
|
||||||
beforeEach(() => {
|
beforeEach(() => {
|
||||||
customMakeSocket = vi.fn()
|
pool = new Pool()
|
||||||
pool = new Pool({ makeSocket: customMakeSocket })
|
|
||||||
})
|
})
|
||||||
|
|
||||||
afterEach(() => {
|
afterEach(() => {
|
||||||
vi.clearAllMocks()
|
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", () => {
|
describe("has", () => {
|
||||||
it("should return false for non-existent socket", () => {
|
it("should return false for non-existent socket", () => {
|
||||||
expect(pool.has("wss://test.relay")).toBe(false)
|
expect(pool.has("wss://test.relay")).toBe(false)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should return true for existing socket", () => {
|
it("should return true for existing socket, normalizing the url", () => {
|
||||||
const mockSocket = { url: "wss://test.relay" }
|
pool.get("wss://test.relay/")
|
||||||
customMakeSocket.mockReturnValue(mockSocket)
|
|
||||||
pool.get("wss://test.relay")
|
|
||||||
expect(pool.has("wss://test.relay")).toBe(true)
|
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", () => {
|
describe("get", () => {
|
||||||
it("should normalize relay URL", () => {
|
it("should create new socket if none exists, normalizing the 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)
|
|
||||||
|
|
||||||
const socket = pool.get("wss://test.relay")
|
const socket = pool.get("wss://test.relay")
|
||||||
|
|
||||||
expect(customMakeSocket).toHaveBeenCalledWith("wss://test.relay")
|
expect(socket.url).toEqual("wss://test.relay/")
|
||||||
expect(socket).toBe(mockSocket)
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should return existing socket if it exists", () => {
|
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 firstSocket = pool.get("wss://test.relay")
|
||||||
const secondSocket = pool.get("wss://test.relay")
|
const secondSocket = pool.get("wss://test.relay")
|
||||||
|
|
||||||
expect(customMakeSocket).toHaveBeenCalledTimes(1)
|
|
||||||
expect(firstSocket).toBe(secondSocket)
|
expect(firstSocket).toBe(secondSocket)
|
||||||
})
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
describe("subscribe", () => {
|
||||||
it("should notify subscribers of new sockets", () => {
|
it("should notify subscribers of new sockets", () => {
|
||||||
const sub1 = vi.fn()
|
const sub1 = vi.fn()
|
||||||
const sub2 = vi.fn()
|
const sub2 = vi.fn()
|
||||||
const mockSocket = { url: "wss://test.relay" }
|
|
||||||
customMakeSocket.mockReturnValue(mockSocket)
|
|
||||||
|
|
||||||
pool.subscribe(sub1)
|
pool.subscribe(sub1)
|
||||||
pool.subscribe(sub2)
|
pool.subscribe(sub2)
|
||||||
pool.get("wss://test.relay")
|
pool.get("wss://test.relay")
|
||||||
|
|
||||||
expect(sub1).toHaveBeenCalledWith(mockSocket)
|
expect(sub1).toHaveBeenCalledTimes(1)
|
||||||
expect(sub2).toHaveBeenCalledWith(mockSocket)
|
expect(sub2).toHaveBeenCalledTimes(1)
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should not notify subscribers for existing sockets", () => {
|
it("should not notify subscribers for existing sockets", () => {
|
||||||
const mockSocket = { url: "wss://test.relay" }
|
|
||||||
customMakeSocket.mockReturnValue(mockSocket)
|
|
||||||
pool.get("wss://test.relay")
|
pool.get("wss://test.relay")
|
||||||
|
|
||||||
const sub = vi.fn()
|
const sub = vi.fn()
|
||||||
pool.subscribe(sub)
|
pool.subscribe(sub)
|
||||||
pool.get("wss://test.relay")
|
pool.get("wss://test.relay")
|
||||||
|
|
||||||
expect(sub).not.toHaveBeenCalled()
|
expect(sub).not.toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
})
|
|
||||||
|
|
||||||
describe("subscribe", () => {
|
|
||||||
it("should add subscription", () => {
|
it("should add subscription", () => {
|
||||||
const sub = vi.fn()
|
const sub = vi.fn()
|
||||||
pool.subscribe(sub)
|
pool.subscribe(sub)
|
||||||
@@ -194,9 +86,9 @@ describe("Pool", () => {
|
|||||||
it("should return unsubscribe function", () => {
|
it("should return unsubscribe function", () => {
|
||||||
const sub = vi.fn()
|
const sub = vi.fn()
|
||||||
const unsubscribe = pool.subscribe(sub)
|
const unsubscribe = pool.subscribe(sub)
|
||||||
|
|
||||||
unsubscribe()
|
unsubscribe()
|
||||||
|
|
||||||
expect(pool._subs).not.toContain(sub)
|
expect(pool._subs).not.toContain(sub)
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@@ -204,13 +96,12 @@ describe("Pool", () => {
|
|||||||
describe("remove", () => {
|
describe("remove", () => {
|
||||||
it("should remove and cleanup existing socket", () => {
|
it("should remove and cleanup existing socket", () => {
|
||||||
const mockSocket = { url: "wss://test.relay", cleanup: vi.fn() }
|
const mockSocket = { url: "wss://test.relay", cleanup: vi.fn() }
|
||||||
customMakeSocket.mockReturnValue(mockSocket)
|
|
||||||
|
pool._data.set(mockSocket.url, mockSocket)
|
||||||
pool.get("wss://test.relay")
|
pool.remove(mockSocket.url)
|
||||||
pool.remove("wss://test.relay")
|
|
||||||
|
|
||||||
expect(mockSocket.cleanup).toHaveBeenCalled()
|
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", () => {
|
it("should do nothing for non-existent socket", () => {
|
||||||
@@ -223,10 +114,11 @@ describe("Pool", () => {
|
|||||||
it("should remove all sockets", () => {
|
it("should remove all sockets", () => {
|
||||||
const urls = ["wss://test1.relay", "wss://test2.relay"]
|
const urls = ["wss://test1.relay", "wss://test2.relay"]
|
||||||
const mockSockets = urls.map(url => ({ url, cleanup: vi.fn() }))
|
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()
|
pool.clear()
|
||||||
|
|
||||||
expect(pool._data.size).toBe(0)
|
expect(pool._data.size).toBe(0)
|
||||||
@@ -234,9 +126,5 @@ describe("Pool", () => {
|
|||||||
expect(socket.cleanup).toHaveBeenCalled()
|
expect(socket.cleanup).toHaveBeenCalled()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should do nothing on empty pool", () => {
|
|
||||||
expect(() => pool.clear()).not.toThrow()
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|||||||
+29
-20
@@ -14,21 +14,11 @@ import {
|
|||||||
import {Socket, SocketStatus, SocketEventType} from "./socket.js"
|
import {Socket, SocketStatus, SocketEventType} from "./socket.js"
|
||||||
import {AuthState, AuthStatus, AuthStateEventType} from "./auth.js"
|
import {AuthState, AuthStatus, AuthStateEventType} from "./auth.js"
|
||||||
|
|
||||||
// Pause sending messages when the socket isn't open
|
/**
|
||||||
export const socketPolicySendWhenOpen = (socket: Socket) => {
|
* Defers sending messages when a challenge has been presented and not answered yet
|
||||||
const unsubscribers = [
|
* @param socket - a Socket object
|
||||||
on(socket, SocketEventType.Status, (newStatus: SocketStatus) => {
|
* @return a cleanup function
|
||||||
if (newStatus === SocketStatus.Open) {
|
*/
|
||||||
socket._sendQueue.start()
|
|
||||||
} else {
|
|
||||||
socket._sendQueue.stop()
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
]
|
|
||||||
|
|
||||||
return () => unsubscribers.forEach(call)
|
|
||||||
}
|
|
||||||
|
|
||||||
export const socketPolicyDeferOnAuth = (socket: Socket) => {
|
export const socketPolicyDeferOnAuth = (socket: Socket) => {
|
||||||
const buffer: ClientMessage[] = []
|
const buffer: ClientMessage[] = []
|
||||||
const authState = new AuthState(socket)
|
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) => {
|
export const socketPolicyRetryAuthRequired = (socket: Socket) => {
|
||||||
const retried = new Set<string>()
|
const retried = new Set<string>()
|
||||||
const pending = new Map<string, ClientMessage>()
|
const pending = new Map<string, ClientMessage>()
|
||||||
@@ -132,6 +127,11 @@ export const socketPolicyRetryAuthRequired = (socket: Socket) => {
|
|||||||
return () => unsubscribers.forEach(call)
|
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) => {
|
export const socketPolicyConnectOnSend = (socket: Socket) => {
|
||||||
let lastError = 0
|
let lastError = 0
|
||||||
let currentStatus = SocketStatus.Closed
|
let currentStatus = SocketStatus.Closed
|
||||||
@@ -146,9 +146,9 @@ export const socketPolicyConnectOnSend = (socket: Socket) => {
|
|||||||
// Keep track of the current status
|
// Keep track of the current status
|
||||||
currentStatus = newStatus
|
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)
|
// 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()
|
socket.open()
|
||||||
}
|
}
|
||||||
}),
|
}),
|
||||||
@@ -157,8 +157,13 @@ export const socketPolicyConnectOnSend = (socket: Socket) => {
|
|||||||
return () => unsubscribers.forEach(call)
|
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) => {
|
export const socketPolicyCloseOnTimeout = (socket: Socket) => {
|
||||||
let lastActivity = 0
|
let lastActivity = now()
|
||||||
|
|
||||||
const unsubscribers = [
|
const unsubscribers = [
|
||||||
on(socket, SocketEventType.Send, (message: ClientMessage) => {
|
on(socket, SocketEventType.Send, (message: ClientMessage) => {
|
||||||
@@ -170,7 +175,7 @@ export const socketPolicyCloseOnTimeout = (socket: Socket) => {
|
|||||||
]
|
]
|
||||||
|
|
||||||
const interval = setInterval(() => {
|
const interval = setInterval(() => {
|
||||||
if (lastActivity < ago(30)) {
|
if (socket.status === SocketStatus.Open && lastActivity < ago(30)) {
|
||||||
socket.close()
|
socket.close()
|
||||||
}
|
}
|
||||||
}, 3000)
|
}, 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) => {
|
export const socketPolicyReopenActive = (socket: Socket) => {
|
||||||
const pending = new Map<string, ClientMessage>()
|
const pending = new Map<string, ClientMessage>()
|
||||||
|
|
||||||
@@ -226,7 +236,6 @@ export const socketPolicyReopenActive = (socket: Socket) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export const defaultSocketPolicies = [
|
export const defaultSocketPolicies = [
|
||||||
socketPolicySendWhenOpen,
|
|
||||||
socketPolicyDeferOnAuth,
|
socketPolicyDeferOnAuth,
|
||||||
socketPolicyRetryAuthRequired,
|
socketPolicyRetryAuthRequired,
|
||||||
socketPolicyConnectOnSend,
|
socketPolicyConnectOnSend,
|
||||||
|
|||||||
@@ -23,10 +23,10 @@ export class Pool {
|
|||||||
_data = new Map<string, Socket>()
|
_data = new Map<string, Socket>()
|
||||||
_subs: PoolSubscription[] = []
|
_subs: PoolSubscription[] = []
|
||||||
|
|
||||||
constructor(readonly options: PoolOptions) {}
|
constructor(readonly options: PoolOptions = {}) {}
|
||||||
|
|
||||||
has(url: string) {
|
has(url: string) {
|
||||||
return this._data.has(url)
|
return this._data.has(normalizeRelayUrl(url))
|
||||||
}
|
}
|
||||||
|
|
||||||
makeSocket(url: string) {
|
makeSocket(url: string) {
|
||||||
|
|||||||
@@ -62,15 +62,20 @@ export class Socket extends (EventEmitter as new () => TypedEmitter<SocketEvents
|
|||||||
this._ws = new WebSocket(this.url)
|
this._ws = new WebSocket(this.url)
|
||||||
this.emit(SocketEventType.Status, SocketStatus.Opening, 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._ws.onerror = () => {
|
||||||
this.emit(SocketEventType.Status, SocketStatus.Error, this.url)
|
this.emit(SocketEventType.Status, SocketStatus.Error, this.url)
|
||||||
|
this._sendQueue.stop()
|
||||||
this._ws = undefined
|
this._ws = undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
this._ws.onclose = () => {
|
this._ws.onclose = () => {
|
||||||
this.emit(SocketEventType.Status, SocketStatus.Closed, this.url)
|
this.emit(SocketEventType.Status, SocketStatus.Closed, this.url)
|
||||||
|
this._sendQueue.stop()
|
||||||
this._ws = undefined
|
this._ws = undefined
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user