-
Notifications
You must be signed in to change notification settings - Fork 1
/
aggregate.test.ts
290 lines (264 loc) · 10.4 KB
/
aggregate.test.ts
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/// <reference types="vite/client" />
import { convexTest } from "convex-test";
import { afterEach, beforeEach, describe, expect, test, vi } from "vitest";
import schema from "./schema";
import componentSchema from "../../src/component/schema";
import migrationsSchema from "../node_modules/@convex-dev/migrations/src/component/schema";
import { api, components, internal } from "./_generated/api";
const modules = import.meta.glob("./**/*.ts");
const componentModules = import.meta.glob("../../src/component/**/*.ts");
const migrationsModules = import.meta.glob(
"../node_modules/@convex-dev/migrations/src/component/**/*.ts"
);
describe("leaderboard", () => {
async function setupTest() {
const t = convexTest(schema, modules);
t.registerComponent("aggregateByScore", componentSchema, componentModules);
t.registerComponent(
"aggregateScoreByUser",
componentSchema,
componentModules
);
t.registerComponent("migrations", migrationsSchema, migrationsModules);
// Reduce maxNodeSize so we can test complex trees with fewer items.
await t.mutation(components.aggregateByScore.public.clear, {
maxNodeSize: 4,
});
await t.mutation(components.aggregateScoreByUser.public.clear, {
maxNodeSize: 4,
});
return t;
}
let t: Awaited<ReturnType<typeof setupTest>>;
beforeEach(async () => {
vi.useFakeTimers();
t = await setupTest();
});
afterEach(async () => {
// `clear` schedules cleanup mutations to run async, so we need to wait for them,
// otherwise they'll run after the test finishes and the next one may have started.
await t.finishAllScheduledFunctions(vi.runAllTimers);
vi.useRealTimers();
});
test("score count and sum", async () => {
await t.mutation(api.leaderboard.addScore, { name: "Sujay", score: 10 });
expect(await t.query(api.leaderboard.countScores)).toStrictEqual(1);
expect(await t.query(api.leaderboard.sumNumbers)).toStrictEqual(10);
await t.mutation(api.leaderboard.addScore, { name: "Sujay", score: 20 });
await t.mutation(api.leaderboard.addScore, { name: "Lee", score: 15 });
await t.mutation(api.leaderboard.addScore, { name: "Lee", score: 25 });
await t.mutation(api.leaderboard.addScore, { name: "Lee", score: 30 });
const highScoreId = await t.mutation(api.leaderboard.addScore, {
name: "Sarah",
score: 35,
});
await t.mutation(api.leaderboard.addScore, { name: "Sarah", score: 5 });
expect(await t.query(api.leaderboard.countScores)).toStrictEqual(7);
expect(await t.query(api.leaderboard.sumNumbers)).toStrictEqual(140);
await t.mutation(api.leaderboard.removeScore, { id: highScoreId });
expect(await t.query(api.leaderboard.countScores)).toStrictEqual(6);
expect(await t.query(api.leaderboard.sumNumbers)).toStrictEqual(105);
});
test("score ranks", async () => {
await t.mutation(api.leaderboard.addScore, { name: "Sujay", score: 10 });
await t.mutation(api.leaderboard.addScore, { name: "Sujay", score: 20 });
await t.mutation(api.leaderboard.addScore, { name: "Lee", score: 15 });
await t.mutation(api.leaderboard.addScore, { name: "Lee", score: 25 });
await t.mutation(api.leaderboard.addScore, { name: "Lee", score: 30 });
await t.mutation(api.leaderboard.addScore, { name: "Sarah", score: 35 });
await t.mutation(api.leaderboard.addScore, { name: "Sarah", score: 5 });
expect(
await t.query(api.leaderboard.scoreAtRank, { rank: 0 })
).toMatchObject({ name: "Sarah", score: 35 });
expect(
await t.query(api.leaderboard.scoreAtRank, { rank: 1 })
).toMatchObject({ name: "Lee", score: 30 });
expect(
await t.query(api.leaderboard.scoreAtRank, { rank: 5 })
).toMatchObject({ name: "Sujay", score: 10 });
expect(
await t.query(api.leaderboard.rankOfScore, { score: 35 })
).toStrictEqual(0);
expect(
await t.query(api.leaderboard.rankOfScore, { score: 30 })
).toStrictEqual(1);
expect(
await t.query(api.leaderboard.rankOfScore, { score: 10 })
).toStrictEqual(5);
expect(
await t.query(api.leaderboard.rankOfScore, { score: 33 })
).toStrictEqual(1);
const scoresInOrder = await t.query(api.leaderboard.scoresInOrder);
expect(scoresInOrder).toEqual([
"Sarah: 35",
"Lee: 30",
"Lee: 25",
"Sujay: 20",
"Lee: 15",
"Sujay: 10",
"Sarah: 5",
]);
});
test("backfill", async () => {
const t = await setupTest();
await t.mutation(api.leaderboard.addScore, { name: "Sujay", score: 10 });
await t.mutation(api.leaderboard.addScore, { name: "Sujay", score: 20 });
await t.mutation(api.leaderboard.addScore, { name: "Lee", score: 15 });
await t.mutation(api.leaderboard.addScore, { name: "Lee", score: 25 });
await t.mutation(api.leaderboard.addScore, { name: "Lee", score: 30 });
await t.mutation(internal.leaderboard.clearAggregates, {});
expect(await t.query(api.leaderboard.countScores)).toStrictEqual(0);
await t.mutation(internal.leaderboard.runAggregateBackfill, {});
await t.finishAllScheduledFunctions(vi.runAllTimers);
expect(await t.query(api.leaderboard.countScores)).toStrictEqual(5);
});
test("leaderboard", async () => {
const t = await setupTest();
await t.mutation(api.leaderboard.addScore, { name: "Sujay", score: 10 });
await t.mutation(api.leaderboard.addScore, { name: "Sujay", score: 20 });
await t.mutation(api.leaderboard.addScore, { name: "Sujay", score: 15 });
await t.mutation(api.leaderboard.addScore, { name: "Lee", score: 15 });
await t.mutation(api.leaderboard.addScore, { name: "Lee", score: 25 });
const highScore1 = await t.query(api.leaderboard.userHighScore, {
name: "Sujay",
});
expect(highScore1).toEqual(20);
const highScore2 = await t.query(api.leaderboard.userHighScore, {
name: "Lee",
});
expect(highScore2).toEqual(25);
const averageScore = await t.query(api.leaderboard.userAverageScore, {
name: "Sujay",
});
expect(averageScore).toEqual(15);
});
});
describe("photos", () => {
async function setupTest() {
const t = convexTest(schema, modules);
t.registerComponent("photos", componentSchema, componentModules);
await t.mutation(internal.photos.init);
return t;
}
let t: Awaited<ReturnType<typeof setupTest>>;
beforeEach(async () => {
vi.useFakeTimers();
t = await setupTest();
});
afterEach(async () => {
await t.finishAllScheduledFunctions(vi.runAllTimers);
vi.useRealTimers();
});
test("triggers and pagination", async () => {
const album = "birthday party";
for (let i = 0; i < 30; i++) {
await t.mutation(api.photos.addPhoto, { album, url: `photo${i}` });
}
const page0 = await t.query(api.photos.pageOfPhotos, {
album,
offset: 0,
numItems: 10,
});
expect(page0).toEqual(Array.from({ length: 10 }, (_, i) => `photo${i}`));
const emptyPage = await t.query(api.photos.pageOfPhotos, {
album,
offset: 0,
numItems: 0,
});
expect(emptyPage).toEqual([]);
const lastPage = await t.query(api.photos.pageOfPhotos, {
album,
offset: 28,
numItems: 10,
});
expect(lastPage).toEqual(["photo28", "photo29"]);
});
});
describe("shuffle", () => {
async function setupTest() {
const t = convexTest(schema, modules);
t.registerComponent("music", componentSchema, componentModules);
return t;
}
let t: Awaited<ReturnType<typeof setupTest>>;
beforeEach(async () => {
vi.useFakeTimers();
t = await setupTest();
await t.mutation(components.music.public.clear, { maxNodeSize: 4 });
});
afterEach(async () => {
await t.finishAllScheduledFunctions(vi.runAllTimers);
vi.useRealTimers();
});
test("shuffle", async () => {
await t.mutation(api.shuffle.addMusic, { title: "Song1" });
await t.mutation(api.shuffle.addMusic, { title: "Song2" });
await t.mutation(api.shuffle.addMusic, { title: "Song3" });
const idToDelete = await t.mutation(api.shuffle.addMusic, {
title: "Song4",
});
await t.mutation(api.shuffle.addMusic, { title: "Song5" });
await t.mutation(api.shuffle.addMusic, { title: "Song6" });
await t.mutation(api.shuffle.removeMusic, { id: idToDelete });
const randomSong = await t.query(api.shuffle.getRandomMusicTitle, {});
expect(randomSong).toMatch(/Song[12356]/);
// With same seed, pagination should return unique songs
const shufflePage0 = await t.query(api.shuffle.shufflePaginated, {
offset: 0,
numItems: 3,
seed: "",
});
expect(shufflePage0).toEqual(["Song6", "Song1", "Song3"]);
const shufflePage1 = await t.query(api.shuffle.shufflePaginated, {
offset: 3,
numItems: 3,
seed: "",
});
expect(shufflePage1).toEqual(["Song5", "Song2"]);
// With different seed, we should get a different shuffle
const shufflePage0Seed1 = await t.query(api.shuffle.shufflePaginated, {
offset: 0,
numItems: 3,
seed: "x",
});
expect(shufflePage0Seed1).toEqual(["Song1", "Song6", "Song5"]);
const shufflePage1Seed1 = await t.query(api.shuffle.shufflePaginated, {
offset: 3,
numItems: 3,
seed: "x",
});
expect(shufflePage1Seed1).toEqual(["Song3", "Song2"]);
});
});
describe("stats", () => {
async function setupTest() {
const t = convexTest(schema, modules);
t.registerComponent("stats", componentSchema, componentModules);
await t.mutation(components.stats.public.clear, { maxNodeSize: 4 });
return t;
}
let t: Awaited<ReturnType<typeof setupTest>>;
beforeEach(async () => {
vi.useFakeTimers();
t = await setupTest();
});
afterEach(async () => {
await t.finishAllScheduledFunctions(vi.runAllTimers);
vi.useRealTimers();
});
test("report latency", async () => {
await t.mutation(api.stats.reportLatency, { latency: 10 });
await t.mutation(api.stats.reportLatency, { latency: 20 });
await t.mutation(api.stats.reportLatency, { latency: 15 });
await t.mutation(api.stats.reportLatency, { latency: 25 });
await t.mutation(api.stats.reportLatency, { latency: 30 });
await t.mutation(api.stats.reportLatency, { latency: 35 });
const stats = await t.query(api.stats.getStats);
expect(stats.max).toEqual(35);
expect(stats.min).toEqual(10);
expect(stats.mean).toBeCloseTo(22.5);
expect(stats.median).toEqual(25);
expect(stats.p75).toEqual(30);
expect(stats.p95).toEqual(35);
});
});