Skip to content

Commit

Permalink
test: mock websocket server with mock-socket
Browse files Browse the repository at this point in the history
  • Loading branch information
hasundue committed Sep 4, 2023
1 parent c65596f commit c5d1643
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 9 deletions.
7 changes: 6 additions & 1 deletion core/nodes_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,18 @@ import {
describe,
it,
} from "../lib/std/testing.ts";
import { Server, WebSocket } from "../lib/x/mock-socket.ts";

describe("NostrNode", () => {
const url = "wss://localhost:8080";
let server: Server;
let node: NostrNode;
let ws: WebSocket;

beforeEach(() => {
server = new Server(url);
node = new NostrNode(() => {
ws = new WebSocket("wss://nostr-dev.wellorder.net");
ws = new WebSocket(url);
return ws;
});
});
Expand All @@ -26,6 +30,7 @@ describe("NostrNode", () => {
throw err;
}
});
server.close();
});

it("should be able to create a NostrNode instance", () => {
Expand Down
7 changes: 6 additions & 1 deletion core/websockets_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,26 @@ import {
describe,
it,
} from "../lib/std/testing.ts";
import { Server, WebSocket } from "../lib/x/mock-socket.ts";
import { LazyWebSocket } from "./websockets.ts";

describe("LazyWebSocket", () => {
let server: Server;
let ws: WebSocket;
let lazy: LazyWebSocket;

beforeEach(() => {
const url = "wss://localhost:8080";
server = new Server(url);
lazy = new LazyWebSocket(() => {
ws = new WebSocket("wss://nostr-dev.wellorder.net");
ws = new WebSocket(url);
return ws;
});
});

afterEach(async () => {
await lazy.close();
server.close();
});

it("should be able to create a LazyWebSocket instance", () => {
Expand Down
1 change: 1 addition & 0 deletions lib/x/mock-socket.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { Server, WebSocket } from "npm:[email protected]";
41 changes: 34 additions & 7 deletions tests/client_test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { Relay } from "../client.ts";
import {
ClientToRelayMessage,
Relay,
RelayToClientMessage,
} from "../client.ts";
import { pop } from "../lib/x/streamtools.ts";
import {
afterAll,
Expand All @@ -11,25 +15,28 @@ import {
describe,
it,
} from "../lib/std/testing.ts";
import { Server, WebSocket } from "../lib/x/mock-socket.ts";

const url = "wss://localhost:8080";

describe("Relay constructor", () => {
let relay: Relay;

describe("called with url only", () => {
beforeAll(() => {
relay = new Relay("wss://nostr-dev.wellorder.net");
relay = new Relay(url);
});

it("should be constructable", () => {
assert(relay instanceof Relay);
});

it("should have a url", () => {
assertEquals(relay.config.url, "wss://nostr-dev.wellorder.net");
assertEquals(relay.config.url, url);
});

it("should have a name", () => {
assertEquals(relay.config.name, "nostr-dev.wellorder.net");
assertEquals(relay.config.name, "localhost:8080");
});

it("should have default options", () => {
Expand All @@ -45,7 +52,7 @@ describe("Relay constructor", () => {
const logger = { info: () => {} };

beforeAll(() => {
relay = new Relay("wss://nostr-dev.wellorder.net", {
relay = new Relay(url, {
name: "test",
read: false,
write: false,
Expand All @@ -65,7 +72,7 @@ describe("Relay constructor", () => {
it("should have the given options", () => {
assertObjectMatch(relay.config, {
name: "test",
url: "wss://nostr-dev.wellorder.net",
url,
nbuffer: 20,
read: false,
write: false,
Expand All @@ -76,14 +83,34 @@ describe("Relay constructor", () => {
});

describe("Relay", () => {
const url = "wss://localhost:8080";
let server: Server;
let relay: Relay;

beforeEach(() => {
relay = new Relay("wss://nostr-dev.wellorder.net");
server = new Server(url);
server.on("connection", (ws) => {
ws.on("message", (data) => {
if (typeof data !== "string") throw new Error();
const msg = JSON.parse(data) as ClientToRelayMessage;
if (msg[0] === "REQ") {
ws.send(JSON.stringify(
[
"EVENT",
msg[1],
// deno-lint-ignore no-explicit-any
{ kind: 1 } as any,
] satisfies RelayToClientMessage,
));
}
});
});
relay = new Relay(url);
});

afterEach(async () => {
await relay.close();
server.close();
});

it("should not be connected initially", () => {
Expand Down

0 comments on commit c5d1643

Please sign in to comment.