Skip to content

Commit

Permalink
core/messages: point at new ably-js fields
Browse files Browse the repository at this point in the history
No public API changes as discussed, just mapping the new ably-js fields to existing chat ones
so that things work in the meantime and we can unblock merging PRs
  • Loading branch information
AndyTWF committed Nov 27, 2024
1 parent d5dbc8c commit 248d2be
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 89 deletions.
19 changes: 8 additions & 11 deletions src/core/message-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ interface MessagePayload {
};

serial: string;
updatedAt?: number;
updateSerial?: string;
createdAt: number;
version?: string;
action: Ably.MessageAction;
operation?: Ably.Operation;
}
Expand Down Expand Up @@ -69,11 +69,11 @@ export function parseMessage(roomId: string | undefined, inboundMessage: Ably.In
text: message.data.text,
metadata: message.data.metadata ?? {},
headers: message.extras.headers ?? {},
createdAt: new Date(message.timestamp),
createdAt: new Date(message.createdAt),
latestAction: message.action as ChatMessageActions,
latestActionSerial: message.updateSerial ?? message.serial,
updatedAt: message.updatedAt ? new Date(message.updatedAt) : undefined,
deletedAt: message.updatedAt ? new Date(message.updatedAt) : undefined,
latestActionSerial: message.version ?? message.serial,
updatedAt: message.timestamp ? new Date(message.timestamp) : undefined,
deletedAt: message.timestamp ? new Date(message.timestamp) : undefined,
operation: message.operation as MessageActionDetails,
};

