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(prompt): improve test coverage for prompt.js #104

Merged
Merged
Show file tree
Hide file tree
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
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>");
});