Skip to content

armgit / macro

Public Imported from GitHub
0

Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.

feat(soup): mirror channel notif stacking for doc comments (#2959)

Seamusd6a5d60a981a
diff --git a/js/app/packages/notifications/notification-helpers.ts b/js/app/packages/notifications/notification-helpers.ts
index 3f80e63edf..6cb891d5d6 100644
--- a/js/app/packages/notifications/notification-helpers.ts
+++ b/js/app/packages/notifications/notification-helpers.ts
@@ -7,7 +7,11 @@ import {
} from '@queries/notification/user-notifications';
import { type Accessor, createEffect, createMemo, onCleanup } from 'solid-js';
import { isMatching, P } from 'ts-pattern';
-import { CHANNEL_EVENT_TYPES, setDoneOverride } from './notification-source';
+import {
+ CHANNEL_EVENT_TYPES,
+ DOCUMENT_COMMENT_EVENT_TYPES,
+ setDoneOverride,
+} from './notification-source';
import type { NotificationSource } from './notification-source';
import { type UnifiedNotification, compositeEntity } from './types';
@@ -15,6 +19,10 @@ export const isChannelNotification = isMatching({
notification_metadata: { tag: P.union(...CHANNEL_EVENT_TYPES) },
});
+export const isDocumentCommentNotification = isMatching({
+ notification_metadata: { tag: P.union(...DOCUMENT_COMMENT_EVENT_TYPES) },
+});
+
/**
* Returns a reactive accessor to all notifications for a given entity
* @param notificationSource
diff --git a/js/app/packages/notifications/notification-source.ts b/js/app/packages/notifications/notification-source.ts
index d7df423f2b..f2d3483e5e 100644
--- a/js/app/packages/notifications/notification-source.ts
+++ b/js/app/packages/notifications/notification-source.ts
@@ -42,6 +42,12 @@ export const CHANNEL_EVENT_TYPES = [
'channel_message_reply',
] as const;
+export const DOCUMENT_COMMENT_EVENT_TYPES = [
+ 'mentioned_in_document_comment',
+ 'replied_to_document_comment_thread',
+ 'commented_on_document',
+] as const;
+
type NotificationsByEntity = Record<CompositeEntity, UnifiedNotification[]>;
type UnsubscribeFn = () => void;
diff --git a/js/app/packages/notifications/notification-stacking.ts b/js/app/packages/notifications/notification-stacking.ts
index bf09f5a7a5..7cadbe350a 100644
--- a/js/app/packages/notifications/notification-stacking.ts
+++ b/js/app/packages/notifications/notification-stacking.ts
@@ -1,7 +1,10 @@
import { compareDateDesc } from '@core/util/date';
import type { UnifiedNotification } from './types';
import type { NotificationType } from '@core/types';
-import { isChannelNotification } from './notification-helpers';
+import {
+ isChannelNotification,
+ isDocumentCommentNotification,
+} from './notification-helpers';
import { match } from 'ts-pattern';
export interface NotificationStack {
@@ -28,13 +31,35 @@ export function getAllNotificationsFromGroup(
}
/**
- * Gets the threadId from a thread stack (replies, thread-mentions, or absorbed root sends)
+ * Gets the threadId from a thread stack (replies, thread-mentions, or absorbed root sends).
+ * Works for both channel threads and document comment threads.
+ *
+ * Returns '' for stack types that represent standalone (non-thread) groups —
+ * the "new sends" stack and orphan root mentions for either domain.
*/
export function getThreadId(group: NotificationStack): string {
+ // Standalone-group stack types do not represent a thread.
+ if (
+ group.type === 'channel_message_send' ||
+ group.type === 'channel_mention' ||
+ group.type === 'commented_on_document' ||
+ group.type === 'mentioned_in_document_comment'
+ ) {
+ return '';
+ }
for (const notification of group.notifications) {
const threadId = match(notification.notification_metadata)
.with({ tag: 'channel_message_reply' }, (m) => m.content.threadId ?? '')
.with({ tag: 'channel_mention' }, (m) => m.content.threadId ?? '')
+ .with({ tag: 'replied_to_document_comment_thread' }, (m) =>
+ m.content.threadId.toString()
+ )
+ .with({ tag: 'mentioned_in_document_comment' }, (m) =>
+ m.content.threadId.toString()
+ )
+ .with({ tag: 'commented_on_document' }, (m) =>
+ m.content.threadId.toString()
+ )
.otherwise(() => '');
if (threadId) return threadId;
}
@@ -44,134 +69,249 @@ export function getThreadId(group: NotificationStack): string {
/**
* Stacks notifications by type for unrolled notification display.
*
- * Stacking rules:
- * - Replies, thread-mentions (channel_mention with threadId), and the root send
- * for a thread all group into a single thread stack.
- * - Root-level new message notifications are grouped into a single stack.
- * - Root mentions (channel_mention without threadId) each form their own stack.
+ * Channel rules:
+ * - Replies, thread-mentions, and the root send for a thread all group into
+ * a single thread stack.
+ * - Root-level new sends are grouped into a single stack.
+ * - Root mentions each form their own stack.
* - Any send/reply whose messageId matches a mention's messageId is shadowed
* (the mention is more informative).
+ *
+ * Document comment rules (`commented_on_document` cannot be statically
+ * distinguished as root vs. reply — the metadata only carries `commentId` and
+ * `threadId`, which come from independent id namespaces — so grouping is
+ * inferred from peers within the same threadId):
+ * - A threadId is a "thread" when it has any reply notification or 2+
+ * notifications of any kind. All notifications for that threadId fold into
+ * a single thread stack.
+ * - Otherwise the lone notification is treated as standalone:
+ * - `commented_on_document` standalones bundle into a "new comments" stack.
+ * - `mentioned_in_document_comment` standalones each form their own stack.
+ * - A lone `replied_to_document_comment_thread` is still a thread stack.
+ * - Mention shadowing applies the same way as for channels.
*/
export function stackNotifications(
notifications: UnifiedNotification[]
): NotificationStack[] {
- const filteredNotifications = notifications.filter(isChannelNotification);
+ const channelViews = notifications
+ .filter(isChannelNotification)
+ .map(toChannelView)
+ .filter((v): v is NormalizedView => v !== null);
+
+ const docCommentViews = notifications
+ .filter(isDocumentCommentNotification)
+ .map(toDocCommentView)
+ .filter((v): v is NormalizedView => v !== null);
- const byTag = (tag: string) => (n: UnifiedNotification) =>
- n.notification_metadata.tag === tag;
+ const channelStacks = stackNormalizedViews(channelViews, {
+ send: 'channel_message_send',
+ reply: 'channel_message_reply',
+ mention: 'channel_mention',
+ });
- const allMentions = filteredNotifications.filter(byTag('channel_mention'));
+ const docCommentStacks = stackDocCommentViews(docCommentViews);
- // Thread mentions have a threadId; root mentions don't
- const rootMentions = allMentions.filter((n) =>
- match(n.notification_metadata)
- .with({ tag: 'channel_mention' }, (m) => !m.content.threadId)
- .otherwise(() => false)
+ const docMentions = notifications.filter(
+ (n) => n.notification_metadata.tag === 'document_mention'
);
- const threadMentions = allMentions.filter((n) =>
- match(n.notification_metadata)
- .with({ tag: 'channel_mention' }, (m) => !!m.content.threadId)
- .otherwise(() => false)
+ const others = notifications.filter(
+ (n) =>
+ !isChannelNotification(n) &&
+ !isDocumentCommentNotification(n) &&
+ n.notification_metadata.tag !== 'document_mention'
);
- // All mention messageIds for shadowing sends/replies
+ const groups: NotificationStack[] = [
+ ...channelStacks,
+ ...docCommentStacks,
+ ...makeStack('document_mention', docMentions),
+ ...others.flatMap((n) => makeStack(n.notification_metadata.tag, [n])),
+ ];
+
+ return groups.sort((a, b) =>
+ compareDateDesc(
+ a.notifications[0].created_at,
+ b.notifications[0].created_at
+ )
+ );
+}
+
+type ViewRole = 'send' | 'reply' | 'mention';
+
+interface NormalizedView {
+ notification: UnifiedNotification;
+ role: ViewRole;
+ messageId: string;
+ threadId: string | undefined;
+}
+
+interface DomainTags {
+ send: NotificationType;
+ reply: NotificationType;
+ mention: NotificationType;
+}
+
+function toChannelView(n: UnifiedNotification): NormalizedView | null {
+ return match(n.notification_metadata)
+ .with({ tag: 'channel_message_send' }, (m) => ({
+ notification: n,
+ role: 'send' as const,
+ messageId: m.content.messageId,
+ threadId: undefined,
+ }))
+ .with({ tag: 'channel_message_reply' }, (m) => ({
+ notification: n,
+ role: 'reply' as const,
+ messageId: m.content.messageId,
+ threadId: m.content.threadId,
+ }))
+ .with({ tag: 'channel_mention' }, (m) => ({
+ notification: n,
+ role: 'mention' as const,
+ messageId: m.content.messageId,
+ threadId: m.content.threadId ?? undefined,
+ }))
+ .otherwise(() => null);
+}
+
+function toDocCommentView(n: UnifiedNotification): NormalizedView | null {
+ // commentId and threadId come from separate DB tables (Comment.id and
+ // Thread.id), so equality between them carries no meaning. Roots vs.
+ // replies are inferred at the stacking layer from peers sharing a threadId.
+ return match(n.notification_metadata)
+ .with({ tag: 'commented_on_document' }, (m) => ({
+ notification: n,
+ role: 'send' as const,
+ messageId: m.content.commentId.toString(),
+ threadId: m.content.threadId.toString(),
+ }))
+ .with({ tag: 'replied_to_document_comment_thread' }, (m) => ({
+ notification: n,
+ role: 'reply' as const,
+ messageId: m.content.commentId.toString(),
+ threadId: m.content.threadId.toString(),
+ }))
+ .with({ tag: 'mentioned_in_document_comment' }, (m) => ({
+ notification: n,
+ role: 'mention' as const,
+ messageId: m.content.commentId.toString(),
+ threadId: m.content.threadId.toString(),
+ }))
+ .otherwise(() => null);
+}
+
+function stackDocCommentViews(views: NormalizedView[]): NotificationStack[] {
const mentionedMsgIds = new Set(
- allMentions
- .map((n) =>
- match(n.notification_metadata)
- .with({ tag: 'channel_mention' }, (m) => m.content.messageId)
- .otherwise(() => undefined)
- )
- .filter((id): id is string => id !== undefined)
+ views.filter((v) => v.role === 'mention').map((v) => v.messageId)
);
- const isShadowed = (n: UnifiedNotification) =>
- match(n.notification_metadata)
- .with(
- { tag: 'channel_message_send' },
- { tag: 'channel_message_reply' },
- (m) => !!m.content.messageId && mentionedMsgIds.has(m.content.messageId)
- )
- .otherwise(() => false);
+ const filtered = views.filter(
+ (v) => v.role === 'mention' || !mentionedMsgIds.has(v.messageId)
+ );
+
+ const byThread = groupBy(filtered, (v) => v.threadId ?? '');
+
+ const standaloneSends: NormalizedView[] = [];
+ const stacks: NotificationStack[] = [];
+ for (const [threadId, group] of byThread) {
+ if (threadId === '') continue;
+
+ const isThread = group.length >= 2 || group.some((v) => v.role === 'reply');
+
+ if (isThread) {
+ stacks.push({
+ type: 'replied_to_document_comment_thread',
+ notifications: sortByRecency(group.map((v) => v.notification)),
+ });
+ continue;
+ }
+
+ const v = group[0];
+ if (v.role === 'send') {
+ standaloneSends.push(v);
+ } else if (v.role === 'mention') {
+ stacks.push({
+ type: 'mentioned_in_document_comment',
+ notifications: [v.notification],
+ });
+ }
+ }
+
+ if (standaloneSends.length > 0) {
+ stacks.push({
+ type: 'commented_on_document',
+ notifications: sortByRecency(standaloneSends.map((v) => v.notification)),
+ });
+ }
+
+ return stacks;
+}
+
+function stackNormalizedViews(
+ views: NormalizedView[],
+ tags: DomainTags
+): NotificationStack[] {
+ const mentions = views.filter((v) => v.role === 'mention');
+ const rootMentions = mentions.filter((v) => v.threadId === undefined);
+ const threadMentions = mentions.filter((v) => v.threadId !== undefined);
+
+ const mentionedMsgIds = new Set(mentions.map((v) => v.messageId));
+
+ const isShadowed = (v: NormalizedView) =>
+ (v.role === 'send' || v.role === 'reply') &&
+ mentionedMsgIds.has(v.messageId);
+
+ // A thread is "active" if it has any reply or any thread-mention.
const activeThreadIds = new Set(
- filteredNotifications
- .map((n) =>
- match(n.notification_metadata)
- .with({ tag: 'channel_message_reply' }, (m) => m.content.threadId)
- .with(
- { tag: 'channel_mention' },
- (m) => m.content.threadId ?? undefined
- )
- .otherwise(() => undefined)
+ views
+ .map((v) =>
+ v.role === 'reply' || (v.role === 'mention' && v.threadId !== undefined)
+ ? v.threadId
+ : undefined
)
.filter((id): id is string => id !== undefined)
);
- const replies = filteredNotifications
- .filter(byTag('channel_message_reply'))
- .filter((n) => !isShadowed(n));
+ const replies = views
+ .filter((v) => v.role === 'reply')
+ .filter((v) => !isShadowed(v));
- const allSends = filteredNotifications.filter(byTag('channel_message_send'));
+ const allSends = views.filter((v) => v.role === 'send');
- const isAbsorbedIntoThread = (n: UnifiedNotification) =>
- match(n.notification_metadata)
- .with({ tag: 'channel_message_send' }, (m) =>
- activeThreadIds.has(m.content.messageId)
- )
- .otherwise(() => false);
+ const isAbsorbedIntoThread = (v: NormalizedView) =>
+ activeThreadIds.has(v.messageId);
- // Unshadowed sends not belonging to a thread → "new messages" stack
- const newMsgs = allSends.filter(
- (n) => !isShadowed(n) && !isAbsorbedIntoThread(n)
+ const newSends = allSends.filter(
+ (v) => !isShadowed(v) && !isAbsorbedIntoThread(v)
);
- // Unshadowed sends whose messageId is a known threadId → join thread stack
const absorbedSends = allSends.filter(
- (n) => !isShadowed(n) && isAbsorbedIntoThread(n)
+ (v) => !isShadowed(v) && isAbsorbedIntoThread(v)
);
- // Root mentions whose messageId is a known threadId → join that thread stack
- const absorbedRootMentions = rootMentions.filter((n) =>
- match(n.notification_metadata)
- .with({ tag: 'channel_mention' }, (m) =>
- activeThreadIds.has(m.content.messageId)
- )
- .otherwise(() => false)
- );
- // Root mentions with no matching thread → remain as individual stacks
- const orphanRootMentions = rootMentions.filter((n) =>
- match(n.notification_metadata)
- .with(
- { tag: 'channel_mention' },
- (m) => !activeThreadIds.has(m.content.messageId)
- )
- .otherwise(() => false)
+ const absorbedRootMentions = rootMentions.filter((v) =>
+ activeThreadIds.has(v.messageId)
);
-
- const docMentions = notifications.filter(byTag('document_mention'));
- const others = notifications.filter(
- (n) => !isChannelNotification(n) && !byTag('document_mention')(n)
+ const orphanRootMentions = rootMentions.filter(
+ (v) => !activeThreadIds.has(v.messageId)
);
- const groups: NotificationStack[] = [
- ...orphanRootMentions.flatMap((n) => makeStack('channel_mention', [n])),
- ...makeStack('channel_message_send', newMsgs),
+ return [
+ ...orphanRootMentions.flatMap((v) =>
+ makeStack(tags.mention, [v.notification])
+ ),
+ ...makeStack(
+ tags.send,
+ newSends.map((v) => v.notification)
+ ),
...makeThreadStacks(
+ tags.reply,
replies,
threadMentions,
absorbedSends,
absorbedRootMentions
),
- ...makeStack('document_mention', docMentions),
- ...others.flatMap((n) => makeStack(n.notification_metadata.tag, [n])),
];
-
- return groups.sort((a, b) =>
- compareDateDesc(
- a.notifications[0].created_at,
- b.notifications[0].created_at
- )
- );
}
function sortByRecency(items: UnifiedNotification[]): UnifiedNotification[] {
@@ -203,22 +343,19 @@ function makeStack(
}
function makeThreadStacks(
- replies: UnifiedNotification[],
- threadMentions: UnifiedNotification[],
- absorbedSends: UnifiedNotification[],
- absorbedRootMentions: UnifiedNotification[]
+ replyTag: NotificationType,
+ replies: NormalizedView[],
+ threadMentions: NormalizedView[],
+ absorbedSends: NormalizedView[],
+ absorbedRootMentions: NormalizedView[]
): NotificationStack[] {
- // Key each notification by its threadId. For absorbed root sends and root
- // mentions, their messageId IS the threadId (they are the thread root).
- const keyOf = (n: UnifiedNotification): string =>
- match(n.notification_metadata)
- .with({ tag: 'channel_message_reply' }, (m) => m.content.threadId ?? '')
- .with(
- { tag: 'channel_mention' },
- (m) => m.content.threadId ?? m.content.messageId
- )
- .with({ tag: 'channel_message_send' }, (m) => m.content.messageId)
- .otherwise(() => '');
+ // Key each view by its threadId. For absorbed sends and root mentions, their
+ // messageId IS the threadId (they are the thread root).
+ const keyOf = (v: NormalizedView): string => {
+ if (v.role === 'reply') return v.threadId ?? '';
+ if (v.role === 'mention') return v.threadId ?? v.messageId;
+ return v.messageId;
+ };
const byThread = groupBy(
[...replies, ...threadMentions, ...absorbedSends, ...absorbedRootMentions],
@@ -228,7 +365,7 @@ function makeThreadStacks(
return [...byThread.entries()]
.filter(([threadId]) => threadId !== '')
.map(([, group]) => ({
- type: 'channel_message_reply' as const,
- notifications: sortByRecency(group),
+ type: replyTag,
+ notifications: sortByRecency(group.map((v) => v.notification)),
}));
}
diff --git a/js/app/packages/notifications/tests/notification-stacking.test.ts b/js/app/packages/notifications/tests/notification-stacking.test.ts
index ab13ea967c..36bb0aec2a 100644
--- a/js/app/packages/notifications/tests/notification-stacking.test.ts
+++ b/js/app/packages/notifications/tests/notification-stacking.test.ts
@@ -81,6 +81,61 @@ function createMentionNotification(
});
}
+function createDocCommentNotification(
+ id: string,
+ commentId: number,
+ threadId: number,
+ createdAt: number
+): UnifiedNotification {
+ return createBaseNotification(id, createdAt, {
+ tag: 'commented_on_document',
+ content: {
+ commentId,
+ threadId,
+ documentName: 'doc',
+ owner: 'user-1',
+ text: `Comment ${id}`,
+ },
+ });
+}
+
+function createDocReplyNotification(
+ id: string,
+ commentId: number,
+ threadId: number,
+ createdAt: number
+): UnifiedNotification {
+ return createBaseNotification(id, createdAt, {
+ tag: 'replied_to_document_comment_thread',
+ content: {
+ commentId,
+ threadId,
+ documentName: 'doc',
+ owner: 'user-1',
+ text: `Reply ${id}`,
+ },
+ });
+}
+
+function createDocMentionNotification(
+ id: string,
+ commentId: number,
+ threadId: number,
+ createdAt: number
+): UnifiedNotification {
+ return createBaseNotification(id, createdAt, {
+ tag: 'mentioned_in_document_comment',
+ content: {
+ commentId,
+ threadId,
+ documentName: 'doc',
+ mentionId: `mention-${id}`,
+ owner: 'user-1',
+ text: `Mention ${id}`,
+ },
+ });
+}
+
describe('stackNotifications', () => {
describe('basic stacking', () => {
it('stacks multiple new messages into a single group', () => {
@@ -446,3 +501,249 @@ describe('getAllNotificationsFromGroup', () => {
expect(allNotifications[0].id).toBe('m1');
});
});
+
+describe('stackNotifications — document comments', () => {
+ // commentId and threadId come from independent DB tables, so equality
+ // between them is essentially never true in real data. Tests use disjoint
+ // id ranges (comment ids 100+, thread ids 1+) to reflect that.
+
+ describe('basic stacking', () => {
+ it('bundles standalone top-level comments into a single new-comments stack', () => {
+ const notifications = [
+ createDocCommentNotification('c1', 100, 1, 1000),
+ createDocCommentNotification('c2', 101, 2, 2000),
+ createDocCommentNotification('c3', 102, 3, 3000),
+ ];
+
+ const result = stackNotifications(notifications);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].type).toBe('commented_on_document');
+ expect(result[0].notifications).toHaveLength(3);
+ expect(result[0].notifications[0].id).toBe('c3'); // most recent
+ expect(getThreadId(result[0])).toBe(''); // not a thread stack
+ });
+
+ it('stacks replies by threadId', () => {
+ const notifications = [
+ createDocReplyNotification('r1', 110, 10, 1000),
+ createDocReplyNotification('r2', 111, 10, 2000),
+ createDocReplyNotification('r3', 112, 20, 3000),
+ ];
+
+ const result = stackNotifications(notifications);
+
+ expect(result).toHaveLength(2);
+ const threadAStack = result.find(
+ (g) =>
+ g.type === 'replied_to_document_comment_thread' &&
+ getThreadId(g) === '10'
+ );
+ const threadBStack = result.find(
+ (g) =>
+ g.type === 'replied_to_document_comment_thread' &&
+ getThreadId(g) === '20'
+ );
+ expect(threadAStack).toBeDefined();
+ expect(threadBStack).toBeDefined();
+ expect(threadAStack!.notifications).toHaveLength(2);
+ expect(threadBStack!.notifications).toHaveLength(1);
+ });
+
+ it('keeps standalone mentions as individual stacks', () => {
+ const notifications = [
+ createDocMentionNotification('m1', 100, 1, 1000),
+ createDocMentionNotification('m2', 101, 2, 2000),
+ ];
+
+ const result = stackNotifications(notifications);
+
+ expect(result).toHaveLength(2);
+ expect(
+ result.every((g) => g.type === 'mentioned_in_document_comment')
+ ).toBe(true);
+ expect(getThreadId(result[0])).toBe('');
+ expect(getThreadId(result[1])).toBe('');
+ });
+ });
+
+ describe('thread stacking', () => {
+ it('folds top-level comment into thread stack when replies exist', () => {
+ const notifications = [
+ createDocCommentNotification('c1', 100, 10, 1000),
+ createDocReplyNotification('r1', 110, 10, 2000),
+ ];
+
+ const result = stackNotifications(notifications);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].type).toBe('replied_to_document_comment_thread');
+ expect(getThreadId(result[0])).toBe('10');
+ const ids = result[0].notifications.map((n) => n.id);
+ expect(ids).toContain('c1');
+ expect(ids).toContain('r1');
+ });
+
+ it('groups owner-side commented_on_document reply with the thread', () => {
+ // The doc owner gets `commented_on_document` for any comment on their
+ // document, including replies. When peers share the threadId, they fold.
+ const notifications = [
+ createDocReplyNotification('r1', 110, 10, 1000),
+ createDocCommentNotification('c1', 111, 10, 2000),
+ ];
+
+ const result = stackNotifications(notifications);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].type).toBe('replied_to_document_comment_thread');
+ expect(getThreadId(result[0])).toBe('10');
+ expect(result[0].notifications).toHaveLength(2);
+ });
+
+ it('groups mention into thread stack when other thread activity exists', () => {
+ const notifications = [
+ createDocReplyNotification('r1', 110, 10, 1000),
+ createDocMentionNotification('m1', 111, 10, 2000),
+ ];
+
+ const result = stackNotifications(notifications);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].type).toBe('replied_to_document_comment_thread');
+ expect(getThreadId(result[0])).toBe('10');
+ const ids = result[0].notifications.map((n) => n.id);
+ expect(ids).toContain('r1');
+ expect(ids).toContain('m1');
+ });
+
+ it('groups top-level comment, replies, and mention into one thread stack', () => {
+ const notifications = [
+ createDocCommentNotification('c1', 100, 10, 1000),
+ createDocReplyNotification('r1', 110, 10, 2000),
+ createDocMentionNotification('m1', 111, 10, 3000),
+ ];
+
+ const result = stackNotifications(notifications);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].type).toBe('replied_to_document_comment_thread');
+ expect(result[0].notifications).toHaveLength(3);
+ expect(result[0].notifications[0].id).toBe('m1');
+ });
+
+ it('mention on the thread root shadows the root comment when both share commentId', () => {
+ const notifications = [
+ createDocCommentNotification('c1', 100, 10, 1000),
+ createDocReplyNotification('r1', 110, 10, 2000),
+ createDocMentionNotification('m1', 100, 10, 3000), // same commentId as c1
+ ];
+
+ const result = stackNotifications(notifications);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].type).toBe('replied_to_document_comment_thread');
+ expect(getThreadId(result[0])).toBe('10');
+ const ids = result[0].notifications.map((n) => n.id);
+ expect(ids).toContain('m1');
+ expect(ids).toContain('r1');
+ expect(ids).not.toContain('c1'); // shadowed by mention with same commentId
+ });
+
+ it('lone mention with no other thread activity stays as an individual stack', () => {
+ const notifications = [createDocMentionNotification('m1', 100, 10, 1000)];
+
+ const result = stackNotifications(notifications);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].type).toBe('mentioned_in_document_comment');
+ expect(getThreadId(result[0])).toBe('');
+ });
+
+ it('two top-level comments sharing a threadId fold into a thread stack', () => {
+ // E.g. doc owner getting both their own root comment and a reply.
+ const notifications = [
+ createDocCommentNotification('c1', 100, 10, 1000),
+ createDocCommentNotification('c2', 101, 10, 2000),
+ ];
+
+ const result = stackNotifications(notifications);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].type).toBe('replied_to_document_comment_thread');
+ expect(getThreadId(result[0])).toBe('10');
+ expect(result[0].notifications).toHaveLength(2);
+ });
+ });
+
+ describe('getThreadId standalone regression', () => {
+ it('returns "" for a standalone commented_on_document stack', () => {
+ const notifications = [createDocCommentNotification('c1', 100, 1, 1000)];
+
+ const result = stackNotifications(notifications);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].type).toBe('commented_on_document');
+ expect(getThreadId(result[0])).toBe('');
+ });
+
+ it('returns "" for a standalone mentioned_in_document_comment stack', () => {
+ const notifications = [createDocMentionNotification('m1', 100, 1, 1000)];
+
+ const result = stackNotifications(notifications);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].type).toBe('mentioned_in_document_comment');
+ expect(getThreadId(result[0])).toBe('');
+ });
+ });
+
+ describe('mention shadowing', () => {
+ it('shadows commented_on_document when mention exists for same commentId', () => {
+ const notifications = [
+ createDocCommentNotification('c1', 100, 1, 1000),
+ createDocMentionNotification('m1', 100, 1, 2000),
+ ];
+
+ const result = stackNotifications(notifications);
+
+ expect(result).toHaveLength(1);
+ expect(result[0].type).toBe('mentioned_in_document_comment');
+ });
+
+ it('shadows replied_to_document_comment_thread when mention exists for same commentId', () => {
+ const notifications = [
+ createDocReplyNotification('r1', 110, 10, 1000),
+ createDocMentionNotification('m1', 110, 10, 2000),
+ ];
+
+ const result = stackNotifications(notifications);
+
+ expect(result).toHaveLength(1);
+ // The reply was shadowed; only the mention remains in the thread, with
+ // no other peers, so it stands alone as an orphan mention.
+ expect(result[0].type).toBe('mentioned_in_document_comment');
+ const ids = result[0].notifications.map((n) => n.id);
+ expect(ids).toContain('m1');
+ expect(ids).not.toContain('r1');
+ });
+ });
+
+ describe('isolation', () => {
+ it('does not mix channel and document comment notifications in the same stack', () => {
+ const notifications = [
+ createNewMessageNotification('n1', 'msg-1', 1000),
+ createDocCommentNotification('c1', 100, 1, 2000),
+ ];
+
+ const result = stackNotifications(notifications);
+
+ expect(result).toHaveLength(2);
+ const channelStack = result.find(
+ (g) => g.type === 'channel_message_send'
+ );
+ const docStack = result.find((g) => g.type === 'commented_on_document');
+ expect(channelStack).toBeDefined();
+ expect(docStack).toBeDefined();
+ });
+ });
+});