Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(test): add coverage for empty chunks and Groq token usage handling #39

Merged
merged 1 commit into from
Nov 14, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 44 additions & 0 deletions tests/unit/writeOutput.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,48 @@ describe("writeOutput() function", () => {

exitSpy.mockRestore();
});

test("Should handle Groq-specific token usage format in stream response", async () => {
const completion = [
{
choices: [{ delta: { content: "Content chunk" } }],
x_groq: {
usage: { prompt_tokens: 5, completion_tokens: 10, total_tokens: 15 },
},
},
];

const consoleErrorSpy = jest.spyOn(console, "error").mockImplementation();

await writeOutput(completion, outputFilePath, true, true);

expect(consoleErrorSpy).toHaveBeenCalledWith(
`\nToken Usage Report:\n`,
`Prompt tokens: 5\n`,
`Completion tokens: 10\n`,
`Total tokens: 15`,
);

consoleErrorSpy.mockRestore();
});

test("Should handle empty content in stream response chunks", async () => {
const completion = [
{ choices: [{ delta: {} }] },
{ choices: [{ delta: { content: "Valid content" } }] },
{ choices: [{ delta: { content: "" } }] },
];

const stdoutSpy = jest.spyOn(process.stdout, "write").mockImplementation();

await writeOutput(completion, undefined, false, true);

expect(process.stdout.write).toHaveBeenCalledTimes(4);
expect(process.stdout.write).toHaveBeenCalledWith(""); // First empty chunk
expect(process.stdout.write).toHaveBeenCalledWith("Valid content");
expect(process.stdout.write).toHaveBeenCalledWith(""); // Third empty chunk
expect(process.stdout.write).toHaveBeenCalledWith("\n");

stdoutSpy.mockRestore();
});
});