forked from Bitcoindefi/OpenAO
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcharacter-save.integration.test.ts
More file actions
190 lines (163 loc) · 5.49 KB
/
Copy pathcharacter-save.integration.test.ts
File metadata and controls
190 lines (163 loc) · 5.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import assert from "node:assert/strict";
import { beforeAll, test } from "vitest";
import {
createCharacterFixture,
ensureApiReady,
getCharacterBankAndSpells,
getCharacterHomeState,
getCharacterState,
patchCharacter,
putCharacterBank,
putCharacterItems,
putCharacterSpells,
putCharacterStorage,
requestJson,
} from "./helpers/api";
beforeAll(async () => {
await ensureApiReady();
});
test("character_save patches top-level fields and nested items", async () => {
const owner = await createCharacterFixture({
namePrefix: "Save",
gold: 100,
items: [{ idPos: 1, idItem: 1, cant: 1, equipped: false }],
});
await patchCharacter(owner.id, {
gold: 777,
level: 12,
criminal: true,
items: [{ idPos: 2, idItem: 474, cant: 3, equipped: true }],
ignoredField: "should-pass-through-without-effect",
});
const state = await getCharacterState(
owner.accountId,
owner.id,
owner.email,
);
assert.equal(state.gold, 777);
assert.deepEqual(state.items, [
{ idPos: 2, idItem: 474, cant: 3, equipped: true },
]);
const homeState = await getCharacterHomeState(
owner.accountId,
owner.id,
owner.email,
);
assert.equal(homeState.homeMap, 1);
assert.equal(homeState.homeX, 54);
assert.equal(homeState.homeY, 60);
});
test("character_save patches custom home coordinates", async () => {
const owner = await createCharacterFixture({ namePrefix: "Home" });
await patchCharacter(owner.id, {
homeMap: 62,
homeX: 74,
homeY: 44,
});
const state = await getCharacterHomeState(
owner.accountId,
owner.id,
owner.email,
);
assert.equal(state.homeMap, 62);
assert.equal(state.homeX, 74);
assert.equal(state.homeY, 44);
});
test("character_save items endpoint replaces inventory", async () => {
const owner = await createCharacterFixture({
namePrefix: "Item",
items: [{ idPos: 1, idItem: 1, cant: 2, equipped: false }],
});
const response = await putCharacterItems(owner.id, [
{ idPos: 3, idItem: 2, cant: 5, equipped: false },
]);
assert.equal(response.status, 200);
const state = await getCharacterState(
owner.accountId,
owner.id,
owner.email,
);
assert.deepEqual(state.items, [
{ idPos: 3, idItem: 2, cant: 5, equipped: false },
]);
});
test("character_save items endpoint rejects invalid inventory slots", async () => {
const owner = await createCharacterFixture({ namePrefix: "BadInv" });
const response = await putCharacterItems(owner.id, [
{ idPos: 0, idItem: 474, cant: 1, equipped: false },
]);
assert.equal(response.status, 400);
});
test("character_save bank and spells endpoints persist their collections", async () => {
const owner = await createCharacterFixture({ namePrefix: "Bank" });
const bankResponse = await putCharacterBank(owner.id, [
{ idPos: 1, idItem: 474, cant: 10 },
{ idPos: 5, idItem: 2, cant: 1 },
]);
const spellsResponse = await putCharacterSpells(owner.id, [
{ idPos: 0, idSpell: 7 },
{ idPos: 2, idSpell: 9 },
]);
assert.equal(bankResponse.status, 200);
assert.equal(spellsResponse.status, 200);
const state = await getCharacterBankAndSpells(owner.id);
assert.deepEqual(state.bankItems, [
{ idPos: 1, idItem: 474, cant: 10 },
{ idPos: 5, idItem: 2, cant: 1 },
]);
assert.deepEqual(state.spells, [
{ idPos: 0, idSpell: 7 },
{ idPos: 2, idSpell: 9 },
]);
});
test("character_save bank endpoint rejects invalid bank slots", async () => {
const owner = await createCharacterFixture({ namePrefix: "BadBank" });
const response = await putCharacterBank(owner.id, [
{ idPos: 101, idItem: 474, cant: 1 },
]);
assert.equal(response.status, 400);
});
test("character_save storage updates items and bank atomically", async () => {
const owner = await createCharacterFixture({
namePrefix: "Stor",
items: [{ idPos: 1, idItem: 1, cant: 2, equipped: false }],
});
await putCharacterBank(owner.id, [{ idPos: 1, idItem: 474, cant: 2 }]);
const beforeCharacter = await getCharacterState(
owner.accountId,
owner.id,
owner.email,
);
const beforeCollections = await getCharacterBankAndSpells(owner.id);
const invalid = await putCharacterStorage(owner.id, {
items: [{ idPos: 9, idItem: 2, cant: 4, equipped: false }],
bankItems: [{ idPos: 2, idItem: -1, cant: 1 }],
});
assert.equal(invalid.status, 400);
assert.deepEqual(
await getCharacterState(owner.accountId, owner.id, owner.email),
beforeCharacter,
);
assert.deepEqual(
await getCharacterBankAndSpells(owner.id),
beforeCollections,
);
});
test("character_save returns 404 for missing characters", async () => {
const response = await requestJson<{ error?: string }>(
"/character_save/not-a-uuid",
{
method: "PUT",
headers: {
"Content-Type": "application/json",
Authorization:
process.env.API_TEST_AUTH ||
process.env.TOKEN_AUTH ||
"changeme",
},
body: JSON.stringify({ gold: 1 }),
},
);
assert.equal(response.status, 404);
assert.equal(response.data.error, "Character not found");
});