-
Notifications
You must be signed in to change notification settings - Fork 13.8k
test: add unit tests for message sending validation #41565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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', () => { | ||
| const message: any = {}; | ||
| expect(() => { | ||
| prepareMessageObject(message, 'rid123', { _id: 'u123', name: 'Test User' }); | ||
| }).to.throw('error-invalid-user'); | ||
| }); | ||
|
Comment on lines
+36
to
+41
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🎯 Functional Correctness | 🔴 Critical | ⚡ Quick win This test contradicts The supplied implementation does not validate or throw for a missing username. Either add the intended validation in 🤖 Prompt for AI Agents |
||
|
|
||
| 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'); | ||
| }); | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
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
prepareMessageObjectthrows'error-invalid-user'when username is omitted from the user object, but the implementation never throws — it simply setsmessage.u.username = username as string(which becomesundefined). This test will fail at runtime.Prompt for AI agents