mirror of
https://github.com/getcompanion-ai/co-mono.git
synced 2026-04-21 03:04:28 +00:00
test(ai): Add empty assistant message tests
- Test providers handling empty assistant messages in conversation flow - Pattern: user message -> empty assistant -> user message - All providers handle empty assistant messages gracefully - Tests ensure providers can continue conversation after empty response
This commit is contained in:
parent
0ac05a0676
commit
be07c08a75
1 changed files with 76 additions and 1 deletions
|
|
@ -3,7 +3,7 @@ import { GoogleLLM } from "../src/providers/google.js";
|
||||||
import { OpenAICompletionsLLM } from "../src/providers/openai-completions.js";
|
import { OpenAICompletionsLLM } from "../src/providers/openai-completions.js";
|
||||||
import { OpenAIResponsesLLM } from "../src/providers/openai-responses.js";
|
import { OpenAIResponsesLLM } from "../src/providers/openai-responses.js";
|
||||||
import { AnthropicLLM } from "../src/providers/anthropic.js";
|
import { AnthropicLLM } from "../src/providers/anthropic.js";
|
||||||
import type { LLM, LLMOptions, Context, UserMessage } from "../src/types.js";
|
import type { LLM, LLMOptions, Context, UserMessage, AssistantMessage } from "../src/types.js";
|
||||||
import { getModel } from "../src/models.js";
|
import { getModel } from "../src/models.js";
|
||||||
|
|
||||||
async function testEmptyMessage<T extends LLMOptions>(llm: LLM<T>, options: T = {} as T) {
|
async function testEmptyMessage<T extends LLMOptions>(llm: LLM<T>, options: T = {} as T) {
|
||||||
|
|
@ -76,6 +76,53 @@ async function testWhitespaceOnlyMessage<T extends LLMOptions>(llm: LLM<T>, opti
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function testEmptyAssistantMessage<T extends LLMOptions>(llm: LLM<T>, options: T = {} as T) {
|
||||||
|
// Test with empty assistant message in conversation flow
|
||||||
|
// User -> Empty Assistant -> User
|
||||||
|
const emptyAssistant: AssistantMessage = {
|
||||||
|
role: "assistant",
|
||||||
|
content: [],
|
||||||
|
api: llm.getApi(),
|
||||||
|
provider: llm.getModel().provider,
|
||||||
|
model: llm.getModel().id,
|
||||||
|
usage: {
|
||||||
|
input: 10,
|
||||||
|
output: 0,
|
||||||
|
cacheRead: 0,
|
||||||
|
cacheWrite: 0,
|
||||||
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 }
|
||||||
|
},
|
||||||
|
stopReason: "stop"
|
||||||
|
};
|
||||||
|
|
||||||
|
const context: Context = {
|
||||||
|
messages: [
|
||||||
|
{
|
||||||
|
role: "user",
|
||||||
|
content: "Hello, how are you?"
|
||||||
|
},
|
||||||
|
emptyAssistant,
|
||||||
|
{
|
||||||
|
role: "user",
|
||||||
|
content: "Please respond this time."
|
||||||
|
}
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
const response = await llm.generate(context, options);
|
||||||
|
|
||||||
|
expect(response).toBeDefined();
|
||||||
|
expect(response.role).toBe("assistant");
|
||||||
|
|
||||||
|
// Should handle empty assistant message in context gracefully
|
||||||
|
if (response.stopReason === "error") {
|
||||||
|
expect(response.error).toBeDefined();
|
||||||
|
} else {
|
||||||
|
expect(response.content).toBeDefined();
|
||||||
|
expect(response.content.length).toBeGreaterThan(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
describe("AI Providers Empty Message Tests", () => {
|
describe("AI Providers Empty Message Tests", () => {
|
||||||
describe.skipIf(!process.env.GEMINI_API_KEY)("Google Provider Empty Messages", () => {
|
describe.skipIf(!process.env.GEMINI_API_KEY)("Google Provider Empty Messages", () => {
|
||||||
let llm: GoogleLLM;
|
let llm: GoogleLLM;
|
||||||
|
|
@ -95,6 +142,10 @@ describe("AI Providers Empty Message Tests", () => {
|
||||||
it("should handle whitespace-only content", async () => {
|
it("should handle whitespace-only content", async () => {
|
||||||
await testWhitespaceOnlyMessage(llm);
|
await testWhitespaceOnlyMessage(llm);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should handle empty assistant message in conversation", async () => {
|
||||||
|
await testEmptyAssistantMessage(llm);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe.skipIf(!process.env.OPENAI_API_KEY)("OpenAI Completions Provider Empty Messages", () => {
|
describe.skipIf(!process.env.OPENAI_API_KEY)("OpenAI Completions Provider Empty Messages", () => {
|
||||||
|
|
@ -115,6 +166,10 @@ describe("AI Providers Empty Message Tests", () => {
|
||||||
it("should handle whitespace-only content", async () => {
|
it("should handle whitespace-only content", async () => {
|
||||||
await testWhitespaceOnlyMessage(llm);
|
await testWhitespaceOnlyMessage(llm);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should handle empty assistant message in conversation", async () => {
|
||||||
|
await testEmptyAssistantMessage(llm);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe.skipIf(!process.env.OPENAI_API_KEY)("OpenAI Responses Provider Empty Messages", () => {
|
describe.skipIf(!process.env.OPENAI_API_KEY)("OpenAI Responses Provider Empty Messages", () => {
|
||||||
|
|
@ -139,6 +194,10 @@ describe("AI Providers Empty Message Tests", () => {
|
||||||
it("should handle whitespace-only content", async () => {
|
it("should handle whitespace-only content", async () => {
|
||||||
await testWhitespaceOnlyMessage(llm);
|
await testWhitespaceOnlyMessage(llm);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should handle empty assistant message in conversation", async () => {
|
||||||
|
await testEmptyAssistantMessage(llm);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe.skipIf(!process.env.ANTHROPIC_OAUTH_TOKEN)("Anthropic Provider Empty Messages", () => {
|
describe.skipIf(!process.env.ANTHROPIC_OAUTH_TOKEN)("Anthropic Provider Empty Messages", () => {
|
||||||
|
|
@ -159,6 +218,10 @@ describe("AI Providers Empty Message Tests", () => {
|
||||||
it("should handle whitespace-only content", async () => {
|
it("should handle whitespace-only content", async () => {
|
||||||
await testWhitespaceOnlyMessage(llm);
|
await testWhitespaceOnlyMessage(llm);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should handle empty assistant message in conversation", async () => {
|
||||||
|
await testEmptyAssistantMessage(llm);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Test with xAI/Grok if available
|
// Test with xAI/Grok if available
|
||||||
|
|
@ -184,6 +247,10 @@ describe("AI Providers Empty Message Tests", () => {
|
||||||
it("should handle whitespace-only content", async () => {
|
it("should handle whitespace-only content", async () => {
|
||||||
await testWhitespaceOnlyMessage(llm);
|
await testWhitespaceOnlyMessage(llm);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should handle empty assistant message in conversation", async () => {
|
||||||
|
await testEmptyAssistantMessage(llm);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Test with Groq if available
|
// Test with Groq if available
|
||||||
|
|
@ -209,6 +276,10 @@ describe("AI Providers Empty Message Tests", () => {
|
||||||
it("should handle whitespace-only content", async () => {
|
it("should handle whitespace-only content", async () => {
|
||||||
await testWhitespaceOnlyMessage(llm);
|
await testWhitespaceOnlyMessage(llm);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should handle empty assistant message in conversation", async () => {
|
||||||
|
await testEmptyAssistantMessage(llm);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Test with Cerebras if available
|
// Test with Cerebras if available
|
||||||
|
|
@ -234,5 +305,9 @@ describe("AI Providers Empty Message Tests", () => {
|
||||||
it("should handle whitespace-only content", async () => {
|
it("should handle whitespace-only content", async () => {
|
||||||
await testWhitespaceOnlyMessage(llm);
|
await testWhitespaceOnlyMessage(llm);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it("should handle empty assistant message in conversation", async () => {
|
||||||
|
await testEmptyAssistantMessage(llm);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue