agent-conversation-order-smoke-test.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. 'use strict';
  2. const assert = require('assert/strict');
  3. const {
  4. MISSING_TIMESTAMP,
  5. conversationTimestamp,
  6. compareConversationRecency,
  7. sortConversationsByRecency,
  8. maxConversationTimestamp,
  9. } = require('../mcp/src/core/conversation-order');
  10. function main() {
  11. assert.equal(conversationTimestamp(null), MISSING_TIMESTAMP);
  12. assert.equal(conversationTimestamp(''), MISSING_TIMESTAMP);
  13. assert.equal(conversationTimestamp('not-a-time'), MISSING_TIMESTAMP);
  14. const mixed = [
  15. { id: 'private-old', channelType: 'private', lastMessageAt: '2026-08-04T08:00:00.000Z' },
  16. { id: 'group-new', channelType: 'group', lastMessageAt: '2026-08-04T10:00:00.000Z' },
  17. { id: 'private-invalid', channelType: 'private', lastMessageAt: 'not-a-time' },
  18. { id: 'group-empty', channelType: 'group', lastMessageAt: '' },
  19. { id: 'private-null', channelType: 'private', lastMessageAt: null },
  20. ];
  21. const sorted = sortConversationsByRecency(mixed);
  22. assert.deepEqual(sorted.map(item => item.id), [
  23. 'group-new',
  24. 'private-old',
  25. 'private-invalid',
  26. 'group-empty',
  27. 'private-null',
  28. ]);
  29. assert.deepEqual(mixed.map(item => item.id), [
  30. 'private-old',
  31. 'group-new',
  32. 'private-invalid',
  33. 'group-empty',
  34. 'private-null',
  35. ]);
  36. const sameTime = [
  37. { id: 'private-first', channelType: 'private', lastMessageAt: '2026-08-04T09:00:00.000Z' },
  38. { id: 'group-second', channelType: 'group', lastMessageAt: '2026-08-04T09:00:00.000Z' },
  39. { id: 'private-third', channelType: 'private', lastMessageAt: '2026-08-04T09:00:00.000Z' },
  40. ];
  41. assert.deepEqual(sortConversationsByRecency(sameTime).map(item => item.id), [
  42. 'private-first',
  43. 'group-second',
  44. 'private-third',
  45. ]);
  46. assert(compareConversationRecency(mixed[1], mixed[0]) < 0);
  47. assert(compareConversationRecency(mixed[2], mixed[3]) === 0);
  48. assert.equal(maxConversationTimestamp(
  49. null,
  50. 'not-a-time',
  51. '2026-08-04T08:00:00.000Z',
  52. '2026-08-04T10:00:00.000Z',
  53. '2026-08-04T09:00:00.000Z',
  54. ), '2026-08-04T10:00:00.000Z');
  55. assert.equal(maxConversationTimestamp(['', null, 'not-a-time']), null);
  56. process.stdout.write(`${JSON.stringify({ status: 'ok', checks: 6 }, null, 2)}\n`);
  57. }
  58. try { main(); }
  59. catch (error) {
  60. process.stderr.write(`${error.stack || error.message}\n`);
  61. process.exit(1);
  62. }