Files
upstream-mirror 30810eecd7
Some checks failed
Create issues from TODOs / todos (push) Failing after 3s
Close inactive issues / close-issues (push) Has been cancelled
Deploy GitHub Pages / deploy-preview (push) Has been cancelled
CI / ⚙️ Variables Setup (push) Has been cancelled
CI / ⚙️ Test Guard (push) Has been cancelled
CI / 🚀 Notify external services - draft (push) Has been cancelled
CI / 📦 Build Packages (push) Has been cancelled
CI / 📦 Meteor Build (coverage) (push) Has been cancelled
CI / 📦 Meteor Build (production) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [account-service presence-service omnichannel-transcript-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [authorization-service queue-worker-service ddp-streamer-service], fips) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Build Docker (amd64, [rocketchat], fips) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [account-service presence-service omnichannel-transcript-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [authorization-service queue-worker-service ddp-streamer-service], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], ${{ (github.event_name != 'release' && github.ref != 'refs/heads/develop') && 'coverage' || 'production' }}) (push) Has been cancelled
CI / 🚢 Build Docker (arm64, [rocketchat], coverage) (push) Has been cancelled
CI / 🚢 Publish Docker Images (ghcr.io) (push) Has been cancelled
CI / 📦 Track Image Sizes (push) Has been cancelled
CI / 🔎 Code Check - Action Lint (push) Has been cancelled
CI / 🔎 Code Check (push) Has been cancelled
CI / 🔨 Test Storybook (push) Has been cancelled
CI / 🔨 Test Unit (push) Has been cancelled
CI / 🔨 Test API (CE) (push) Has been cancelled
CI / 🔨 Test API Livechat (CE) (push) Has been cancelled
CI / 🔨 Test UI (CE) (push) Has been cancelled
CI / 🔨 Test API (EE) (push) Has been cancelled
CI / 🔨 Test API Livechat (EE) (push) Has been cancelled
CI / 🔨 Test API Apps (deno-runtime - EE) (push) Has been cancelled
CI / 🔨 Test UI (EE) (push) Has been cancelled
CI / 🔨 Test API (FIPS) (push) Has been cancelled
CI / 🔨 Test API Livechat (FIPS) (push) Has been cancelled
CI / 🔨 Test UI (FIPS) (push) Has been cancelled
CI / 🔨 Test Federation Matrix (push) Has been cancelled
CI / 📊 Report Coverage (push) Has been cancelled
CI / ✅ Tests Done (push) Has been cancelled
CI / 🚀 Publish build assets (push) Has been cancelled
CI / 🚀 Publish Docker Images (DockerHub) (push) Has been cancelled
CI / 🚀 Notify external services (push) Has been cancelled
CI / Update Version Durability (push) Has been cancelled
Release / Release (push) Has been cancelled
Auto-close duplicate issues / auto-close-duplicates (push) Failing after 1s
Code scanning - action / CodeQL-Build (push) Failing after 2s
snapshot of https://github.com/RocketChat/Rocket.Chat.git (develop, 2026-09-16; blobs > 131072B omitted)
2026-09-16 01:21:02 +05:00

315 lines
11 KiB
JavaScript

