PronounsPage/server/analyseLinks.js
Adaline Simonian 23a3862ca0
test: introduce snapshot-based smoke tests
- Adds a new test suite with Docker-based smoke tests for all locales.
  Can be run using the ./smoketest.sh script.
- Replaces all calls to Math.random() with a new helper that returns 0.5
  in snapshot testing mode, ensuring deterministic snapshots.
- Similarly replaces all calls to new Date() and Date.now() with new
  helpers that return a fixed date in snapshot testing mode.
- Replaces checks against NODE_ENV with APP_ENV, to ensure that the
  bundles can be built with Nuxt for testing without losing code that
  would otherwise be stripped out by production optimizations.
- Adds a database init script that can be used to initialize the
  database with a single admin user and a long-lived JWT token for use
  in automation tests.
- Adds a JWT decoding/encoding CLI tool for debugging JWTs.

Note: Snapshots are not checked in, and must be generated manually. See
test/__snapshots__/.gitignore for more information.
2025-02-02 23:11:19 -08:00

56 lines
2.1 KiB
JavaScript

import './setup.ts';
import SQL from 'sql-template-strings';
import { newDate, random } from '../src/helpers.ts';
import { LinkAnalyser } from '../src/links.ts';
import dbConnection from './db.ts';
import copyImage from './imageCopy.ts';
const timer = (ms) => new Promise((res) => setTimeout(res, ms));
(async () => {
const analyser = new LinkAnalyser();
const db = await dbConnection();
while (true) {
const chunk = await db.all(
SQL`SELECT url
FROM links
WHERE (expiresAt IS NULL OR expiresAt <= ${newDate() / 1000})
ORDER BY RANDOM()
LIMIT 64`,
);
console.log(`Fetching ${chunk.length} links: (${chunk.map((l) => l.url).join(', ')})`);
if (chunk.length === 0) {
await timer(1000);
continue;
}
const results = await Promise.all(chunk.map(({ url }) => Promise.race([
analyser.analyse(url).then(async (result) => {
if (result.favicon) {
result.faviconCache = await copyImage('favicon-cache', result.favicon, 30);
}
return result;
}),
new Promise((resolve) => setTimeout(() => resolve({ url, error: new Error('timeout') }), 12000)),
])));
for (const result of results) {
// random TTL (0-30 days) in order to spread out the legacy load
const expireAt = parseInt(newDate() / 1000) + parseInt(30 * 24 * 60 * 60 * random());
if (result.error) {
console.error(result);
await db.get(SQL`UPDATE links SET expiresAt = ${expireAt} WHERE url=${result.url}`);
} else {
await db.get(SQL`UPDATE links
SET expiresAt = ${expireAt},
favicon = ${result.favicon},
faviconCache = ${result.faviconCache},
relMe = ${JSON.stringify(result.relMe)},
nodeinfo = ${JSON.stringify(result.nodeinfo)}
WHERE url=${result.url}`);
}
}
}
})();