]> jfr.im git - irc/evilnet/node-irc-nefarious.git/commitdiff
Move the test utils to src so they can be exposed to the IRC bridge.
authorHalf-Shot <redacted>
Tue, 25 Apr 2023 16:27:51 +0000 (17:27 +0100)
committerHalf-Shot <redacted>
Tue, 25 Apr 2023 16:27:51 +0000 (17:27 +0100)
spec/client.spec.ts
spec/external-connection.spec.ts
src/index.ts
src/testing/index.ts [moved from spec/util/irc-server.ts with 67% similarity]

index 07067877573c1f7cb8e4b805bd8af44d091759da..cf6cbb5feb92e01675222b73cd4caed98dcc28fa 100644 (file)
@@ -1,8 +1,8 @@
 import { describe, expect, test } from '@jest/globals';
-import { IrcServer } from './util/irc-server';
+import { TestIrcServer } from '..';
 
 
-IrcServer.describe('Client', (server) => {
+TestIrcServer.describe('Client', (server) => {
     describe('joining channels', () => {
         test('will get a join event from a newly joined user', async () => {
             const [speaker, listener] = server().clients;
index 1a9b30eb272031209dad162a77e1f8c95a8132b8..407cb31db2b2a6176b417b51ba8799ce2433afb8 100644 (file)
@@ -1,5 +1,5 @@
 import { test, expect } from '@jest/globals';
-import { IrcServer, TestClient } from './util/irc-server';
+import { TestIrcServer, TestClient } from '..';
 import { DefaultIrcSupported, IrcConnection, IrcInMemoryState } from '../src';
 import { createConnection } from 'net';
 
@@ -10,12 +10,12 @@ class TestIrcInMemoryState extends IrcInMemoryState {
     }
 }
 
-IrcServer.describe('Client with external connection', (getServer) => {
+TestIrcServer.describe('Client with external connection', (getServer) => {
     let client: TestClient;
     test('can connect with a fresh session', async () => {
         const server = getServer();
         const inMemoryState = new TestIrcInMemoryState(DefaultIrcSupported);
-        client = new TestClient(server.address, IrcServer.generateUniqueNick("mynick"), {
+        client = new TestClient(server.address, TestIrcServer.generateUniqueNick("mynick"), {
             port: server.port,
             autoConnect: false,
             connectionTimeout: 4000,
@@ -36,7 +36,7 @@ IrcServer.describe('Client with external connection', (getServer) => {
             port: server.port,
             host: server.address,
         }) as IrcConnection;
-        client = new TestClient(server.address, IrcServer.generateUniqueNick("mynick"), {
+        client = new TestClient(server.address, TestIrcServer.generateUniqueNick("mynick"), {
             port: server.port,
             autoConnect: false,
             connectionTimeout: 4000,
@@ -46,7 +46,7 @@ IrcServer.describe('Client with external connection', (getServer) => {
         client.destroy();
 
         // Somehow we need to clear away the client.
-        const reusedClient = new TestClient(server.address, IrcServer.generateUniqueNick("mynick"), {
+        const reusedClient = new TestClient(server.address, TestIrcServer.generateUniqueNick("mynick"), {
             port: server.port,
             autoConnect: false,
             connectionTimeout: 4000,
index d29e0d62035f6c876782c7211dbb61e01492c0cf..53bb05eddf4575c9d9a090228eac16b387971f07 100644 (file)
@@ -4,3 +4,4 @@ export * from './events';
 export * from './parse_message';
 export * from './state';
 export * from './capabilities';
+export * from './testing/index';
similarity index 67%
rename from spec/util/irc-server.ts
rename to src/testing/index.ts
index c93fef9e2063ea07256abf694d7e99e46a1ececa..faac1c90213bb645378b8383a1d18473a3b043ff 100644 (file)
@@ -5,6 +5,10 @@ import { describe, beforeEach, afterEach } from '@jest/globals';
 const DEFAULT_PORT = parseInt(process.env.IRC_TEST_PORT ?? '6667', 10);
 const DEFAULT_ADDRESS = process.env.IRC_TEST_ADDRESS ?? "127.0.0.1";
 
+/**
+ * Exposes a client instance with helper methods to listen
+ * for events.
+ */
 export class TestClient extends Client {
     public readonly errors: Message[] = [];
 
@@ -29,7 +33,34 @@ export class TestClient extends Client {
     }
 }
 
-export class IrcServer {
+/**
+ * A jest-compatible test rig that can be used to run tests against an IRC server.
+ *
+ * @example
+ * ```ts
+ *  IrcServer.describe('Client', (server) => {
+      describe('joining channels', () => {
+        test('will get a join event from a newly joined user', async () => {
+            const [speaker, listener] = server().clients;
+
+            // Join the room and listen
+            const listenerJoinPromise = listener.waitForEvent('join');
+            await listener.join('#foobar');
+            const [lChannel, lNick] = await listenerJoinPromise;
+            expect(lNick).toBe(listener.nick);
+            expect(lChannel).toBe('#foobar');
+
+            const speakerJoinPromise = listener.waitForEvent('join');
+            await speaker.join('#foobar');
+            const [channel, nick] = await speakerJoinPromise;
+            expect(nick).toBe(speaker.nick);
+            expect(channel).toBe('#foobar');
+        });
+      });
+    });
+ * ```
+ */
+export class TestIrcServer {
 
     /**
      * Test wrapper that automatically provisions an IRC server
@@ -37,11 +68,11 @@ export class IrcServer {
      * @param fn The inner function
      * @returns A jest describe function.
      */
-    static describe(name: string, fn: (server: () => IrcServer) => void, opts?: {clients: string[]}) {
+    static describe(name: string, fn: (server: () => TestIrcServer) => void, opts?: {clients: string[]}) {
         return describe(name, () => {
-            let server: IrcServer;
+            let server: TestIrcServer;
             beforeEach(async () => {
-                server = new IrcServer();
+                server = new TestIrcServer();
                 await server.setUp(opts?.clients);
             });
             afterEach(async () => {
@@ -62,7 +93,7 @@ export class IrcServer {
         const connections: Promise<void>[] = [];
         for (let index = 0; index < clients.length; index++) {
             const client =
-                new TestClient(this.address, IrcServer.generateUniqueNick(clients[index]), {
+                new TestClient(this.address, TestIrcServer.generateUniqueNick(clients[index]), {
                     port: this.port,
                     autoConnect: false,
                     connectionTimeout: 4000,