Macro is a unified workspace for teams: email, chat, docs, tasks, agents, calls, and CRM — @-linked together with shared AI memory.
TypeScript agentaiai-agentsall-in-onecrmcrm-systememaillinearmcpmessagingnotesnotionofficerustslackslack-alternativestartupstartup-toolstypescriptworkspace
import type { NotifEvent } from '@service-notification/generated/schemas';
import { describe, expect, it, vi } from 'vitest';
import { scopeChannelNotificationsForEntity } from '../../soup/entity-notifications';
import {
getAllNotificationsFromGroup,
getMostRecentNotification,
getThreadId,
stackNotifications,
} from '../notification-stacking';
import type { UnifiedNotification } from '../types';
// Helper to create a base notification with proper typing
function createBaseNotification(
id: string,
createdAt: number,
notificationMetadata: NotifEvent
): UnifiedNotification {
return {
id,
entity_id: 'channel-1',
entity_type: 'channel',
created_at: new Date(createdAt).toISOString(),
updated_at: new Date(createdAt).toISOString(),
viewed_at: null,
deleted_at: null,
state: 'unseen',
sent: true,
sender_id: 'user-1',
notification_event_type: notificationMetadata.tag,
notification_metadata: notificationMetadata,
};
}
function createNewMessageNotification(
id: string,
messageId: string,
createdAt: number
): UnifiedNotification {
return createBaseNotification(id, createdAt, {
tag: 'channel_message_send',
content: {
messageId,
messageContent: `Message ${id}`,
sender: 'user-1',
channelType: 'private',
},
});
}
function createReplyNotification(
id: string,
messageId: string,
threadId: string,
createdAt: number
): UnifiedNotification {
return createBaseNotification(id, createdAt, {
tag: 'channel_message_reply',
content: {
messageId,
threadId,
messageContent: `Reply ${id}`,
userId: 'user-1',
channelType: 'private',
},
});
}
function createMentionNotification(
id: string,
messageId: string,
createdAt: number,
threadId?: string
): UnifiedNotification {
return createBaseNotification(id, createdAt, {
tag: 'channel_mention',
content: {
messageId,
messageContent: `Mention ${id}`,
threadId: threadId ?? null,
channelType: 'private',
},
});
}
function createReactionNotification(
id: string,
messageId: string,
createdAt: number,
threadId?: string
): UnifiedNotification {
return createBaseNotification(id, createdAt, {
tag: 'channel_message_reaction',
content: {
messageId,
messageContent: `Message ${id}`,
emoji: '👍',
threadId,
channelType: 'private',
},
});
}
function createDocCommentNotification(
id: string,
commentId: string,
threadId: string,
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: string,
threadId: string,
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: string,
threadId: string,
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('channel notification scoping', () => {
function expectStackMembership(notifications: UnifiedNotification[]) {
const threadIds = new Set(
stackNotifications(notifications)
.filter(
(stack) =>
stack.type === 'channel_message_reply' ||
stack.type === 'channel_mention' ||
stack.type === 'channel_message_reaction'
)
.flatMap((stack) => stack.notifications.map((n) => n.id))
);
expect(
scopeChannelNotificationsForEntity({ type: 'channel' }, notifications)
).toEqual(notifications.filter((n) => !threadIds.has(n.id)));
}
it('matches display-stack membership for every combination of channel events', () => {
const candidates = [
createNewMessageNotification('root-send', 'root', 2000),
createNewMessageNotification('standalone-send', 'standalone', 1000),
createReplyNotification('reply', 'reply-message', 'root', 5000),
createMentionNotification('reply-mention', 'reply-message', 4000, 'root'),
createMentionNotification('root-mention', 'root', 6000),
createMentionNotification('orphan-mention', 'orphan', 3000),
createMentionNotification(
'thread-mention',
'other-message',
8000,
'other'
),
createDocCommentNotification('document-comment', '1', '2', 7000),
createReactionNotification('root-reaction', 'root', 9000),
createReactionNotification(
'reply-reaction',
'reply-message',
10000,
'root'
),
];
for (let mask = 0; mask < 2 ** candidates.length; mask++) {
const notifications = candidates.filter((_, i) => mask & (1 << i));
expectStackMembership(notifications);
expectStackMembership([...notifications].reverse());
}
});
it('matches stack membership for empty thread and message keys', () => {
const candidates = [
createNewMessageNotification('empty-send', '', 1000),
createReplyNotification('empty-thread-reply', 'reply', '', 2000),
createMentionNotification('empty-root-mention', '', 3000),
createMentionNotification('empty-thread-mention', 'mention', 4000, ''),
];
for (let mask = 0; mask < 2 ** candidates.length; mask++) {
expectStackMembership(candidates.filter((_, i) => mask & (1 << i)));
}
});
it('does not read timestamps or sort stacks to scope channel notifications', () => {
const readTimestamp = vi.fn(() => new Date(1000).toISOString());
const notifications = [
createNewMessageNotification('root', 'root', 1000),
createReplyNotification('reply', 'reply-message', 'root', 2000),
createNewMessageNotification('standalone', 'standalone', 3000),
].map((notification) => ({
...notification,
get created_at() {
return readTimestamp();
},
}));
const scoped = scopeChannelNotificationsForEntity(
{ type: 'channel' },
notifications
);
expect(scoped.map((n) => n.id)).toEqual(['standalone']);
expect(scoped[0]).toBe(notifications[2]);
expect(readTimestamp).not.toHaveBeenCalled();
});
it('keeps root sends and mentions in their explicit thread row', () => {
const notifications = [
createNewMessageNotification('root-send', 'root', 1000),
createMentionNotification('root-mention', 'root', 2000),
createReplyNotification('reply', 'reply-message', 'root', 3000),
createMentionNotification('reply-mention', 'reply-message', 4000, 'root'),
createReplyNotification('other-reply', 'other-message', 'other', 5000),
];
expect(
scopeChannelNotificationsForEntity(
{ type: 'channel_thread', messageId: 'root' },
notifications
)
).toEqual(notifications.slice(0, 4));
expect(
scopeChannelNotificationsForEntity(
{ type: 'channel_message' },
notifications
)
).toBe(notifications);
});
it('scopes reactions on roots and replies to their thread row', () => {
const root = createReactionNotification('root-reaction', 'root', 1000);
const reply = createReactionNotification(
'reply-reaction',
'reply',
2000,
'root'
);
const other = createReactionNotification('other-reaction', 'other', 3000);
const notifications = [root, reply, other];
expect(
scopeChannelNotificationsForEntity({ type: 'channel' }, notifications)
).toEqual([]);
expect(
scopeChannelNotificationsForEntity(
{ type: 'channel_thread', messageId: 'root' },
notifications
)
).toEqual([root, reply]);
expect(
scopeChannelNotificationsForEntity(
{ type: 'channel_thread', messageId: 'other' },
notifications
)
).toEqual([other]);
expect(
scopeChannelNotificationsForEntity(
{ type: 'channel_thread', messageId: 'reply' },
notifications
)
).toEqual([]);
});
it('recomputes membership when an existing notification array changes', () => {
const notifications = [createNewMessageNotification('root', 'root', 1000)];
const entity = { type: 'channel' } as const;
expect(scopeChannelNotificationsForEntity(entity, notifications)).toEqual(
notifications
);
notifications.push(
createReplyNotification('reply', 'message', 'root', 2000)
);
expect(scopeChannelNotificationsForEntity(entity, notifications)).toEqual(
[]
);
notifications.pop();
expect(scopeChannelNotificationsForEntity(entity, notifications)).toEqual(
notifications
);
});
});
describe('stackNotifications', () => {
it('keeps reactions visible in their own stack', () => {
const reaction = createReactionNotification('reaction-1', 'message-1', 2);
const send = createNewMessageNotification('send-1', 'message-2', 1);
const stacks = stackNotifications([reaction, send]);
expect(stacks).toHaveLength(2);
expect(stacks[0]).toMatchObject({
type: 'channel_message_reaction',
notifications: [reaction],
});
});
describe('basic stacking', () => {
it('stacks multiple new messages into a single group', () => {
const notifications = [
createNewMessageNotification('n1', 'msg-1', 1000),
createNewMessageNotification('n2', 'msg-2', 2000),
createNewMessageNotification('n3', 'msg-3', 3000),
];
const result = stackNotifications(notifications);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('channel_message_send');
if (result[0].type === 'channel_message_send') {
expect(result[0].notifications).toHaveLength(3);
expect(result[0].notifications[0].id).toBe('n3'); // Most recent
}
});
it('stacks replies by threadId', () => {
const notifications = [
createReplyNotification('r1', 'msg-1', 'thread-A', 1000),
createReplyNotification('r2', 'msg-2', 'thread-A', 2000),
createReplyNotification('r3', 'msg-3', 'thread-B', 3000),
];
const result = stackNotifications(notifications);
expect(result).toHaveLength(2);
const threadAStack = result.find(
(g) =>
g.type === 'channel_message_reply' && getThreadId(g) === 'thread-A'
);
const threadBStack = result.find(
(g) =>
g.type === 'channel_message_reply' && getThreadId(g) === 'thread-B'
);
expect(threadAStack).toBeDefined();
expect(threadBStack).toBeDefined();
if (threadAStack?.type === 'channel_message_reply') {
expect(threadAStack.notifications).toHaveLength(2);
expect(getMostRecentNotification(threadAStack).id).toBe('r2'); // Most recent in thread A
}
if (threadBStack?.type === 'channel_message_reply') {
expect(threadBStack.notifications).toHaveLength(1);
expect(getMostRecentNotification(threadBStack).id).toBe('r3');
}
});
it('keeps root mentions (no threadId) as individual items', () => {
const notifications = [
createMentionNotification('m1', 'msg-1', 1000),
createMentionNotification('m2', 'msg-2', 2000),
];
const result = stackNotifications(notifications);
expect(result).toHaveLength(2);
expect(result.every((g) => g.type === 'channel_mention')).toBe(true);
});
});
describe('thread stacking', () => {
it('absorbs root send into thread stack when replies exist', () => {
const notifications = [
createNewMessageNotification('n1', 'msg-1', 1000),
createReplyNotification('r1', 'reply-1', 'msg-1', 2000),
];
const result = stackNotifications(notifications);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('channel_message_reply');
expect(getThreadId(result[0])).toBe('msg-1');
const ids = result[0].notifications.map((n) => n.id);
expect(ids).toContain('n1');
expect(ids).toContain('r1');
});
it('keeps root send in new messages stack when no thread activity', () => {
const notifications = [
createNewMessageNotification('n1', 'msg-1', 1000),
createNewMessageNotification('n2', 'msg-2', 2000),
];
const result = stackNotifications(notifications);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('channel_message_send');
expect(result[0].notifications).toHaveLength(2);
});
it('groups thread mention (with threadId) into the thread stack', () => {
const notifications = [
createReplyNotification('r1', 'reply-1', 'thread-A', 1000),
createMentionNotification('m1', 'reply-2', 2000, 'thread-A'),
];
const result = stackNotifications(notifications);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('channel_message_reply');
expect(getThreadId(result[0])).toBe('thread-A');
const ids = result[0].notifications.map((n) => n.id);
expect(ids).toContain('r1');
expect(ids).toContain('m1');
});
it('groups root send, replies, and thread mention all in one thread stack', () => {
const notifications = [
createNewMessageNotification('n1', 'msg-1', 1000),
createReplyNotification('r1', 'reply-1', 'msg-1', 2000),
createMentionNotification('m1', 'reply-2', 3000, 'msg-1'),
];
const result = stackNotifications(notifications);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('channel_message_reply');
expect(result[0].notifications).toHaveLength(3);
const ids = result[0].notifications.map((n) => n.id);
expect(ids).toContain('n1');
expect(ids).toContain('r1');
expect(ids).toContain('m1');
// Sorted most-recent first
expect(result[0].notifications[0].id).toBe('m1');
});
it('thread mention shadows reply with same messageId in same thread', () => {
const notifications = [
createReplyNotification('r1', 'reply-1', 'thread-A', 1000),
createMentionNotification('m1', 'reply-1', 2000, 'thread-A'),
];
const result = stackNotifications(notifications);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('channel_message_reply');
const ids = result[0].notifications.map((n) => n.id);
expect(ids).toContain('m1');
expect(ids).not.toContain('r1'); // shadowed by the thread mention
});
it('root mention for thread root merges into thread stack', () => {
const notifications = [
createNewMessageNotification('n1', 'msg-1', 1000),
createReplyNotification('r1', 'reply-1', 'msg-1', 2000),
createMentionNotification('m1', 'msg-1', 3000), // root mention, no threadId
];
const result = stackNotifications(notifications);
// All in one thread stack: m1 + r1 (n1 shadowed by root mention m1)
expect(result).toHaveLength(1);
expect(result[0].type).toBe('channel_message_reply');
expect(getThreadId(result[0])).toBe('msg-1');
const ids = result[0].notifications.map((n) => n.id);
expect(ids).toContain('m1');
expect(ids).toContain('r1');
expect(ids).not.toContain('n1'); // shadowed by root mention
});
it('root mention with no thread activity stays as individual stack', () => {
const notifications = [createMentionNotification('m1', 'msg-1', 1000)];
const result = stackNotifications(notifications);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('channel_mention');
});
});
describe('mention shadowing', () => {
it('shadows new message notification when mention exists for same messageId', () => {
const notifications = [
createNewMessageNotification('n1', 'msg-1', 1000),
createMentionNotification('m1', 'msg-1', 2000), // Same messageId
];
const result = stackNotifications(notifications);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('channel_mention');
});
it('shadows reply notification when mention exists for same messageId', () => {
const notifications = [
createReplyNotification('r1', 'msg-1', 'thread-A', 1000),
createMentionNotification('m1', 'msg-1', 2000), // Same messageId
];
const result = stackNotifications(notifications);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('channel_mention');
});
it('keeps non-shadowed notifications alongside mentions', () => {
const notifications = [
createNewMessageNotification('n1', 'msg-1', 1000),
createNewMessageNotification('n2', 'msg-2', 2000),
createMentionNotification('m1', 'msg-1', 3000), // Shadows n1
];
const result = stackNotifications(notifications);
expect(result).toHaveLength(2); // One mention + one new_messages stack
const mentionGroup = result.find((g) => g.type === 'channel_mention');
const newMessagesGroup = result.find(
(g) => g.type === 'channel_message_send'
);
expect(mentionGroup).toBeDefined();
expect(newMessagesGroup).toBeDefined();
if (newMessagesGroup?.type === 'channel_message_send') {
expect(newMessagesGroup.notifications).toHaveLength(1);
expect(newMessagesGroup.notifications[0].id).toBe('n2'); // n1 was shadowed
}
});
});
describe('sorting', () => {
it('sorts stacks by newest notification first', () => {
const notifications = [
createNewMessageNotification('n1', 'msg-1', 5000), // Most recent overall
createMentionNotification('m1', 'msg-2', 1000), // Oldest
];
const result = stackNotifications(notifications);
expect(result).toHaveLength(2);
expect(result[0].type).toBe('channel_message_send'); // Most recent first
expect(result[1].type).toBe('channel_mention');
});
it('sorts non-mention groups by timestamp descending', () => {
const notifications = [
createNewMessageNotification('n1', 'msg-1', 1000),
createReplyNotification('r1', 'msg-2', 'thread-A', 3000),
];
const result = stackNotifications(notifications);
expect(result).toHaveLength(2);
expect(result[0].type).toBe('channel_message_reply'); // More recent
expect(result[1].type).toBe('channel_message_send');
});
});
describe('mixed notifications', () => {
it('handles complex scenario with all notification types', () => {
const notifications = [
createNewMessageNotification('n1', 'msg-1', 1000),
createNewMessageNotification('n2', 'msg-2', 2000),
createReplyNotification('r1', 'msg-3', 'thread-A', 3000),
createReplyNotification('r2', 'msg-4', 'thread-A', 4000),
createReplyNotification('r3', 'msg-5', 'thread-B', 5000),
createMentionNotification('m1', 'msg-6', 6000),
createMentionNotification('m2', 'msg-2', 7000), // Shadows n2
];
const result = stackNotifications(notifications);
// Should have: 2 mentions, 1 new_messages stack (only n1), 2 replies stacks
expect(result).toHaveLength(5);
const mentions = result.filter((g) => g.type === 'channel_mention');
const newMessages = result.filter(
(g) => g.type === 'channel_message_send'
);
const replies = result.filter((g) => g.type === 'channel_message_reply');
expect(mentions).toHaveLength(2);
expect(newMessages).toHaveLength(1);
expect(replies).toHaveLength(2);
// Check ordering by recency (mentions are newest in this scenario)
expect(result[0].type).toBe('channel_mention');
expect(result[1].type).toBe('channel_mention');
// Check new messages stack only has n1 (n2 was shadowed)
if (newMessages[0].type === 'channel_message_send') {
expect(newMessages[0].notifications).toHaveLength(1);
expect(newMessages[0].notifications[0].id).toBe('n1');
}
});
it('returns empty array for empty input', () => {
const result = stackNotifications([]);
expect(result).toEqual([]);
});
});
});
describe('getMostRecentNotification', () => {
it('returns most recent from new_messages stack', () => {
const notifications = [
createNewMessageNotification('n1', 'msg-1', 1000),
createNewMessageNotification('n2', 'msg-2', 2000),
];
const stacked = stackNotifications(notifications);
const notification = getMostRecentNotification(stacked[0]);
expect(notification.id).toBe('n2');
});
it('returns most recent from replies stack', () => {
const notifications = [
createReplyNotification('r1', 'msg-1', 'thread-A', 1000),
createReplyNotification('r2', 'msg-2', 'thread-A', 2000),
];
const stacked = stackNotifications(notifications);
const notification = getMostRecentNotification(stacked[0]);
expect(notification.id).toBe('r2');
});
it('returns the notification from mention group', () => {
const notifications = [createMentionNotification('m1', 'msg-1', 1000)];
const stacked = stackNotifications(notifications);
const notification = getMostRecentNotification(stacked[0]);
expect(notification.id).toBe('m1');
});
});
describe('getAllNotificationsFromGroup', () => {
it('returns all notifications from new_messages stack', () => {
const notifications = [
createNewMessageNotification('n1', 'msg-1', 1000),
createNewMessageNotification('n2', 'msg-2', 2000),
createNewMessageNotification('n3', 'msg-3', 3000),
];
const stacked = stackNotifications(notifications);
const allNotifications = getAllNotificationsFromGroup(stacked[0]);
expect(allNotifications).toHaveLength(3);
expect(allNotifications.map((n) => n.id).sort()).toEqual([
'n1',
'n2',
'n3',
]);
});
it('returns all notifications from replies stack', () => {
const notifications = [
createReplyNotification('r1', 'msg-1', 'thread-A', 1000),
createReplyNotification('r2', 'msg-2', 'thread-A', 2000),
];
const stacked = stackNotifications(notifications);
const allNotifications = getAllNotificationsFromGroup(stacked[0]);
expect(allNotifications).toHaveLength(2);
expect(allNotifications.map((n) => n.id).sort()).toEqual(['r1', 'r2']);
});
it('returns single notification from mention group', () => {
const notifications = [createMentionNotification('m1', 'msg-1', 1000)];
const stacked = stackNotifications(notifications);
const allNotifications = getAllNotificationsFromGroup(stacked[0]);
expect(allNotifications).toHaveLength(1);
expect(allNotifications[0].id).toBe('m1');
});
});
const rootId = '01990000-0000-7000-8000-000000000001';
const replyId = '01990000-0000-7000-8000-000000000002';
const anotherRootId = '01990000-0000-7000-8000-000000000003';
describe('stackNotifications — entity discussions', () => {
it('groups new roots and keeps replies in their discussion', () => {
const result = stackNotifications([
createDocCommentNotification('root', rootId, rootId, 1000),
createDocCommentNotification(
'another',
anotherRootId,
anotherRootId,
2000
),
createDocReplyNotification('reply', replyId, rootId, 3000),
]);
expect(result).toHaveLength(2);
expect(result[0].type).toBe('replied_to_document_comment_thread');
expect(getThreadId(result[0])).toBe(rootId);
expect(result[0].notifications.map((n) => n.id)).toEqual(['reply', 'root']);
expect(result[1].type).toBe('commented_on_document');
expect(getThreadId(result[1])).toBe('');
});
it('identifies a lone owner notification for a reply by its shared root ID', () => {
const result = stackNotifications([
createDocCommentNotification('reply', replyId, rootId, 1000),
]);
expect(result[0].type).toBe('replied_to_document_comment_thread');
expect(getThreadId(result[0])).toBe(rootId);
});
it('bundles standalone roots', () => {
const result = stackNotifications([
createDocCommentNotification('one', rootId, rootId, 1000),
createDocCommentNotification('two', anotherRootId, anotherRootId, 2000),
]);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('commented_on_document');
expect(result[0].notifications.map((n) => n.id)).toEqual(['two', 'one']);
});
it('keeps a standalone mention separate and shadows the same message event', () => {
const result = stackNotifications([
createDocCommentNotification('root', rootId, rootId, 1000),
createDocMentionNotification('mention', rootId, rootId, 2000),
]);
expect(result).toHaveLength(1);
expect(result[0].type).toBe('mentioned_in_document_comment');
expect(result[0].notifications.map((n) => n.id)).toEqual(['mention']);
});
it('combines a root mention and replies, with mention precedence on replies', () => {
const result = stackNotifications([
createDocMentionNotification('root-mention', rootId, rootId, 1000),
createDocReplyNotification('reply', replyId, rootId, 2000),
createDocMentionNotification('reply-mention', replyId, rootId, 3000),
]);
expect(result).toHaveLength(1);
expect(getThreadId(result[0])).toBe(rootId);
expect(result[0].notifications.map((n) => n.id)).toEqual([
'reply-mention',
'root-mention',
]);
});
it('keeps channel and document notifications separate', () => {
const result = stackNotifications([
createNewMessageNotification('channel', 'msg', 1000),
createDocCommentNotification('document', rootId, rootId, 2000),
]);
expect(result).toHaveLength(2);
expect(result.map((stack) => stack.type)).toEqual([
'commented_on_document',
'channel_message_send',
]);
});
});
describe('CRM discussion notifications', () => {
function crmNotification(
id: string,
recordId: string,
threadId: string,
createdAt: number
): UnifiedNotification {
return {
...createBaseNotification(id, createdAt, {
tag: 'crm_discussion',
content: {
recordName: 'Acme',
reason: 'owner',
messageId: `${id}-message`,
threadId,
text: `Comment ${id}`,
},
}),
entity_id: recordId,
entity_type: 'crm_company',
};
}
it('stacks one group per record thread', () => {
const groups = stackNotifications([
crmNotification('a', 'company-1', 'thread-1', 1000),
crmNotification('b', 'company-1', 'thread-1', 2000),
crmNotification('c', 'company-1', 'thread-2', 3000),
crmNotification('d', 'company-2', 'thread-1', 4000),
]);
expect(groups.map((group) => group.type)).toEqual([
'crm_discussion',
'crm_discussion',
'crm_discussion',
]);
expect(
groups.map((group) => group.notifications.map((n) => n.id).sort())
).toEqual([['d'], ['c'], ['a', 'b']]);
expect(getThreadId(groups[2])).toBe('thread-1');
});
});