Files
plugins/packages/fingerprint/tests/detector.test.ts
T
KaranocaVe 00f4d84b8f fingerprint 0.1.0: a page that checks whether a model answers like the one it claims to be (#516)
Adds the Fingerprint page plugin (packages/fingerprint): it checks whether a model behind the local magpie gateway answers like the model it claims to be.

Reviewed at 531ca11f4 (KaranocaVe). Run at that head merged with main 848497c, through mote-run on the Mac mini with Bun 1.4.2:
- bun run rebuild from the committed bun.lock (frozen, --ignore-scripts): typecheck, 30/30 tests, build; diff -r against the committed dist/ is empty.
- HOME=$(mktemp -d) bun test: 3838 pass, 0 fail. bun scripts/check.mjs: exit 0.
- Offline preload refusing every non-loopback fetch/socket: 30/30, nothing refused.
- Broken by hand, each red: ensureDetector() in bootstrap, dropping ttl: Infinity, validating the job after the download.

Maintainer decision: the reference data comes from two third-party hosts (lm.ikale.io, cdn.jsdmirror.com), only on an explicit Download or a valid job, and both are named on the page, in registry.json and the READMEs. It is data used locally to rank candidates and is sent nowhere; the @latest fallback has no pinned hash, so a bad snapshot would rank wrongly, which the page states.

Not on npm yet (E404): magpie installs it from the dist branch until an owner publishes the first version.
2026-10-31 02:00:33 +08:00

158 lines
5.2 KiB
TypeScript

import { describe, expect, test } from 'bun:test';
import { createJob, runJob } from '../src/detector.js';
import type { Model, RunConfig } from '../src/types.js';
const model: Model = {
id: 'm1',
name: 'Model 1',
provider: 'p',
efforts: ['default'],
nativeEndpoints: ['/v1/responses', '/v1/chat/completions'],
};
const config: RunConfig = {
targets: [{ model: 'm1' }, { model: 'm1', account: 'a' }],
api: 'auto',
effort: 'default',
serviceTier: 'default',
strict: false,
targetParallel: 2,
sampleParallel: 2,
deadlineSeconds: 30,
};
const sample = (expected_count: number) => ({
state: 'complete' as const,
text: '1, 2, 3',
raw_text: '1, 2, 3',
count: 3,
expected_count,
first_byte_ms: 1,
response_model: 'm1',
response_id: 'r',
error: null,
});
describe('detector jobs', () => {
test('generates one shared challenge batch and resolves auto API', () => {
const job = createJob(config, [model], '2026-01-01');
expect(job.challenges).toHaveLength(3);
expect(job.results[0].api).toBe('responses');
expect(job.results[0].samples.map((s) => s.expected_count)).toEqual(
job.challenges.map((c) => c.expected_count),
);
expect(job.results[1].samples.map((s) => s.expected_count)).toEqual(
job.results[0].samples.map((s) => s.expected_count),
);
});
test('uses per-sample endpoint and scores injected samples', async () => {
const job = createJob({ ...config, targets: [{ model: 'm1' }] }, [model], 'build');
const indexes: number[] = [];
let analyzed = 0;
let seenStrictCount = 0;
await runJob(job, {
detector: {
analyze: (outputs: unknown[]) => {
analyzed = outputs.length;
return { decision: 'partial' } as never;
},
} as never,
signal: new AbortController().signal,
sampleEndpoint: (_result, index) => {
indexes.push(index);
return { baseUrl: 'https://proxy.test', apiKey: 'secret' };
},
sample: async (requestConfig, challenge) => {
seenStrictCount = requestConfig.count ?? 0;
return sample(challenge.expected_count);
},
onUpdate: () => {},
});
expect(indexes.sort()).toEqual([0, 1, 2]);
expect(analyzed).toBe(3);
expect(seenStrictCount).toBe(3);
expect(job.state).toBe('completed');
expect(job.results[0].analysis?.decision).toBe('partial');
});
test('keeps partial evidence when one sample fails', async () => {
const job = createJob({ ...config, targets: [{ model: 'm1' }] }, [model], 'build');
let analyzed = 0;
await runJob(job, {
detector: {
analyze: (outputs: unknown[]) => {
analyzed = outputs.length;
return {} as never;
},
} as never,
signal: new AbortController().signal,
sampleEndpoint: () => ({ baseUrl: 'https://proxy.test', apiKey: 'secret' }),
sample: async (_config, challenge) =>
challenge.expected_count === job.challenges[0].expected_count
? { ...sample(challenge.expected_count), state: 'failed', error: 'upstream' }
: sample(challenge.expected_count),
onUpdate: () => {},
});
expect(analyzed).toBe(2);
expect(job.results[0].state).toBe('failed');
});
test('honors proxy mismatch and does not overwrite provenance with vendor alias', async () => {
const job = createJob({ ...config, targets: [{ model: 'm1' }] }, [model], 'build');
job.results[0].provenance[0] = {
session: 'proxy-session',
requestedModel: 'm1',
model: 'm1',
provider: 'p',
};
job.results[0].provenance[1] = {
session: 'proxy-session',
requestedModel: 'm1',
model: 'other',
mismatch: true,
};
let analyzed = false;
await runJob(job, {
detector: {
analyze: () => {
analyzed = true;
return {} as never;
},
} as never,
signal: new AbortController().signal,
sampleEndpoint: () => ({ baseUrl: 'https://proxy.test', apiKey: 'secret' }),
sample: async (_config, challenge) => ({
...sample(challenge.expected_count),
response_model: 'vendor-alias',
}),
onUpdate: () => {},
});
expect(analyzed).toBe(false);
expect(job.results[0].provenance[0].session).toBe('proxy-session');
expect(job.results[0].provenance[0].model).toBe('m1');
expect(job.results[0].state).toBe('failed');
expect(job.state).toBe('failed');
});
test('does not score cancelled queued samples', async () => {
const job = createJob({ ...config, targets: [{ model: 'm1' }] }, [model], 'build');
const controller = new AbortController();
controller.abort();
let analyzed = false;
await runJob(job, {
detector: {
analyze: () => {
analyzed = true;
return {} as never;
},
} as never,
signal: controller.signal,
sampleEndpoint: () => ({ baseUrl: 'https://proxy.test', apiKey: 'secret' }),
sample: async (_config, challenge) => sample(challenge.expected_count),
onUpdate: () => {},
});
expect(analyzed).toBe(false);
expect(job.state).toBe('cancelled');
expect(job.results[0].samples.every((item) => item.state === 'cancelled')).toBe(true);
});
});