|
| 1 | +import { formatMention } from "@posthog/shared"; |
| 2 | +import type { |
| 3 | + Task, |
| 4 | + TaskThreadMessage, |
| 5 | + UserBasic, |
| 6 | +} from "@posthog/shared/domain-types"; |
| 7 | +import { describe, expect, it } from "vitest"; |
| 8 | +import { |
| 9 | + buildMentionActivity, |
| 10 | + countUnseenActivity, |
| 11 | + type MentionActivityTaskRef, |
| 12 | +} from "./mentionActivity"; |
| 13 | + |
| 14 | +const me: UserBasic = { |
| 15 | + id: 1, |
| 16 | + uuid: "me-uuid", |
| 17 | + email: "me@posthog.com", |
| 18 | +}; |
| 19 | +const ann: UserBasic = { |
| 20 | + id: 2, |
| 21 | + uuid: "ann-uuid", |
| 22 | + email: "ann@posthog.com", |
| 23 | + first_name: "Ann", |
| 24 | +}; |
| 25 | + |
| 26 | +function task(id: string, title = `Task ${id}`): Task { |
| 27 | + return { id, title } as Task; |
| 28 | +} |
| 29 | + |
| 30 | +function message( |
| 31 | + id: string, |
| 32 | + author: UserBasic | null, |
| 33 | + content: string, |
| 34 | + createdAt: string, |
| 35 | +): TaskThreadMessage { |
| 36 | + return { |
| 37 | + id, |
| 38 | + task: "t1", |
| 39 | + content, |
| 40 | + created_at: createdAt, |
| 41 | + author, |
| 42 | + }; |
| 43 | +} |
| 44 | + |
| 45 | +const meToken = formatMention("Me", me.email); |
| 46 | + |
| 47 | +function refs(...tasks: Task[]): MentionActivityTaskRef[] { |
| 48 | + return tasks.map((t) => ({ |
| 49 | + task: t, |
| 50 | + channelName: "general", |
| 51 | + folderChannelId: "folder-1", |
| 52 | + })); |
| 53 | +} |
| 54 | + |
| 55 | +describe("buildMentionActivity", () => { |
| 56 | + it("returns messages from others that mention me, newest first", () => { |
| 57 | + const threads = new Map([ |
| 58 | + [ |
| 59 | + "t1", |
| 60 | + [ |
| 61 | + message("m1", ann, `ping ${meToken}`, "2026-07-01T10:00:00Z"), |
| 62 | + message("m2", ann, "no mention", "2026-07-01T11:00:00Z"), |
| 63 | + ], |
| 64 | + ], |
| 65 | + ["t2", [message("m3", ann, `also ${meToken}`, "2026-07-02T09:00:00Z")]], |
| 66 | + ]); |
| 67 | + const items = buildMentionActivity( |
| 68 | + me.email, |
| 69 | + refs(task("t1"), task("t2")), |
| 70 | + threads, |
| 71 | + ); |
| 72 | + expect(items.map((i) => i.messageId)).toEqual(["m3", "m1"]); |
| 73 | + expect(items[1]).toMatchObject({ |
| 74 | + taskId: "t1", |
| 75 | + taskTitle: "Task t1", |
| 76 | + channelName: "general", |
| 77 | + folderChannelId: "folder-1", |
| 78 | + author: ann, |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + it("matches the mention email case-insensitively", () => { |
| 83 | + const threads = new Map([ |
| 84 | + [ |
| 85 | + "t1", |
| 86 | + [ |
| 87 | + message( |
| 88 | + "m1", |
| 89 | + ann, |
| 90 | + "hi @[Me](ME@PostHog.com)", |
| 91 | + "2026-07-01T10:00:00Z", |
| 92 | + ), |
| 93 | + ], |
| 94 | + ], |
| 95 | + ]); |
| 96 | + expect( |
| 97 | + buildMentionActivity(me.email, refs(task("t1")), threads), |
| 98 | + ).toHaveLength(1); |
| 99 | + }); |
| 100 | + |
| 101 | + it("skips my own messages even when they mention me", () => { |
| 102 | + const threads = new Map([ |
| 103 | + [ |
| 104 | + "t1", |
| 105 | + [message("m1", me, `note to self ${meToken}`, "2026-07-01T10:00:00Z")], |
| 106 | + ], |
| 107 | + ]); |
| 108 | + expect(buildMentionActivity(me.email, refs(task("t1")), threads)).toEqual( |
| 109 | + [], |
| 110 | + ); |
| 111 | + }); |
| 112 | + |
| 113 | + it("includes authorless messages that mention me", () => { |
| 114 | + const threads = new Map([ |
| 115 | + ["t1", [message("m1", null, meToken, "2026-07-01T10:00:00Z")]], |
| 116 | + ]); |
| 117 | + expect( |
| 118 | + buildMentionActivity(me.email, refs(task("t1")), threads), |
| 119 | + ).toHaveLength(1); |
| 120 | + }); |
| 121 | + |
| 122 | + it("returns nothing without a current user email", () => { |
| 123 | + const threads = new Map([ |
| 124 | + ["t1", [message("m1", ann, meToken, "2026-07-01T10:00:00Z")]], |
| 125 | + ]); |
| 126 | + expect(buildMentionActivity(null, refs(task("t1")), threads)).toEqual([]); |
| 127 | + }); |
| 128 | + |
| 129 | + it("labels untitled tasks", () => { |
| 130 | + const threads = new Map([ |
| 131 | + ["t1", [message("m1", ann, meToken, "2026-07-01T10:00:00Z")]], |
| 132 | + ]); |
| 133 | + const items = buildMentionActivity(me.email, refs(task("t1", "")), threads); |
| 134 | + expect(items[0]?.taskTitle).toBe("Untitled task"); |
| 135 | + }); |
| 136 | +}); |
| 137 | + |
| 138 | +describe("countUnseenActivity", () => { |
| 139 | + const threads = new Map([ |
| 140 | + [ |
| 141 | + "t1", |
| 142 | + [ |
| 143 | + message("m1", ann, meToken, "2026-07-01T10:00:00Z"), |
| 144 | + message("m2", ann, meToken, "2026-07-03T10:00:00Z"), |
| 145 | + ], |
| 146 | + ], |
| 147 | + ]); |
| 148 | + const items = buildMentionActivity(me.email, refs(task("t1")), threads); |
| 149 | + |
| 150 | + it("counts everything when never seen", () => { |
| 151 | + expect(countUnseenActivity(items, null)).toBe(2); |
| 152 | + }); |
| 153 | + |
| 154 | + it("counts only items after the last-seen timestamp", () => { |
| 155 | + expect(countUnseenActivity(items, "2026-07-02T00:00:00Z")).toBe(1); |
| 156 | + expect(countUnseenActivity(items, "2026-07-04T00:00:00Z")).toBe(0); |
| 157 | + }); |
| 158 | +}); |
0 commit comments