test: add the first E2E test and configuration to CI (#316)

* add tests
This commit is contained in:
Takanori Matsumoto
2024-03-25 13:01:05 +09:00
committed by GitHub
parent bdc98c6fa8
commit 5cda8b1b03
17 changed files with 4380 additions and 29 deletions
+1
View File
@@ -0,0 +1 @@
.env
+31
View File
@@ -0,0 +1,31 @@
name: E2E Testing
on: [pull_request]
jobs:
e2e-test:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [20.x]
steps:
- uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
- name: Install git
run: |
sudo apt-get update
sudo apt-get install -y git
git --version
- name: Setup git
run: |
git config --global user.email "test@example.com"
git config --global user.name "Test User"
- name: Install dependencies
run: npm install
- name: Build
run: npm run build
- name: Run E2E Tests
run: npm run test:e2e
+27
View File
@@ -0,0 +1,27 @@
/**
* For a detailed explanation regarding each configuration property, visit:
* https://jestjs.io/docs/configuration
*/
import type {Config} from 'jest';
const config: Config = {
coverageProvider: "v8",
moduleDirectories: [
"node_modules",
"src",
],
preset: 'ts-jest',
setupFilesAfterEnv: ['<rootDir>/test/jest-setup.ts'],
testEnvironment: "node",
testRegex: [
'.*\\.test\\.ts$',
],
transform: {
'^.+\\.(ts|tsx)$': ['ts-jest', {
diagnostics: false,
}],
}
};
export default config;
+21 -4
View File
@@ -16431,19 +16431,25 @@ var package_default = {
"build:push": "npm run build && git add . && git commit -m 'build' && git push",
deploy: "npm version patch && npm run build:push && git push --tags && npm publish --tag latest",
lint: "eslint src --ext ts && tsc --noEmit",
format: "prettier --write src"
format: "prettier --write src",
"test:e2e": "jest test/e2e",
"test:e2e:docker": "docker build -t oco-e2e -f test/Dockerfile . && DOCKER_CONTENT_TRUST=0 docker run oco-e2e"
},
devDependencies: {
"@commitlint/types": "^17.4.4",
"@types/ini": "^1.3.31",
"@types/inquirer": "^9.0.3",
"@types/jest": "^29.5.12",
"@types/node": "^16.18.14",
"@typescript-eslint/eslint-plugin": "^5.45.0",
"@typescript-eslint/parser": "^5.45.0",
"cli-testing-library": "^2.0.2",
dotenv: "^16.0.3",
esbuild: "^0.15.18",
eslint: "^8.28.0",
jest: "^29.7.0",
prettier: "^2.8.4",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.1",
typescript: "^4.9.3"
},
@@ -18662,7 +18668,7 @@ var configValidators = {
["OCO_OPENAI_API_KEY" /* OCO_OPENAI_API_KEY */](value, config8 = {}) {
validateConfig(
"API_KEY",
value || config8.OCO_AI_PROVIDER == "ollama",
value || config8.OCO_AI_PROVIDER == "ollama" || config8.OCO_AI_PROVIDER == "test",
"You need to provide an API key"
);
validateConfig(
@@ -18778,7 +18784,8 @@ var configValidators = {
[
"",
"openai",
"ollama"
"ollama",
"test"
].includes(value),
`${value} is not supported yet, use 'ollama' or 'openai' (default)`
);
@@ -21945,7 +21952,7 @@ var MAX_TOKENS_INPUT = config3?.OCO_TOKENS_MAX_INPUT || 4096 /* DEFAULT_MAX_TOKE
var basePath = config3?.OCO_OPENAI_BASE_PATH;
var apiKey = config3?.OCO_OPENAI_API_KEY;
var [command, mode] = process.argv.slice(2);
var isLocalModel = config3?.OCO_AI_PROVIDER == "ollama";
var isLocalModel = config3?.OCO_AI_PROVIDER == "ollama" || config3?.OCO_AI_PROVIDER == "test";
if (!apiKey && command !== "config" && mode !== "set" /* set */ && !isLocalModel) {
ae("opencommit");
ce(
@@ -22029,11 +22036,21 @@ var OllamaAi = class {
};
var ollamaAi = new OllamaAi();
// src/engine/testAi.ts
var TestAi = class {
async generateCommitMessage(messages) {
return "test commit message";
}
};
var testAi = new TestAi();
// src/utils/engine.ts
function getEngine() {
const config8 = getConfig();
if (config8?.OCO_AI_PROVIDER == "ollama") {
return ollamaAi;
} else if (config8?.OCO_AI_PROVIDER == "test") {
return testAi;
}
return api;
}
+14 -3
View File
@@ -24157,7 +24157,7 @@ var configValidators = {
["OCO_OPENAI_API_KEY" /* OCO_OPENAI_API_KEY */](value, config7 = {}) {
validateConfig(
"API_KEY",
value || config7.OCO_AI_PROVIDER == "ollama",
value || config7.OCO_AI_PROVIDER == "ollama" || config7.OCO_AI_PROVIDER == "test",
"You need to provide an API key"
);
validateConfig(
@@ -24273,7 +24273,8 @@ var configValidators = {
[
"",
"openai",
"ollama"
"ollama",
"test"
].includes(value),
`${value} is not supported yet, use 'ollama' or 'openai' (default)`
);
@@ -27440,7 +27441,7 @@ var MAX_TOKENS_INPUT = config3?.OCO_TOKENS_MAX_INPUT || 4096 /* DEFAULT_MAX_TOKE
var basePath = config3?.OCO_OPENAI_BASE_PATH;
var apiKey = config3?.OCO_OPENAI_API_KEY;
var [command, mode] = process.argv.slice(2);
var isLocalModel = config3?.OCO_AI_PROVIDER == "ollama";
var isLocalModel = config3?.OCO_AI_PROVIDER == "ollama" || config3?.OCO_AI_PROVIDER == "test";
if (!apiKey && command !== "config" && mode !== "set" /* set */ && !isLocalModel) {
ae("opencommit");
ce(
@@ -27524,11 +27525,21 @@ var OllamaAi = class {
};
var ollamaAi = new OllamaAi();
// src/engine/testAi.ts
var TestAi = class {
async generateCommitMessage(messages) {
return "test commit message";
}
};
var testAi = new TestAi();
// src/utils/engine.ts
function getEngine() {
const config7 = getConfig();
if (config7?.OCO_AI_PROVIDER == "ollama") {
return ollamaAi;
} else if (config7?.OCO_AI_PROVIDER == "test") {
return testAi;
}
return api;
}
+4129 -18
View File
File diff suppressed because it is too large Load Diff
+7 -1
View File
@@ -47,19 +47,25 @@
"build:push": "npm run build && git add . && git commit -m 'build' && git push",
"deploy": "npm version patch && npm run build:push && git push --tags && npm publish --tag latest",
"lint": "eslint src --ext ts && tsc --noEmit",
"format": "prettier --write src"
"format": "prettier --write src",
"test:e2e": "jest test/e2e",
"test:e2e:docker": "docker build -t oco-e2e -f test/Dockerfile . && DOCKER_CONTENT_TRUST=0 docker run oco-e2e"
},
"devDependencies": {
"@commitlint/types": "^17.4.4",
"@types/ini": "^1.3.31",
"@types/inquirer": "^9.0.3",
"@types/jest": "^29.5.12",
"@types/node": "^16.18.14",
"@typescript-eslint/eslint-plugin": "^5.45.0",
"@typescript-eslint/parser": "^5.45.0",
"cli-testing-library": "^2.0.2",
"dotenv": "^16.0.3",
"esbuild": "^0.15.18",
"eslint": "^8.28.0",
"jest": "^29.7.0",
"prettier": "^2.8.4",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.1",
"typescript": "^4.9.3"
},
+3 -2
View File
@@ -57,7 +57,7 @@ export const configValidators = {
//need api key unless running locally with ollama
validateConfig(
'API_KEY',
value || config.OCO_AI_PROVIDER == 'ollama',
value || config.OCO_AI_PROVIDER == 'ollama' || config.OCO_AI_PROVIDER == 'test',
'You need to provide an API key'
);
validateConfig(
@@ -190,7 +190,8 @@ export const configValidators = {
[
'',
'openai',
'ollama'
'ollama',
'test'
].includes(value),
`${value} is not supported yet, use 'ollama' or 'openai' (default)`
);
+1 -1
View File
@@ -27,7 +27,7 @@ let apiKey = config?.OCO_OPENAI_API_KEY
const [command, mode] = process.argv.slice(2);
const isLocalModel = config?.OCO_AI_PROVIDER == 'ollama'
const isLocalModel = config?.OCO_AI_PROVIDER == 'ollama' || config?.OCO_AI_PROVIDER == 'test';
if (!apiKey && command !== 'config' && mode !== CONFIG_MODES.set && !isLocalModel) {
+12
View File
@@ -0,0 +1,12 @@
import { ChatCompletionRequestMessage } from 'openai';
import { AiEngine } from './Engine';
export class TestAi implements AiEngine {
async generateCommitMessage(
messages: Array<ChatCompletionRequestMessage>
): Promise<string | undefined> {
return 'test commit message';
}
}
export const testAi = new TestAi();
+3
View File
@@ -2,11 +2,14 @@ import { AiEngine } from '../engine/Engine';
import { api } from '../engine/openAi';
import { getConfig } from '../commands/config';
import { ollamaAi } from '../engine/ollama';
import { testAi } from '../engine/testAi';
export function getEngine(): AiEngine {
const config = getConfig();
if (config?.OCO_AI_PROVIDER == 'ollama') {
return ollamaAi;
} else if (config?.OCO_AI_PROVIDER == 'test') {
return testAi;
}
//open ai gpt by default
return api;
+21
View File
@@ -0,0 +1,21 @@
FROM ubuntu:latest
RUN apt-get update && apt-get install -y curl git
# Install Node.js v20
RUN curl -fsSL https://deb.nodesource.com/setup_20.x | bash -
RUN apt-get install -y nodejs
# Setup git
RUN git config --global user.email "test@example.com"
RUN git config --global user.name "Test User"
COPY . /app
WORKDIR /app
RUN ls -la
RUN npm install
RUN npm run build
CMD ["npm", "run", "test:e2e"]
+13
View File
@@ -0,0 +1,13 @@
import { resolve } from 'path'
import { render } from 'cli-testing-library'
import 'cli-testing-library/extend-expect';
import { prepareEnvironment } from './utils';
it('cli flow when there are no changes', async () => {
const { gitDir, cleanup } = await prepareEnvironment();
const { findByText } = await render(`OCO_AI_PROVIDER='test' node`, [resolve('./out/cli.cjs')], { cwd: gitDir });
expect(await findByText('No changes detected')).toBeInTheConsole();
await cleanup();
});
+56
View File
@@ -0,0 +1,56 @@
import { resolve } from 'path'
import { render } from 'cli-testing-library'
import 'cli-testing-library/extend-expect';
import { prepareEnvironment } from './utils';
it('cli flow to generate commit message for 1 new file (staged)', async () => {
const { gitDir, cleanup } = await prepareEnvironment();
await render('echo' ,[`'console.log("Hello World");' > index.ts`], { cwd: gitDir });
await render('git' ,['add index.ts'], { cwd: gitDir });
const { queryByText, findByText, userEvent } = await render(`OCO_AI_PROVIDER='test' node`, [resolve('./out/cli.cjs')], { cwd: gitDir });
expect(await queryByText('No files are staged')).not.toBeInTheConsole();
expect(await queryByText('Do you want to stage all files and generate commit message?')).not.toBeInTheConsole();
expect(await findByText('Generating the commit message')).toBeInTheConsole();
expect(await findByText('Confirm the commit message?')).toBeInTheConsole();
userEvent.keyboard('[Enter]');
expect(await findByText('Do you want to run `git push`?')).toBeInTheConsole();
userEvent.keyboard('[Enter]');
expect(await findByText('Successfully pushed all commits to origin')).toBeInTheConsole();
await cleanup();
});
it('cli flow to generate commit message for 1 changed file (not staged)', async () => {
const { gitDir, cleanup } = await prepareEnvironment();
await render('echo' ,[`'console.log("Hello World");' > index.ts`], { cwd: gitDir });
await render('git' ,['add index.ts'], { cwd: gitDir });
await render('git' ,[`commit -m 'add new file'`], { cwd: gitDir });
await render('echo' ,[`'console.log("Good night World");' >> index.ts`], { cwd: gitDir });
const { findByText, userEvent } = await render(`OCO_AI_PROVIDER='test' node`, [resolve('./out/cli.cjs')], { cwd: gitDir });
expect(await findByText('No files are staged')).toBeInTheConsole();
expect(await findByText('Do you want to stage all files and generate commit message?')).toBeInTheConsole();
userEvent.keyboard('[Enter]');
expect(await findByText('Generating the commit message')).toBeInTheConsole();
expect(await findByText('Confirm the commit message?')).toBeInTheConsole();
userEvent.keyboard('[Enter]');
expect(await findByText('Successfully committed')).toBeInTheConsole();
expect(await findByText('Do you want to run `git push`?')).toBeInTheConsole();
userEvent.keyboard('[Enter]');
expect(await findByText('Successfully pushed all commits to origin')).toBeInTheConsole();
await cleanup();
});
+31
View File
@@ -0,0 +1,31 @@
import path from 'path'
import { mkdtemp, rm } from 'fs'
import { promisify } from 'util';
import { tmpdir } from 'os';
import { exec } from 'child_process';
const fsMakeTempDir = promisify(mkdtemp);
const fsExec = promisify(exec);
const fsRemove = promisify(rm);
/**
* Prepare the environment for the test
* Create a temporary git repository in the temp directory
*/
export const prepareEnvironment = async (): Promise<{
gitDir: string;
cleanup: () => Promise<void>;
}> => {
const tempDir = await fsMakeTempDir(path.join(tmpdir(), 'opencommit-test-'));
// Create a remote git repository int the temp directory. This is necessary to execute the `git push` command
await fsExec('git init --bare remote.git', { cwd: tempDir });
await fsExec('git clone remote.git test', { cwd: tempDir });
const gitDir = path.resolve(tempDir, 'test');
const cleanup = async () => {
return fsRemove(tempDir, { recursive: true });
}
return {
gitDir,
cleanup,
}
}
+7
View File
@@ -0,0 +1,7 @@
import 'cli-testing-library/extend-expect'
import { configure } from 'cli-testing-library'
/**
* Adjusted the wait time for waitFor/findByText to 2000ms, because the default 1000ms makes the test results flaky
*/
configure({ asyncUtilTimeout: 2000 })
+3
View File
@@ -21,6 +21,9 @@
"skipLibCheck": true
},
"include": [
"test/jest-setup.ts"
],
"exclude": ["node_modules"],
"ts-node": {
"esm": true,