Debian Codegraph @

web/src/app/SubscriptionManager.test.js

raw
1 import { beforeEach, describe, expect, it, vi } from "vitest";
2
3 // SubscriptionManager pulls in a handful of browser/Dexie-heavy singletons at import time. Mock
4 // them so the module imports cleanly under the node test environment; the tests construct their
5 // own SubscriptionManager with an in-memory fake db, so the real db singleton is never used.
6 vi.mock("./Api", () => ({ default: {} }));
7 vi.mock("./Notifier", () => ({ default: {} }));
8 vi.mock("./Prefs", () => ({ default: {} }));
9 vi.mock("./db", () => ({ default: () => ({}) }));
10
11 const { SubscriptionManager } = await import("./SubscriptionManager");
12
13 // Minimal in-memory stand-in for the Dexie "subscriptions" table, implementing just the surface
14 // that syncFromRemote() (and the upsert/remove/update helpers it calls) touches.
15 const fakeDb = () => {
16 const rows = new Map();
17 return {
18 rows,
19 subscriptions: {
20 get: async (id) => rows.get(id),
21 put: async (sub) => {
22 rows.set(sub.id, sub);
23 },
24 update: async (id, changes) => {
25 const existing = rows.get(id);
26 if (existing) {
27 rows.set(id, { ...existing, ...changes });
28 }
29 },
30 delete: async (id) => {
31 rows.delete(id);
32 },
33 toArray: async () => Array.from(rows.values()),
34 },
35 };
36 };
37
38 const baseUrl = "https://ntfy.sh";
39
40 beforeEach(() => {
41 vi.spyOn(console, "log").mockImplementation(() => {});
42 });
43
44 describe("SubscriptionManager.upsert", () => {
45 it("merges fields into an existing subscription without clobbering local-only state", async () => {
46 const db = fakeDb();
47 const manager = new SubscriptionManager(db);
48
49 await manager.upsert(baseUrl, "mytopic");
50 await manager.setMutedUntil("https://ntfy.sh/mytopic", 123);
51
52 const reservation = { topic: "mytopic", everyone: "deny-all" };
53 await manager.upsert(baseUrl, "mytopic", { displayName: "My Topic", reservation });
54
55 const stored = db.rows.get("https://ntfy.sh/mytopic");
56 expect(stored.reservation).toEqual(reservation);
57 expect(stored.displayName).toBe("My Topic");
58 expect(stored.mutedUntil).toBe(123); // local-only state preserved
59 });
60
61 it("does not write when an existing subscription would not change", async () => {
62 const db = fakeDb();
63 const manager = new SubscriptionManager(db);
64
65 await manager.upsert(baseUrl, "mytopic", { internal: true });
66 const putSpy = vi.spyOn(db.subscriptions, "put");
67
68 const result = await manager.upsert(baseUrl, "mytopic", { internal: true });
69
70 expect(putSpy).not.toHaveBeenCalled();
71 expect(result.topic).toBe("mytopic");
72 });
73 });
74
75 describe("SubscriptionManager.syncFromRemote", () => {
76 it("persists a reservation onto a subscription that already exists locally", async () => {
77 const db = fakeDb();
78 const manager = new SubscriptionManager(db);
79
80 // Topic was subscribed to before it was reserved, so it already exists locally without a
81 // reservation -- exactly the state when a user clicks "Reserve topic" in the navbar.
82 await manager.upsert(baseUrl, "mytopic");
83 expect(db.rows.get("https://ntfy.sh/mytopic").reservation).toBeFalsy();
84
85 const reservation = { topic: "mytopic", everyone: "deny-all" };
86 await manager.syncFromRemote([{ base_url: baseUrl, topic: "mytopic" }], [reservation]);
87
88 expect(db.rows.get("https://ntfy.sh/mytopic").reservation).toEqual(reservation);
89 });
90
91 it("clears the reservation when the remote no longer reports one", async () => {
92 const db = fakeDb();
93 const manager = new SubscriptionManager(db);
94
95 await manager.upsert(baseUrl, "mytopic", { reservation: { topic: "mytopic", everyone: "deny-all" } });
96
97 await manager.syncFromRemote([{ base_url: baseUrl, topic: "mytopic" }], []);
98
99 expect(db.rows.get("https://ntfy.sh/mytopic").reservation).toBeNull();
100 });
101
102 it("updates the display name on an existing subscription", async () => {
103 const db = fakeDb();
104 const manager = new SubscriptionManager(db);
105
106 await manager.upsert(baseUrl, "mytopic");
107 await manager.syncFromRemote([{ base_url: baseUrl, topic: "mytopic", display_name: "My Topic" }], []);
108
109 expect(db.rows.get("https://ntfy.sh/mytopic").displayName).toBe("My Topic");
110 });
111 });