Skip to content

Commit

Permalink
test: options.messages for prompt() and prompt(options)
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Sep 2, 2024
1 parent 2938e55 commit bb08b13
Showing 1 changed file with 123 additions and 0 deletions.
123 changes: 123 additions & 0 deletions test/prompt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,129 @@ suite("prompt", () => {
});
});

test("options.messages", 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`,
body: JSON.stringify({
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "What is the capital of France?" },
{ role: "assistant", content: "The capital of France is Paris." },
{ role: "user", content: "What about Spain?" },
],
model: "gpt-4",
}),
})
.reply(
200,
{
choices: [
{
message: {
content: "<response text>",
},
},
],
},
{
headers: {
"content-type": "application/json",
"x-request-id": "<request-id>",
},
}
);

const result = await prompt("What about Spain?", {
model: "gpt-4",
token: "secret",
messages: [
{ role: "user", content: "What is the capital of France?" },
{ role: "assistant", content: "The capital of France is Paris." },
],
request: { fetch: fetchMock },
});

t.assert.deepEqual(result, {
requestId: "<request-id>",
message: {
content: "<response text>",
},
});
});

test("single options argument", 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`,
body: JSON.stringify({
messages: [
{ role: "system", content: "You are a helpful assistant." },
{ role: "user", content: "What is the capital of France?" },
{ role: "assistant", content: "The capital of France is Paris." },
{ role: "user", content: "What about Spain?" },
],
model: "gpt-4",
}),
})
.reply(
200,
{
choices: [
{
message: {
content: "<response text>",
},
},
],
},
{
headers: {
"content-type": "application/json",
"x-request-id": "<request-id>",
},
}
);

const result = await prompt({
model: "gpt-4",
token: "secret",
messages: [
{ role: "user", content: "What is the capital of France?" },
{ role: "assistant", content: "The capital of France is Paris." },
{ role: "user", content: "What about Spain?" },
],
request: { fetch: fetchMock },
});

t.assert.deepEqual(result, {
requestId: "<request-id>",
message: {
content: "<response text>",
},
});
});

test("function calling", async (t) => {
const mockAgent = new MockAgent();
function fetchMock(url, opts) {
Expand Down

0 comments on commit bb08b13

Please sign in to comment.