delivery_status.test.mjs 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /**
  2. * Delivery/read status events for messages Hermes sent (logged to gateway.log by the adapter).
  3. */
  4. import test from 'node:test';
  5. import assert from 'node:assert/strict';
  6. import { proto } from '@whiskeysockets/baileys';
  7. import { deliveryStatusEvent } from './bridge_helpers.js';
  8. const Status = proto.WebMessageInfo.Status;
  9. const own = new Set(['sent-1']);
  10. const isOwnSend = id => own.has(id);
  11. const dmKey = { id: 'sent-1', remoteJid: '15551234567@s.whatsapp.net', fromMe: true };
  12. test('each 1:1 status change of our own send becomes one event', () => {
  13. const names = [];
  14. for (const status of [Status.SERVER_ACK, Status.DELIVERY_ACK, Status.READ, Status.PLAYED]) {
  15. const event = deliveryStatusEvent({ key: dmKey, update: { status, messageTimestamp: 1760000000 }, isOwnSend });
  16. assert.equal(event.event, 'delivery_status');
  17. assert.equal(event.messageId, 'sent-1');
  18. assert.equal(event.chatId, '15551234567@s.whatsapp.net');
  19. assert.equal(event.timestamp, 1760000000);
  20. names.push(event.status);
  21. }
  22. assert.deepEqual(names, ['server_ack', 'delivered', 'read', 'played']);
  23. });
  24. test('a failed send carries the error code', () => {
  25. const event = deliveryStatusEvent({
  26. key: dmKey, update: { status: Status.ERROR, messageStubParameters: ['463'] }, isOwnSend,
  27. });
  28. assert.equal(event.status, 'error');
  29. assert.equal(event.error, '463');
  30. });
  31. test('group receipts name the recipient and the furthest state', () => {
  32. const key = { id: 'sent-1', remoteJid: '120363001234567890@g.us', fromMe: true };
  33. const read = deliveryStatusEvent({
  34. key, receipt: { userJid: '15550001111@s.whatsapp.net', readTimestamp: 1760000100 }, isOwnSend,
  35. });
  36. assert.deepEqual(
  37. { status: read.status, recipientId: read.recipientId, timestamp: read.timestamp },
  38. { status: 'read', recipientId: '15550001111@s.whatsapp.net', timestamp: 1760000100 },
  39. );
  40. const delivered = deliveryStatusEvent({
  41. key, receipt: { userJid: '15550001111@s.whatsapp.net', receiptTimestamp: 1760000050 }, isOwnSend,
  42. });
  43. assert.equal(delivered.status, 'delivered');
  44. });
  45. test('nothing is reported for messages Hermes did not send', () => {
  46. const update = { status: Status.READ };
  47. // Typed by a human on the linked phone: fromMe but not one of our sends.
  48. assert.equal(deliveryStatusEvent({ key: { ...dmKey, id: 'human-typed' }, update, isOwnSend }), null);
  49. // Inbound message (another device of ours read it).
  50. assert.equal(deliveryStatusEvent({ key: { ...dmKey, fromMe: false }, update, isOwnSend }), null);
  51. // Poll vote / edit / stub updates carry no status.
  52. assert.equal(deliveryStatusEvent({ key: dmKey, update: { pollUpdates: [] }, isOwnSend }), null);
  53. assert.equal(deliveryStatusEvent({ key: dmKey, update: { status: Status.PENDING }, isOwnSend }), null);
  54. assert.equal(deliveryStatusEvent({ key: dmKey, receipt: { userJid: 'x@s.whatsapp.net' }, isOwnSend }), null);
  55. });