BlueGit

timfa questward

Files

questward / server / tests / friends.test.js

friends.test.js

jewelerty, skulls
timfa Tim Falken · pushed by timfa · 2 months ago
2b6e44d0 History
friends.test.js 3.7 KiB Download Raw
1import test from "node:test";
2import assert from "node:assert/strict";
3import { mkdtempSync } from "node:fs";
4import { tmpdir } from "node:os";
5import { join } from "node:path";
6import { StateDatabase } from "../src/persistence/stateDb.js";
7import { FriendService } from "../src/friends/service.js";
8
9function setup ()
10{
11 const dir = mkdtempSync(join(tmpdir(), "mmo-friends-"));
12 const stateDb = new StateDatabase(join(dir, "state.sqlite"));
13 const accountA = stateDb.createAccount("admin-1", true, null, "ada_acc");
14 const accountB = stateDb.createAccount("admin-2", true, null, "ben_acc");
15 const left = stateDb.createCharacter(accountA.account_id, "Ada", "West", "female", 8, 8);
16 const leftAlt = stateDb.createCharacter(accountA.account_id, "Ada", "Alt", "female", 8, 8);
17 const right = stateDb.createCharacter(accountB.account_id, "Ben", "East", "male", 8, 8);
18 const rightAlt = stateDb.createCharacter(accountB.account_id, "Ben", "Twin", "male", 8, 8);
19 return {
20 stateDb,
21 friends: new FriendService(stateDb),
22 leftId: left.character_id,
23 leftAltId: leftAlt.character_id,
24 rightId: right.character_id,
25 rightAltId: rightAlt.character_id,
26 accountA: accountA.account_id,
27 accountB: accountB.account_id
28 };
29}
30
31test("a friend request must be accepted before both sides are friends", () =>
32{
33 const { stateDb, friends, leftId, rightId } = setup();
34 try
35 {
36 const pending = friends.request(leftId, rightId);
37 assert.equal(pending.outgoing.length, 1);
38 assert.equal(pending.friends.length, 0);
39 assert.equal(friends.snapshot(rightId).incoming[0].username, "ada_acc");
40
41 friends.accept(rightId, leftId);
42 assert.equal(friends.snapshot(leftId).friends.length, 1);
43 assert.equal(friends.snapshot(rightId).friends[0].username, "ada_acc");
44 assert.equal(friends.snapshot(leftId).outgoing.length, 0);
45 assert.equal(friends.snapshot(rightId).incoming.length, 0);
46 }
47 finally
48 { stateDb.close(); }
49});
50
51test("account friendship covers all characters on both accounts", () =>
52{
53 const { stateDb, friends, leftId, leftAltId, rightId, rightAltId } = setup();
54 try
55 {
56 friends.request(leftId, rightId);
57 friends.accept(rightId, leftId);
58 assert.equal(friends.areFriends(leftId, rightId), true);
59 assert.equal(friends.areFriends(leftAltId, rightAltId), true);
60 assert.equal(friends.areFriends(leftId, leftAltId), true);
61 const flat = friends.listFriends(leftId);
62 assert.ok(flat.some((entry) => entry.characterId === rightId));
63 assert.ok(flat.some((entry) => entry.characterId === rightAltId));
64 }
65 finally
66 { stateDb.close(); }
67});
68
69test("removing a friend removes them for both accounts", () =>
70{
71 const { stateDb, friends, leftId, rightId } = setup();
72 try
73 {
74 friends.request(leftId, rightId);
75 friends.accept(rightId, leftId);
76 friends.remove(leftId, rightId);
77 assert.equal(friends.snapshot(leftId).friends.length, 0);
78 assert.equal(friends.snapshot(rightId).friends.length, 0);
79 }
80 finally
81 { stateDb.close(); }
82});
83
84test("a reverse request accepts the pending invite", () =>
85{
86 const { stateDb, friends, leftId, rightId } = setup();
87 try
88 {
89 friends.request(leftId, rightId);
90 friends.request(rightId, leftId);
91 assert.equal(friends.snapshot(leftId).friends.length, 1);
92 assert.equal(friends.snapshot(rightId).incoming.length, 0);
93 }
94 finally
95 { stateDb.close(); }
96});
97
98test("friends list is the pigeon address book of first and last names", () =>
99{
100 const { stateDb, friends, leftId, rightId } = setup();
101 try
102 {
103 friends.request(leftId, rightId);
104 friends.accept(rightId, leftId);
105 const found = friends.findByFullName("Ben", "East");
106 assert.equal(found.characterId, rightId);
107 assert.ok(friends.listFriends(leftId).some((entry) => entry.characterId === found.characterId));
108 }
109 finally
110 { stateDb.close(); }
111});