Skip to content

Commit ab45116

Browse files
authoredSep 18, 2024
test(prompt): improve test coverage for prompt.js (#104)
### 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? -->
1 parent 7ca71ee commit ab45116

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed
 

‎lib/prompt.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const DEFAULT_ENDPOINT = "https://api.githubcopilot.com/chat/completions";
44
const DEFAULT_MODEL = "gpt-4o";
55

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

‎test/prompt.test.js

+56
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import test from "ava";
33
import { MockAgent } from "undici";
44

55
import { prompt, getFunctionCalls } from "../index.js";
6+
import { parsePromptArguments } from "../lib/prompt.js";
67

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

480481
t.deepEqual(result, []);
481482
});
483+
484+
test("parsePromptArguments - uses Node fetch if no options.fetch passed as argument", (t) => {
485+
const [parsedFetch] = parsePromptArguments(
486+
"What is the capital of France?",
487+
{}
488+
);
489+
490+
t.deepEqual(fetch, parsedFetch);
491+
});
492+
493+
test("prompt.stream", async (t) => {
494+
const mockAgent = new MockAgent();
495+
function fetchMock(url, opts) {
496+
opts ||= {};
497+
opts.dispatcher = mockAgent;
498+
return fetch(url, opts);
499+
}
500+
501+
mockAgent.disableNetConnect();
502+
const mockPool = mockAgent.get("https://api.githubcopilot.com");
503+
mockPool
504+
.intercept({
505+
method: "post",
506+
path: `/chat/completions`,
507+
})
508+
.reply(200, "<response text>", {
509+
headers: {
510+
"content-type": "text/plain",
511+
"x-request-id": "<request-id>",
512+
},
513+
});
514+
515+
const { requestId, stream } = await prompt.stream(
516+
"What is the capital of France?",
517+
{
518+
token: "secret",
519+
request: {
520+
fetch: fetchMock,
521+
},
522+
}
523+
);
524+
525+
t.is(requestId, "<request-id>");
526+
527+
let data = "";
528+
const reader = stream.getReader();
529+
530+
while (true) {
531+
const { done, value } = await reader.read();
532+
if (done) break;
533+
data += new TextDecoder().decode(value);
534+
}
535+
536+
t.deepEqual(data, "<response text>");
537+
});

0 commit comments

Comments
 (0)
Failed to load comments.