-
Notifications
You must be signed in to change notification settings - Fork 77
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
fix: replace binarySearchByDateEqualOrNearestGreater with findIndexInSortedArray \w midpoint match #1370
base: master
Are you sure you want to change the base?
fix: replace binarySearchByDateEqualOrNearestGreater with findIndexInSortedArray \w midpoint match #1370
Conversation
… binarySearchByDateEqualOrNearestGreater
Size Change: -343 B (-0.08%) Total Size: 427 kB
|
test/unit/channel_state.js
Outdated
expect(state.messages[7].id).to.be.equal('id'); | ||
expect(state.messages[8].id).to.be.equal('7'); |
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.
One hurdle of the midpoint check addition is that message with the same created_at
will be added before existing one but since the existence of the messages with the same created_at
in real-world applications is almost impossible - I adjusted the test.
Even without midpoint check, the previous solution - in case of multiple messages with the same created_at
- the new message would be placed at most one index after the already existing one which is still incorrect.
describe('', () => { | ||
const messages = [ | ||
{ created_at: '2024-08-05T08:55:00.199808Z', id: '0' }, | ||
{ created_at: '2024-08-05T08:55:01.199808Z', id: '1' }, | ||
{ created_at: '2024-08-05T08:55:02.199808Z', id: '2' }, | ||
{ created_at: '2024-08-05T08:55:03.199808Z', id: '3' }, | ||
{ created_at: '2024-08-05T08:55:04.199808Z', id: '4' }, | ||
{ created_at: '2024-08-05T08:55:05.199808Z', id: '5' }, | ||
{ created_at: '2024-08-05T08:55:06.199808Z', id: '6' }, | ||
{ created_at: '2024-08-05T08:55:07.199808Z', id: '7' }, | ||
{ created_at: '2024-08-05T08:55:08.199808Z', id: '8' }, | ||
]; | ||
it('finds the nearest newer item', () => { | ||
expect(binarySearchByDateEqualOrNearestGreater(messages, new Date('2024-08-05T08:55:02.299808Z'))).to.eql(3); | ||
}); | ||
it('finds the nearest matching item', () => { | ||
expect(binarySearchByDateEqualOrNearestGreater(messages, new Date('2024-08-05T08:55:07.199808Z'))).to.eql(7); | ||
}); | ||
}); |
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.
Moved to utils.test.ts
with additions.
398af35
to
544753a
Compare
it("updates an existing message that wasn't filtered due to changed timestamp (timestampChanged)", () => { | ||
const newMessage = getNewFormattedMessage({ timeOffset: 30 * 1000, id: messagesBefore[4].id }); | ||
const newMessage = getNewFormattedMessage({ timeOffset: 30 * 1000, id: messagesBefore[3].id }); | ||
|
||
expect(messagesBefore[4].id).to.equal(newMessage.id); | ||
expect(messagesBefore[4].text).to.not.equal(newMessage.text); | ||
expect(messagesBefore[4]).to.not.equal(newMessage); | ||
expect(messagesBefore[3].id).to.equal(newMessage.id); | ||
expect(messagesBefore[3].text).to.not.equal(newMessage.text); | ||
expect(messagesBefore[3]).to.not.equal(newMessage); | ||
|
||
const messagesAfter = addToMessageList(messagesBefore, newMessage, false, 'created_at', false); | ||
const messagesAfter = addToMessageList(messagesBefore, newMessage, false); | ||
|
||
expect(messagesAfter).to.have.length(5); | ||
expect(messagesAfter[4]).to.equal(newMessage); | ||
expect(messagesAfter[3]).to.equal(newMessage); | ||
}); |
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.
I messed up here previously.
efd7c38
to
d009882
Compare
This PR removes and replaces
binarySearchByDateEqualOrNearestGreater
with universalfindIndexInSortedArray
function and enables exact matching by checking midpoint.Changes to
channel
andchannel_state
tests are to ensure proper order of generated messages (addedcreated_at
to each generated message with proper offset).