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