BlueGit

timfa questward

Files

questward / server / tests / friends.test.js

friends.test.js

friends.test.js 2.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);
14 const accountB = stateDb.createAccount("admin-2", true);
15 const left = stateDb.createCharacter(accountA.account_id, "Ada", "West", "female", 8, 8);
16 const right = stateDb.createCharacter(accountB.account_id, "Ben", "East", "male", 8, 8);
17 return {
18 stateDb,
19 friends: new FriendService(stateDb),
20 leftId: left.character_id,
21 rightId: right.character_id
22 };
23}
24
25test("a friend request must be accepted before both sides are friends", () =>
26{
27 const { stateDb, friends, leftId, rightId } = setup();
28 try
29 {
30 const pending = friends.request(leftId, rightId);
31 assert.equal(pending.outgoing.length, 1);
32 assert.equal(pending.friends.length, 0);
33 assert.equal(friends.snapshot(rightId).incoming[0].firstName, "Ada");
34
35 friends.accept(rightId, leftId);
36 assert.equal(friends.snapshot(leftId).friends.length, 1);
37 assert.equal(friends.snapshot(rightId).friends[0].lastName, "West");
38 assert.equal(friends.snapshot(leftId).outgoing.length, 0);
39 assert.equal(friends.snapshot(rightId).incoming.length, 0);
40 }
41 finally
42 { stateDb.close(); }
43});
44
45test("removing a friend removes them for both characters", () =>
46{
47 const { stateDb, friends, leftId, rightId } = setup();
48 try
49 {
50 friends.request(leftId, rightId);
51 friends.accept(rightId, leftId);
52 friends.remove(leftId, rightId);
53 assert.equal(friends.snapshot(leftId).friends.length, 0);
54 assert.equal(friends.snapshot(rightId).friends.length, 0);
55 }
56 finally
57 { stateDb.close(); }
58});
59
60test("a reverse request accepts the pending invite", () =>
61{
62 const { stateDb, friends, leftId, rightId } = setup();
63 try
64 {
65 friends.request(leftId, rightId);
66 friends.request(rightId, leftId);
67 assert.equal(friends.snapshot(leftId).friends.length, 1);
68 assert.equal(friends.snapshot(rightId).incoming.length, 0);
69 }
70 finally
71 { stateDb.close(); }
72});
73
74test("friends list is the pigeon address book of first and last names", () =>
75{
76 const { stateDb, friends, leftId, rightId } = setup();
77 try
78 {
79 friends.request(leftId, rightId);
80 friends.accept(rightId, leftId);
81 const found = friends.findByFullName("Ben", "East");
82 assert.equal(found.characterId, rightId);
83 assert.deepEqual(friends.listFriends(leftId)[0], found);
84 }
85 finally
86 { stateDb.close(); }
87});