Skip to content

Commit

Permalink
fix: add Client.isConnected() and Client.assertConnected() methods; r…
Browse files Browse the repository at this point in the history
…emove commands module (#734)
  • Loading branch information
garrappachc authored Jan 2, 2025
1 parent 1a500f6 commit 1215232
Show file tree
Hide file tree
Showing 28 changed files with 357 additions and 722 deletions.
5 changes: 3 additions & 2 deletions e2e/creates-and-removes-channels.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ describe('Creates and removes channels (e2e)', () => {
channelCreatedEventEmitted = true;
});

expect(client.user?.channel).toBeTruthy();
const channel = client.user!.channel;
client.assertConnected();
expect(client.user.channel).toBeTruthy();
const channel = client.user.channel;
expect(channel).toBeTruthy();
const sub1 = await channel.createSubChannel('sub1');
expect(sub1.parent).toEqual(channel.id);
Expand Down
4 changes: 2 additions & 2 deletions e2e/handles-server-disconnects.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ describe('Handles server disconnects (e2e)', () => {
let wasDisconnected = false;
client1.on('disconnect', () => (wasDisconnected = true));

await waitABit(5000);
await waitABit(1000);

await client2.connect(); // will cause client1 to get disconnected
await waitABit(5000);
await waitABit(1000);
expect(wasDisconnected).toBe(true);
});
});
3 changes: 2 additions & 1 deletion e2e/logs-in-registered.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ describe('Logs in with a certificate (e2e)', () => {
});

it('should grab a registered username', async () => {
expect(client.user?.name).toEqual('registered-tester');
client.assertConnected();
expect(client.user.name).toEqual('registered-tester');
});

it('should reject connections gracefully', async () => {
Expand Down
5 changes: 3 additions & 2 deletions e2e/manages-as-super-user.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,9 @@ describe('Manages server as the super-user (e2e)', () => {
channelCreatedEventEmitted = true;
});

expect(client.user?.channel).toBeTruthy();
const channel = client.user!.channel;
client.assertConnected();
expect(client.user.channel).toBeTruthy();
const channel = client.user.channel;
expect(channel).toBeTruthy();
const sub1 = await channel.createSubChannel('sub10');
expect(sub1.parent).toEqual(channel.id);
Expand Down
Binary file modified e2e/mumble-data/murmur.sqlite
Binary file not shown.
72 changes: 38 additions & 34 deletions e2e/registers-user.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Client } from '../src';
import { waitABit } from './utils/wait-a-bit';
import { userRenameRegistered, userUnregister } from '../src/commands';
import { createCertificate as createCertificateCb } from 'pem';

interface MumbleCert {
Expand Down Expand Up @@ -60,83 +59,88 @@ describe('registers a user', () => {
});

beforeEach(async () => {
if (!client1.user!.isRegistered) {
await client1.user!.register();
expect(client1.user!.isRegistered).toBe(true);
client1.assertConnected();
if (!client1.user.isRegistered) {
await client1.user.register();
expect(client1.user.isRegistered).toBe(true);
}
});

afterEach(async () => {
if (client2.user!.isRegistered) {
await client2.user!.deregister();
expect(client2.user!.isRegistered).toBe(false);
client2.assertConnected();
if (client2.user.isRegistered) {
await client2.user.deregister();
expect(client2.user.isRegistered).toBe(false);
}
});

it('should register self and deregister', async () => {
expect(client2.user!.userId).toBeUndefined();
expect(client2.user!.isRegistered).toBe(false);
client2.assertConnected();
expect(client2.user.userId).toBeUndefined();
expect(client2.user.isRegistered).toBe(false);

await client2.user!.register();
expect(client2.user!.userId).toBeTruthy();
expect(client2.user!.isRegistered).toBe(true);
await client2.user.register();
expect(client2.user.userId).toBeTruthy();
expect(client2.user.isRegistered).toBe(true);

const registeredUsers = await client2.fetchRegisteredUsers();
const user = registeredUsers.find(user => user.name === 'client2');
expect(user).toBeTruthy();
});

it('should rename', async () => {
await client1.user!.rename('client1_new_name');
expect(client1.user!.name).toBe('client1_new_name');
client1.assertConnected();
await client1.user.rename('client1_new_name');
expect(client1.user.name).toBe('client1_new_name');

const registeredUsers = await client1.fetchRegisteredUsers();
const user = registeredUsers.find(user => user.name === 'client1_new_name');
expect(user).toBeTruthy();

await client1.user!.rename('client1');
await client1.user.rename('client1');
});

it('should rename an offline user', async () => {
expect(client2.user!.userId).toBeUndefined();
expect(client2.user!.isRegistered).toBe(false);
await client2.user!.register();
client2.assertConnected();
expect(client2.user.userId).toBeUndefined();
expect(client2.user.isRegistered).toBe(false);
await client2.user.register();
client2.disconnect();
await waitABit(100);

// rename client2 while they are offline
client1.assertConnected();
const registeredUsers = await client1.fetchRegisteredUsers();
const user = registeredUsers.find(user => user.name === 'client2');
expect(user).toBeTruthy();
await userRenameRegistered(
client1.socket!,
user!.userId,
'client2_new_name',
);
await client1.renameRegisteredUser(user!.userId, 'client2_new_name');

await client2.connect();
expect(client2.user!.name).toEqual('client2_new_name');
expect(client2.user!.userId).toBeTruthy();
expect(client2.user!.isRegistered).toBe(true);

await client2.user!.rename('client2');
client2.assertConnected();
expect(client2.user.name).toEqual('client2_new_name');
expect(client2.user.userId).toBeTruthy();
expect(client2.user.isRegistered).toBe(true);
await client2.user.rename('client2');
});

it('should unregister an offline user', async () => {
expect(client2.user!.userId).toBeUndefined();
expect(client2.user!.isRegistered).toBe(false);
await client2.user!.register();
client2.assertConnected();
expect(client2.user.userId).toBeUndefined();
expect(client2.user.isRegistered).toBe(false);
await client2.user.register();
client2.disconnect();
await waitABit(100);

// deregister client2 while they are offline
client1.assertConnected();
const registeredUsers = await client1.fetchRegisteredUsers();
const user = registeredUsers.find(user => user.name === 'client2');
expect(user).toBeTruthy();
await userUnregister(client1.socket!, user!.userId);
await client1.deregisterUser(user!.userId);

await client2.connect();
expect(client2.user!.userId).toBeFalsy();
expect(client2.user!.isRegistered).toBe(false);
client2.assertConnected();
expect(client2.user.userId).toBeFalsy();
expect(client2.user.isRegistered).toBe(false);
});
});
5 changes: 3 additions & 2 deletions e2e/rejects-creating-channel.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ describe('Rejects creating channel when there are insufficient permissions (e2e)
});

it('should throw an error when attempting to create a new channel', async () => {
const channel = client.user?.channel;
client.assertConnected();
const channel = client.user.channel;
await expect(channel?.createSubChannel('test')).rejects.toThrow();

const one = client.channels.byName('one');
expect(one).toBeTruthy();
await client.user?.moveToChannel(one!.id);
await client.user.moveToChannel(one!.id);
await expect(channel?.createSubChannel('test')).rejects.toThrow();
});
});
4 changes: 3 additions & 1 deletion e2e/rejects-joining-channel.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ describe('Rejects moving to a channel when there are insufficient permissions (e
});

