forked from WikiDeck/wikideck
Default UA unchanged; the testnet sets a contact URL in deploy/night only. Testnet wallet password moves to an env var. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
49 lines
2.6 KiB
TypeScript
49 lines
2.6 KiB
TypeScript
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { WikipediaArticleSource } from '../server/wiki.ts';
|
|
import { WikipediaEnrichment } from '../server/enrichment.ts';
|
|
import { ARTICLE_USER_AGENT, ENRICHMENT_USER_AGENT, wikimediaUserAgent } from '../server/wikimedia-user-agent.ts';
|
|
|
|
function capture() {
|
|
const agents: string[] = [];
|
|
const fetcher: typeof fetch = async (_input, init) => {
|
|
agents.push(new Headers(init?.headers).get('User-Agent') ?? '');
|
|
return new Response('rate limited', { status: 429, headers: { 'Content-Type': 'text/plain' } });
|
|
};
|
|
return { agents, fetcher };
|
|
}
|
|
async function agentsFor(env: string | undefined) {
|
|
const previous = process.env.WIKIDECK_USER_AGENT;
|
|
if (env === undefined) delete process.env.WIKIDECK_USER_AGENT; else process.env.WIKIDECK_USER_AGENT = env;
|
|
try {
|
|
const article = capture(), enrichment = capture();
|
|
await assert.rejects(new WikipediaArticleSource(article.fetcher).random());
|
|
await new WikipediaEnrichment({ fetch: enrichment.fetcher }).getFresh(12345).catch(() => {});
|
|
return { article: article.agents, enrichment: enrichment.agents };
|
|
} finally {
|
|
if (previous === undefined) delete process.env.WIKIDECK_USER_AGENT; else process.env.WIKIDECK_USER_AGENT = previous;
|
|
}
|
|
}
|
|
|
|
test('default Wikimedia user agents are unchanged when WIKIDECK_USER_AGENT is unset', async () => {
|
|
assert.equal(ARTICLE_USER_AGENT, 'Wikideck/0.1 (experimental Wikipedia article cards)');
|
|
assert.equal(ENRICHMENT_USER_AGENT, 'Wikideck/0.1 (Wikipedia off-chain page enrichment)');
|
|
const { article, enrichment } = await agentsFor(undefined);
|
|
assert.ok(article.length > 0 && article.every(a => a === ARTICLE_USER_AGENT));
|
|
assert.ok(enrichment.length > 0 && enrichment.every(a => a === ENRICHMENT_USER_AGENT));
|
|
});
|
|
|
|
test('WIKIDECK_USER_AGENT overrides both Wikimedia clients', async () => {
|
|
const ua = 'WikiDeck-testnet/0.1 (https://gitea.eom.dev/claude/wikideck; private testnet)';
|
|
const { article, enrichment } = await agentsFor(ua);
|
|
assert.ok(article.length > 0 && article.every(a => a === ua));
|
|
assert.ok(enrichment.length > 0 && enrichment.every(a => a === ua));
|
|
});
|
|
|
|
test('blank, oversized or control-character overrides fall back to the default', () => {
|
|
for (const bad of ['', ' ', 'x'.repeat(257), 'bad\r\nX-Injected: 1', 'tab\there'])
|
|
assert.equal(wikimediaUserAgent('fallback', { WIKIDECK_USER_AGENT: bad }), 'fallback');
|
|
assert.equal(wikimediaUserAgent('fallback', {}), 'fallback');
|
|
assert.equal(wikimediaUserAgent('fallback', { WIKIDECK_USER_AGENT: ' Custom/1.0 (x) ' }), 'Custom/1.0 (x)');
|
|
});
|