Files
wikideck/tests/wikimedia-helpers.ts
claudeandClaude Opus 5.5 f544019bfe Polite Wikimedia client, mint-path fix, disk cache and mining prefetch pool (#62)
- server/wikimedia/client.ts: one process-wide client. Per-host token bucket
  (1 req/s, burst 3), 1 in flight per host, mint > interactive > background
  lanes with a bounded queue, maxlag=5 on Action API requests, Retry-After
  and backoff, a circuit breaker after 3 consecutive 429s, a daily cap, and
  a per-egress-IP budget (WIKIDECK_WIKIMEDIA_NODES_PER_IP).
- server/wikimedia/cache.ts: node:sqlite HTTP cache with ETag/If-None-Match,
  stale-while-revalidate on the background lane. Mint reads never use it.
- Mint fix: a 429/5xx/timeout/non-JSON reply on pageviews now fails getFresh()
  (template 503, miner retries) instead of minting attention: null forever.
- server/wikimedia/pool.ts: per-tip article+snapshot prefetch pool for
  /api/mining/template, about 3 requests per block regardless of miner count.
- User-Agent with an operator contact (WIKIDECK_OPERATOR_CONTACT or
  PUBLIC_ORIGIN) and a start-up warning when it is unset.
- Mint request parameters are byte-identical apart from maxlag (golden test).
  No consensus, block or protocol changes. Tests use fake fetch only.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 21:51:20 -05:00

53 lines
2.0 KiB
TypeScript

// Shared fakes for tests/wikimedia-*.test.ts: a manual clock and a recording fetch. Zero network.
import type { Clock } from '../server/wikimedia/client.ts';
export class FakeClock implements Clock {
time: number;
private timers: { at: number; fn: () => void; id: number }[] = [];
private nextId = 1;
constructor(start = Date.parse('2026-11-17T12:00:00Z')) { this.time = start; }
now(): number { return this.time; }
setTimeout(fn: () => void, ms: number): unknown {
const id = this.nextId++;
this.timers.push({ at: this.time + Math.max(0, ms), fn, id });
return id;
}
clearTimeout(handle: unknown): void { this.timers = this.timers.filter(t => t.id !== handle); }
/** Advance time, firing due timers in order and letting promise chains settle between them. */
async advance(ms: number): Promise<void> {
const target = this.time + ms;
await flush();
for (;;) {
this.timers.sort((a, b) => a.at - b.at || a.id - b.id);
const next = this.timers[0];
if (!next || next.at > target) break;
this.timers.shift();
this.time = Math.max(this.time, next.at);
next.fn();
await flush();
}
this.time = target;
await flush();
}
}
export async function flush(): Promise<void> {
for (let i = 0; i < 10; i++) await new Promise(resolve => setImmediate(resolve));
}
export function json(value: unknown, headers: Record<string, string> = {}, status = 200): Response {
return new Response(JSON.stringify(value), { status, headers: { 'Content-Type': 'application/json; charset=utf-8', ...headers } });
}
export type Call = { url: URL; headers: Headers; at: number };
export function recorder(clock: { now(): number }, handler: (url: URL, headers: Headers) => Response | Promise<Response>) {
const calls: Call[] = [];
const fetcher: typeof fetch = async (input, init) => {
const url = new URL(String(input));
const headers = new Headers(init?.headers);
calls.push({ url, headers, at: clock.now() });
return handler(url, headers);
};
return { calls, fetcher };
}