mirror of
https://github.com/HeyPuter/puter.git
synced 2026-11-26 15:48:41 +00:00
test(ai): fuzz vendor replies, gateway catalogs and recorded usage (#4729)
Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
This commit is contained in:
1 parent
3b5d6619dd
commit
02a2d87d5e
2 files changed
+787
-2
No files matched your search
@@ -182,6 +182,49 @@ const healthyUpstream = ({
|
||||
json,
|
||||
}: UpstreamRequest): UpstreamReply | undefined => {
|
||||
const path = url.pathname;
|
||||
if (path.endsWith('/api/tags'))
|
||||
return { body: { models: [{ name: 'gpt-oss:20b', size: 8192 }] } };
|
||||
if (path.endsWith('/quota'))
|
||||
return { body: { accounting_method: 'token' } };
|
||||
if (path.endsWith('/endpoints'))
|
||||
return { body: { data: { endpoints: [] } } };
|
||||
if (path.endsWith('/models')) {
|
||||
if (url.hostname.includes('together'))
|
||||
return {
|
||||
body: [
|
||||
{
|
||||
id: 'meta-llama/Llama-3.3-70B-Instruct-Turbo',
|
||||
type: 'chat',
|
||||
display_name: 'Llama',
|
||||
context_length: 8192,
|
||||
pricing: { input: 1, output: 2 },
|
||||
},
|
||||
],
|
||||
};
|
||||
return {
|
||||
body: {
|
||||
data: [
|
||||
{
|
||||
id: 'fuzz/model',
|
||||
name: 'Fuzz',
|
||||
category_type: 'LLM',
|
||||
supported_endpoint_types: ['openai'],
|
||||
min_prompt_price: 1,
|
||||
min_completion_price: 2,
|
||||
context_length: 8192,
|
||||
pricing: { prompt: '0.000001', completion: '0.000002' },
|
||||
metadata: {
|
||||
pricing: {
|
||||
currency: 'USD',
|
||||
input_per_million: 1,
|
||||
output_per_million: 2,
|
||||
},
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
}
|
||||
// A caller-supplied media link that leads nowhere.
|
||||
if (url.hostname === 'upstream.test') return { status: 404, body: '' };
|
||||
if (url.hostname === 'media.upstream.test') {
|
||||
@@ -597,17 +640,57 @@ const providers = {
|
||||
alibaba: key,
|
||||
byteplus: key,
|
||||
hoonify: key,
|
||||
ollama: { enabled: false },
|
||||
infron: key,
|
||||
openrouter: key,
|
||||
neuralwatt: key,
|
||||
'together-ai': key,
|
||||
ollama: { apiBaseUrl: 'https://ollama.upstream.test' },
|
||||
};
|
||||
|
||||
let server: PuterServer;
|
||||
let driver: ChatCompletionDriver;
|
||||
// Charges no vendor answer should be able to cause.
|
||||
const charges: string[] = [];
|
||||
const isCatalogRequest = (path: string) =>
|
||||
/\/(models|api\/tags|quota|endpoints)$/.test(path);
|
||||
// Every provider gets the same share of cases, whatever its catalog size.
|
||||
let routes: Map<string, { provider: string; id: string }[]>;
|
||||
|
||||
beforeAll(async () => {
|
||||
upstream.install();
|
||||
upstream.respond = healthyUpstream;
|
||||
server = await setupTestServer();
|
||||
const metering = server.services.metering as unknown as Record<string, any>;
|
||||
const realBatch = metering.batchIncrementUsages.bind(metering);
|
||||
metering.batchIncrementUsages = (who: unknown, usages: any[]) => {
|
||||
for (const usage of usages ?? []) {
|
||||
// The ledger skips a zero amount.
|
||||
if (usage?.usageAmount === 0) continue;
|
||||
for (const [name, value] of [
|
||||
['amount', usage?.usageAmount],
|
||||
['cost', usage?.costOverride],
|
||||
] as const) {
|
||||
if (value === undefined || value === null) continue;
|
||||
const kind =
|
||||
typeof value !== 'number'
|
||||
? 'non-number'
|
||||
: !Number.isFinite(value)
|
||||
? 'non-finite'
|
||||
: value < 0
|
||||
? 'negative'
|
||||
: value > 1e15
|
||||
? 'huge'
|
||||
: !Number.isInteger(value) && name === 'amount'
|
||||
? ''
|
||||
: '';
|
||||
if (kind)
|
||||
charges.push(
|
||||
`${kind} ${name} ${usage?.usageType} ${String(value)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
return realBatch(who, usages);
|
||||
};
|
||||
driver = new ChatCompletionDriver(
|
||||
{ providers } as never,
|
||||
server.clients,
|
||||
@@ -665,12 +748,17 @@ describe('ChatCompletionDriver seeded fuzzing', () => {
|
||||
'gemini',
|
||||
'groq',
|
||||
'hoonify',
|
||||
'infron',
|
||||
'meta',
|
||||
'minimax',
|
||||
'mistral',
|
||||
'moonshotai',
|
||||
'neuralwatt',
|
||||
'ollama',
|
||||
'openai-completion',
|
||||
'openai-responses',
|
||||
'openrouter',
|
||||
'together-ai',
|
||||
'xai',
|
||||
'zai',
|
||||
]);
|
||||
@@ -758,9 +846,224 @@ describe('ChatCompletionDriver seeded fuzzing', () => {
|
||||
replies,
|
||||
);
|
||||
}
|
||||
for (const charge of charges.splice(0)) {
|
||||
findings.add(
|
||||
`${route.provider} metering ${charge.split(' ').slice(0, 2).join(' ')}`,
|
||||
index,
|
||||
`${route.id} ${charge}`,
|
||||
replies,
|
||||
);
|
||||
}
|
||||
clearUnhealthyRoutes();
|
||||
}
|
||||
findings.assertNone();
|
||||
expect(upstream.asked).toBeGreaterThan(askedBefore);
|
||||
}, 1_800_000);
|
||||
|
||||
it('classifies a 200 whose JSON body is cut short, on every provider', async () => {
|
||||
const findings = new Findings();
|
||||
const rejections: unknown[] = [];
|
||||
const onRejection = (reason: unknown) => rejections.push(reason);
|
||||
process.on('unhandledRejection', onRejection);
|
||||
const bodies = [
|
||||
'{"id":"x","choices":[{"mess',
|
||||
'',
|
||||
'<html>502</html>',
|
||||
'null',
|
||||
'[]',
|
||||
'"ok"',
|
||||
'0',
|
||||
];
|
||||
for (const [provider, list] of routes) {
|
||||
for (const body of bodies) {
|
||||
for (const stream of [false, true]) {
|
||||
upstream.respond = (request) =>
|
||||
isCatalogRequest(request.url.pathname)
|
||||
? healthyUpstream(request)
|
||||
: { body, contentType: 'application/json' };
|
||||
const error = await complete({
|
||||
messages: [{ role: 'user', content: 'hi' }],
|
||||
provider,
|
||||
model: list[0]!.id,
|
||||
stream,
|
||||
fallback: false,
|
||||
});
|
||||
await new Promise((resolve) => setTimeout(resolve, 5));
|
||||
const label = `${provider} stream=${stream} body=${JSON.stringify(body)}`;
|
||||
const unclassified = error && unclassifiedFailure(error);
|
||||
if (unclassified)
|
||||
findings.add(
|
||||
`${provider} ${unclassified.slice(0, 160)}`,
|
||||
0,
|
||||
label,
|
||||
body,
|
||||
);
|
||||
// A stream reports its failure in-band, so only a unary answer counts.
|
||||
if (!error && !stream)
|
||||
findings.add(
|
||||
`${provider} answered a request the vendor did not answer (stream=${stream})`,
|
||||
0,
|
||||
label,
|
||||
body,
|
||||
);
|
||||
for (const reason of rejections.splice(0))
|
||||
findings.add(
|
||||
`${provider} unhandled rejection ${(reason as Error)?.name}`,
|
||||
0,
|
||||
label,
|
||||
String((reason as Error)?.stack).slice(0, 500),
|
||||
);
|
||||
clearUnhealthyRoutes();
|
||||
}
|
||||
}
|
||||
}
|
||||
process.off('unhandledRejection', onRejection);
|
||||
upstream.respond = healthyUpstream;
|
||||
findings.assertNone();
|
||||
}, 600_000);
|
||||
|
||||
it('builds a sane catalog from a malformed vendor model list', async () => {
|
||||
const findings = new Findings();
|
||||
const dynamic = [
|
||||
'infron',
|
||||
'openrouter',
|
||||
'neuralwatt',
|
||||
'together-ai',
|
||||
'ollama',
|
||||
];
|
||||
for (let index = 0; index < fuzzCases(300); index++) {
|
||||
const random = caseRandom(0xca7a, index);
|
||||
const replies: unknown[] = [];
|
||||
upstream.respond = (request) => {
|
||||
const healthy = healthyUpstream(request);
|
||||
if (!healthy || !isCatalogRequest(request.url.pathname))
|
||||
return healthy;
|
||||
const reply = corruptReply(healthy, random);
|
||||
replies.push(
|
||||
request.url.hostname + request.url.pathname,
|
||||
reply.body,
|
||||
);
|
||||
return reply;
|
||||
};
|
||||
const fresh = new ChatCompletionDriver(
|
||||
{
|
||||
providers: Object.fromEntries(
|
||||
Object.entries(providers).filter(([name]) =>
|
||||
dynamic.includes(name),
|
||||
),
|
||||
),
|
||||
} as never,
|
||||
server.clients,
|
||||
server.stores,
|
||||
server.services,
|
||||
);
|
||||
try {
|
||||
await fresh.onServerStart();
|
||||
const models = await fresh.models();
|
||||
for (const model of models as unknown as Record<
|
||||
string,
|
||||
any
|
||||
>[]) {
|
||||
if (model.provider === 'fake-chat') continue;
|
||||
const bad: string[] = [];
|
||||
if (
|
||||
typeof model.id !== 'string' ||
|
||||
!model.id.trim() ||
|
||||
/undefined|\[object/.test(model.id)
|
||||
)
|
||||
bad.push(`id ${JSON.stringify(model.id)}`);
|
||||
for (const alias of model.aliases ?? [])
|
||||
if (typeof alias !== 'string')
|
||||
bad.push(`alias ${JSON.stringify(alias)}`);
|
||||
for (const [k, v] of Object.entries(model.costs ?? {}))
|
||||
if (
|
||||
typeof v !== 'number' ||
|
||||
!Number.isFinite(v) ||
|
||||
v < 0
|
||||
)
|
||||
bad.push(`cost ${k}=${String(v)}`);
|
||||
// A catalog writes 0 for a model that declares no output cap.
|
||||
for (const k of ['max_tokens', 'context'])
|
||||
if (
|
||||
model[k] !== undefined &&
|
||||
model[k] !== null &&
|
||||
(typeof model[k] !== 'number' ||
|
||||
!Number.isFinite(model[k]) ||
|
||||
model[k] < 0 ||
|
||||
(k === 'context' && model[k] === 0))
|
||||
)
|
||||
bad.push(`${k}=${JSON.stringify(model[k])}`);
|
||||
if (
|
||||
model.name !== undefined &&
|
||||
typeof model.name !== 'string'
|
||||
)
|
||||
bad.push(`name ${JSON.stringify(model.name)}`);
|
||||
for (const b of bad)
|
||||
findings.add(
|
||||
`${model.provider} catalog ${b.split(/[ =]/)[0]}`,
|
||||
index,
|
||||
`${b}`,
|
||||
replies,
|
||||
);
|
||||
}
|
||||
JSON.stringify(models);
|
||||
// One request per listed provider, answered healthily.
|
||||
upstream.respond = healthyUpstream;
|
||||
const seen = new Set<string>();
|
||||
for (const model of models) {
|
||||
if (
|
||||
!model.provider ||
|
||||
model.provider === 'fake-chat' ||
|
||||
seen.has(model.provider)
|
||||
)
|
||||
continue;
|
||||
seen.add(model.provider);
|
||||
try {
|
||||
const result = await withTestActor(() =>
|
||||
fresh.complete({
|
||||
messages: [{ role: 'user', content: 'hi' }],
|
||||
provider: model.provider,
|
||||
model: model.id,
|
||||
fallback: false,
|
||||
} as unknown as ICompleteArguments),
|
||||
);
|
||||
if (isDriverStreamResult(result))
|
||||
await readStreamResult(result);
|
||||
} catch (error) {
|
||||
const unclassified = unclassifiedFailure(error);
|
||||
if (unclassified)
|
||||
findings.add(
|
||||
`${model.provider} ${unclassified}`,
|
||||
index,
|
||||
String(model.id),
|
||||
replies,
|
||||
);
|
||||
}
|
||||
for (const charge of charges.splice(0))
|
||||
findings.add(
|
||||
`${model.provider} metering ${charge.split(' ').slice(0, 2).join(' ')}`,
|
||||
index,
|
||||
`${String(model.id)} ${charge} MODEL ${JSON.stringify(model)}`,
|
||||
[],
|
||||
);
|
||||
}
|
||||
} catch (error) {
|
||||
findings.add(
|
||||
`catalog build threw ${(error as Error)?.name}: ${String((error as Error)?.message).slice(0, 150)} ${(
|
||||
error as Error
|
||||
)?.stack
|
||||
?.split('\n')
|
||||
.filter((l) => l.includes('/src/'))
|
||||
.slice(0, 4)
|
||||
.join(' <- ')}`,
|
||||
index,
|
||||
'',
|
||||
replies,
|
||||
);
|
||||
}
|
||||
clearUnhealthyRoutes();
|
||||
}
|
||||
upstream.respond = healthyUpstream;
|
||||
findings.assertNone();
|
||||
}, 1_800_000);
|
||||
});
|
||||
@@ -24,6 +24,8 @@
|
||||
* each provider's request building, and the mapping of the refusal.
|
||||
*/
|
||||
|
||||
import { Readable } from 'node:stream';
|
||||
|
||||
import { PollyClient } from '@aws-sdk/client-polly';
|
||||
import { TextractClient } from '@aws-sdk/client-textract';
|
||||
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
|
||||
@@ -34,12 +36,16 @@ import { setupTestServer } from '../../../testUtil.js';
|
||||
import { isDriverStreamResult } from '../../meta.js';
|
||||
import {
|
||||
fuzzCalls,
|
||||
caseRandom,
|
||||
Findings,
|
||||
unclassifiedFailure,
|
||||
fuzzCases,
|
||||
hostileValue,
|
||||
pick,
|
||||
pickStable,
|
||||
type Random,
|
||||
upstream,
|
||||
type UpstreamReply,
|
||||
} from '../shared/fuzzTestUtil.js';
|
||||
import {
|
||||
readStreamResult,
|
||||
@@ -180,6 +186,7 @@ const TARGETS: Target[] = [
|
||||
'gemini',
|
||||
'kling',
|
||||
'openai',
|
||||
'speechify',
|
||||
'xai',
|
||||
],
|
||||
routes: providerRoutes,
|
||||
@@ -278,6 +285,301 @@ const TARGETS: Target[] = [
|
||||
},
|
||||
];
|
||||
|
||||
|
||||
// -- Malformed successes --
|
||||
|
||||
const realSetTimeout = globalThis.setTimeout;
|
||||
const MEDIA_URL = 'https://media.upstream.test/a.bin';
|
||||
const DONE = [
|
||||
'succeeded',
|
||||
'completed',
|
||||
'success',
|
||||
'Success',
|
||||
'succeed',
|
||||
'SUCCEEDED',
|
||||
'COMPLETED',
|
||||
'done',
|
||||
'finished',
|
||||
'failed',
|
||||
'Fail',
|
||||
'error',
|
||||
'canceled',
|
||||
'expired',
|
||||
];
|
||||
const PENDING = [
|
||||
'queued',
|
||||
'processing',
|
||||
'running',
|
||||
'submitted',
|
||||
'starting',
|
||||
'in_progress',
|
||||
'Queueing',
|
||||
'Processing',
|
||||
'Preparing',
|
||||
];
|
||||
const REPLY_KEYS = [
|
||||
'status',
|
||||
'message',
|
||||
'usage',
|
||||
'data',
|
||||
'id',
|
||||
'model',
|
||||
'text',
|
||||
'error',
|
||||
'output',
|
||||
'outputs',
|
||||
'url',
|
||||
'urls',
|
||||
'get',
|
||||
'stream',
|
||||
'b64_json',
|
||||
'image',
|
||||
'images',
|
||||
'video',
|
||||
'videos',
|
||||
'video_url',
|
||||
'result',
|
||||
'results',
|
||||
'content',
|
||||
'duration',
|
||||
'seconds',
|
||||
'words',
|
||||
'pages',
|
||||
'language',
|
||||
'task_id',
|
||||
'task_status',
|
||||
'task_status_msg',
|
||||
'task_result',
|
||||
'code',
|
||||
'success',
|
||||
'result_info',
|
||||
'errors',
|
||||
'file_id',
|
||||
'file',
|
||||
'download_url',
|
||||
'base_resp',
|
||||
'status_code',
|
||||
'status_msg',
|
||||
'audio_data',
|
||||
'audio',
|
||||
'audio_format',
|
||||
'audio_base64',
|
||||
'alignment',
|
||||
'usageInfo',
|
||||
'usage_info',
|
||||
'pagesProcessed',
|
||||
'pages_processed',
|
||||
'markdown',
|
||||
'index',
|
||||
'documentAnnotation',
|
||||
'document_annotation',
|
||||
'metrics',
|
||||
'predict_time',
|
||||
'image_count',
|
||||
'input_tokens',
|
||||
'output_tokens',
|
||||
'total_tokens',
|
||||
'completion_tokens',
|
||||
'prompt_tokens',
|
||||
'input_tokens_details',
|
||||
'output_tokens_details',
|
||||
'image_tokens',
|
||||
'text_tokens',
|
||||
'generated_images',
|
||||
'cost',
|
||||
'cost_in_usd_ticks',
|
||||
'resolution',
|
||||
'ratio',
|
||||
'width',
|
||||
'height',
|
||||
'size',
|
||||
'quality',
|
||||
'last_frame_image',
|
||||
'first_frame_image',
|
||||
'image_list',
|
||||
'final_unit_deduction',
|
||||
'unit_deduction',
|
||||
'video_width',
|
||||
'video_height',
|
||||
'created_at',
|
||||
'completed_at',
|
||||
'version',
|
||||
'logs',
|
||||
'input',
|
||||
'voices',
|
||||
'voice_id',
|
||||
'name',
|
||||
'type',
|
||||
'has_more',
|
||||
'request_id',
|
||||
'character_count',
|
||||
'characters',
|
||||
'segments',
|
||||
'speakers',
|
||||
'channels',
|
||||
'start',
|
||||
'end',
|
||||
'speaker',
|
||||
'logprobs',
|
||||
'tokens',
|
||||
'info',
|
||||
'AudioStream',
|
||||
'Blocks',
|
||||
'BlockType',
|
||||
'Text',
|
||||
'Confidence',
|
||||
'Id',
|
||||
'RequestCharacters',
|
||||
'ContentType',
|
||||
'DocumentMetadata',
|
||||
'Pages',
|
||||
'Relationships',
|
||||
'Ids',
|
||||
'Type',
|
||||
'Page',
|
||||
'Geometry',
|
||||
'Voices',
|
||||
];
|
||||
|
||||
const replyLeaf = (random: Random): unknown => {
|
||||
const roll = Math.floor(random() * 12);
|
||||
if (roll <= 1) return MEDIA_URL;
|
||||
if (roll === 2) return PNG.split(',')[1];
|
||||
if (roll === 3) return PNG;
|
||||
if (roll <= 5) return pick(random, DONE);
|
||||
if (roll === 6) return random() < 0.3 ? pick(random, PENDING) : 0;
|
||||
if (roll === 7) return pick(random, [1, 5, 0, 200, 1024, 3.5]);
|
||||
if (roll === 8) return pick(random, ['id_1', 'LINE', 'PAGE', 'mp3', 'en']);
|
||||
if (roll === 9) return random() < 0.5;
|
||||
return hostileValue(random);
|
||||
};
|
||||
|
||||
const ID_KEY = /(^id$|_id$|^Id$|request_id)/;
|
||||
const URL_KEY = /(url|^get$|^stream$|^output$|_image$|^video$|^image$|^file$)/i;
|
||||
const STATUS_KEY = /(status$|^code$|^success$|^type$|BlockType)/;
|
||||
const COUNT_KEY =
|
||||
/(duration|seconds|tokens|count|^cost|deduction|width|height|time|Pages|pagesProcessed|pages_processed|characters|Confidence|^index$|^start$|^end$)/;
|
||||
const LIST_KEY =
|
||||
/(^data$|outputs?$|images$|videos$|results$|words$|pages$|segments$|voices$|Blocks$|image_list|errors$|content$)/;
|
||||
|
||||
/** Mostly the kind of value the vendors put under `key`, sometimes not. */
|
||||
const replyValue = (random: Random, depth: number, key = ''): unknown => {
|
||||
if (random() < 0.7) {
|
||||
if (ID_KEY.test(key)) return 'id_1';
|
||||
if (STATUS_KEY.test(key) && random() < 0.8) {
|
||||
return pick(random, [
|
||||
...DONE,
|
||||
...DONE,
|
||||
0,
|
||||
200,
|
||||
true,
|
||||
'LINE',
|
||||
'PAGE',
|
||||
pick(random, PENDING),
|
||||
]);
|
||||
}
|
||||
if (COUNT_KEY.test(key)) {
|
||||
return pick(random, [
|
||||
1,
|
||||
5,
|
||||
0,
|
||||
3.5,
|
||||
1024,
|
||||
'5',
|
||||
-1,
|
||||
1e308,
|
||||
2 ** 53,
|
||||
null,
|
||||
'1e309',
|
||||
]);
|
||||
}
|
||||
if (LIST_KEY.test(key) && depth <= 4 && random() < 0.75) {
|
||||
return Array.from({ length: 1 + Math.floor(random() * 2) }, () =>
|
||||
random() < 0.8
|
||||
? replyObject(random, depth + 1)
|
||||
: replyLeaf(random),
|
||||
);
|
||||
}
|
||||
if (URL_KEY.test(key) && random() < 0.7) {
|
||||
return pick(random, [
|
||||
MEDIA_URL,
|
||||
MEDIA_URL,
|
||||
PNG,
|
||||
PNG.split(',')[1],
|
||||
[MEDIA_URL],
|
||||
{ url: MEDIA_URL },
|
||||
]);
|
||||
}
|
||||
}
|
||||
const roll = random();
|
||||
if (depth > 4 || roll < 0.4) return replyLeaf(random);
|
||||
if (roll < 0.55) {
|
||||
return Array.from({ length: Math.floor(random() * 4) }, () =>
|
||||
replyValue(random, depth + 1),
|
||||
);
|
||||
}
|
||||
return replyObject(random, depth + 1);
|
||||
};
|
||||
|
||||
const replyObject = (
|
||||
random: Random,
|
||||
depth: number,
|
||||
): Record<string, unknown> => {
|
||||
const object: Record<string, unknown> = {};
|
||||
for (let count = 2 + Math.floor(random() * 10); count > 0; count--) {
|
||||
const key = pick(random, REPLY_KEYS);
|
||||
object[key] = replyValue(random, depth, key);
|
||||
}
|
||||
return object;
|
||||
};
|
||||
|
||||
/** A 2xx answer shaped like no vendor's, built from the fields they use. */
|
||||
const vendorReply = (random: Random, url: URL): UpstreamReply => {
|
||||
const roll = random();
|
||||
if (roll < 0.04) return refused;
|
||||
if (roll < 0.1) {
|
||||
return {
|
||||
body: pick(random, [
|
||||
'',
|
||||
'<html>Bad gateway</html>',
|
||||
'{',
|
||||
'null',
|
||||
'[]',
|
||||
'0',
|
||||
'"x"',
|
||||
]),
|
||||
contentType: pick(random, [
|
||||
'text/html',
|
||||
'application/json',
|
||||
'text/plain',
|
||||
]),
|
||||
};
|
||||
}
|
||||
if (roll < 0.2 || url.hostname === 'media.upstream.test') {
|
||||
return {
|
||||
body: pick(random, ['bytes', '', 'RIFF', '\u0089PNG']),
|
||||
contentType:
|
||||
pick(random, [
|
||||
'image/png',
|
||||
'audio/mpeg',
|
||||
'video/mp4',
|
||||
'application/octet-stream',
|
||||
'text/html',
|
||||
'',
|
||||
]) || undefined,
|
||||
};
|
||||
}
|
||||
const body = replyObject(random, 1);
|
||||
for (const key of ['id', 'task_id', 'data', 'status', 'output', 'code']) {
|
||||
if (random() < 0.6) body[key] = replyValue(random, 1, key);
|
||||
}
|
||||
return { body, contentType: 'application/json' };
|
||||
};
|
||||
|
||||
let awsReply: ((command: unknown) => unknown) | undefined;
|
||||
// Charges no vendor answer should be able to cause.
|
||||
const charges: string[] = [];
|
||||
|
||||
const key = { apiKey: 'fuzz-key' };
|
||||
const aws = {
|
||||
aws: { access_key: 'AKIAFUZZ', secret_key: 'x', region: 'us-west-2' },
|
||||
@@ -293,6 +595,7 @@ const providers = {
|
||||
'together-ai': key,
|
||||
byteplus: key,
|
||||
elevenlabs: key,
|
||||
speechify: key,
|
||||
kling: key,
|
||||
'replicate-image-generation': key,
|
||||
'cloudflare-image-generation': { apiToken: 'fuzz-key', accountId: 'acct' },
|
||||
@@ -310,7 +613,8 @@ let actor: Actor;
|
||||
beforeAll(async () => {
|
||||
upstream.install();
|
||||
upstream.respond = () => refused;
|
||||
const refuse = async () => {
|
||||
const refuse = async (command: unknown) => {
|
||||
if (awsReply) return awsReply(command);
|
||||
throw Object.assign(new Error('refused'), {
|
||||
name: 'ValidationException',
|
||||
$metadata: { httpStatusCode: 400 },
|
||||
@@ -329,6 +633,45 @@ beforeAll(async () => {
|
||||
email: 'media-fuzz@test.local',
|
||||
requires_email_confirmation: false,
|
||||
});
|
||||
// Poll intervals pass at once; request timeouts are far longer.
|
||||
globalThis.setTimeout = ((
|
||||
fn: () => void,
|
||||
ms?: number,
|
||||
...rest: unknown[]
|
||||
) =>
|
||||
realSetTimeout(
|
||||
fn,
|
||||
ms && ms >= 400 && ms <= 6000 ? 1 : ms,
|
||||
...rest,
|
||||
)) as typeof setTimeout;
|
||||
const metering = server.services.metering as unknown as Record<string, any>;
|
||||
const realBatch = metering.batchIncrementUsages.bind(metering);
|
||||
metering.batchIncrementUsages = (who: unknown, usages: any[]) => {
|
||||
// A flagged charge is recorded and dropped, so it can't drain the
|
||||
// balance the later cases run on.
|
||||
const sane = (usages ?? []).filter((usage) => {
|
||||
let ok = true;
|
||||
for (const [name, value] of [
|
||||
['amount', usage?.usageAmount],
|
||||
['cost', usage?.costOverride],
|
||||
] as const) {
|
||||
if (value === undefined || value === null) continue;
|
||||
if (
|
||||
typeof value !== 'number' ||
|
||||
!Number.isFinite(value) ||
|
||||
value < 0 ||
|
||||
value > 1e12
|
||||
) {
|
||||
charges.push(
|
||||
`${name} ${usage?.usageType} ${String(value)}`,
|
||||
);
|
||||
ok = false;
|
||||
}
|
||||
}
|
||||
return ok;
|
||||
});
|
||||
return realBatch(who, sane);
|
||||
};
|
||||
actor = {
|
||||
user: {
|
||||
id: user.id,
|
||||
@@ -343,6 +686,7 @@ beforeAll(async () => {
|
||||
afterAll(async () => {
|
||||
await server?.shutdown();
|
||||
upstream.uninstall();
|
||||
globalThis.setTimeout = realSetTimeout;
|
||||
vi.restoreAllMocks();
|
||||
});
|
||||
|
||||
@@ -400,4 +744,142 @@ describe.each(TARGETS)('$name driver seeded fuzzing', (target) => {
|
||||
// Some of the requests have to get as far as a vendor.
|
||||
expect(upstream.asked).toBeGreaterThan(askedBefore);
|
||||
}, 600_000);
|
||||
it('settles every request when the vendor answers with a malformed success', async () => {
|
||||
const driver = (server.drivers as unknown as Record<string, any>)[
|
||||
target.driver
|
||||
];
|
||||
const routes = await target.routes(driver);
|
||||
const only = process.env.PUTER_FUZZ_PROVIDER;
|
||||
const names = target.providers.filter((name) => !only || name === only);
|
||||
if (!names.length) return;
|
||||
const findings = new Findings();
|
||||
const seed = Number(process.env.PUTER_FUZZ_SEED ?? 0x5eed);
|
||||
for (let index = 0; index < fuzzCases(1500); index++) {
|
||||
const random = caseRandom(seed, index);
|
||||
const provider = pickStable(random, names, (name) => name);
|
||||
const candidates = routes.filter((r) => r.provider === provider);
|
||||
const route = pickStable(random, candidates, (c) =>
|
||||
String(c.model),
|
||||
);
|
||||
const args = {
|
||||
...(random() < 0.6
|
||||
? target.corpus[0]
|
||||
: pick(random, target.corpus)),
|
||||
...route,
|
||||
};
|
||||
const replies: unknown[] = [];
|
||||
let asks = 0;
|
||||
const replyRandom = caseRandom(seed ^ 0x77, index);
|
||||
upstream.respond = ({ url }) => {
|
||||
if (++asks > 8) return refused;
|
||||
const reply = vendorReply(replyRandom, url);
|
||||
replies.push(`${url.hostname}${url.pathname}`, reply.body);
|
||||
return reply;
|
||||
};
|
||||
// The SDK types its output, so only optional fields vary.
|
||||
awsReply = (command) => {
|
||||
const name = (command as object).constructor.name;
|
||||
if (name === 'DescribeVoicesCommand') {
|
||||
return {
|
||||
Voices: [
|
||||
{
|
||||
Id: 'Joanna',
|
||||
Name: 'Joanna',
|
||||
LanguageCode: 'en-US',
|
||||
LanguageName: 'US English',
|
||||
SupportedEngines: [
|
||||
'neural',
|
||||
'standard',
|
||||
'generative',
|
||||
'long-form',
|
||||
],
|
||||
},
|
||||
],
|
||||
};
|
||||
}
|
||||
const maybe = <T>(value: T) =>
|
||||
replyRandom() < 0.25 ? undefined : value;
|
||||
if (name === 'SynthesizeSpeechCommand') {
|
||||
return {
|
||||
AudioStream: maybe(Readable.from(Buffer.from('mp3'))),
|
||||
RequestCharacters: maybe(
|
||||
pick(replyRandom, [5, 0, -1, 2 ** 31]),
|
||||
),
|
||||
ContentType: maybe('audio/mpeg'),
|
||||
};
|
||||
}
|
||||
return {
|
||||
DocumentMetadata: maybe({
|
||||
Pages: maybe(pick(replyRandom, [1, 0, 3000])),
|
||||
}),
|
||||
Blocks: maybe(
|
||||
Array.from(
|
||||
{ length: Math.floor(replyRandom() * 5) },
|
||||
() => ({
|
||||
BlockType: maybe(
|
||||
pick(replyRandom, [
|
||||
'PAGE',
|
||||
'LINE',
|
||||
'WORD',
|
||||
'TABLE',
|
||||
]),
|
||||
),
|
||||
Text: maybe(
|
||||
pick(replyRandom, ['hi', '', '\u0000']),
|
||||
),
|
||||
Confidence: maybe(
|
||||
pick(replyRandom, [99.5, 0, NaN]),
|
||||
),
|
||||
Id: maybe('b1'),
|
||||
Page: maybe(pick(replyRandom, [1, 0, 7])),
|
||||
}),
|
||||
),
|
||||
),
|
||||
};
|
||||
};
|
||||
charges.length = 0;
|
||||
let timer: ReturnType<typeof realSetTimeout> | undefined;
|
||||
try {
|
||||
await Promise.race([
|
||||
(async () => {
|
||||
const result = await withTestActor(
|
||||
() => driver[target.method](structuredClone(args)),
|
||||
actor,
|
||||
);
|
||||
if (isDriverStreamResult(result))
|
||||
await readStreamResult(result);
|
||||
else JSON.stringify(result);
|
||||
})(),
|
||||
new Promise((_, reject) => {
|
||||
timer = realSetTimeout(
|
||||
() => reject(new Error('the call did not finish')),
|
||||
15_000,
|
||||
);
|
||||
}),
|
||||
]);
|
||||
} catch (error) {
|
||||
const failure = unclassifiedFailure(error);
|
||||
if (failure)
|
||||
findings.add(
|
||||
`${provider} ${failure.replace(/"[^"]*"\.*/g, '…').replace(/Body:.*/, '')}`,
|
||||
index,
|
||||
`${route.model ?? ''}`,
|
||||
replies,
|
||||
);
|
||||
} finally {
|
||||
clearTimeout(timer);
|
||||
}
|
||||
for (const charge of charges.splice(0)) {
|
||||
findings.add(
|
||||
`${provider} metering ${charge.split(' ')[0]}`,
|
||||
index,
|
||||
`${route.model ?? ''} ${charge}`,
|
||||
replies,
|
||||
);
|
||||
}
|
||||
}
|
||||
upstream.respond = () => refused;
|
||||
awsReply = undefined;
|
||||
findings.assertNone();
|
||||
}, 1_800_000);
|
||||
});
|
||||
Reference in new issue
Block a user