Files
aicommits/tests/specs/config.ts
Rocktim 42a2a39f6f feat: request timeout config (#191)
Co-authored-by: Hiroki Osame <hiroki.osame@gmail.com>
2023-04-08 08:24:47 -04:00

74 lines
2.0 KiB
TypeScript

import fs from 'fs/promises';
import path from 'path';
import { testSuite, expect } from 'manten';
import { createFixture } from '../utils.js';
export default testSuite(({ describe }) => {
describe('config', async ({ test, describe }) => {
const { fixture, aicommits } = await createFixture();
const configPath = path.join(fixture.path, '.aicommits');
const openAiToken = 'OPENAI_KEY=sk-abc';
test('set unknown config file', async () => {
const { stderr } = await aicommits(['config', 'set', 'UNKNOWN=1'], {
reject: false,
});
expect(stderr).toMatch('Invalid config property: UNKNOWN');
});
test('set invalid OPENAI_KEY', async () => {
const { stderr } = await aicommits(['config', 'set', 'OPENAI_KEY=abc'], {
reject: false,
});
expect(stderr).toMatch('Invalid config property OPENAI_KEY: Must start with "sk-"');
});
await test('set config file', async () => {
await aicommits(['config', 'set', openAiToken]);
const configFile = await fs.readFile(configPath, 'utf8');
expect(configFile).toMatch(openAiToken);
});
await test('get config file', async () => {
const { stdout } = await aicommits(['config', 'get', 'OPENAI_KEY']);
expect(stdout).toBe(openAiToken);
});
await test('reading unknown config', async () => {
await fs.appendFile(configPath, 'UNKNOWN=1');
const { stdout, stderr } = await aicommits(['config', 'get', 'UNKNOWN'], {
reject: false,
});
expect(stdout).toBe('');
expect(stderr).toBe('');
});
await describe('timeout', ({ test }) => {
test('setting invalid timeout config', async () => {
const { stderr } = await aicommits(['config', 'set', 'timeout=abc'], {
reject: false,
});
expect(stderr).toMatch('Must be an integer');
});
test('setting valid timeout config', async () => {
const timeout = 'timeout=20000';
await aicommits(['config', 'set', timeout]);
const configFile = await fs.readFile(configPath, 'utf8');
expect(configFile).toMatch(timeout);
});
});
await fixture.rm();
});
});