Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions apps/meteor/server/lib/messages/sendMessage.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { expect } from 'chai';
import { describe, it } from 'mocha';

import { prepareMessageObject } from './sendMessage';

describe('sendMessage', () => {
describe('prepareMessageObject', () => {
it('should set ts if not provided', () => {
const message: any = {};
prepareMessageObject(message, 'rid123', { _id: 'u123', username: 'testuser', name: 'Test User' });
expect(message.ts).to.be.a('date');
});

it('should preserve existing ts', () => {
const date = new Date('2023-01-01T00:00:00.000Z');
const message: any = { ts: date };
prepareMessageObject(message, 'rid123', { _id: 'u123', username: 'testuser', name: 'Test User' });
expect(message.ts).to.equal(date);
});

it('should set rid correctly', () => {
const message: any = {};
prepareMessageObject(message, 'rid123', { _id: 'u123', username: 'testuser', name: 'Test User' });
expect(message.rid).to.equal('rid123');
});

it('should set user details correctly', () => {
const message: any = {};
prepareMessageObject(message, 'rid123', { _id: 'u123', username: 'testuser', name: 'Test User' });
expect(message.u).to.be.an('object');
expect(message.u._id).to.equal('u123');
expect(message.u.username).to.equal('testuser');
expect(message.u.name).to.equal('Test User');
});

it('should throw an error if username is not provided', () => {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P0: Test 'should throw an error if username is not provided' asserts that prepareMessageObject throws 'error-invalid-user' when username is omitted from the user object, but the implementation never throws — it simply sets message.u.username = username as string (which becomes undefined). This test will fail at runtime.

Prompt for AI agents
Check if this issue is valid — if so, understand the root cause and fix it. At apps/meteor/server/lib/messages/sendMessage.spec.ts, line 36:

<comment>Test 'should throw an error if username is not provided' asserts that `prepareMessageObject` throws `'error-invalid-user'` when username is omitted from the user object, but the implementation never throws — it simply sets `message.u.username = username as string` (which becomes `undefined`). This test will fail at runtime.</comment>

<file context>
@@ -0,0 +1,49 @@
+			expect(message.u.name).to.equal('Test User');
+		});
+
+		it('should throw an error if username is not provided', () => {
+			const message: any = {};
+			expect(() => {
</file context>

const message: any = {};
expect(() => {
prepareMessageObject(message, 'rid123', { _id: 'u123', name: 'Test User' });
}).to.throw('error-invalid-user');
});
Comment on lines +36 to +41

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win

This test contradicts prepareMessageObject and will fail.

The supplied implementation does not validate or throw for a missing username. Either add the intended validation in sendMessage.ts, or change this test to assert the existing contract.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@apps/meteor/server/lib/messages/sendMessage.spec.ts` around lines 36 - 41,
Resolve the mismatch between the test and prepareMessageObject’s existing
contract: either add missing-username validation in
prepareMessageObject/sendMessage.ts that throws error-invalid-user, or update
the test to assert the current behavior instead of expecting an exception. Keep
the test and implementation consistent.


it('should delete tshow if it is not exactly true', () => {
const message: any = { tshow: false };
prepareMessageObject(message, 'rid123', { _id: 'u123', username: 'testuser', name: 'Test User' });
expect(message).to.not.have.property('tshow');
});
});
});