Basic networking #9
3 changed files with 40 additions and 41 deletions
24
src/testing/MockHelper.ts
Normal file
24
src/testing/MockHelper.ts
Normal file
|
@ -0,0 +1,24 @@
|
||||||
|
import Peer from "peerjs";
|
||||||
|
import { MockPeer } from ".";
|
||||||
|
import { LocalClient, PeerServer, PeerClient, RoomInfo } from "@/network";
|
||||||
|
|
||||||
|
export class MockHelper {
|
||||||
|
private mocks: Record<string, MockPeer> = {};
|
||||||
|
|
||||||
|
createServer(
|
||||||
|
room: RoomInfo,
|
||||||
|
id: string = "test-server",
|
||||||
|
name: string = "server-client"
|
||||||
|
) {
|
||||||
|
const serverPeer = new MockPeer(this.mocks, id, {});
|
||||||
|
const serverPlayer = new LocalClient({ name: name });
|
||||||
|
return new PeerServer(room, serverPlayer, serverPeer as Peer);
|
||||||
|
}
|
||||||
|
|
||||||
|
createClient(id: string = "test-client", name: string = "client-peer") {
|
||||||
|
const clientPeer = new MockPeer(this.mocks, id, {});
|
||||||
|
return new PeerClient({ name }, clientPeer as Peer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default MockHelper;
|
|
@ -1,2 +1,3 @@
|
||||||
export * from "./MockDataConnection";
|
export * from "./MockDataConnection";
|
||||||
export * from "./MockPeer";
|
export * from "./MockPeer";
|
||||||
|
export * from "./MockHelper";
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import Peer from "peerjs";
|
import { MockHelper } from "@/testing";
|
||||||
import { MockPeer } from "@/testing";
|
import { NetworkMessage } from "@/network";
|
||||||
import { PeerServer, LocalClient, PeerClient, NetworkMessage } from "@/network";
|
|
||||||
import { EventHook } from "@/testing/EventHook";
|
import { EventHook } from "@/testing/EventHook";
|
||||||
|
|
||||||
const sampleRoom = () => ({
|
const sampleRoom = () => ({
|
||||||
|
@ -8,31 +7,6 @@ const sampleRoom = () => ({
|
||||||
password: ""
|
password: ""
|
||||||
});
|
});
|
||||||
|
|
||||||
function mockSource(): Record<string, MockPeer> {
|
|
||||||
return {};
|
|
||||||
}
|
|
||||||
|
|
||||||
function createServer(
|
|
||||||
mockSource: Record<string, MockPeer>,
|
|
||||||
id: string = "test-server",
|
|
||||||
name: string = "server-client"
|
|
||||||
) {
|
|
||||||
const serverPeer = new MockPeer(mockSource, id, {});
|
|
||||||
const serverPlayer = new LocalClient({
|
|
||||||
name: name
|
|
||||||
});
|
|
||||||
return new PeerServer(sampleRoom(), serverPlayer, serverPeer as Peer);
|
|
||||||
}
|
|
||||||
|
|
||||||
function createClient(
|
|
||||||
mockSource: Record<string, MockPeer>,
|
|
||||||
id: string = "test-client",
|
|
||||||
name: string = "client-peer"
|
|
||||||
) {
|
|
||||||
const clientPeer = new MockPeer(mockSource, id, {});
|
|
||||||
return new PeerClient({ name }, clientPeer as Peer);
|
|
||||||
}
|
|
||||||
|
|
||||||
function messageKind(kind: string): (msg: NetworkMessage) => void {
|
function messageKind(kind: string): (msg: NetworkMessage) => void {
|
||||||
return msg => {
|
return msg => {
|
||||||
expect(msg.kind).toEqual(kind);
|
expect(msg.kind).toEqual(kind);
|
||||||
|
@ -41,15 +15,15 @@ function messageKind(kind: string): (msg: NetworkMessage) => void {
|
||||||
|
|
||||||
describe("network/PeerServer", () => {
|
describe("network/PeerServer", () => {
|
||||||
test("Create server", () => {
|
test("Create server", () => {
|
||||||
const mox = mockSource();
|
const mox = new MockHelper();
|
||||||
createServer(mox);
|
mox.createServer(sampleRoom());
|
||||||
});
|
});
|
||||||
|
|
||||||
test("Test client join", async () => {
|
test("Test client join", async () => {
|
||||||
const mox = mockSource();
|
const mox = new MockHelper();
|
||||||
const hook = new EventHook();
|
const hook = new EventHook();
|
||||||
createServer(mox);
|
mox.createServer(sampleRoom());
|
||||||
const client = createClient(mox);
|
const client = mox.createClient();
|
||||||
hook.hookEmitter(client, "data", "client-data");
|
hook.hookEmitter(client, "data", "client-data");
|
||||||
client.connect("test-server");
|
client.connect("test-server");
|
||||||
await hook.expect("client-data", 1000, messageKind("room-info"));
|
await hook.expect("client-data", 1000, messageKind("room-info"));
|
||||||
|
@ -57,11 +31,11 @@ describe("network/PeerServer", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
test("Test multiple clients (w/ name collision)", async () => {
|
test("Test multiple clients (w/ name collision)", async () => {
|
||||||
const mox = mockSource();
|
const mox = new MockHelper();
|
||||||
const hook = new EventHook();
|
const hook = new EventHook();
|
||||||
createServer(mox);
|
mox.createServer(sampleRoom());
|
||||||
const client1 = createClient(mox);
|
const client1 = mox.createClient();
|
||||||
const client2 = createClient(mox);
|
const client2 = mox.createClient();
|
||||||
hook.hookEmitter(client1, "data", "client1-data");
|
hook.hookEmitter(client1, "data", "client1-data");
|
||||||
hook.hookEmitter(client2, "data", "client2-data");
|
hook.hookEmitter(client2, "data", "client2-data");
|
||||||
client1.connect("test-server");
|
client1.connect("test-server");
|
||||||
|
@ -77,11 +51,11 @@ describe("network/PeerServer", () => {
|
||||||
|
|
||||||
describe("network/PeerClient", () => {
|
describe("network/PeerClient", () => {
|
||||||
test("Client handles forced name change", async () => {
|
test("Client handles forced name change", async () => {
|
||||||
const mox = mockSource();
|
const mox = new MockHelper();
|
||||||
const hook = new EventHook();
|
const hook = new EventHook();
|
||||||
createServer(mox);
|
mox.createServer(sampleRoom());
|
||||||
const client1 = createClient(mox);
|
const client1 = mox.createClient();
|
||||||
const client2 = createClient(mox);
|
const client2 = mox.createClient();
|
||||||
hook.hookEmitter(client2, "rename", "client-rename");
|
hook.hookEmitter(client2, "rename", "client-rename");
|
||||||
client1.connect("test-server");
|
client1.connect("test-server");
|
||||||
client2.connect("test-server");
|
client2.connect("test-server");
|
||||||
|
|
Loading…
Reference in a new issue