Files
plugins/packages/fingerprint/tests/bank.test.ts
T
yetone 529929fd93 fingerprint 0.2.2: an account named 北冥 pins its samples, and the job file names the account (#615)
StockRabbit (#615): with the WorkBuddy account 北冥 picked, every sample
failed with "Header 'X-Magpie-Account' has invalid value", since fetch
refuses a header value that isn't ASCII. The saved job also didn't say
which account it ran on.

- gateway.ts: accountHeader() sends printable ASCII as it is and any other
  name percent-encoded; magpie v0.1.2658 (ad53a23dc) decodes it. The
  account list now names an account by its alias or user, not by the
  provider's display name.
- detector.ts: a target's account resolves to the account's id (by id or
  a unique name for that model's provider), and the job keeps
  config.account / config.accountName and results[].target.account /
  accountName. Jobs written before 0.2.2 have neither and load unchanged.
- web: the result card names the account.
- integration.md: the header's encoding and a Job files section.

Run (mote, sandbox HOME): bun install --frozen-lockfile, bun run
typecheck, bun test (36 pass, 0 fail), bun run build, bun scripts/check.mjs
(exit 0). tests/account.test.ts runs the server against a fake gateway
whose quotas list 北冥: an 0.2.1 job loads unchanged, a run pinned to 北冥
reaches the gateway as %E5%8C%97%E5%86%A5 on every request, and the job
file names 北冥. The page on mote in Chromium and WebKit at 420px against
the same fake gateway: the picker shows "北冥 · workbuddy", 12 requests
carry the encoded header, the card says 北冥.

Broken by hand: accountHeader returning the raw name turns account.test
red (no request reaches the gateway pinned).

Not tried: a real WorkBuddy account, Windows, the page in a language other
than English.
2026-11-02 07:20:47 +08:00

180 lines
6.3 KiB
TypeScript

import { expect, test } from 'bun:test';
import { mkdtemp, mkdir, writeFile, utimes, rm } from 'node:fs/promises';
import { tmpdir } from 'node:os';
import { join } from 'node:path';
import { startServer } from '../src/server';
import { cacheFixture } from './bank-fixture';
const config = {
targets: [{ model: 'vendor/model' }],
api: 'auto',
effort: '',
serviceTier: 'default',
strict: false,
targetParallel: 1,
sampleParallel: 3,
deadlineSeconds: 10,
};
async function fixture(cached = false, downloadable = false) {
const dir = await mkdtemp(join(tmpdir(), 'fingerprint-bank-test-'));
const downloads: Request[] = [];
let samples = 0;
const source = Bun.serve({
hostname: '127.0.0.1',
port: 0,
fetch(req) {
downloads.push(req);
if (downloadable) {
const cache = cacheFixture();
const bankLength = cache.readUInt32LE(0);
if (new URL(req.url).pathname.endsWith('/bundle.json'))
return new Response(cache.subarray(4, 4 + bankLength));
const bytes = cache.subarray(4 + bankLength);
// A single-segment Zstandard frame containing one uncompressed block.
const header = Buffer.from([0x28, 0xb5, 0x2f, 0xfd, 0x20, bytes.length, 0, 0, 0]);
header.writeUIntLE((bytes.length << 3) | 1, 6, 3);
return new Response(Buffer.concat([header, bytes]));
}
return new Response('Unavailable', { status: 503 });
},
});
const gateway = Bun.serve({
hostname: '127.0.0.1',
port: 0,
fetch(req) {
const path = new URL(req.url).pathname;
if (path === '/v1/models') return Response.json({ data: [{ id: 'vendor/model' }] });
if (req.method === 'POST') samples++;
return Response.json(
{ error: { message: 'Test gateway does not sample' }, data: [] },
{ status: req.method === 'POST' ? 503 : 200 },
);
},
});
let service: Awaited<ReturnType<typeof startServer>> | undefined;
try {
await mkdir(join(dir, 'ui'));
await writeFile(join(dir, 'ui/index.html'), '<head></head>');
if (cached) {
await writeFile(join(dir, 'reference.bin'), cacheFixture());
await utimes(join(dir, 'reference.bin'), new Date(0), new Date(0));
}
service = await startServer({
port: 0,
dataDir: dir,
uiDir: join(dir, 'ui'),
gateway: `http://127.0.0.1:${gateway.port}`,
apiKey: 'gateway-secret',
bankSource: `http://127.0.0.1:${source.port}/`,
demo: false,
});
const base = `http://127.0.0.1:${service.port}`;
const html = await (await fetch(base)).text();
const token = JSON.parse(html.match(/window\.__FINGERPRINT__=(.*?)<\/script>/)![1]).token;
return {
base,
downloads,
cachedBytes: () => Bun.file(join(dir, 'reference.bin')).arrayBuffer(),
samples: () => samples,
api: (path: string, init: RequestInit = {}) =>
fetch(`${base}/api/${path}`, {
...init,
headers: {
authorization: `Bearer ${token}`,
'content-type': 'application/json',
},
}),
async close() {
await service?.close();
source.stop(true);
gateway.stop(true);
await rm(dir, { recursive: true, force: true });
},
};
} catch (error) {
await service?.close();
source.stop(true);
gateway.stop(true);
await rm(dir, { recursive: true, force: true });
throw error;
}
}
test('opening, refreshing and invalid jobs never download reference data; explicit actions do', async () => {
const f = await fixture();
try {
for (let i = 0; i < 2; i++) {
const boot = await (await f.api('bootstrap')).json();
expect(boot.bank).toBeNull();
expect(boot.bankStatus.state).toBe('missing');
}
expect((await fetch(`${f.base}/api/bank/download`, { method: 'POST' })).status).toBe(401);
expect((await f.api('jobs', { method: 'POST', body: 'null' })).status).toBe(400);
expect(f.downloads).toHaveLength(0);
expect(f.samples()).toBe(0);
expect((await f.api('bank/download', { method: 'POST' })).status).toBe(503);
expect(f.downloads).toHaveLength(1);
expect(f.downloads[0].headers.has('authorization')).toBe(false);
expect((await f.api('bootstrap')).status).toBe(200);
expect(f.downloads).toHaveLength(1);
expect((await f.api('jobs', { method: 'POST', body: JSON.stringify(config) })).status).toBe(
503,
);
expect(f.downloads).toHaveLength(2);
expect(f.samples()).toBe(0);
} finally {
await f.close();
}
});
test('an old cached snapshot is reused without background network refresh', async () => {
const f = await fixture(true);
try {
expect((await (await f.api('bootstrap')).json()).bankStatus.state).toBe('cached');
const created = await f.api('jobs', {
method: 'POST',
body: JSON.stringify(config),
});
expect(created.status).toBe(202);
const job = await created.json();
expect(job.bank.builtAt).toBe('test-snapshot');
for (let i = 0; i < 100; i++) {
const current = await (await f.api(`jobs/${job.id}`)).json();
if (!['queued', 'running'].includes(current.state)) break;
await Bun.sleep(10);
}
expect(f.downloads).toHaveLength(0);
expect((await (await f.api('bootstrap')).json()).bankStatus.state).toBe('ready');
expect((await f.api('bank/download', { method: 'POST' })).status).toBe(503);
expect(f.downloads).toHaveLength(1);
expect((await (await f.api('bootstrap')).json()).bank.builtAt).toBe('test-snapshot');
expect(f.downloads).toHaveLength(1);
} finally {
await f.close();
}
});
test('an explicit download installs a usable snapshot without sending model requests', async () => {
const f = await fixture(false, true);
try {
expect((await (await f.api('bootstrap')).json()).bankStatus.state).toBe('missing');
const response = await f.api('bank/download', { method: 'POST' });
expect(response.status).toBe(200);
expect(await response.json()).toEqual({
bank: { builtAt: 'test-snapshot', modelCount: 0 },
bankStatus: { state: 'ready' },
});
expect(Buffer.from(await f.cachedBytes())).toEqual(cacheFixture());
expect(f.downloads.map((req) => new URL(req.url).pathname)).toEqual([
'/bundle.json',
'/chunks/fixture.zst',
]);
expect(f.samples()).toBe(0);
await f.api('bootstrap');
expect(f.downloads).toHaveLength(2);
} finally {
await f.close();
}
});