Fix some tests
This commit is contained in:
@@ -1,30 +1,12 @@
|
||||
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"
|
||||
import { EventEmitter } from "events"
|
||||
import { Unicast, Multicast, PublishEvent, PublishStatus, unicast, multicast } from "../src/publish"
|
||||
import { AbstractAdapter, AdapterEvent } from "../src/adapter"
|
||||
import { SinglePublish, MultiPublish, PublishEvent, PublishStatus, } from "../src/publish"
|
||||
import { AbstractAdapter, AdapterEvent, MockAdapter } from "../src/adapter"
|
||||
import { ClientMessageType, RelayMessage } from "../src/message"
|
||||
import { SignedEvent, makeEvent } from "@welshman/util"
|
||||
import { Nip01Signer } from '@welshman/signer'
|
||||
|
||||
class MockAdapter extends AbstractAdapter {
|
||||
constructor(readonly url: string, readonly send) {
|
||||
super()
|
||||
}
|
||||
|
||||
get sockets() {
|
||||
return []
|
||||
}
|
||||
|
||||
get urls() {
|
||||
return [this.url]
|
||||
}
|
||||
|
||||
receive = (message: RelayMessage) => {
|
||||
this.emit(AdapterEvent.Receive, message, this.url)
|
||||
}
|
||||
}
|
||||
|
||||
describe("Unicast", () => {
|
||||
describe("SinglePublish", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers()
|
||||
})
|
||||
@@ -39,7 +21,7 @@ describe("Unicast", () => {
|
||||
const signer = Nip01Signer.ephemeral()
|
||||
const event = await signer.sign(makeEvent(1))
|
||||
|
||||
const pub = unicast({
|
||||
const pub = new SinglePublish({
|
||||
relay: '1',
|
||||
context: {getAdapter: () => adapter},
|
||||
event,
|
||||
@@ -72,7 +54,7 @@ describe("Unicast", () => {
|
||||
const signer = Nip01Signer.ephemeral()
|
||||
const event = await signer.sign(makeEvent(1))
|
||||
|
||||
const pub = unicast({
|
||||
const pub = new SinglePublish({
|
||||
relay: '1',
|
||||
context: {getAdapter: () => adapter},
|
||||
event,
|
||||
@@ -105,7 +87,7 @@ describe("Unicast", () => {
|
||||
const signer = Nip01Signer.ephemeral()
|
||||
const event = await signer.sign(makeEvent(1))
|
||||
|
||||
const pub = unicast({
|
||||
const pub = new SinglePublish({
|
||||
relay: '1',
|
||||
context: {getAdapter: () => adapter},
|
||||
event,
|
||||
@@ -139,7 +121,7 @@ describe("Unicast", () => {
|
||||
const signer = Nip01Signer.ephemeral()
|
||||
const event = await signer.sign(makeEvent(1))
|
||||
|
||||
const pub = unicast({
|
||||
const pub = new SinglePublish({
|
||||
relay: '1',
|
||||
context: {getAdapter: () => adapter},
|
||||
event,
|
||||
@@ -170,7 +152,7 @@ describe("Unicast", () => {
|
||||
})
|
||||
})
|
||||
|
||||
describe("Multicast", () => {
|
||||
describe("MultiPublish", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers()
|
||||
})
|
||||
@@ -189,7 +171,7 @@ describe("Multicast", () => {
|
||||
const signer = Nip01Signer.ephemeral()
|
||||
const event = await signer.sign(makeEvent(1))
|
||||
|
||||
const pub = multicast({
|
||||
const pub = new MultiPublish({
|
||||
event,
|
||||
relays: ['1', '2', '3'],
|
||||
context: {
|
||||
|
||||
@@ -1,10 +1,11 @@
|
||||
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"
|
||||
import { Socket, SocketEvent } from "../src/socket"
|
||||
import { Relay, LOCAL_RELAY_URL, isRelayUrl } from "@welshman/util"
|
||||
import { AdapterEvent, SocketAdapter, LocalAdapter, getAdapter } from "../src/adapter"
|
||||
import { Pool } from "../src/pool"
|
||||
import { ClientMessage, RelayMessage } from "../src/message"
|
||||
import EventEmitter from "events"
|
||||
import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"
|
||||
import { isRelayUrl } from "@welshman/util"
|
||||
import { LocalRelay, LOCAL_RELAY_URL } from "@welshman/relay"
|
||||
import { AdapterEvent, SocketAdapter, LocalAdapter, getAdapter } from "../src/adapter"
|
||||
import { ClientMessage, RelayMessage } from "../src/message"
|
||||
import { Socket, SocketEvent } from "../src/socket"
|
||||
import { Pool } from "../src/pool"
|
||||
|
||||
vi.mock('isomorphic-ws', () => {
|
||||
const WebSocket = vi.fn(function () {
|
||||
@@ -69,7 +70,7 @@ describe("SocketAdapter", () => {
|
||||
})
|
||||
|
||||
describe("LocalAdapter", () => {
|
||||
let relay: Relay & EventEmitter
|
||||
let relay: LocalRelay & EventEmitter
|
||||
let adapter: LocalAdapter
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -78,7 +79,7 @@ describe("LocalAdapter", () => {
|
||||
send: vi.fn(),
|
||||
removeAllListeners: vi.fn()
|
||||
})
|
||||
relay = mockRelay as unknown as Relay & EventEmitter
|
||||
relay = mockRelay as unknown as LocalRelay & EventEmitter
|
||||
adapter = new LocalAdapter(relay)
|
||||
})
|
||||
|
||||
@@ -119,11 +120,11 @@ describe("LocalAdapter", () => {
|
||||
|
||||
describe("getAdapter", () => {
|
||||
let pool: Pool
|
||||
let relay: Relay
|
||||
let relay: LocalRelay
|
||||
|
||||
beforeEach(() => {
|
||||
pool = new Pool()
|
||||
relay = new Relay()
|
||||
relay = new LocalRelay()
|
||||
pool.get = vi.fn().mockReturnValue(new Socket("wss://test.relay"))
|
||||
})
|
||||
|
||||
@@ -143,20 +144,6 @@ describe("getAdapter", () => {
|
||||
expect(adapter).toBeInstanceOf(SocketAdapter)
|
||||
})
|
||||
|
||||
it("should throw error for invalid relay URL", () => {
|
||||
expect(() => getAdapter("invalid-url", {})).toThrow("Invalid relay url invalid-url")
|
||||
})
|
||||
|
||||
it("should throw error for local relay URL without relay context", () => {
|
||||
const url = LOCAL_RELAY_URL
|
||||
expect(() => getAdapter(url, {})).toThrow(`Unable to get local relay for ${url}`)
|
||||
})
|
||||
|
||||
it("should throw error for remote relay URL without pool context", () => {
|
||||
const url = "wss://test.relay"
|
||||
expect(() => getAdapter(url, {})).toThrow(`Unable to get socket for ${url}`)
|
||||
})
|
||||
|
||||
it("should use custom adapter if provided", () => {
|
||||
const customAdapter = new SocketAdapter(new Socket("wss://test.relay"))
|
||||
const getCustomAdapter = vi.fn().mockReturnValue(customAdapter)
|
||||
|
||||
@@ -2,28 +2,10 @@ import { describe, expect, it, vi, beforeEach, afterEach } from "vitest"
|
||||
import { Nip01Signer } from '@welshman/signer'
|
||||
import { LOCAL_RELAY_URL, makeEvent } from '@welshman/util'
|
||||
import { ClientMessageType, RelayMessage } from "../src/message"
|
||||
import { AdapterContext, AbstractAdapter, AdapterEvent } from "../src/adapter"
|
||||
import { unireq, multireq, RequestEvent } from "../src/request"
|
||||
import { AdapterContext, AbstractAdapter, AdapterEvent, MockAdapter } from "../src/adapter"
|
||||
import { SingleRequest, MultiRequest, RequestEvent } from "../src/request"
|
||||
import { Tracker } from "../src/tracker"
|
||||
|
||||
class MockAdapter extends AbstractAdapter {
|
||||
constructor(readonly url: string, readonly send) {
|
||||
super()
|
||||
}
|
||||
|
||||
get sockets() {
|
||||
return []
|
||||
}
|
||||
|
||||
get urls() {
|
||||
return [this.url]
|
||||
}
|
||||
|
||||
receive = (message: RelayMessage) => {
|
||||
this.emit(AdapterEvent.Receive, message, this.url)
|
||||
}
|
||||
}
|
||||
|
||||
describe("Unireq", () => {
|
||||
beforeEach(() => {
|
||||
vi.useFakeTimers()
|
||||
@@ -36,7 +18,7 @@ describe("Unireq", () => {
|
||||
it("everything basically works", async () => {
|
||||
const sendSpy = vi.fn()
|
||||
const adapter = new MockAdapter('1', sendSpy)
|
||||
const req = unireq({
|
||||
const req = new SingleRequest({
|
||||
relay: 'whatever',
|
||||
filter: {kinds: [1]},
|
||||
context: {getAdapter: () => adapter},
|
||||
@@ -100,7 +82,7 @@ describe("Multireq", () => {
|
||||
const adapter1 = new MockAdapter('1', send1Spy)
|
||||
const send2Spy = vi.fn()
|
||||
const adapter2 = new MockAdapter('2', send2Spy)
|
||||
const req = multireq({
|
||||
const req = new MultiRequest({
|
||||
autoClose: true,
|
||||
relays: ['1', '2'],
|
||||
filter: {kinds: [1]},
|
||||
|
||||
Reference in New Issue
Block a user