mirror of
https://github.com/luckyyzh/pi-agent-integrated.git
synced 2026-10-03 11:09:34 +00:00
80 lines
2.3 KiB
TypeScript
80 lines
2.3 KiB
TypeScript
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
import { stream as streamOpenAICompletions } from "../src/api/openai-completions.ts";
|
|
import type { Context, Model } from "../src/types.ts";
|
|
|
|
const mockState = vi.hoisted(() => ({
|
|
chunks: [] as unknown[],
|
|
}));
|
|
|
|
vi.mock("openai", () => {
|
|
class FakeOpenAI {
|
|
chat = {
|
|
completions: {
|
|
create: () => {
|
|
const chunks = mockState.chunks;
|
|
const stream = {
|
|
async *[Symbol.asyncIterator]() {
|
|
for (const chunk of chunks) yield chunk;
|
|
},
|
|
};
|
|
const promise = Promise.resolve(stream) as Promise<typeof stream> & {
|
|
withResponse: () => Promise<{
|
|
data: typeof stream;
|
|
response: { status: number; headers: Headers };
|
|
}>;
|
|
};
|
|
promise.withResponse = async () => ({
|
|
data: stream,
|
|
response: { status: 200, headers: new Headers() },
|
|
});
|
|
return promise;
|
|
},
|
|
},
|
|
};
|
|
}
|
|
return { default: FakeOpenAI };
|
|
});
|
|
|
|
const model: Model<"openai-completions"> = {
|
|
id: "test-model",
|
|
name: "Test Model",
|
|
api: "openai-completions",
|
|
provider: "openai",
|
|
baseUrl: "https://api.openai.com/v1",
|
|
reasoning: false,
|
|
input: ["text"],
|
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
contextWindow: 128_000,
|
|
maxTokens: 4096,
|
|
};
|
|
|
|
const context: Context = {
|
|
messages: [{ role: "user", content: "hello", timestamp: Date.now() }],
|
|
};
|
|
|
|
describe("OpenAI completions raw stop reasons", () => {
|
|
beforeEach(() => {
|
|
mockState.chunks = [];
|
|
});
|
|
|
|
it("preserves raw finish reasons for successful stops", async () => {
|
|
mockState.chunks = [{ id: "chatcmpl-1", choices: [{ index: 0, delta: {}, finish_reason: "stop" }] }];
|
|
|
|
const message = await streamOpenAICompletions(model, context, { apiKey: "test" }).result();
|
|
|
|
expect(message.stopReason).toBe("stop");
|
|
expect(message.rawStopReason).toBe("stop");
|
|
expect(message.errorMessage).toBeUndefined();
|
|
});
|
|
|
|
it("preserves raw finish reasons for provider error stops", async () => {
|
|
mockState.chunks = [{ id: "chatcmpl-2", choices: [{ index: 0, delta: {}, finish_reason: "content_filter" }] }];
|
|
|
|
const message = await streamOpenAICompletions(model, context, { apiKey: "test" }).result();
|
|
|
|
expect(message.stopReason).toBe("error");
|
|
expect(message.rawStopReason).toBe("content_filter");
|
|
expect(message.errorMessage).toBe("Provider finish_reason: content_filter");
|
|
});
|
|
});
|