|
| 1 | +// SPDX-License-Identifier: AGPL-3.0-only |
| 2 | +// Regression (claudemm, 2 Sep 2026): after #92 widened the fetch window, a |
| 3 | +// restart replayed months-old messages whose ids the seen cap had evicted. |
| 4 | +import assert from 'node:assert/strict'; |
| 5 | +import { mkdtempSync, rmSync, writeFileSync, readFileSync, chmodSync, mkdirSync } from 'node:fs'; |
| 6 | +import { tmpdir } from 'node:os'; |
| 7 | +import path from 'node:path'; |
| 8 | +import test from 'node:test'; |
| 9 | +import { loadSeenIds } from '../src/common/seen-ids.mjs'; |
| 10 | +import { startRoomPoller } from '../src/team-relay/room-poller.mjs'; |
| 11 | + |
| 12 | +test('old messages below the room watermark are marked seen and never notified, new ones still are', async () => { |
| 13 | + const dir = mkdtempSync(path.join(tmpdir(), 'iak-wm-')); |
| 14 | + const savedPath = process.env.PATH; |
| 15 | + let timers; |
| 16 | + try { |
| 17 | + const stubDir = path.join(dir, 'bin'); mkdirSync(stubDir); |
| 18 | + // seed (limit=50): two messages from 31 Aug set the watermark. |
| 19 | + // first poll (limit=25): the wide window now shows three February/June |
| 20 | + // messages with ids nobody has seen (evicted from the seen cap on a real |
| 21 | + // box) plus one genuinely new message. |
| 22 | + writeFileSync(path.join(stubDir, 'curl'), `#!/bin/sh |
| 23 | +case "$*" in |
| 24 | + *limit=50*) echo '[{"id":"a1","from":"petrus","body":"old aug one","created_at":"2026-08-31T12:00:00Z"},{"id":"a2","from":"petrus","body":"old aug two","created_at":"2026-08-31T12:05:00Z"}]';; |
| 25 | + *) echo '[{"id":"n1","from":"petrus","body":"genuinely new","created_at":"2026-09-02T14:50:00Z"},{"id":"a2","from":"petrus","body":"old aug two","created_at":"2026-08-31T12:05:00Z"},{"id":"f1","from":"petrus","body":"from february","created_at":"2026-02-10T09:00:00Z"},{"id":"f2","from":"@x","body":"from june","created_at":"2026-06-03T09:00:00Z"},{"id":"f3","from":"petrus","body":"from may","created_at":"2026-05-01T09:00:00Z"}]';; |
| 26 | +esac |
| 27 | +`); |
| 28 | + chmodSync(path.join(stubDir, 'curl'), 0o755); |
| 29 | + process.env.PATH = `${stubDir}:${savedPath}`; |
| 30 | + const seenFile = path.join(dir, 'seen'); const notifyFile = path.join(dir, 'notify'); |
| 31 | + const origLog = console.log; console.log = () => {}; |
| 32 | + try { |
| 33 | + timers = await startRoomPoller({ |
| 34 | + rooms: ['r'], apiKey: 'k', handle: '@t', interval: 3600, |
| 35 | + config: { poller: { seen_file: seenFile, notification_file: notifyFile, heartbeat_file: path.join(dir, 'hb'), nudge_mode: 'none', owner_handle: 'petrus', history_file: path.join(dir, 'hist.json') }, queue: { path: path.join(dir, 'q.jsonl') } } |
| 36 | + }); |
| 37 | + } finally { console.log = origLog; } |
| 38 | + const lines = readFileSync(notifyFile, 'utf8').split('\n').filter(Boolean); |
| 39 | + assert.equal(lines.filter((l) => /petrus: genuinely new/.test(l)).length, 1, `the new message is notified once: ${lines.join(' | ')}`); |
| 40 | + assert.ok(!lines.some((l) => /from february|from june|from may/.test(l)), `old messages never reach the notification file: ${lines.join(' | ')}`); |
| 41 | + const seen = loadSeenIds(seenFile); |
| 42 | + for (const id of ['a1', 'a2', 'n1', 'f1', 'f2', 'f3']) assert.ok(seen.has(id), `${id} is in the seen file`); |
| 43 | + const hist = JSON.parse(readFileSync(path.join(dir, 'hist.json'), 'utf8')); |
| 44 | + assert.equal(hist._marks.r, '2026-09-02T14:50:00.000Z', 'watermark advanced to the new message'); |
| 45 | + } finally { |
| 46 | + if (timers?.roomTimer) clearInterval(timers.roomTimer); |
| 47 | + if (timers?.dmTimer) clearInterval(timers.dmTimer); |
| 48 | + process.env.PATH = savedPath; |
| 49 | + rmSync(dir, { recursive: true, force: true }); |
| 50 | + } |
| 51 | +}); |
0 commit comments