Something went wrong. Try again.
Mirror of https://github.com/improsocial/improAn extensible Bluesky client for web
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238import { describe, it } from "node:test";import assert from "node:assert/strict";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("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); });});