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
207 lines
7.6 KiB
TypeScript
207 lines
7.6 KiB
TypeScript
import type { TFunction } from 'i18next';
|
|
|
|
import { getMessagePreview } from './getMessagePreview';
|
|
import { normalizeMessagePreview } from './normalizeMessagePreview';
|
|
import { createFakeSubscription, createFakeMessage } from '../../../../tests/mocks/data';
|
|
|
|
jest.mock('./normalizeMessagePreview');
|
|
|
|
const mockNormalizeMessagePreview = normalizeMessagePreview as jest.MockedFunction<typeof normalizeMessagePreview>;
|
|
|
|
describe('getMessagePreview', () => {
|
|
const mockT = jest.fn((key: string) => key) as unknown as TFunction;
|
|
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
mockNormalizeMessagePreview.mockReturnValue('normalized message');
|
|
});
|
|
|
|
describe('when room has a draft', () => {
|
|
it('should return "Draft: content" taking precedence over the last message', () => {
|
|
const room = createFakeSubscription({ draft: 'unfinished text' });
|
|
const message = createFakeMessage();
|
|
|
|
const result = getMessagePreview(room, message, mockT);
|
|
|
|
expect(result).toBe('Draft: unfinished text');
|
|
expect(mockT).toHaveBeenCalledWith('Draft');
|
|
expect(mockNormalizeMessagePreview).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should escape HTML in the draft content', () => {
|
|
const room = createFakeSubscription({ draft: '<img src=x onerror=alert(1)>' });
|
|
|
|
const result = getMessagePreview(room, undefined, mockT);
|
|
|
|
expect(result).toBe('Draft: <img src=x onerror=alert(1)>');
|
|
expect(mockNormalizeMessagePreview).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should fall back to a thread-composer draft when there is no main draft', () => {
|
|
const room = createFakeSubscription({ draft: undefined, threadDrafts: { 'thread-id': 'unfinished thread reply' } });
|
|
const message = createFakeMessage();
|
|
|
|
const result = getMessagePreview(room, message, mockT);
|
|
|
|
expect(result).toBe('Draft: unfinished thread reply');
|
|
expect(mockNormalizeMessagePreview).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should prefer the main draft over thread-composer drafts', () => {
|
|
const room = createFakeSubscription({ draft: 'main draft', threadDrafts: { 'thread-id': 'thread draft' } });
|
|
|
|
const result = getMessagePreview(room, undefined, mockT);
|
|
|
|
expect(result).toBe('Draft: main draft');
|
|
});
|
|
});
|
|
|
|
describe('when lastMessage is undefined', () => {
|
|
it('should return translation for No_messages_yet', () => {
|
|
const room = createFakeSubscription();
|
|
const result = getMessagePreview(room, undefined, mockT);
|
|
|
|
expect(result).toBe('No_messages_yet');
|
|
expect(mockT).toHaveBeenCalledWith('No_messages_yet');
|
|
expect(mockNormalizeMessagePreview).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('when message is a video conference message', () => {
|
|
it('should return translation for Call_started', () => {
|
|
const room = createFakeSubscription();
|
|
const message = createFakeMessage({ t: 'videoconf' });
|
|
const result = getMessagePreview(room, message, mockT);
|
|
|
|
expect(result).toBe('Call_started');
|
|
expect(mockT).toHaveBeenCalledWith('Call_started');
|
|
expect(mockNormalizeMessagePreview).not.toHaveBeenCalled();
|
|
});
|
|
});
|
|
|
|
describe('when message is E2EE', () => {
|
|
it('should return encrypted message preview when e2e is not done', () => {
|
|
const room = createFakeSubscription();
|
|
const message = createFakeMessage({ t: 'e2e', e2e: 'pending' });
|
|
const result = getMessagePreview(room, message, mockT);
|
|
|
|
expect(result).toBe('Encrypted_message_preview_unavailable');
|
|
expect(mockT).toHaveBeenCalledWith('Encrypted_message_preview_unavailable');
|
|
expect(mockNormalizeMessagePreview).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it('should proceed to normalization when e2e is done', () => {
|
|
const room = createFakeSubscription({ t: 'c' });
|
|
const message = createFakeMessage({ t: 'e2e', e2e: 'done', u: { _id: 'user-id', username: 'testuser', name: 'Test User' } });
|
|
mockNormalizeMessagePreview.mockReturnValue('decrypted message');
|
|
|
|
const result = getMessagePreview(room, message, mockT);
|
|
|
|
expect(result).toBe('Test User: decrypted message');
|
|
expect(mockNormalizeMessagePreview).toHaveBeenCalledWith(message, mockT);
|
|
});
|
|
});
|
|
|
|
describe('when message has no user', () => {
|
|
it('should return normalized message directly', () => {
|
|
const room = createFakeSubscription();
|
|
const message = createFakeMessage({ u: undefined });
|
|
mockNormalizeMessagePreview.mockReturnValue('normalized message');
|
|
|
|
const result = getMessagePreview(room, message, mockT);
|
|
|
|
expect(result).toBe('normalized message');
|
|
expect(mockNormalizeMessagePreview).toHaveBeenCalledWith(message, mockT);
|
|
});
|
|
});
|
|
|
|
describe('when message is from current user', () => {
|
|
it('should return "You: normalized message" when usernames match', () => {
|
|
const room = createFakeSubscription({ u: { _id: 'user-id', username: 'testuser', name: 'Test User' } });
|
|
const message = createFakeMessage({ u: { _id: 'user-id', username: 'testuser', name: 'Test User' } });
|
|
mockNormalizeMessagePreview.mockReturnValue('my message');
|
|
|
|
const result = getMessagePreview(room, message, mockT);
|
|
|
|
expect(result).toBe('You: my message');
|
|
expect(mockT).toHaveBeenCalledWith('You');
|
|
expect(mockNormalizeMessagePreview).toHaveBeenCalledWith(message, mockT);
|
|
});
|
|
});
|
|
|
|
describe('when room is a direct message room', () => {
|
|
it('should return normalized message without sender name for single DM', () => {
|
|
const room = createFakeSubscription({
|
|
t: 'd',
|
|
uids: ['user1', 'user2'],
|
|
});
|
|
const message = createFakeMessage({
|
|
u: { _id: 'other-user-id', username: 'otheruser', name: 'Other User' },
|
|
});
|
|
mockNormalizeMessagePreview.mockReturnValue('direct message');
|
|
|
|
const result = getMessagePreview(room, message, mockT);
|
|
|
|
expect(result).toBe('direct message');
|
|
expect(mockNormalizeMessagePreview).toHaveBeenCalledWith(message, mockT);
|
|
});
|
|
|
|
it('should return sender name and message for multiple DM', () => {
|
|
const room = createFakeSubscription({
|
|
t: 'd',
|
|
uids: ['user1', 'user2', 'user3'],
|
|
});
|
|
const message = createFakeMessage({
|
|
u: { _id: 'other-user-id', username: 'otheruser', name: 'Other User' },
|
|
});
|
|
mockNormalizeMessagePreview.mockReturnValue('group message');
|
|
|
|
const result = getMessagePreview(room, message, mockT);
|
|
|
|
expect(result).toBe('Other User: group message');
|
|
expect(mockNormalizeMessagePreview).toHaveBeenCalledWith(message, mockT);
|
|
});
|
|
});
|
|
|
|
describe('when room is a group or channel', () => {
|
|
it('should return sender name and message when user has name', () => {
|
|
const room = createFakeSubscription({ t: 'c' });
|
|
const message = createFakeMessage({
|
|
u: { _id: 'other-user-id', username: 'otheruser', name: 'Other User' },
|
|
});
|
|
mockNormalizeMessagePreview.mockReturnValue('channel message');
|
|
|
|
const result = getMessagePreview(room, message, mockT);
|
|
|
|
expect(result).toBe('Other User: channel message');
|
|
expect(mockNormalizeMessagePreview).toHaveBeenCalledWith(message, mockT);
|
|
});
|
|
|
|
it('should return username and message when user has no name', () => {
|
|
const room = createFakeSubscription({ t: 'p' });
|
|
const message = createFakeMessage({
|
|
u: { _id: 'other-user-id', username: 'otheruser' },
|
|
});
|
|
mockNormalizeMessagePreview.mockReturnValue('private message');
|
|
|
|
const result = getMessagePreview(room, message, mockT);
|
|
|
|
expect(result).toBe('otheruser: private message');
|
|
expect(mockNormalizeMessagePreview).toHaveBeenCalledWith(message, mockT);
|
|
});
|
|
});
|
|
|
|
describe('when message has no preview', () => {
|
|
it('should return empty string', () => {
|
|
const room = createFakeSubscription();
|
|
const message = createFakeMessage();
|
|
mockNormalizeMessagePreview.mockReturnValue(undefined);
|
|
|
|
const result = getMessagePreview(room, message, mockT);
|
|
|
|
expect(result).toBe('');
|
|
expect(mockNormalizeMessagePreview).toHaveBeenCalledWith(message, mockT);
|
|
});
|
|
});
|
|
});
|