|
| 1 | +import { |
| 2 | + latestActivityForChannel, |
| 3 | + unreadChannelIds, |
| 4 | +} from "@posthog/core/canvas/channelUnread"; |
| 5 | +import type { MentionActivityItem } from "@posthog/core/canvas/mentionActivity"; |
| 6 | +import { describe, expect, it } from "vitest"; |
| 7 | + |
| 8 | +function mention( |
| 9 | + overrides: Partial<MentionActivityItem> & { createdAt: string }, |
| 10 | +): MentionActivityItem { |
| 11 | + return { |
| 12 | + messageId: `m-${overrides.createdAt}`, |
| 13 | + taskId: "t1", |
| 14 | + taskTitle: "Task", |
| 15 | + channelId: "c1", |
| 16 | + channelName: "mobile", |
| 17 | + author: null, |
| 18 | + content: "hey @adam", |
| 19 | + ...overrides, |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +describe("unreadChannelIds", () => { |
| 24 | + const cases: { |
| 25 | + name: string; |
| 26 | + lastSeen: Record<string, string>; |
| 27 | + expected: string[]; |
| 28 | + }[] = [ |
| 29 | + { |
| 30 | + name: "a channel never seen is unread", |
| 31 | + lastSeen: {}, |
| 32 | + expected: ["c1"], |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "activity newer than the last visit is unread", |
| 36 | + lastSeen: { c1: "2026-07-16T09:00:00.000Z" }, |
| 37 | + expected: ["c1"], |
| 38 | + }, |
| 39 | + { |
| 40 | + name: "activity older than the last visit is read", |
| 41 | + lastSeen: { c1: "2026-07-16T11:00:00.000Z" }, |
| 42 | + expected: [], |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "activity exactly at the last visit is read", |
| 46 | + lastSeen: { c1: "2026-07-16T10:00:00.000Z" }, |
| 47 | + expected: [], |
| 48 | + }, |
| 49 | + ]; |
| 50 | + it.each(cases)("$name", ({ lastSeen, expected }) => { |
| 51 | + const items = [mention({ createdAt: "2026-07-16T10:00:00.000Z" })]; |
| 52 | + expect([...unreadChannelIds(items, lastSeen)]).toEqual(expected); |
| 53 | + }); |
| 54 | + |
| 55 | + it("compares each channel against its own last visit", () => { |
| 56 | + const items = [ |
| 57 | + mention({ channelId: "c1", createdAt: "2026-07-16T10:00:00.000Z" }), |
| 58 | + mention({ channelId: "c2", createdAt: "2026-07-16T10:00:00.000Z" }), |
| 59 | + ]; |
| 60 | + const unread = unreadChannelIds(items, { |
| 61 | + c1: "2026-07-16T11:00:00.000Z", |
| 62 | + c2: "2026-07-16T09:00:00.000Z", |
| 63 | + }); |
| 64 | + expect([...unread]).toEqual(["c2"]); |
| 65 | + }); |
| 66 | + |
| 67 | + it("uses the newest item in a channel, whatever the order", () => { |
| 68 | + const items = [ |
| 69 | + mention({ messageId: "old", createdAt: "2026-07-16T08:00:00.000Z" }), |
| 70 | + mention({ messageId: "new", createdAt: "2026-07-16T12:00:00.000Z" }), |
| 71 | + ]; |
| 72 | + expect([ |
| 73 | + ...unreadChannelIds(items, { c1: "2026-07-16T10:00:00.000Z" }), |
| 74 | + ]).toEqual(["c1"]); |
| 75 | + }); |
| 76 | + |
| 77 | + it("ignores channel-less mentions", () => { |
| 78 | + const items = [ |
| 79 | + mention({ channelId: null, createdAt: "2026-07-16T10:00Z" }), |
| 80 | + ]; |
| 81 | + expect([...unreadChannelIds(items, {})]).toEqual([]); |
| 82 | + }); |
| 83 | +}); |
| 84 | + |
| 85 | +describe("latestActivityForChannel", () => { |
| 86 | + it("returns the newest timestamp for that channel only", () => { |
| 87 | + const items = [ |
| 88 | + mention({ channelId: "c1", createdAt: "2026-07-16T08:00:00.000Z" }), |
| 89 | + mention({ channelId: "c1", createdAt: "2026-07-16T12:00:00.000Z" }), |
| 90 | + mention({ channelId: "c2", createdAt: "2026-07-16T13:00:00.000Z" }), |
| 91 | + ]; |
| 92 | + expect(latestActivityForChannel(items, "c1")).toBe( |
| 93 | + "2026-07-16T12:00:00.000Z", |
| 94 | + ); |
| 95 | + }); |
| 96 | + |
| 97 | + it("is undefined for a channel with no activity, or no channel", () => { |
| 98 | + expect(latestActivityForChannel([], "c1")).toBeUndefined(); |
| 99 | + expect(latestActivityForChannel([], undefined)).toBeUndefined(); |
| 100 | + }); |
| 101 | +}); |
0 commit comments