Files
plugins/packages/fingerprint/tests/gateway.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

80 lines
2.7 KiB
TypeScript

import { expect, test } from 'bun:test';
import { Gateway, accountHeader } from '../src/gateway';
test('catalog reads cached local and remote subscription accounts without leaking quota data', async () => {
const paths: string[] = [];
const server = Bun.serve({
hostname: '127.0.0.1',
port: 0,
fetch(req) {
const url = new URL(req.url);
paths.push(url.pathname + url.search);
return Response.json(
url.pathname === '/v1/models'
? { data: [{ id: 'vendor/model' }] }
: {
data: [
{
provider: 'vendor',
user: 'local-account',
name: 'Vendor',
alias: 'Local',
balance: '$42',
secret: 'excluded',
},
{
provider: 'vendor',
user: 'remote-account',
name: 'Vendor',
kind: 'subscription',
},
{ provider: 'vendor', user: 'key-card', name: 'Key', kind: 'balance' },
{ provider: 'vendor', kind: 'subscription' },
],
},
);
},
});
try {
const { accounts } = await new Gateway(`http://127.0.0.1:${server.port}`).catalog();
expect(paths).toContain('/v1/magpie/quotas?kept=1');
expect(accounts).toEqual([
{ provider: 'vendor', id: 'local-account', name: 'Local' },
// name is the provider's; an account with no alias is named by its user
{ provider: 'vendor', id: 'remote-account', name: 'remote-account' },
]);
} finally {
server.stop(true);
}
});
// fetch refuses a header value that isn't ASCII (StockRabbit's 北冥,
// plugins#615): such a name goes percent-encoded, an ASCII one as it is
test('X-Magpie-Account carries a name that is not ASCII percent-encoded', async () => {
expect(accountHeader('北冥')).toBe('%E5%8C%97%E5%86%A5');
expect(decodeURIComponent(accountHeader('北冥'))).toBe('北冥');
expect(accountHeader('csmlovenn@gmail.com')).toBe('csmlovenn@gmail.com');
const seen: string[] = [];
const server = Bun.serve({
hostname: '127.0.0.1',
port: 0,
fetch(req) {
seen.push(req.headers.get('x-magpie-account') || '');
return new Response('ok');
},
});
try {
const url = `http://127.0.0.1:${server.port}/`;
// the reporter's error, raw
const raw = await (async () => fetch(url, { headers: { 'X-Magpie-Account': '北冥' } }))().then(
() => '',
(e) => String(e),
);
expect(raw).toMatch(/invalid/i);
await fetch(url, { headers: { 'X-Magpie-Account': accountHeader('北冥') } });
expect(seen).toEqual(['%E5%8C%97%E5%86%A5']);
} finally {
server.stop(true);
}
});