From 884c40637fbd5ae0831b724fcebfd8fc577b72fb Mon Sep 17 00:00:00 2001 From: Gregor Martynus <39992+gr2m@users.noreply.github.com> Date: Mon, 2 Sep 2024 16:23:24 -0700 Subject: [PATCH] test: `options.messages` for `prompt()` and `prompt(options)` --- test/prompt.test.js | 133 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) diff --git a/test/prompt.test.js b/test/prompt.test.js index 24a3538..11b3f0f 100644 --- a/test/prompt.test.js +++ b/test/prompt.test.js @@ -70,6 +70,139 @@ 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 is the capital of France?", + }, + ], + model: "gpt-4", + }), + }) + .reply( + 200, + { + choices: [ + { + message: { + content: "", + }, + }, + ], + }, + { + headers: { + "content-type": "application/json", + "x-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." }, + ], + }); + + t.assert.deepEqual(result, { + requestId: "", + message: { + content: "", + }, + }); + }); + + 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 is the capital of France?", + }, + ], + model: "gpt-4", + }), + }) + .reply( + 200, + { + choices: [ + { + message: { + content: "", + }, + }, + ], + }, + { + headers: { + "content-type": "application/json", + "x-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?" }, + ], + }); + + t.assert.deepEqual(result, { + requestId: "", + message: { + content: "", + }, + }); + }); + test("function calling", async (t) => { const mockAgent = new MockAgent(); function fetchMock(url, opts) {