Skip to content

Commit 5b27059

Browse files
committed
test(nips/01/relays): polish the code
1 parent 58c2d8d commit 5b27059

File tree

1 file changed

+23
-26
lines changed

1 file changed

+23
-26
lines changed

nips/01/relays_test.ts

+23-26
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ import {
1313
RelayToClientMessage,
1414
SubscriptionId,
1515
} from "../../core/protocol.d.ts";
16-
import { ConnectionClosed, Relay } from "../../core/relays.ts?nips=1";
16+
import {
17+
ConnectionClosed,
18+
EventRejected,
19+
Relay,
20+
} from "../../core/relays.ts?nips=1";
1721
import { SubscriptionClosed } from "../../nips/01/relays.ts";
1822

1923
describe("NIP-01/Relay", () => {
@@ -42,11 +46,8 @@ describe("NIP-01/Relay", () => {
4246
it("should receive text notes", async () => {
4347
const reader = sub_1.getReader();
4448
const read = reader.read();
45-
const ws = MockWebSocket.instances[0];
46-
ws.dispatchEvent(
47-
new MessageEvent("message", {
48-
data: JSON.stringify(["EVENT", "test-1", { kind: 1 }]),
49-
}),
49+
MockWebSocket.instances[0].remote.send(
50+
JSON.stringify(["EVENT", "test-1", { kind: 1 }]),
5051
);
5152
const { value, done } = await read;
5253
assert(!done);
@@ -63,16 +64,8 @@ describe("NIP-01/Relay", () => {
6364
const reader_0 = sub_0.getReader();
6465
const reader_1 = sub_1.getReader();
6566
const ws = MockWebSocket.instances[0];
66-
ws.dispatchEvent(
67-
new MessageEvent("message", {
68-
data: JSON.stringify(["EVENT", "test-0", { kind: 0 }]),
69-
}),
70-
);
71-
ws.dispatchEvent(
72-
new MessageEvent("message", {
73-
data: JSON.stringify(["EVENT", "test-1", { kind: 1 }]),
74-
}),
75-
);
67+
ws.remote.send(JSON.stringify(["EVENT", "test-0", { kind: 0 }]));
68+
ws.remote.send(JSON.stringify(["EVENT", "test-1", { kind: 1 }]));
7669
const [{ value: value_0 }, { value: value_1 }] = await Promise.all([
7770
reader_0.read(),
7871
reader_1.read(),
@@ -86,7 +79,6 @@ describe("NIP-01/Relay", () => {
8679
});
8780
it("should publish an event and recieve an accepting OK message", async () => {
8881
const eid = "test-true" as EventId;
89-
const ok = ["OK", eid, true, ""] satisfies RelayToClientMessage<"OK">;
9082
const ws = MockWebSocket.instances[0];
9183
const arrived = new Promise<true>((resolve) => {
9284
ws.remote.addEventListener(
@@ -96,15 +88,18 @@ describe("NIP-01/Relay", () => {
9688
const [, event] = JSON.parse(ev.data) as ClientToRelayMessage<"EVENT">;
9789
if (event.id === eid) {
9890
assertEquals(event.kind, 1);
91+
ws.remote.send(
92+
JSON.stringify(
93+
["OK", eid, true, ""] satisfies RelayToClientMessage<"OK">,
94+
),
95+
);
9996
resolve(true);
100-
ws.remote.send(JSON.stringify(ok));
10197
}
10298
},
10399
);
104100
});
105-
const event = { id: eid, kind: 1 };
106101
// deno-lint-ignore no-explicit-any
107-
await relay.publish(event as any);
102+
await relay.publish({ id: eid, kind: 1 } as any);
108103
assert(await arrived);
109104
});
110105
it("should receieve a rejecting OK message and throw EventRejected", async () => {
@@ -127,12 +122,14 @@ describe("NIP-01/Relay", () => {
127122
);
128123
});
129124
const event = { id: eid, kind: 1 };
130-
// deno-lint-ignore no-explicit-any
131-
const result: unknown = await relay.publish(event as any).catch((e) => e);
132-
// FIXME: This should be an EventRejected instance.
133-
assertInstanceOf(result, Error);
134-
assertEquals(result.message, "error: test");
135-
assertEquals(result.cause, event);
125+
try {
126+
// deno-lint-ignore no-explicit-any
127+
await relay.publish(event as any).catch((e) => e);
128+
} catch (err) {
129+
assertInstanceOf(err, EventRejected);
130+
assertEquals(err.message, "error: test");
131+
assertEquals(err.cause, event);
132+
}
136133
await arrived;
137134
});
138135
it("should throw ConnectionClosed when connection is closed before recieving an OK message", async () => {

0 commit comments

Comments
 (0)