|
| 1 | +import type { TaskMention, UserBasic } from "@posthog/shared/domain-types"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { |
| 4 | + countUnseenActivity, |
| 5 | + mergeTaskMentions, |
| 6 | + toMentionActivityItems, |
| 7 | +} from "./mentionActivity"; |
| 8 | + |
| 9 | +const ann: UserBasic = { |
| 10 | + id: 2, |
| 11 | + uuid: "ann-uuid", |
| 12 | + email: "ann@posthog.com", |
| 13 | + first_name: "Ann", |
| 14 | +}; |
| 15 | + |
| 16 | +function mention(overrides: Partial<TaskMention> = {}): TaskMention { |
| 17 | + return { |
| 18 | + id: "mention-1", |
| 19 | + message_id: "m1", |
| 20 | + task_id: "t1", |
| 21 | + task_title: "Task t1", |
| 22 | + channel_id: "c1", |
| 23 | + channel_name: "general", |
| 24 | + author: ann, |
| 25 | + content: "ping @[Me](me@posthog.com)", |
| 26 | + created_at: "2026-07-01T10:00:00Z", |
| 27 | + ...overrides, |
| 28 | + }; |
| 29 | +} |
| 30 | + |
| 31 | +describe("toMentionActivityItems", () => { |
| 32 | + it("maps mention DTOs to feed items", () => { |
| 33 | + expect(toMentionActivityItems([mention()])).toEqual([ |
| 34 | + { |
| 35 | + messageId: "m1", |
| 36 | + taskId: "t1", |
| 37 | + taskTitle: "Task t1", |
| 38 | + channelId: "c1", |
| 39 | + channelName: "general", |
| 40 | + author: ann, |
| 41 | + content: "ping @[Me](me@posthog.com)", |
| 42 | + createdAt: "2026-07-01T10:00:00Z", |
| 43 | + }, |
| 44 | + ]); |
| 45 | + }); |
| 46 | + |
| 47 | + it("labels untitled tasks and tolerates missing channel and author", () => { |
| 48 | + const items = toMentionActivityItems([ |
| 49 | + mention({ |
| 50 | + task_title: "", |
| 51 | + channel_id: null, |
| 52 | + channel_name: null, |
| 53 | + author: null, |
| 54 | + }), |
| 55 | + ]); |
| 56 | + expect(items[0]).toMatchObject({ |
| 57 | + taskTitle: "Untitled task", |
| 58 | + channelId: null, |
| 59 | + channelName: null, |
| 60 | + author: null, |
| 61 | + }); |
| 62 | + }); |
| 63 | +}); |
| 64 | + |
| 65 | +describe("countUnseenActivity", () => { |
| 66 | + const items = toMentionActivityItems([ |
| 67 | + mention({ message_id: "m2", created_at: "2026-07-03T10:00:00Z" }), |
| 68 | + mention({ message_id: "m1", created_at: "2026-07-01T10:00:00Z" }), |
| 69 | + ]); |
| 70 | + |
| 71 | + it("counts everything when never seen", () => { |
| 72 | + expect(countUnseenActivity(items, null)).toBe(2); |
| 73 | + }); |
| 74 | + |
| 75 | + it("counts only items after the last-seen timestamp", () => { |
| 76 | + expect(countUnseenActivity(items, "2026-07-02T00:00:00Z")).toBe(1); |
| 77 | + expect(countUnseenActivity(items, "2026-07-04T00:00:00Z")).toBe(0); |
| 78 | + }); |
| 79 | +}); |
| 80 | + |
| 81 | +describe("mergeTaskMentions", () => { |
| 82 | + it("prepends newly-fetched mentions ahead of the previous page", () => { |
| 83 | + const previous = [ |
| 84 | + mention({ message_id: "m1", created_at: "2026-07-01T10:00:00Z" }), |
| 85 | + ]; |
| 86 | + const incoming = [ |
| 87 | + mention({ message_id: "m2", created_at: "2026-07-02T10:00:00Z" }), |
| 88 | + ]; |
| 89 | + expect( |
| 90 | + mergeTaskMentions(previous, incoming).map((m) => m.message_id), |
| 91 | + ).toEqual(["m2", "m1"]); |
| 92 | + }); |
| 93 | + |
| 94 | + it("replaces a mention that was re-fetched instead of duplicating it", () => { |
| 95 | + const previous = [ |
| 96 | + mention({ |
| 97 | + message_id: "m1", |
| 98 | + content: "old", |
| 99 | + created_at: "2026-07-01T10:00:00Z", |
| 100 | + }), |
| 101 | + ]; |
| 102 | + const incoming = [ |
| 103 | + mention({ |
| 104 | + message_id: "m1", |
| 105 | + content: "edited", |
| 106 | + created_at: "2026-07-01T10:00:00Z", |
| 107 | + }), |
| 108 | + ]; |
| 109 | + const merged = mergeTaskMentions(previous, incoming); |
| 110 | + expect(merged).toHaveLength(1); |
| 111 | + expect(merged[0].content).toBe("edited"); |
| 112 | + }); |
| 113 | + |
| 114 | + it("returns the previous page unchanged when there is nothing new", () => { |
| 115 | + const previous = [ |
| 116 | + mention({ message_id: "m1", created_at: "2026-07-01T10:00:00Z" }), |
| 117 | + ]; |
| 118 | + expect(mergeTaskMentions(previous, [])).toEqual(previous); |
| 119 | + }); |
| 120 | + |
| 121 | + it("caps the merged result so a long session can't grow it unbounded", () => { |
| 122 | + const previous = Array.from({ length: 300 }, (_, i) => |
| 123 | + mention({ |
| 124 | + message_id: `old-${i}`, |
| 125 | + created_at: `2026-06-01T${String(i % 24).padStart(2, "0")}:00:00Z`, |
| 126 | + }), |
| 127 | + ); |
| 128 | + const incoming = [ |
| 129 | + mention({ message_id: "newest", created_at: "2026-07-05T10:00:00Z" }), |
| 130 | + ]; |
| 131 | + const merged = mergeTaskMentions(previous, incoming); |
| 132 | + expect(merged).toHaveLength(300); |
| 133 | + expect(merged[0].message_id).toBe("newest"); |
| 134 | + }); |
| 135 | +}); |
0 commit comments