306 lines
9.9 KiB
TypeScript
306 lines
9.9 KiB
TypeScript
import {
|
|
GROUP_MAX_MEMBERS,
|
|
type Conversation,
|
|
type MessagePage,
|
|
} from '@repo/contracts';
|
|
import {
|
|
befriend,
|
|
createTestApp,
|
|
listen,
|
|
nextEvent,
|
|
TestAccounts,
|
|
TestSockets,
|
|
type TestApp,
|
|
type TestUser,
|
|
} from './helpers';
|
|
|
|
describe('Group chats (e2e)', () => {
|
|
let app: TestApp;
|
|
let accounts: TestAccounts;
|
|
let sockets: TestSockets;
|
|
// Alice is friends with Bob and Carol; Bob is also friends with Dave.
|
|
// Erin is nobody's friend.
|
|
let alice: TestUser;
|
|
let bob: TestUser;
|
|
let carol: TestUser;
|
|
let dave: TestUser;
|
|
let erin: TestUser;
|
|
let group: Conversation;
|
|
|
|
const idsOf = (conversation: Conversation) =>
|
|
conversation.members.map((member) => member.id).sort();
|
|
|
|
async function listOf(account: TestUser): Promise<Conversation[]> {
|
|
return (await account.agent.get('/conversations').expect(200)).body;
|
|
}
|
|
|
|
beforeAll(async () => {
|
|
app = await createTestApp();
|
|
sockets = new TestSockets(await listen(app));
|
|
accounts = new TestAccounts(app, '@groups-e2e.test');
|
|
await accounts.cleanup();
|
|
[alice, bob, carol, dave, erin] = await Promise.all(
|
|
['Alice', 'Bob', 'Carol', 'Dave', 'Erin'].map((name) =>
|
|
accounts.signUp(name),
|
|
),
|
|
);
|
|
await befriend(alice, bob);
|
|
await befriend(alice, carol);
|
|
await befriend(bob, dave);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
sockets.closeAll();
|
|
await accounts.cleanup();
|
|
await app.close();
|
|
});
|
|
|
|
it('only starts a group with at least two friends', async () => {
|
|
const create = (memberIds: string[]) =>
|
|
alice.agent.post('/conversations/group').send({ memberIds });
|
|
|
|
await create([bob.id]).expect(400);
|
|
await create([bob.id, bob.id]).expect(400);
|
|
await create([bob.id, alice.id]).expect(400);
|
|
await create([bob.id, erin.id]).expect(403);
|
|
await create([bob.id, 'no-such-user']).expect(403);
|
|
expect(await listOf(alice)).toEqual([]);
|
|
});
|
|
|
|
it('creates a group owned by its creator and tells everyone in it', async () => {
|
|
const bobSocket = await sockets.connectAs(bob);
|
|
const carolSocket = await sockets.connectAs(carol);
|
|
const forBob = nextEvent(bobSocket, 'conversations:changed');
|
|
const forCarol = nextEvent(carolSocket, 'conversations:changed');
|
|
|
|
const response = await alice.agent
|
|
.post('/conversations/group')
|
|
.send({ name: ' Coven ', memberIds: [bob.id, carol.id] })
|
|
.expect(201);
|
|
group = response.body;
|
|
|
|
expect(group).toMatchObject({
|
|
type: 'group',
|
|
name: 'Coven',
|
|
ownerId: alice.id,
|
|
lastMessage: null,
|
|
});
|
|
expect(idsOf(group)).toEqual([alice.id, bob.id, carol.id].sort());
|
|
expect(group.members[0].id).toBe(alice.id);
|
|
await Promise.all([forBob, forCarol]);
|
|
for (const member of [alice, bob, carol]) {
|
|
expect((await listOf(member)).map((item) => item.id)).toEqual([group.id]);
|
|
}
|
|
});
|
|
|
|
it('lets members who are not friends with each other talk', async () => {
|
|
const aliceSocket = await sockets.connectAs(alice);
|
|
const bobSocket = await sockets.connectAs(bob);
|
|
const forAlice = nextEvent(aliceSocket, 'message:created');
|
|
const forBob = nextEvent(bobSocket, 'message:created');
|
|
|
|
// Carol and Bob never befriended each other.
|
|
const response = await carol.agent
|
|
.post(`/conversations/${group.id}/messages`)
|
|
.send({ content: 'hello, coven' })
|
|
.expect(201);
|
|
|
|
expect(response.body.author).toMatchObject({ id: carol.id, name: 'Carol' });
|
|
expect(await forAlice).toEqual(response.body);
|
|
expect(await forBob).toEqual(response.body);
|
|
});
|
|
|
|
it('lets any member rename it, and clear the name', async () => {
|
|
const aliceSocket = await sockets.connectAs(alice);
|
|
const forAlice = nextEvent(aliceSocket, 'conversations:changed');
|
|
|
|
const renamed = await bob.agent
|
|
.patch(`/conversations/${group.id}`)
|
|
.send({ name: 'Night shift' })
|
|
.expect(200);
|
|
expect(renamed.body.name).toBe('Night shift');
|
|
await forAlice;
|
|
|
|
const cleared = await carol.agent
|
|
.patch(`/conversations/${group.id}`)
|
|
.send({ name: ' ' })
|
|
.expect(200);
|
|
expect(cleared.body.name).toBeNull();
|
|
|
|
await bob.agent
|
|
.patch(`/conversations/${group.id}`)
|
|
.send({ name: 'x'.repeat(101) })
|
|
.expect(400);
|
|
});
|
|
|
|
it('hides the group from people who are not in it', async () => {
|
|
await dave.agent.get(`/conversations/${group.id}`).expect(404);
|
|
await dave.agent
|
|
.patch(`/conversations/${group.id}`)
|
|
.send({ name: 'Mine now' })
|
|
.expect(404);
|
|
await dave.agent
|
|
.post(`/conversations/${group.id}/members`)
|
|
.send({ userIds: [erin.id] })
|
|
.expect(404);
|
|
await dave.agent
|
|
.delete(`/conversations/${group.id}/members/${bob.id}`)
|
|
.expect(404);
|
|
});
|
|
|
|
it('lets a member add their own friends, who then see the history', async () => {
|
|
const path = `/conversations/${group.id}/members`;
|
|
// Dave is Bob's friend, not Alice's.
|
|
await alice.agent
|
|
.post(path)
|
|
.send({ userIds: [dave.id] })
|
|
.expect(403);
|
|
await alice.agent.post(path).send({ userIds: [] }).expect(400);
|
|
|
|
const daveSocket = await sockets.connectAs(dave);
|
|
const forDave = nextEvent(daveSocket, 'conversations:changed');
|
|
const added = await bob.agent
|
|
.post(path)
|
|
.send({ userIds: [dave.id] })
|
|
.expect(200);
|
|
expect(idsOf(added.body)).toEqual(
|
|
[alice.id, bob.id, carol.id, dave.id].sort(),
|
|
);
|
|
// The owner comes first; whoever joins later goes last.
|
|
expect(added.body.members[0].id).toBe(alice.id);
|
|
expect(added.body.members.at(-1).id).toBe(dave.id);
|
|
await forDave;
|
|
|
|
const history = await dave.agent
|
|
.get(`/conversations/${group.id}/messages`)
|
|
.expect(200);
|
|
expect(
|
|
(history.body as MessagePage).items.map((item) => item.content),
|
|
).toEqual(['hello, coven']);
|
|
|
|
// Adding someone who is already in changes nothing.
|
|
const again = await bob.agent
|
|
.post(path)
|
|
.send({ userIds: [dave.id] })
|
|
.expect(200);
|
|
expect(again.body.members).toHaveLength(4);
|
|
});
|
|
|
|
it('lets only the owner remove someone', async () => {
|
|
const path = `/conversations/${group.id}/members`;
|
|
await bob.agent.delete(`${path}/${carol.id}`).expect(403);
|
|
await alice.agent.delete(`${path}/${erin.id}`).expect(404);
|
|
|
|
const carolSocket = await sockets.connectAs(carol);
|
|
const forCarol = nextEvent(carolSocket, 'conversations:changed');
|
|
await alice.agent.delete(`${path}/${carol.id}`).expect(204);
|
|
await forCarol;
|
|
|
|
expect(await listOf(carol)).toEqual([]);
|
|
await carol.agent.get(`/conversations/${group.id}/messages`).expect(404);
|
|
await carol.agent
|
|
.post(`/conversations/${group.id}/messages`)
|
|
.send({ content: 'still here?' })
|
|
.expect(404);
|
|
});
|
|
|
|
it("keeps a removed member's messages, with their name", async () => {
|
|
const history = await bob.agent
|
|
.get(`/conversations/${group.id}/messages`)
|
|
.expect(200);
|
|
expect((history.body as MessagePage).items[0]).toMatchObject({
|
|
content: 'hello, coven',
|
|
author: { id: carol.id, name: 'Carol' },
|
|
});
|
|
});
|
|
|
|
it('stops messages reaching someone who was removed', async () => {
|
|
const carolSocket = await sockets.connectAs(carol);
|
|
const daveSocket = await sockets.connectAs(dave);
|
|
let carolGotIt = false;
|
|
carolSocket.on('message:created', () => {
|
|
carolGotIt = true;
|
|
});
|
|
|
|
const forDave = nextEvent(daveSocket, 'message:created');
|
|
await bob.agent
|
|
.post(`/conversations/${group.id}/messages`)
|
|
.send({ content: 'members only' })
|
|
.expect(201);
|
|
await forDave;
|
|
expect(carolGotIt).toBe(false);
|
|
});
|
|
|
|
it('passes ownership on when the owner leaves', async () => {
|
|
await alice.agent
|
|
.delete(`/conversations/${group.id}/members/${alice.id}`)
|
|
.expect(204);
|
|
expect(await listOf(alice)).toEqual([]);
|
|
|
|
// Bob has been in it longer than Dave.
|
|
const [remaining] = await listOf(bob);
|
|
expect(remaining.ownerId).toBe(bob.id);
|
|
expect(idsOf(remaining)).toEqual([bob.id, dave.id].sort());
|
|
|
|
// And the new owner has the owner's powers.
|
|
await bob.agent
|
|
.delete(`/conversations/${group.id}/members/${dave.id}`)
|
|
.expect(204);
|
|
});
|
|
|
|
it('deletes the group when the last person leaves', async () => {
|
|
await bob.agent
|
|
.delete(`/conversations/${group.id}/members/${bob.id}`)
|
|
.expect(204);
|
|
for (const account of [alice, bob, carol, dave]) {
|
|
expect(await listOf(account)).toEqual([]);
|
|
await account.agent.get(`/conversations/${group.id}`).expect(404);
|
|
}
|
|
});
|
|
|
|
it('keeps group actions away from direct conversations', async () => {
|
|
const direct = await alice.agent
|
|
.post('/conversations/direct')
|
|
.send({ userId: bob.id })
|
|
.expect(200);
|
|
const path = `/conversations/${direct.body.id}`;
|
|
|
|
expect(direct.body).toMatchObject({ name: null, ownerId: null });
|
|
await alice.agent.patch(path).send({ name: 'Us' }).expect(400);
|
|
await alice.agent
|
|
.post(`${path}/members`)
|
|
.send({ userIds: [carol.id] })
|
|
.expect(400);
|
|
await alice.agent.delete(`${path}/members/${alice.id}`).expect(400);
|
|
});
|
|
|
|
it(`holds a group to ${GROUP_MAX_MEMBERS} people`, async () => {
|
|
const friends = await Promise.all(
|
|
Array.from({ length: GROUP_MAX_MEMBERS }, (_, index) =>
|
|
accounts.signUp(`Friend${index}`),
|
|
),
|
|
);
|
|
for (const friend of friends) await befriend(alice, friend);
|
|
const [extra, ...others] = friends;
|
|
|
|
await alice.agent
|
|
.post('/conversations/group')
|
|
.send({ memberIds: friends.map((friend) => friend.id) })
|
|
.expect(400);
|
|
const full = await alice.agent
|
|
.post('/conversations/group')
|
|
.send({ memberIds: others.map((friend) => friend.id) })
|
|
.expect(201);
|
|
expect(full.body.members).toHaveLength(GROUP_MAX_MEMBERS);
|
|
|
|
await alice.agent
|
|
.post(`/conversations/${full.body.id}/members`)
|
|
.send({ userIds: [extra.id] })
|
|
.expect(409);
|
|
const [unchanged] = (await listOf(alice)).filter(
|
|
(item) => item.id === full.body.id,
|
|
);
|
|
expect(unchanged.members).toHaveLength(GROUP_MAX_MEMBERS);
|
|
});
|
|
});
|