seo-generator-server/tests/_helpers/fakeLLMClient.js
2025-09-03 15:29:19 +08:00

27 lines
972 B
JavaScript

export class FakeLLMClient {
constructor(opts = {}) {
this.delayMs = opts.delayMs ?? 10;
this.plan = opts.plan ?? []; // suite d'événements: 'ok' | {err:{statusCode:429|500|...}} | fn
this.calls = [];
this._i = 0;
this.defaultUsage = { prompt_tokens: 100, completion_tokens: 150 };
this.prefix = opts.prefix ?? 'FAKE';
}
async invoke(input, { model='fake-1' } = {}) {
this.calls.push({ input, model, t: Date.now() });
await new Promise(r => setTimeout(r, this.delayMs));
const step = this.plan[this._i++] ?? 'ok';
if (typeof step === 'function') return step(input, {model});
if (step?.err) {
const e = new Error(`fake error ${step.err.statusCode}`);
e.statusCode = step.err.statusCode;
throw e;
}
const text = typeof input === 'string' ? input : JSON.stringify(input);
return {
completion: `${this.prefix}[${model}]::` + text.slice(0, 80),
usage: this.defaultUsage
};
}
}