Expand All @@ -83,11 +83,8 @@ export function parseMessage(roomId: string | undefined, inboundMessage: Ably.In
}
case ChatMessageActions.MessageUpdate:
case ChatMessageActions.MessageDelete: {
if (!message.updatedAt) {
throw new Ably.ErrorInfo(`received incoming ${message.action} without updatedAt`, 50000, 500);
}
if (!message.updateSerial) {
throw new Ably.ErrorInfo(`received incoming ${message.action} without updateSerial`, 50000, 500);
if (!message.version) {
throw new Ably.ErrorInfo(`received incoming ${message.action} without version`, 50000, 500);
}
break;
}
Expand Down
74 changes: 25 additions & 49 deletions test/core/message-parser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ describe('parseMessage', () => {
message: {
clientId: 'client1',
timestamp: 1728402074206,
createdAt: 1728402074206,
extras: {},
serial: '01728402074206-000@cbfkKvEYgBhDaZ38195418:0',
action: ChatMessageActions.MessageCreate,
Expand All @@ -31,6 +32,7 @@ describe('parseMessage', () => {
message: {
data: { text: 'hello' },
timestamp: 1728402074206,
createdAt: 1728402074206,
extras: {},
serial: '01728402074206-000@cbfkKvEYgBhDaZ38195418:0',
action: ChatMessageActions.MessageCreate,
Expand All @@ -44,6 +46,7 @@ describe('parseMessage', () => {
data: {},
clientId: 'client1',
timestamp: 1728402074206,
createdAt: 1728402074206,
extras: {},
serial: '01728402074206-000@cbfkKvEYgBhDaZ38195418:0',
action: ChatMessageActions.MessageCreate,
Expand All @@ -57,6 +60,7 @@ describe('parseMessage', () => {
data: { text: 'hello' },
clientId: 'client1',
timestamp: 1728402074206,
createdAt: 1728402074206,
serial: '01728402074206-000@cbfkKvEYgBhDaZ38195418:0',
action: ChatMessageActions.MessageCreate,
},
Expand All @@ -69,6 +73,7 @@ describe('parseMessage', () => {
data: { text: 'hello' },
clientId: 'client1',
timestamp: 1728402074206,
createdAt: 1728402074206,
extras: {},
action: ChatMessageActions.MessageCreate,
},
Expand All @@ -81,71 +86,42 @@ describe('parseMessage', () => {
data: { text: 'hello' },
clientId: 'client1',
timestamp: 1728402074206,
createdAt: 1728402074206,
extras: {},
serial: '01728402074206-000@cbfkKvEYgBhDaZ38195418:0',
action: 'unhandled.action',
},
expectedError: 'received incoming message with unhandled action; unhandled.action',
},
{
description: 'message.updatedAt is undefined for update',
description: 'message.version is undefined for update',
roomId: 'room1',
message: {
data: { text: 'hello' },
clientId: 'client1',
timestamp: 1728402074206,
createdAt: 1728402074206,
extras: {},
serial: '01728402074206-000@cbfkKvEYgBhDaZ38195418:0',
action: ChatMessageActions.MessageUpdate,
updatedAt: undefined,
updateSerial: '01728402074206-000@cbfkKvEYgBhDaZ38195418:0',
},
expectedError: 'received incoming message.update without updatedAt',
},
{
description: 'message.updateSerial is undefined for update',
roomId: 'room1',
message: {
data: { text: 'hello' },
clientId: 'client1',
timestamp: 1728402074206,
extras: {},
serial: '01728402074206-000@cbfkKvEYgBhDaZ38195418:0',
action: ChatMessageActions.MessageUpdate,
updatedAt: 1234567890,
updateSerial: undefined,
},
expectedError: 'received incoming message.update without updateSerial',
},
{
description: 'message.updatedAt is undefined for deletion',
roomId: 'room1',
message: {
data: { text: 'hello' },
clientId: 'client1',
timestamp: 1728402074206,
extras: {},
serial: '01728402074206-000@cbfkKvEYgBhDaZ38195418:0',
updatedAt: undefined,
updateSerial: '01728402074206-000@cbfkKvEYgBhDaZ38195418:0',
action: ChatMessageActions.MessageDelete,
timestamp: 1234567890,
version: undefined,
},
expectedError: 'received incoming message.delete without updatedAt',
expectedError: 'received incoming message.update without version',
},
{
description: 'message.updateSerial is undefined for deletion',
description: 'message.version is undefined for deletion',
roomId: 'room1',
message: {
data: { text: 'hello' },
clientId: 'client1',
timestamp: 1728402074206,
createdAt: 1728402074206,
extras: {},
serial: '01728402074206-000@cbfkKvEYgBhDaZ38195418:0',
updatedAt: 1234567890,
updateSerial: undefined,
timestamp: 1234567890,
version: undefined,
action: ChatMessageActions.MessageDelete,
},
expectedError: 'received incoming message.delete without updateSerial',
expectedError: 'received incoming message.delete without version',
},
])('should throw an error ', ({ description, roomId, message, expectedError }) => {
it(`should throw an error if ${description}`, () => {
Expand All @@ -162,12 +138,12 @@ describe('parseMessage', () => {
const message = {
data: { text: 'hello', metadata: { key: 'value' } },
clientId: 'client1',
timestamp: 1728402074206,
createdAt: 1728402074206,
extras: {
headers: { headerKey: 'headerValue' },
},
updatedAt: 1728402074206,
updateSerial: '01728402074206-000@cbfkKvEYgBhDaZ38195418:0',
timestamp: 1728402074206,
version: '01728402074206-000@cbfkKvEYgBhDaZ38195418:0',
action: ChatMessageActions.MessageCreate,
serial: '01728402074206-000@cbfkKvEYgBhDaZ38195418:0',
} as Ably.InboundMessage;
Expand Down Expand Up @@ -200,14 +176,14 @@ describe('parseMessage', () => {
id: 'message-id',
data: { text: 'hello', metadata: { key: 'value' } },
clientId: 'client1',
timestamp: 1728402074206,
createdAt: 1728402074206,
extras: {
headers: { headerKey: 'headerValue' },
},
action: ChatMessageActions.MessageUpdate,
serial: '01728402074206-000@cbfkKvEYgBhDaZ38195418:0',
updatedAt: 1728402074206,
updateSerial: '01728402074206-000@cbfkKvEYgBhDaZ38195418:0',
timestamp: 1728402074206,
version: '01728402074206-000@cbfkKvEYgBhDaZ38195418:0',
operation: { clientId: 'client2', description: 'update message', metadata: { 'custom-update': 'some flag' } },
} as Ably.InboundMessage;

Expand Down Expand Up @@ -241,14 +217,14 @@ describe('parseMessage', () => {
id: 'message-id',
data: { text: 'hello', metadata: { key: 'value' } },
clientId: 'client1',
timestamp: 1728402074206,
createdAt: 1728402074206,
extras: {
headers: { headerKey: 'headerValue' },
},
action: ChatMessageActions.MessageDelete,
serial: '01728402074206-000@cbfkKvEYgBhDaZ38195418:0',
updatedAt: 1728402074206,
updateSerial: '01728402074206-000@cbfkKvEYgBhDaZ38195418:0',
timestamp: 1728402074206,
version: '01728402074206-000@cbfkKvEYgBhDaZ38195418:0',
operation: {
clientId: 'client2',
description: 'delete message',
Expand Down
10 changes: 9 additions & 1 deletion test/core/messages.integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,15 @@ const waitForMessages = (messages: Message[], expectedCount: number) => {

describe('messages integration', () => {
beforeEach<TestContext>((context) => {
context.chat = newChatClient();
context.chat = newChatClient(
undefined,
ablyRealtimeClient({
logLevel: 4,
logHandler: (message) => {
console.log(message);
},
}),
);
});

it<TestContext>('sets the agent version on the channel', async (context) => {
Expand Down
Loading

0 comments on commit 248d2be

Please sign in to comment.