From 918211118e6c4daa65bef4b8570fce124def6b04 Mon Sep 17 00:00:00 2001 From: Thijs Limmen Date: Wed, 22 Mar 2023 19:01:47 +0100 Subject: [PATCH] Generate openai tests with diff being generated by AI --- tests/index.ts | 5 ++- tests/specs/openai.spec.ts | 75 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 tests/specs/openai.spec.ts diff --git a/tests/index.ts b/tests/index.ts index 0efc6e09..e64dd0e5 100644 --- a/tests/index.ts +++ b/tests/index.ts @@ -1,6 +1,7 @@ import { describe } from 'manten'; describe('aicommits', ({ runTestSuite }) => { - runTestSuite(import('./specs/cli.spec.js')); - runTestSuite(import('./specs/config.spec.js')); + // runTestSuite(import('./specs/cli.spec.js')); + // runTestSuite(import('./specs/config.spec.js')); + runTestSuite(import('./specs/openai.spec.js')); }); diff --git a/tests/specs/openai.spec.ts b/tests/specs/openai.spec.ts new file mode 100644 index 00000000..dfdca7ed --- /dev/null +++ b/tests/specs/openai.spec.ts @@ -0,0 +1,75 @@ +import { testSuite, expect } from 'manten'; +import { Configuration, OpenAIApi } from 'openai'; +import { CommitMessage, generateCommitMessage } from '../../src/utils/openai.js'; + +const { OPENAI_KEY } = process.env; + +export default testSuite(({ describe }) => { + if (process.platform === 'win32') { + // https://github.com/nodejs/node/issues/31409 + console.warn('Skipping tests on Windows because Node.js spawn cant open TTYs'); + return; + } + + if (!OPENAI_KEY) { + console.warn('⚠️ process.env.OPENAI_KEY is necessary to run these tests. Skipping...'); + return; + } + + const configuration = new Configuration({ + apiKey: OPENAI_KEY, + }); + + const openai = new OpenAIApi(configuration); + + const model = 'gpt-3.5-turbo'; + + async function gitDiffGenerator(typeOfChanges: string): Promise { + const systemPrompt = ` + I want you to act as a git cli + I will give you the type of content and you will generate a random git diff based on that + `; + + const completion = await openai.createChatCompletion({ + model, + messages: [ + { + role: 'system', + content: systemPrompt, + }, + { + role: 'user', + content: typeOfChanges, + }, + ], + temperature: 0.7, + top_p: 1, + frequency_penalty: 0, + presence_penalty: 0, + stream: false, + n: 1, + }); + + if (!completion.data.choices[0].message) { + throw new Error('Failed to generate git diff for test'); + } + + return completion.data.choices[0].message.content; + } + + async function runGenerateCommitMessage(gitDiff: string) + : Promise { + const commitMessages = await generateCommitMessage(OPENAI_KEY!, 'en', gitDiff, 1, true, false); + return commitMessages[0]; + } + + describe('OpenAI', async ({ test }) => { + await test('Testing a React application', async () => { + const gitDiff = await gitDiffGenerator('Testing a React application'); + + const commitMessage = await runGenerateCommitMessage(gitDiff); + + expect(commitMessage.title).toMatch(/(test):/); + }); + }); +});