| 1 | // Remote thread crawl — the stale-while-revalidate guard. The BFS itself is network-bound
|
|---|
| 2 | // (verified live), so here we only cover that the entry point exists, is safe with no cached
|
|---|
| 3 | // replies (no seeds → no crawl), and is TTL-gated. In-memory SQLite. Run: npm test
|
|---|
| 4 | import { test } from 'node:test';
|
|---|
| 5 | import assert from 'node:assert/strict';
|
|---|
| 6 |
|
|---|
| 7 | process.env.DATABASE_PATH = ':memory:';
|
|---|
| 8 | process.env.PUBLIC_BASE_URL = 'https://test.example';
|
|---|
| 9 |
|
|---|
| 10 | const dbMod = await import('../src/config/database.js');
|
|---|
| 11 | const db = dbMod.default;
|
|---|
| 12 | dbMod.initializeDatabase();
|
|---|
| 13 | const AP = (await import('../src/services/ActivityPubService.js')).default;
|
|---|
| 14 |
|
|---|
| 15 | test('maybeCrawlThread is exported and safe with no cached replies', () => {
|
|---|
| 16 | assert.equal(typeof AP.maybeCrawlThread, 'function');
|
|---|
| 17 | // No ap_interactions rows for this post → the crawl finds no seeds and does nothing.
|
|---|
| 18 | AP.maybeCrawlThread('no-replies-post');
|
|---|
| 19 | assert.ok(true, 'did not throw');
|
|---|
| 20 | });
|
|---|
| 21 |
|
|---|
| 22 | test('a second call within the TTL is gated (timestamp recorded)', () => {
|
|---|
| 23 | AP.maybeCrawlThread('ttl-post');
|
|---|
| 24 | const row = db.prepare('SELECT value FROM app_settings WHERE key = ?').get('thread_crawl:ttl-post');
|
|---|
| 25 | assert.ok(row && Number(row.value) > 0, 'crawl timestamp recorded');
|
|---|
| 26 | // Immediately calling again must not reset/refire (still gated by the fresh timestamp).
|
|---|
| 27 | const before = row.value;
|
|---|
| 28 | AP.maybeCrawlThread('ttl-post');
|
|---|
| 29 | const after = db.prepare('SELECT value FROM app_settings WHERE key = ?').get('thread_crawl:ttl-post').value;
|
|---|
| 30 | assert.equal(after, before, 'timestamp unchanged within TTL');
|
|---|
| 31 | });
|
|---|