// @vitest-environment happy-dom
import { describe, expect, it } from 'vitest';
import { Editor } from '@tiptap/core';
import StarterKit from '@tiptap/starter-kit';
import { CommentMark } from '../../components/CommentMark.js';
function newEditor(initialHtml = '<p>hello world</p>') {
    return new Editor({
        extensions: [StarterKit.configure({ history: false }), CommentMark],
        content: initialHtml,
    });
}
describe('CommentMark', () => {
    it('registers under the name "comment"', () => {
        const ed = newEditor();
        expect(ed.extensionManager.extensions.some((e) => e.name === 'comment')).toBe(true);
        ed.destroy();
    });
    it('setComment applies the mark across the current selection', () => {
        const ed = newEditor('<p>hello world</p>');
        ed.commands.setTextSelection({ from: 1, to: 6 }); // "hello"
        const ok = ed.chain().setComment({ commentId: 'c-1' }).run();
        expect(ok).toBe(true);
        expect(ed.isActive('comment', { commentId: 'c-1' })).toBe(true);
        // The rendered HTML should now contain the data attribute.
        const html = ed.getHTML();
        expect(html).toContain('data-comment-id="c-1"');
        expect(html).toContain('hello');
        ed.destroy();
    });
    it('setComment rejects a missing commentId', () => {
        const ed = newEditor();
        ed.commands.setTextSelection({ from: 1, to: 6 });
        const ok = ed.chain().setComment({ commentId: '' }).run();
        expect(ok).toBe(false);
        expect(ed.getHTML()).not.toContain('data-comment-id');
        ed.destroy();
    });
    it('updateComment swaps the commentId across the mark range', () => {
        const ed = newEditor('<p>hello world</p>');
        ed.commands.setTextSelection({ from: 1, to: 6 });
        ed.chain().setComment({ commentId: 'c-1' }).run();
        // Re-select the same range so updateAttributes can find the mark to
        // mutate. TipTap's updateAttributes applies to the marks that fall
        // within the selection — a collapsed cursor at the boundary would
        // not match this mark.
        ed.commands.setTextSelection({ from: 1, to: 6 });
        const ok = ed.chain().updateComment({ commentId: 'c-2' }).run();
        expect(ok).toBe(true);
        expect(ed.getHTML()).toContain('data-comment-id="c-2"');
        ed.destroy();
    });
    it('unsetComment removes the mark from the selection', () => {
        const ed = newEditor('<p>hello world</p>');
        ed.commands.setTextSelection({ from: 1, to: 6 });
        ed.chain().setComment({ commentId: 'c-1' }).run();
        expect(ed.getHTML()).toContain('data-comment-id');
        ed.commands.setTextSelection({ from: 1, to: 6 });
        ed.chain().unsetComment().run();
        expect(ed.getHTML()).not.toContain('data-comment-id');
        ed.destroy();
    });
    it('parses an existing comment span on load', () => {
        const ed = newEditor('<p><span data-comment-id="c-7">hello</span> world</p>');
        expect(ed.getHTML()).toContain('data-comment-id="c-7"');
        ed.destroy();
    });
    it('applies the default CSS class on every rendered span', () => {
        const ed = newEditor('<p>hello world</p>');
        ed.commands.setTextSelection({ from: 1, to: 6 });
        ed.chain().setComment({ commentId: 'c-9' }).run();
        expect(ed.getHTML()).toMatch(/class="actuate-comment"/);
        ed.destroy();
    });
    it('supports overlapping comments (non-spanning behaviour)', () => {
        // Set a comment over chars 1..6 ("hello").
        const ed = newEditor('<p>hello world</p>');
        ed.commands.setTextSelection({ from: 1, to: 6 });
        ed.chain().setComment({ commentId: 'c-a' }).run();
        // Now apply a second comment over chars 4..8 ("o wo"). TipTap will
        // split the marks — that's the standard mark-overlap behaviour.
        ed.commands.setTextSelection({ from: 4, to: 8 });
        ed.chain().setComment({ commentId: 'c-b' }).run();
        const html = ed.getHTML();
        expect(html).toContain('data-comment-id="c-a"');
        expect(html).toContain('data-comment-id="c-b"');
        ed.destroy();
    });
});
//# sourceMappingURL=comment-mark.test.js.map