fix(HH-576): normalize realtime message timestamps (#148)
Co-authored-by: Rogee <rogee@ipao.vip>
This commit is contained in:
@@ -3,6 +3,8 @@ import ActionCableConnector from '../actionCable';
|
||||
import { playNewMessageNotificationInWidget } from '../WidgetAudioNotificationHelper';
|
||||
import { IFrameHelper } from '../utils';
|
||||
import { emitter } from 'shared/helpers/mitt';
|
||||
import { actions } from 'widget/store/modules/conversation/actions';
|
||||
import { mutations } from 'widget/store/modules/conversation/mutations';
|
||||
|
||||
vi.mock('../WidgetAudioNotificationHelper', () => ({
|
||||
playNewMessageNotificationInWidget: vi.fn(),
|
||||
@@ -41,6 +43,16 @@ describe('Widget ActionCableConnector', () => {
|
||||
);
|
||||
});
|
||||
|
||||
const useRealConversationDispatch = state => {
|
||||
const commit = (type, payload) => mutations[type](state, payload);
|
||||
dispatch.mockImplementation((action, payload) => {
|
||||
if (action === 'conversation/addOrUpdateMessage') {
|
||||
return actions.addOrUpdateMessage({ commit, state }, payload);
|
||||
}
|
||||
return undefined;
|
||||
});
|
||||
};
|
||||
|
||||
it('emits message side effects only once when realtime replays a message', async () => {
|
||||
const message = {
|
||||
id: 42,
|
||||
@@ -73,4 +85,102 @@ describe('Widget ActionCableConnector', () => {
|
||||
|
||||
expect(IFrameHelper.sendMessage).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects malformed timestamps through the real widget mutation path', async () => {
|
||||
const currentMessage = {
|
||||
id: 42,
|
||||
conversation_id: 7,
|
||||
content: 'current',
|
||||
updated_at: '2026-08-24T02:00:00Z',
|
||||
};
|
||||
const state = { conversations: { 42: currentMessage } };
|
||||
useRealConversationDispatch(state);
|
||||
|
||||
await actionCable.onMessageUpdated({
|
||||
...currentMessage,
|
||||
content: 'malformed',
|
||||
updated_at: 'not-a-timestamp',
|
||||
previous_changes: {
|
||||
content_attributes: [{}, { submitted_values: ['yes'] }],
|
||||
},
|
||||
});
|
||||
|
||||
expect(state.conversations[42]).toBe(currentMessage);
|
||||
expect(IFrameHelper.sendMessage).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('rejects an older numeric-string timestamp against an ISO timestamp', async () => {
|
||||
const currentMessage = {
|
||||
id: 42,
|
||||
conversation_id: 7,
|
||||
content: 'current',
|
||||
updated_at: '2026-08-24T02:00:00Z',
|
||||
};
|
||||
const state = { conversations: { 42: currentMessage } };
|
||||
useRealConversationDispatch(state);
|
||||
|
||||
await actionCable.onMessageUpdated({
|
||||
...currentMessage,
|
||||
content: 'older',
|
||||
updated_at: '1787533200',
|
||||
previous_changes: {
|
||||
content_attributes: [{}, { submitted_values: ['yes'] }],
|
||||
},
|
||||
});
|
||||
|
||||
expect(state.conversations[42]).toBe(currentMessage);
|
||||
expect(IFrameHelper.sendMessage).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('replaces a malformed stored timestamp with a valid update', async () => {
|
||||
const state = {
|
||||
conversations: {
|
||||
42: {
|
||||
id: 42,
|
||||
conversation_id: 7,
|
||||
content: 'malformed stored message',
|
||||
updated_at: 'not-a-timestamp',
|
||||
},
|
||||
},
|
||||
};
|
||||
useRealConversationDispatch(state);
|
||||
const message = {
|
||||
id: 42,
|
||||
conversation_id: 7,
|
||||
content: 'valid update',
|
||||
updated_at: '2026-08-24T02:00:00Z',
|
||||
previous_changes: {
|
||||
content_attributes: [{}, { submitted_values: ['yes'] }],
|
||||
},
|
||||
};
|
||||
|
||||
await actionCable.onMessageUpdated(message);
|
||||
|
||||
expect(state.conversations[42]).toEqual(message);
|
||||
expect(IFrameHelper.sendMessage).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('keeps accepting widget updates with a missing timestamp', async () => {
|
||||
const currentMessage = {
|
||||
id: 42,
|
||||
conversation_id: 7,
|
||||
content: 'current',
|
||||
updated_at: '2026-08-24T02:00:00Z',
|
||||
};
|
||||
const state = { conversations: { 42: currentMessage } };
|
||||
useRealConversationDispatch(state);
|
||||
const message = {
|
||||
id: 42,
|
||||
conversation_id: 7,
|
||||
content: 'compatible update',
|
||||
previous_changes: {
|
||||
content_attributes: [{}, { submitted_values: ['yes'] }],
|
||||
},
|
||||
};
|
||||
|
||||
await actionCable.onMessageUpdated(message);
|
||||
|
||||
expect(state.conversations[42]).toEqual(message);
|
||||
expect(IFrameHelper.sendMessage).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -13,6 +13,7 @@ import {
|
||||
import { ON_CONVERSATION_CREATED } from 'widget/constants/widgetBusEvents';
|
||||
import { createTemporaryMessage, getNonDeletedMessages } from './helpers';
|
||||
import { emitter } from 'shared/helpers/mitt';
|
||||
import { isNewerTimestamp } from 'shared/helpers/timeHelper';
|
||||
export const actions = {
|
||||
createConversation: async ({ commit, dispatch }, params) => {
|
||||
commit('setConversationUIFlag', { isCreating: true });
|
||||
@@ -178,9 +179,8 @@ export const actions = {
|
||||
const currentMessage = state?.conversations?.[id];
|
||||
const isNew = !currentMessage;
|
||||
if (
|
||||
currentMessage?.updated_at &&
|
||||
data.updated_at &&
|
||||
data.updated_at <= currentMessage.updated_at
|
||||
currentMessage &&
|
||||
!isNewerTimestamp(currentMessage.updated_at, data.updated_at)
|
||||
) {
|
||||
return { applied: false, isNew };
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user