it('should throw an error when attempting to join a channel', async () => {
client.assertConnected();

// channel 'three' has all permissions denied for all users
const three = client.channels.byName('three');
expect(three).toBeTruthy();
await expect(client.user?.moveToChannel(three!.id)).rejects.toThrow();
await expect(client.user.moveToChannel(three!.id)).rejects.toThrow();
});
});
6 changes: 3 additions & 3 deletions e2e/self-deafens.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ describe('Sets self-deaf (e2e)', () => {
});

it('should set self deaf', async () => {
expect(client.user).toBeTruthy();
await client.user!.setSelfDeaf(true);
expect(client.user!.selfDeaf).toBe(true);
client.assertConnected();
await client.user.setSelfDeaf(true);
expect(client.user.selfDeaf).toBe(true);
});
});
6 changes: 3 additions & 3 deletions e2e/self-mutes.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ describe('Sets self-mute (e2e)', () => {
});

it('should set self mute', async () => {
expect(client.user).toBeTruthy();
await client.user!.setSelfMute(true);
expect(client.user!.selfMute).toBe(true);
client.assertConnected();
await client.user.setSelfMute(true);
expect(client.user.selfMute).toBe(true);
});
});
48 changes: 0 additions & 48 deletions src/channel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,10 @@ import { ChannelState, PermissionQuery } from '@tf2pickup-org/mumble-protocol';
import { Channel } from './channel';
import { ChannelManager } from './channel-manager';
import { Client } from './client';
import { createChannel, removeChannel } from './commands';
import { MumbleSocket } from './mumble-socket';
import { Permissions } from './permissions';

vi.mock('./client');
vi.mock('./commands', () => ({
fetchChannelPermissions: vi
.fn()
.mockResolvedValue(PermissionQuery.create({ permissions: 0x1 | 0x40 })),
createChannel: vi.fn().mockResolvedValue(8),
removeChannel: vi.fn().mockResolvedValue(7),
}));

describe('Channel', () => {
let client: Client;
Expand Down Expand Up @@ -87,44 +79,4 @@ describe('Channel', () => {
});
});
});

describe('createSubChannel()', () => {
let channel: Channel;

beforeEach(() => {
channel = new Channel(
client,
ChannelState.create({ channelId: 7 }) as ChannelState & {
channelId: number;
},
);
});

it('should attempt to create channel', async () => {
await channel.createSubChannel('SUBCHANNEL_NAME');
expect(createChannel).toHaveBeenCalledWith(
client.socket,
7,
'SUBCHANNEL_NAME',
);
});
});

describe('remove()', () => {
let channel: Channel;

beforeEach(() => {
channel = new Channel(
client,
ChannelState.create({ channelId: 7 }) as ChannelState & {
channelId: number;
},
);
});

it('should attempt to remove the channel', async () => {
await channel.remove();
expect(removeChannel).toHaveBeenCalledWith(client.socket, 7);
});
});
});
Loading

0 comments on commit 1215232

Please sign in to comment.