Skip to content
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: improper poll state overwrite #1456

Merged
merged 7 commits into from
Jan 30, 2025
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion src/channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1211,7 +1211,7 @@ export class Channel<StreamChatGenerics extends ExtendableGenerics = DefaultGene
}),
};

this.getClient().polls.hydratePollCache(messageSet.messages, true);
this.getClient().polls.hydratePollCache(state.messages, true);

const areCapabilitiesChanged =
[...(state.channel.own_capabilities || [])].sort().join() !==
Expand Down
2 changes: 1 addition & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1690,7 +1690,7 @@ export class StreamChat<StreamChatGenerics extends ExtendableGenerics = DefaultG
logger: this.logger,
}),
};
this.polls.hydratePollCache(updatedMessagesSet.messages, true);
this.polls.hydratePollCache(channelState.messages, true);
}

channels.push(c);
Expand Down
6 changes: 5 additions & 1 deletion src/poll_manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import type {
CreatePollData,
DefaultGenerics,
ExtendableGenerics,
MessageResponse,
PollResponse,
PollSort,
QueryPollsFilters,
Expand Down Expand Up @@ -90,7 +91,10 @@ export class PollManager<SCG extends ExtendableGenerics = DefaultGenerics> {
};
};

public hydratePollCache = (messages: FormatMessageResponse<SCG>[], overwriteState?: boolean) => {
public hydratePollCache = (
messages: FormatMessageResponse<SCG>[] | MessageResponse<SCG>[],
overwriteState?: boolean,
) => {
for (const message of messages) {
if (!message.poll) {
continue;
Expand Down
94 changes: 94 additions & 0 deletions test/unit/poll_manager.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { expect } from 'chai';
import { v4 as uuidv4 } from 'uuid';

import { generateChannel } from './test-utils/generateChannel';
import { generateMsg } from './test-utils/generateMessage';
import { mockChannelQueryResponse } from './test-utils/mockChannelQueryResponse';

Expand Down Expand Up @@ -189,6 +191,98 @@ describe('PollManager', () => {
expect(client.polls.data).not.to.be.null;
});

it('populates pollCache on client.queryChannels invocation', async () => {
const mockedChannelsQueryResponse = [];

let pollMessages: MessageResponse[] = [];
for (let ci = 0; ci < 5; ci++) {
const { messages, pollMessages: onlyPollMessages } = generateRandomMessagesWithPolls(5, `_${ci}`);
pollMessages = pollMessages.concat(onlyPollMessages);
mockedChannelsQueryResponse.push(generateChannel({ channel: { id: uuidv4() }, messages }));
}
const mock = sinon.mock(client);
const spy = sinon.spy(client.polls, 'hydratePollCache');
mock.expects('post').returns(Promise.resolve({ channels: mockedChannelsQueryResponse }));
await client.queryChannels({});
expect(client.polls.data.size).to.equal(pollMessages.length);
expect(spy.callCount).to.be.equal(5);
for (let i = 0; i < 5; i++) {
expect(spy.calledWith(mockedChannelsQueryResponse[i].messages, true)).to.be.true;
}
});

it('populates pollCache only with new messages on client.queryChannels invocation', async () => {
let channels = [];
let pollMessages: MessageResponse[] = [];
const spy = sinon.spy(client.polls, 'hydratePollCache');
for (let ci = 0; ci < 5; ci++) {
const { messages: prevMessages, pollMessages: prevPollMessages } = generateRandomMessagesWithPolls(
5,
`_prev_${ci}`,
);
pollMessages = pollMessages.concat(prevPollMessages);
const channelResponse = generateChannel({ channel: { id: uuidv4() }, messages: prevMessages });
channels.push(channelResponse);
client.channel(channelResponse.channel.type, channelResponse.channel.id);
client.polls.hydratePollCache(prevMessages, true);
}

const mockedChannelsQueryResponse = [];
for (let ci = 0; ci < 5; ci++) {
const { messages, pollMessages: onlyPollMessages } = generateRandomMessagesWithPolls(5, `_${ci}`);
pollMessages = pollMessages.concat(onlyPollMessages);
const channelResponse = { ...channels[ci], messages };
mockedChannelsQueryResponse.push(channelResponse);
}
const mock = sinon.mock(client);
mock.expects('post').returns(Promise.resolve({ channels: mockedChannelsQueryResponse }));
await client.queryChannels({});
expect(client.polls.data.size).to.equal(pollMessages.length);
expect(spy.callCount).to.be.equal(10);
for (let i = 0; i < 5; i++) {
expect(spy.calledWith(mockedChannelsQueryResponse[i].messages, true)).to.be.true;
expect(spy.calledWith(channels[i].messages, true)).to.be.true;
expect(spy.calledWith([...channels[i].messages, ...mockedChannelsQueryResponse[i].messages], true)).to.be.false;
}
});

it('populates pollCache on channel.query invocation', async () => {
const channel = client.channel('messaging', uuidv4());
const { messages, pollMessages } = generateRandomMessagesWithPolls(5, ``);
const mockedChannelQueryResponse = {
...mockChannelQueryResponse,
messages,
};
const mock = sinon.mock(client);
const spy = sinon.spy(client.polls, 'hydratePollCache');
mock.expects('post').returns(Promise.resolve(mockedChannelQueryResponse));
await channel.query();
expect(client.polls.data.size).to.equal(pollMessages.length);
expect(spy.calledOnce).to.be.true;
expect(spy.calledWith(mockedChannelQueryResponse.messages, true)).to.be.true;
});

it('populates pollCache with only new messages on channel.query invocation', async () => {
const channel = client.channel('messaging', mockChannelQueryResponse.channel.id);
const { messages: prevMessages, pollMessages: prevPollMessages } = generateRandomMessagesWithPolls(5, `_prev`);
channel.state.addMessagesSorted(prevMessages);
const { messages, pollMessages } = generateRandomMessagesWithPolls(5, ``);
const mockedChannelQueryResponse = {
...mockChannelQueryResponse,
messages,
};
const mock = sinon.mock(client);
const spy = sinon.spy(client.polls, 'hydratePollCache');
mock.expects('post').returns(Promise.resolve(mockedChannelQueryResponse));
client.polls.hydratePollCache(prevMessages);
await channel.query();
expect(client.polls.data.size).to.equal(prevPollMessages.length + pollMessages.length);
expect(spy.calledTwice).to.be.true;
expect(spy.args[0][0]).to.deep.equal(prevMessages);
expect(spy.args[1][0]).to.deep.equal(mockedChannelQueryResponse.messages);
expect(spy.calledWith([...prevMessages, ...messages], true)).to.be.false;
});

it('populates pollCache on client.hydrateActiveChannels', async () => {
const mockedChannelsQueryResponse = [];

Expand Down