forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotifications-enabled-share.test.ts
More file actions
130 lines (115 loc) · 4.13 KB
/
Copy pathnotifications-enabled-share.test.ts
File metadata and controls
130 lines (115 loc) · 4.13 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
import { describe, expect, it, vi, beforeEach } from "vitest";
/**
* The enabled/disabled gauge used to compute `enabled = total - disabled`,
* which folded every user document MISSING `notifications_enabled` into the
* enabled bucket and inflated the published share. The unknown population must
* be its own number.
*/
let db: any;
vi.mock("@/lib/firebase/admin", () => ({ getDb: () => db }));
vi.mock("@/lib/auth", () => ({ verifyAdmin: vi.fn(async () => ({ uid: "t" })) }));
vi.mock("@/lib/payload-cache", () => ({
getPayload: vi.fn(async () => null),
setPayload: vi.fn(async () => undefined),
withFreshness: (data: object, freshAt: number) => ({ ...data, freshAt }),
}));
vi.mock("@/lib/posthog", () => ({
cachedPosthogFetch: vi.fn(async () => ({ ok: true, json: async () => ({ results: [] }) })),
}));
import { computeNotifications } from "@/app/api/omi/stats/notifications/route";
type Opts = {
enabled: number;
disabled: number;
total: number;
/** Collection-group index missing -> the per-user fallback path runs. */
collectionGroupFails?: boolean;
mentorUserCount?: number;
};
function fakeDb(opts: Opts) {
const emptySnap = { docs: [] as unknown[] };
const userDoc = () => ({
collection: () => ({ where: () => ({ get: async () => emptySnap }) }),
});
const usersRef: any = {
// `.count().get()` with no filter = total users.
count: () => ({ get: async () => ({ data: () => ({ count: opts.total }) }) }),
doc: () => userDoc(),
where: (field: string, _op: string, value: unknown) => {
if (field === "notifications_enabled") {
const count = value === true ? opts.enabled : opts.disabled;
return { count: () => ({ get: async () => ({ data: () => ({ count }) }) }) };
}
// mentor_notification_frequency > 0 -> the fallback user list
const docs = Array.from({ length: opts.mentorUserCount ?? 0 }, (_, i) => ({
id: `u${i}`,
}));
return {
select: () => ({
limit: (n: number) => ({ get: async () => ({ docs: docs.slice(0, n) }) }),
}),
};
},
};
return {
collection: () => usersRef,
collectionGroup: () => ({
where: function () {
return this;
},
get: async () => {
if (opts.collectionGroupFails) throw new Error("index missing");
return emptySnap;
},
}),
};
}
describe("notifications enabled/disabled/unset", () => {
beforeEach(() => {
delete process.env.POSTHOG_PERSONAL_API_KEY;
delete process.env.POSTHOG_PROJECT_ID;
});
it("counts users with no notifications_enabled field as unset, not enabled", async () => {
db = fakeDb({ enabled: 300, disabled: 100, total: 1000 });
const payload = await computeNotifications(2);
expect(payload.enabledDisabled).toMatchObject({
enabled: 300,
disabled: 100,
unset: 600,
total: 1000,
});
// The gauge divides enabled/total; the old math published 90%.
expect((payload.enabledDisabled.enabled / payload.enabledDisabled.total) * 100).toBe(30);
});
it("never reports a negative unset population", async () => {
db = fakeDb({ enabled: 700, disabled: 500, total: 1000 });
const payload = await computeNotifications(2);
expect(payload.enabledDisabled.unset).toBe(0);
});
it("does not flag truncation when the fallback path is not used", async () => {
db = fakeDb({ enabled: 1, disabled: 1, total: 2 });
const payload = await computeNotifications(2);
expect(payload.fallbackTruncated).toBe(false);
});
it("flags truncation when the fallback user list hits its cap", async () => {
db = fakeDb({
enabled: 1,
disabled: 1,
total: 2,
collectionGroupFails: true,
mentorUserCount: 5000,
});
const payload = await computeNotifications(2);
expect(payload.fallbackTruncated).toBe(true);
});
it("does not flag truncation when the fallback list is under the cap", async () => {
db = fakeDb({
enabled: 1,
disabled: 1,
total: 2,
collectionGroupFails: true,
mentorUserCount: 3,
});
const payload = await computeNotifications(2);
expect(payload.fallbackTruncated).toBe(false);
});
});