import { expect } from 'chai';
import { MentionsParser } from '../../../../app/mentions/lib/MentionsParser';
let mentionsParser;
beforeEach(() => {
mentionsParser = new MentionsParser({
pattern: () => '[0-9a-zA-Z-_.]+',
me: () => 'me',
});
});
describe('Mention', () => {
describe('getUserMentions', () => {
describe('for simple text, no mentions', () => {
const result = [];
['#rocket.cat', 'hello rocket.cat how are you?'].forEach((text) => {
it(`should return "${JSON.stringify(result)}" from "${text}"`, () => {
expect(result).to.be.deep.equal(mentionsParser.getUserMentions(text));
});
});
});
describe('for one user', () => {
const result = ['@rocket.cat'];
[
'@rocket.cat',
' @rocket.cat ',
'hello @rocket.cat',
// 'hello,@rocket.cat', // this test case is ignored since is not compatible with the message box behavior
'@rocket.cat, hello',
'@rocket.cat,hello',
'hello @rocket.cat how are you?',
].forEach((text) => {
it(`should return "${JSON.stringify(result)}" from "${text}"`, () => {
expect(result).to.be.deep.equal(mentionsParser.getUserMentions(text));
});
});
it.skip('should return without the "." from "@rocket.cat."', () => {
expect(result).to.be.deep.equal(mentionsParser.getUserMentions('@rocket.cat.'));
});
it.skip('should return without the "_" from "@rocket.cat_"', () => {
expect(result).to.be.deep.equal(mentionsParser.getUserMentions('@rocket.cat_'));
});
it.skip('should return without the "-" from "@rocket.cat-"', () => {
expect(result).to.be.deep.equal(mentionsParser.getUserMentions('@rocket.cat-'));
});
});
describe('for two users', () => {
const result = ['@rocket.cat', '@all'];
[
'@rocket.cat @all',
' @rocket.cat @all ',
'hello @rocket.cat and @all',
'@rocket.cat, hello @all',
'hello @rocket.cat and @all how are you?',
].forEach((text) => {
it(`should return "${JSON.stringify(result)}" from "${text}"`, () => {
expect(result).to.be.deep.equal(mentionsParser.getUserMentions(text));
});
});
});
});
describe('getChannelMentions', () => {
describe('for simple text, no mentions', () => {
const result = [];
['@rocket.cat', 'hello rocket.cat how are you?'].forEach((text) => {
it(`should return "${JSON.stringify(result)}" from "${text}"`, () => {
expect(result).to.be.deep.equal(mentionsParser.getChannelMentions(text));
});
});
});
describe('for one channel', () => {
const result = ['#general'];
['#general', ' #general ', 'hello #general', '#general, hello', 'hello #general, how are you?'].forEach((text) => {
it(`should return "${JSON.stringify(result)}" from "${text}"`, () => {
expect(result).to.be.deep.equal(mentionsParser.getChannelMentions(text));
});
});
it.skip('should return without the "." from "#general."', () => {
expect(result).to.be.deep.equal(mentionsParser.getUserMentions('#general.'));
});
it.skip('should return without the "_" from "#general_"', () => {
expect(result).to.be.deep.equal(mentionsParser.getUserMentions('#general_'));
});
it.skip('should return without the "-" from "#general."', () => {
expect(result).to.be.deep.equal(mentionsParser.getUserMentions('#general-'));
});
});
describe('for two channels', () => {
const result = ['#general', '#other'];
[
'#general #other',
' #general #other',
'hello #general and #other',
'#general, hello #other',
'hello #general #other, how are you?',
].forEach((text) => {
it(`should return "${JSON.stringify(result)}" from "${text}"`, () => {
expect(result).to.be.deep.equal(mentionsParser.getChannelMentions(text));
});
});
});
describe('for url with fragments', () => {
const result = [];
['http://localhost/#general'].forEach((text) => {
it(`should return nothing from "${text}"`, () => {
expect(result).to.be.deep.equal(mentionsParser.getChannelMentions(text));
});
});
});
describe('for messages with url and channels', () => {
const result = ['#general'];
['http://localhost/#general #general'].forEach((text) => {
it(`should return "${JSON.stringify(result)}" from "${text}"`, () => {
expect(result).to.be.deep.equal(mentionsParser.getChannelMentions(text));
});
});
});
});
});
const message = {
mentions: [
{ username: 'rocket.cat', name: 'Rocket.Cat' },
{ username: 'admin', name: 'Admin' },
{ username: 'me', name: 'Me' },
{ username: 'specialchars', name: '<img onerror=alert(hello)>' },
],
channels: [
{ name: 'general', _id: '42' },
{ name: 'rocket.cat', _id: '169' },
],
};
describe('replace methods', () => {
describe('replaceUsers', () => {
it('should render for @all', () => {
const result = mentionsParser.replaceUsers('@all', message, 'me');
expect(result).to.be.equal('<a class="mention-link mention-link--all mention-link--group" data-group="all">all</a>');
});
const str2 = 'rocket.cat';
it(`should render for "@${str2}"`, () => {
const result = mentionsParser.replaceUsers(`@${str2}`, message, 'me');
expect(result).to.be.equal(`<a class="mention-link mention-link--user" data-username="${str2}" title="${str2}">${str2}</a>`);
});
it(`should render for "hello ${str2}"`, () => {
const result = mentionsParser.replaceUsers(`hello @${str2}`, message, 'me');
expect(result).to.be.equal(`hello <a class="mention-link mention-link--user" data-username="${str2}" title="${str2}">${str2}</a>`);
});
it('should render for unknow/private user "hello @unknow"', () => {
const result = mentionsParser.replaceUsers('hello @unknow', message, 'me');
expect(result).to.be.equal('hello @unknow');
});
it('should render for me', () => {
const result = mentionsParser.replaceUsers('hello @me', message, 'me');
expect(result).to.be.equal('hello <a class="mention-link mention-link--me mention-link--user" data-username="me" title="me">me</a>');
});
});
describe('replaceUsers (RealNames)', () => {
beforeEach(() => {
mentionsParser.useRealName = () => true;
});
it('should render for @all', () => {
const result = mentionsParser.replaceUsers('@all', message, 'me');
expect(result).to.be.equal('<a class="mention-link mention-link--all mention-link--group" data-group="all">all</a>');
});
const str2 = 'rocket.cat';
const str2Name = 'Rocket.Cat';
it(`should render for "@${str2}"`, () => {
const result = mentionsParser.replaceUsers(`@${str2}`, message, 'me');
expect(result).to.be.equal(`<a class="mention-link mention-link--user" data-username="${str2}" title="${str2}">${str2Name}</a>`);
});
it(`should render for "hello @${str2}"`, () => {
const result = mentionsParser.replaceUsers(`hello @${str2}`, message, 'me');
expect(result).to.be.equal(
`hello <a class="mention-link mention-link--user" data-username="${str2}" title="${str2}">${str2Name}</a>`,
);
});
const specialchars = 'specialchars';
const specialcharsName = '&lt;img onerror=alert(hello)&gt;';
it(`should escape special characters in "hello @${specialchars}"`, () => {
const result = mentionsParser.replaceUsers(`hello @${specialchars}`, message, 'me');
expect(result).to.be.equal(
`hello <a class="mention-link mention-link--user" data-username="${specialchars}" title="${specialchars}">${specialcharsName}</a>`,
);
});
it(`should render for "hello<br>@${str2} <br>"`, () => {
const result = mentionsParser.replaceUsers(`hello<br>@${str2} <br>`, message, 'me');
expect(result).to.be.equal(
`hello<br><a class="mention-link mention-link--user" data-username="${str2}" title="${str2}">${str2Name}</a> <br>`,
);
});
it('should render for unknow/private user "hello @unknow"', () => {
const result = mentionsParser.replaceUsers('hello @unknow', message, 'me');
expect(result).to.be.equal('hello @unknow');
});
it('should render for me', () => {
const result = mentionsParser.replaceUsers('hello @me', message, 'me');
expect(result).to.be.equal('hello <a class="mention-link mention-link--me mention-link--user" data-username="me" title="me">Me</a>');
});
});
describe('replaceChannels', () => {
it('should render for #general', () => {
const result = mentionsParser.replaceChannels('#general', message);
expect('<).to.be.equal(class="mention-link mention-link--room" data-channel="42">#general</a>', result);
});
const str2 = '#rocket.cat';
it(`should render for ${str2}`, () => {
const result = mentionsParser.replaceChannels(str2, message);
expect(result).to.be.equal(`<a class="mention-link mention-link--room" data-channel="169">${str2}</a>`);
});
it(`should render for "hello ${str2}"`, () => {
const result = mentionsParser.replaceChannels(`hello ${str2}`, message);
expect(result).to.be.equal(`hello <a class="mention-link mention-link--room" data-channel="169">${str2}</a>`);
});
it('should render for unknow/private channel "hello #unknow"', () => {
const result = mentionsParser.replaceChannels('hello #unknow', message);
expect(result).to.be.equal('hello #unknow');
});
});
describe('parse all', () => {
it('should render for #general', () => {
message.html = '#general';
const result = mentionsParser.parse(message, 'me');
expect(result.html).to.be.equal('<a class="mention-link mention-link--room" data-channel="42">#general</a>');
});
it('should render for "#general and @rocket.cat', () => {
message.html = '#general and @rocket.cat';
const result = mentionsParser.parse(message, 'me');
expect(result.html).to.be.equal(
'<a class="mention-link mention-link--room" data-channel="42">#general</a> and <a class="mention-link mention-link--user" data-username="rocket.cat" title="rocket.cat">rocket.cat</a>',
);
});
it('should render for "', () => {
message.html = '';
const result = mentionsParser.parse(message, 'me');
expect(result.html).to.be.equal('');
});
it('should render for "simple text', () => {
message.html = 'simple text';
const result = mentionsParser.parse(message, 'me');
expect(result.html).to.be.equal('simple text');
});
});
describe('parse all (RealNames)', () => {
beforeEach(() => {
mentionsParser.useRealName = () => true;
});
it('should render for #general', () => {
message.html = '#general';
const result = mentionsParser.parse(message, 'me');
expect(result.html).to.be.equal('<a class="mention-link mention-link--room" data-channel="42">#general</a>');
});
it('should render for "#general and @rocket.cat', () => {
message.html = '#general and @rocket.cat';
const result = mentionsParser.parse(message, 'me');
expect(result.html).to.be.equal(
'<a class="mention-link mention-link--room" data-channel="42">#general</a> and <a class="mention-link mention-link--user" data-username="rocket.cat" title="rocket.cat">Rocket.Cat</a>',
);
});
it('should render for "', () => {
message.html = '';
const result = mentionsParser.parse(message, 'me');
expect(result.html).to.be.equal('');
});
it('should render for "simple text', () => {
message.html = 'simple text';
const result = mentionsParser.parse(message, 'me');
expect(result.html).to.be.equal('simple text');
});
});
});