owner_message_gate.test.mjs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. import test from 'node:test';
  2. import assert from 'node:assert/strict';
  3. import { classifyOwnerMessageGate } from './owner_message_gate.js';
  4. function makeRecentlySent(ids = []) {
  5. const set = new Set(ids);
  6. return { has: (id) => set.has(id) };
  7. }
  8. function makeAllowlist(allowedChatIds) {
  9. if (allowedChatIds === '*') {
  10. return () => true;
  11. }
  12. const set = new Set(allowedChatIds);
  13. return (id) => set.has(id);
  14. }
  15. test('non-fromMe messages always pass through', () => {
  16. const decision = classifyOwnerMessageGate({
  17. fromMe: false,
  18. fromOwnerEnabled: true,
  19. recentlySent: makeRecentlySent(),
  20. allowlistMatches: makeAllowlist([]),
  21. messageId: 'M1',
  22. chatId: '6281234567890@s.whatsapp.net',
  23. });
  24. assert.deepEqual(decision, { action: 'pass' });
  25. });
  26. test('fromMe echo of our own /send is dropped', () => {
  27. const decision = classifyOwnerMessageGate({
  28. fromMe: true,
  29. fromOwnerEnabled: true,
  30. recentlySent: makeRecentlySent(['M-OWN-1']),
  31. allowlistMatches: makeAllowlist('*'),
  32. messageId: 'M-OWN-1',
  33. chatId: '6281234567890@s.whatsapp.net',
  34. });
  35. assert.deepEqual(decision, { action: 'drop_echo' });
  36. });
  37. test('fromMe is dropped when forwarding is disabled', () => {
  38. const decision = classifyOwnerMessageGate({
  39. fromMe: true,
  40. fromOwnerEnabled: false,
  41. recentlySent: makeRecentlySent(),
  42. allowlistMatches: makeAllowlist('*'),
  43. messageId: 'M-OWN-2',
  44. chatId: '6281234567890@s.whatsapp.net',
  45. });
  46. assert.deepEqual(decision, { action: 'drop_disabled' });
  47. });
  48. test('fromMe is dropped when chatId is not on the allowlist (regression)', () => {
  49. // This is the bug. Before the fix, an owner reply in a non-allowlisted
  50. // chat was still forwarded with fromOwner: true, which made the
  51. // gateway-policy owner-implicit branch create stray handover rows for
  52. // the non-allowlisted contact.
  53. const decision = classifyOwnerMessageGate({
  54. fromMe: true,
  55. fromOwnerEnabled: true,
  56. recentlySent: makeRecentlySent(),
  57. allowlistMatches: makeAllowlist(['6281234567890@s.whatsapp.net']),
  58. messageId: 'M-OWN-3',
  59. chatId: '111600547700784@lid',
  60. });
  61. assert.deepEqual(decision, { action: 'drop_allowlist' });
  62. });
  63. test('fromMe is forwarded as owner when chatId is allowlisted', () => {
  64. const decision = classifyOwnerMessageGate({
  65. fromMe: true,
  66. fromOwnerEnabled: true,
  67. recentlySent: makeRecentlySent(),
  68. allowlistMatches: makeAllowlist(['6281234567890@s.whatsapp.net']),
  69. messageId: 'M-OWN-4',
  70. chatId: '6281234567890@s.whatsapp.net',
  71. });
  72. assert.deepEqual(decision, { action: 'forward_owner' });
  73. });
  74. test('open-allowlist (matchesAllowedUser short-circuits true) forwards as owner', () => {
  75. // matchesAllowedUser returns true on empty allowlist or "*"; the gate
  76. // must respect that so deployments without an allowlist are unaffected
  77. // by the new check.
  78. const decision = classifyOwnerMessageGate({
  79. fromMe: true,
  80. fromOwnerEnabled: true,
  81. recentlySent: makeRecentlySent(),
  82. allowlistMatches: () => true,
  83. messageId: 'M-OWN-5',
  84. chatId: '111600547700784@lid',
  85. });
  86. assert.deepEqual(decision, { action: 'forward_owner' });
  87. });
  88. test('echo check fires before allowlist check', () => {
  89. // A bot-API echo whose chatId happens to be off-allowlist should still
  90. // be dropped as drop_echo, not drop_allowlist, so logging stays
  91. // honest about the actual reason.
  92. const decision = classifyOwnerMessageGate({
  93. fromMe: true,
  94. fromOwnerEnabled: true,
  95. recentlySent: makeRecentlySent(['M-ECHO-1']),
  96. allowlistMatches: makeAllowlist([]),
  97. messageId: 'M-ECHO-1',
  98. chatId: '111600547700784@lid',
  99. });
  100. assert.deepEqual(decision, { action: 'drop_echo' });
  101. });
  102. test('disabled flag fires before allowlist check', () => {
  103. // Pre-existing deployments with WHATSAPP_FORWARD_OWNER_MESSAGES unset
  104. // must see drop_disabled regardless of allowlist state, otherwise
  105. // every fromMe message would log a misleading allowlist_mismatch.
  106. const decision = classifyOwnerMessageGate({
  107. fromMe: true,
  108. fromOwnerEnabled: false,
  109. recentlySent: makeRecentlySent(),
  110. allowlistMatches: makeAllowlist([]),
  111. messageId: 'M-OWN-6',
  112. chatId: '111600547700784@lid',
  113. });
  114. assert.deepEqual(decision, { action: 'drop_disabled' });
  115. });