Skip to content
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
19 changes: 18 additions & 1 deletion backend/__tests__/unit/services/socketMessageWrite.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,15 @@ jest.mock('../../../models/pg/Pod', () => ({ findById: jest.fn() }));
jest.mock('../../../services/pgPodSyncService', () => ({ syncPodFromMongo: jest.fn() }));
jest.mock('../../../models/Message', () => jest.fn());

const fs = require('fs');
const path = require('path');
const PGMessage = require('../../../models/pg/Message');
const PGPod = require('../../../models/pg/Pod');
const { syncPodFromMongo } = require('../../../services/pgPodSyncService');
const MongoMessage = require('../../../models/Message');
const { writeSocketMessage, MESSAGE_STORE_UNAVAILABLE } = require('../../../services/socketMessageWrite');
const {
writeSocketMessage, MESSAGE_STORE_UNAVAILABLE, MESSAGE_SEND_FAILED_EVENT,
} = require('../../../services/socketMessageWrite');

// The refusal names the send so the client can drop its optimistic copy.
const REFUSED = {
Expand Down Expand Up @@ -85,3 +89,16 @@ describe('writeSocketMessage', () => {
.toBeLessThan(PGMessage.create.mock.invocationCallOrder[0]);
});
});

describe('the refused-send event name', () => {
// The server names the event in one constant; the legacy ChatRoom listens
// for it by literal. A rename on either side would drop every refusal
// silently, so pin the two together.
it('is the event ChatRoom subscribes to and unsubscribes from', () => {
const chatRoomPath = path.resolve(__dirname, '../../../../frontend/src/components/ChatRoom.tsx');
const chatRoom = fs.readFileSync(chatRoomPath, 'utf8');

expect(chatRoom).toContain(`socket.on('${MESSAGE_SEND_FAILED_EVENT}', handleSendFailed)`);
expect(chatRoom).toContain(`socket.off('${MESSAGE_SEND_FAILED_EVENT}', handleSendFailed)`);
});
});
7 changes: 6 additions & 1 deletion frontend/src/components/ChatRoom.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,12 @@ test('a send the server refuses is removed and shown in the composer (TASK-503)'
});
});

expect(container.textContent).not.toContain('store is down');
// The optimistic bubble is gone (the composer field is not part of the list).
const listOnly = container.cloneNode(true);
listOnly.querySelectorAll('textarea, input').forEach((field) => field.remove());
expect(listOnly.textContent).not.toContain('store is down');
// The refused text is back in the composer, ready to resend.
expect(container.querySelector('[placeholder="Message #Room"]').value).toBe('store is down');
expect(container.querySelector('[role="alert"]').textContent)
.toContain('Messages are temporarily unavailable. Please try again shortly.');
// The room stays mounted and listening: the refusal is not a page error.
Expand Down
5 changes: 5 additions & 0 deletions frontend/src/components/ChatRoom.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -1932,6 +1932,11 @@ const ChatRoom = () => {
if (i < 0) return prevMessages;
return [...prevMessages.slice(0, i), ...prevMessages.slice(i + 1)];
});
// Give the refused text back so a retry is one keystroke, unless
// the person has already started typing something else.
if (failure.messageType === 'text' && failure.content) {
setMessage(current => current || failure.content);
}
setSendError(failure.message || 'Failed to send message. Please try again.');
};
socket.on('messageSendFailed', handleSendFailed);
Expand Down
Loading