Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/improsocial/impro
An extensible Bluesky client for web impro.social
Something went wrong. Try again.
JavaScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306import { describe, it, mock, beforeEach, afterEach } from "node:test";import assert from "node:assert/strict";import { flushMicrotasks, restoreDocumentVisibility, setDocumentVisibility, waitFor,} from "../testHelpers.js";import { ChatNotificationService } from "/js/chatNotificationService.js";
function createMockApi({ unreadAcceptedConvos = 0, unreadRequestConvos = 0,} = {}) { return { getChatUnreadCounts: async () => ({ unreadAcceptedConvos, unreadRequestConvos, }), };}
describe("constructor", () => { it("should initialize with zero notifications", () => { const api = createMockApi(); const service = new ChatNotificationService(api); assert.deepEqual(service.$numNotifications.get(), 0); assert.deepEqual(service.$numUnreadRequestConvos.get(), 0); });});
describe("fetchNumNotifications", () => { it("should update notification count from API", async () => { const api = createMockApi({ unreadAcceptedConvos: 3 }); const service = new ChatNotificationService(api);
await service.fetchNumNotifications();
assert.deepEqual(service.$numNotifications.get(), 3); });
it("should sum accepted and request convo counts", async () => { const api = createMockApi({ unreadAcceptedConvos: 2, unreadRequestConvos: 3, }); const service = new ChatNotificationService(api);
await service.fetchNumNotifications();
assert.deepEqual(service.$numNotifications.get(), 5); });
it("should update $numNotifications signal when count changes", async () => { const api = createMockApi({ unreadAcceptedConvos: 1 }); const service = new ChatNotificationService(api);
assert.deepEqual(service.$numNotifications.get(), 0);
await service.fetchNumNotifications();
assert.deepEqual(service.$numNotifications.get(), 1); });
it("should handle zero counts", async () => { const api = createMockApi(); const service = new ChatNotificationService(api);
await service.fetchNumNotifications();
assert.deepEqual(service.$numNotifications.get(), 0); });
it("should publish the unread request count from the server", async () => { const api = createMockApi({ unreadAcceptedConvos: 2, unreadRequestConvos: 3, }); const service = new ChatNotificationService(api);
await service.fetchNumNotifications();
assert.deepEqual(service.$numUnreadRequestConvos.get(), 3); });
it("should overwrite the unread request count on each poll", async () => { let unreadRequestConvos = 3; const api = { getChatUnreadCounts: async () => ({ unreadAcceptedConvos: 0, unreadRequestConvos, }), }; const service = new ChatNotificationService(api);
await service.fetchNumNotifications(); assert.deepEqual(service.$numUnreadRequestConvos.get(), 3);
unreadRequestConvos = 1; await service.fetchNumNotifications(); assert.deepEqual(service.$numUnreadRequestConvos.get(), 1); });});
describe("start", () => { const originalSetTimeout = globalThis.setTimeout; beforeEach(() => { globalThis.setTimeout = (fn) => originalSetTimeout(fn, 0); }); afterEach(() => { globalThis.setTimeout = originalSetTimeout; });
it("keeps polling after a poll throws", async (t) => { t.mock.method(console, "error", () => {}); let calls = 0; const api = { getChatUnreadCounts: async () => { calls++; if (calls === 1) { throw new Error("network blip"); } return { unreadAcceptedConvos: 3, unreadRequestConvos: 0 }; }, }; const service = new ChatNotificationService(api);
const stopPolling = service.start(); t.after(stopPolling);
await waitFor(() => service.$numNotifications.get() === 3); });});
describe("refresh on becoming visible", () => { // Fake timers so the poll loop's interval never elapses and every fetch // after the first is attributable to the visibility change. beforeEach(() => { mock.timers.enable({ apis: ["setTimeout"] }); }); afterEach(() => { mock.timers.reset(); restoreDocumentVisibility(); });
it("fetches when the document becomes visible", async (t) => { let calls = 0; const api = { getChatUnreadCounts: async () => { calls++; return { unreadAcceptedConvos: 0, unreadRequestConvos: 0 }; }, }; const service = new ChatNotificationService(api); t.after(service.start()); await flushMicrotasks(); assert.deepEqual(calls, 1);
setDocumentVisibility("hidden"); assert.deepEqual(calls, 1);
setDocumentVisibility("visible"); assert.deepEqual(calls, 2); });});
describe("markNotificationsAsReadForConvo", () => { it("should optimistically decrement the count", async () => { const api = createMockApi({ unreadAcceptedConvos: 3 }); const service = new ChatNotificationService(api);
await service.fetchNumNotifications(); assert.deepEqual(service.$numNotifications.get(), 3);
service.markNotificationsAsReadForConvo("2"); assert.deepEqual(service.$numNotifications.get(), 2); });
it("should not decrement when the count is already 0", async () => { const api = createMockApi(); const service = new ChatNotificationService(api);
await service.fetchNumNotifications(); assert.deepEqual(service.$numNotifications.get(), 0);
service.markNotificationsAsReadForConvo("any"); assert.deepEqual(service.$numNotifications.get(), 0); });
it("should not decrement twice for the same convo id", async () => { const api = createMockApi({ unreadAcceptedConvos: 3 }); const service = new ChatNotificationService(api);
await service.fetchNumNotifications(); assert.deepEqual(service.$numNotifications.get(), 3);
service.markNotificationsAsReadForConvo("a"); assert.deepEqual(service.$numNotifications.get(), 2); service.markNotificationsAsReadForConvo("a"); assert.deepEqual(service.$numNotifications.get(), 2); });
it("should not bounce when the server hasn't yet acked the read", async () => { let unreadAcceptedConvos = 3; const api = { getChatUnreadCounts: async () => ({ unreadAcceptedConvos, unreadRequestConvos: 0, }), }; const service = new ChatNotificationService(api);
await service.fetchNumNotifications(); assert.deepEqual(service.$numNotifications.get(), 3);
service.markNotificationsAsReadForConvo("a"); assert.deepEqual(service.$numNotifications.get(), 2);
// Server hasn't caught up yet — still reports 3. Badge should stay at 2. await service.fetchNumNotifications(); assert.deepEqual(service.$numNotifications.get(), 2);
// Server catches up. unreadAcceptedConvos = 2; await service.fetchNumNotifications(); assert.deepEqual(service.$numNotifications.get(), 2); });
it("should decrement again after a refetch clears the dedup set", async () => { let unreadAcceptedConvos = 3; const api = { getChatUnreadCounts: async () => ({ unreadAcceptedConvos, unreadRequestConvos: 0, }), }; const service = new ChatNotificationService(api);
await service.fetchNumNotifications(); assert.deepEqual(service.$numNotifications.get(), 3);
service.markNotificationsAsReadForConvo("a"); assert.deepEqual(service.$numNotifications.get(), 2);
// Next poll: server caught up. unreadAcceptedConvos = 2; await service.fetchNumNotifications(); assert.deepEqual(service.$numNotifications.get(), 2);
// Server later re-flags convo "a" (e.g. new message) — count goes back up. unreadAcceptedConvos = 3; await service.fetchNumNotifications(); assert.deepEqual(service.$numNotifications.get(), 3);
// Reading "a" again should decrement, since the dedup set was cleared. service.markNotificationsAsReadForConvo("a"); assert.deepEqual(service.$numNotifications.get(), 2); });
it("should decrement the request count for request convos", async () => { const api = createMockApi({ unreadRequestConvos: 3 }); const service = new ChatNotificationService(api);
await service.fetchNumNotifications(); assert.deepEqual(service.$numUnreadRequestConvos.get(), 3);
service.markNotificationsAsReadForConvo("a", { isRequest: true }); assert.deepEqual(service.$numUnreadRequestConvos.get(), 2); assert.deepEqual(service.$numNotifications.get(), 2); });
it("should not decrement the request count for accepted convos", async () => { const api = createMockApi({ unreadAcceptedConvos: 2, unreadRequestConvos: 3, }); const service = new ChatNotificationService(api);
await service.fetchNumNotifications();
service.markNotificationsAsReadForConvo("a"); assert.deepEqual(service.$numUnreadRequestConvos.get(), 3); });
it("should not decrement the request count twice for the same convo id", async () => { const api = createMockApi({ unreadRequestConvos: 3 }); const service = new ChatNotificationService(api);
await service.fetchNumNotifications();
service.markNotificationsAsReadForConvo("a", { isRequest: true }); service.markNotificationsAsReadForConvo("a", { isRequest: true }); assert.deepEqual(service.$numUnreadRequestConvos.get(), 2); });
it("should not decrement the request count below zero", async () => { const api = createMockApi(); const service = new ChatNotificationService(api);
await service.fetchNumNotifications();
service.markNotificationsAsReadForConvo("a", { isRequest: true }); assert.deepEqual(service.$numUnreadRequestConvos.get(), 0); });});