bridge_helpers.timestamp.test.mjs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * Unit tests for the bridge's log-line stamping: formatLogStamp,
  3. * installConsoleStamps and writeJsonLine.
  4. *
  5. * Regression for issue #97021: startup/connection lifecycle console.log/
  6. * console.warn lines (bridge listening, connected, logged out, reconnect,
  7. * "[bridge] ..." warnings) carried no timestamp, and the platform adapter
  8. * captures the bridge's stdout/stderr verbatim into bridge.log, making
  9. * bridge.log impossible to sequence on its own during incident forensics.
  10. * Machine-read JSON event lines (pair events parsed line-by-line by the
  11. * dashboard pairing watcher, `ignored` events) must stay byte-identical.
  12. */
  13. import { strict as assert } from 'node:assert';
  14. import { formatLogStamp, installConsoleStamps, writeJsonLine } from './bridge_helpers.js';
  15. const LOCAL_STAMP_PREFIX_RE = /^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2},\d{3} /;
  16. function captureWrites(stream, fn) {
  17. const chunks = [];
  18. const originalWrite = stream.write;
  19. stream.write = (chunk, ...rest) => {
  20. chunks.push(String(chunk));
  21. const callback = rest.find(arg => typeof arg === 'function');
  22. if (callback) callback();
  23. return true;
  24. };
  25. try {
  26. fn();
  27. } finally {
  28. stream.write = originalWrite;
  29. }
  30. return chunks;
  31. }
  32. function withStampedConsole(fn) {
  33. const saved = { log: console.log, warn: console.warn, error: console.error };
  34. installConsoleStamps();
  35. try {
  36. fn();
  37. } finally {
  38. Object.assign(console, saved);
  39. }
  40. }
  41. // The stamp is local wall-clock time in Python's asctime shape.
  42. {
  43. assert.equal(formatLogStamp(new Date(2026, 8, 28, 3, 4, 5, 62)), '2026-09-28 03:04:05,062');
  44. }
  45. // A console.log line is stamped with the current local time, and the message
  46. // text survives byte-for-byte after the stamp.
  47. withStampedConsole(() => {
  48. const before = Date.now();
  49. const out = captureWrites(process.stdout, () => {
  50. console.log('❌ Logged out. Delete session and restart to re-authenticate.');
  51. });
  52. assert.equal(out.length, 1);
  53. assert.match(out[0], LOCAL_STAMP_PREFIX_RE);
  54. assert.ok(out[0].endsWith(' ❌ Logged out. Delete session and restart to re-authenticate.\n'));
  55. const [day, time] = out[0].split(' ');
  56. const stampedMs = new Date(`${day}T${time.replace(',', '.')}`).getTime();
  57. assert.ok(Math.abs(stampedMs - before) < 5000, `stamp ${day} ${time} is not local now`);
  58. });
  59. // console.warn keeps its multi-argument formatting and still goes to stderr.
  60. withStampedConsole(() => {
  61. const err = captureWrites(process.stderr, () => {
  62. console.warn('[bridge] failed to send read receipt:', 'boom');
  63. });
  64. assert.equal(err.length, 1);
  65. assert.match(err[0], LOCAL_STAMP_PREFIX_RE);
  66. assert.ok(err[0].endsWith(' [bridge] failed to send read receipt: boom\n'));
  67. });
  68. // A JSON event line is not stamped, even with the console wrapped, so the
  69. // pairing watcher's per-line json.loads still parses it.
  70. withStampedConsole(() => {
  71. const out = captureWrites(process.stdout, () => {
  72. writeJsonLine({ ts: 1790000000000, event: 'qr', qr: 'abc' });
  73. });
  74. assert.deepEqual(out, ['{"ts":1790000000000,"event":"qr","qr":"abc"}\n']);
  75. });
  76. // An empty console.log() stays a bare newline rather than a lone stamp.
  77. withStampedConsole(() => {
  78. assert.deepEqual(captureWrites(process.stdout, () => console.log()), ['\n']);
  79. });
  80. console.log('bridge_helpers.timestamp.test.mjs: all assertions passed');