Skip to content

Commit

Permalink
test(prompt): improve test coverage for prompt.js (#104)
Browse files Browse the repository at this point in the history
### What are you trying to accomplish?
- **test(prompt): add 100% test coverage for `parsePromptArguments()`**
- **test(prompt): add test coverage for prompt.stream()**

### Context
Relates to #87 
<!--
This pull request template provides suggested sections for framing your
work.
You're welcome to change or remove headers if it doesn't fit your use
case. :)

### What are you trying to accomplish?

### What approach did you choose and why?

### What should reviewers focus on?
-->
  • Loading branch information
oscard0m authored Sep 18, 2024
1 parent 7ca71ee commit ab45116
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const DEFAULT_ENDPOINT = "https://api.githubcopilot.com/chat/completions";
const DEFAULT_MODEL = "gpt-4o";

/** @type {import('..').PromptInterface} */
function parsePromptArguments(userPrompt, promptOptions) {
export function parsePromptArguments(userPrompt, promptOptions) {
const { request: requestOptions, ...options } =
typeof userPrompt === "string" ? promptOptions : userPrompt;

Expand Down
56 changes: 56 additions & 0 deletions test/prompt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import test from "ava";
import { MockAgent } from "undici";

import { prompt, getFunctionCalls } from "../index.js";
import { parsePromptArguments } from "../lib/prompt.js";

test("smoke", (t) => {
t.is(typeof prompt, "function");
Expand Down Expand Up @@ -479,3 +480,58 @@ test("does not include function calls", async (t) => {

t.deepEqual(result, []);
});

test("parsePromptArguments - uses Node fetch if no options.fetch passed as argument", (t) => {
const [parsedFetch] = parsePromptArguments(
"What is the capital of France?",
{}
);

t.deepEqual(fetch, parsedFetch);
});

test("prompt.stream", async (t) => {
const mockAgent = new MockAgent();
function fetchMock(url, opts) {
opts ||= {};
opts.dispatcher = mockAgent;
return fetch(url, opts);
}

mockAgent.disableNetConnect();
const mockPool = mockAgent.get("https://api.githubcopilot.com");
mockPool
.intercept({
method: "post",
path: `/chat/completions`,
})
.reply(200, "<response text>", {
headers: {
"content-type": "text/plain",
"x-request-id": "<request-id>",
},
});

const { requestId, stream } = await prompt.stream(
"What is the capital of France?",
{
token: "secret",
request: {
fetch: fetchMock,
},
}
);

t.is(requestId, "<request-id>");

let data = "";
const reader = stream.getReader();

while (true) {
const { done, value } = await reader.read();
if (done) break;
data += new TextDecoder().decode(value);
}

t.deepEqual(data, "<response text>");
});

0 comments on commit ab45116

Please sign in to comment.