// A link-preview crawler (Telegram, Slack, X…) reads a post's page once and // keeps that card, so it waits for the thread a visitor wouldn't: a post that // takes 1.5 s to find gets its own title and text for TelegramBot and the plain // page (on time) for a browser. Run: bun tools/note-page-bot-test.js import assert from 'node:assert/strict'; import { generateSecretKey, getPublicKey, finalizeEvent } from 'nostr-tools/pure'; import { neventEncode } from 'nostr-tools/nip19'; import { createThreadCache, notePageResponse, isPreviewBot } from '../feed/note-page.js'; const sk = generateSecretKey(), pk = getPublicKey(sk); const post = finalizeEvent({ kind: 1, created_at: 1, tags: [], content: 'Moving coins out of old addresses into a fresh wallet', }, sk); const prof = finalizeEvent({ kind: 0, created_at: 1, tags: [], content: JSON.stringify({ name: 'Adam' }) }, sk); const html = 'Coinos'; const path = '/' + neventEncode({ id: post.id, author: pk }); const slow = () => createThreadCache({ build: () => new Promise((r) => setTimeout(() => r({ v: 1, root: post, replies: [], profiles: [prof] }), 1500)) }); const req = (ua) => new Request('https://v3.coinos.io' + path, { headers: { 'user-agent': ua } }); for (const ua of ['TelegramBot (like TwitterBot)', 'Mozilla/5.0 (compatible; Discordbot/2.0; +https://discordapp.com)', 'facebookexternalhit/1.1', 'Slackbot-LinkExpanding 1.0', 'WhatsApp/2.23']) assert.ok(isPreviewBot(ua), ua); assert.ok(!isPreviewBot('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0 Safari/537.36')); let t = Date.now(); let page = await (await notePageResponse(req('TelegramBot (like TwitterBot)'), { index: async () => html, threads: slow() })).text(); assert.match(page, /Adam on Coinos<\/title>/); assert.match(page, /og:description" content="Moving coins out of old addresses/); assert.doesNotMatch(page, /og:title" content="Coinos"/); console.log('✅ the crawler waited', Date.now() - t, 'ms for the post'); t = Date.now(); page = await (await notePageResponse(req('Mozilla/5.0 (X11; Linux x86_64) Chrome/131.0'), { index: async () => html, threads: slow() })).text(); assert.ok(Date.now() - t < 1000, 'a browser is not held up'); assert.match(page, /<title>Coinos<\/title>/); console.log('✅ a browser gets the page on time', Date.now() - t, 'ms');