import { expect, test } from 'vitest';

import { parseMentions, reconstructContent } from './mentions';

test('parseMentions', () => {
  expect(parseMentions('@john', new Map([['john', 'John']]))).toEqual({
    content: `John`,
    userMentions: [{ handle: 'john', start: 0, end: 4, display_name: 'John' }],
  });
});

test('reconstructContent', () => {
  const mention1 = [{ handle: 'john', start: 0, end: 4, display_name: 'John' }];
  expect(reconstructContent(`John what's up man`, mention1)).toEqual({
    content: `@john what's up man`,
    mentionsUsedMap: new Map([['john', 'John']]),
  });

  const mention2 = [
    { handle: 'john', start: 0, end: 4, display_name: 'John' },
    { handle: 'john', start: 9, end: 13, display_name: 'John' },
  ];
  expect(reconstructContent(`John hey Johnas yo John`, mention2)).toEqual({
    content: `@john hey @johnas yo John`,
    mentionsUsedMap: new Map([['john', 'John']]),
  });

  const mention3 = [{ handle: 'a', start: 0, end: 4, display_name: 'John' }];
  expect(reconstructContent(`John hi`, mention3)).toEqual({
    content: `@a hi`,
    mentionsUsedMap: new Map([['a', 'John']]),
  });

  const mention4 = [{ handle: 'john', start: 0, end: 1, display_name: 'a' }];
  expect(reconstructContent(`a hi`, mention4)).toEqual({
    content: `@john hi`,
    mentionsUsedMap: new Map([['john', 'a']]),
  });
});
