Add CLI argument parsing tests and improve argument handling
- Introduced `test/cli.test.ts` with unit tests for CLI argument parsing. - Enhanced CLI argument handling in `cli.ts` for better error reporting and help message support. - Updated `MarkdownGenerator` and related files for improved robustness.
This commit is contained in:

committed by
Geoff Seemueller

parent
c8b0215435
commit
6f9d37a292
@@ -11,7 +11,7 @@ import { glob } from 'glob';
|
||||
import { isPreset, type PresetPrompt, prompts } from './prompts.ts';
|
||||
|
||||
|
||||
interface MarkdownGeneratorOptions {
|
||||
export interface MarkdownGeneratorOptions {
|
||||
dir?: string;
|
||||
outputFilePath?: string;
|
||||
fileTypeExclusions?: Set<string>;
|
||||
@@ -48,6 +48,7 @@ export class MarkdownGenerator {
|
||||
options.fileTypeExclusions || fileTypeExclusions,
|
||||
);
|
||||
this.fileExclusions = options.fileExclusions || [...fileExclusions];
|
||||
// @ts-ignore - options.customPatterns signature is valid
|
||||
this.tokenCleaner = new TokenCleaner(options.customPatterns, options.customSecretPatterns);
|
||||
this.verbose = options.verbose !== undefined ? options.verbose : true;
|
||||
this.initialized = false;
|
||||
@@ -232,7 +233,7 @@ export class MarkdownGenerator {
|
||||
if (this.verbose) {
|
||||
console.log('File not found, creating a new \'todo\' file.');
|
||||
}
|
||||
await writeFile(todoPath, todoPrompt); // Create an empty 'todo' file
|
||||
await writeFile(todoPath, ''); // Create an empty 'todo' file
|
||||
return await this.getTodo(); // Await the recursive call
|
||||
}
|
||||
if (this.verbose) {
|
||||
@@ -281,7 +282,7 @@ export class MarkdownGenerator {
|
||||
console.log({ total_tokens: totalTokens });
|
||||
}
|
||||
return { success: true, tokenCount: llama3Tokenizer.encode(markdown).length };
|
||||
} catch (error) {
|
||||
} catch (error: any) {
|
||||
if (this.verbose) {
|
||||
console.error('Error writing markdown document:', error);
|
||||
}
|
||||
|
33
src/cli.ts
33
src/cli.ts
@@ -1,8 +1,35 @@
|
||||
#!/usr/bin/env node
|
||||
console.log('RUNNING TOKENIZER');
|
||||
import { MarkdownGenerator } from './MarkdownGenerator.js';
|
||||
import type { PresetPrompt } from './prompts';
|
||||
|
||||
const generator = new MarkdownGenerator();
|
||||
console.log('RUNNING TOKENIZER');
|
||||
import { MarkdownGenerator, type MarkdownGeneratorOptions } from './MarkdownGenerator';
|
||||
|
||||
const args = process.argv.slice(2);
|
||||
const options: { prompt?: PresetPrompt; } & MarkdownGeneratorOptions = {
|
||||
|
||||
};
|
||||
type ValidArg = keyof MarkdownGeneratorOptions;
|
||||
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
if (args[i] === '--help') {
|
||||
console.log(`${Object.keys(options).map(item => "--" + item).join(', ')}`);
|
||||
}
|
||||
if (args[i] === '--prompt') {
|
||||
options["todoPrompt"] = args[i + 1]
|
||||
i++;
|
||||
}
|
||||
const arg = args[i].replace(/^--/, '');
|
||||
if (arg as any satisfies ValidArg) {
|
||||
// @ts-ignore - arg can't be used to index options
|
||||
options[arg] = args[i + 1]
|
||||
i++;
|
||||
} else {
|
||||
console.log(`Invalid argument specified: ${arg}`);
|
||||
console.log(`Possible arguments: ${Object.keys(options).map(item => "--" + item).join(', ')}`);
|
||||
}
|
||||
}
|
||||
|
||||
const generator = new MarkdownGenerator(options);
|
||||
generator
|
||||
.createMarkdownDocument()
|
||||
.then((result: { success: boolean }) => {
|
||||
|
@@ -1,14 +1,21 @@
|
||||
const taskConditionsStandard_FixErrors = `
|
||||
TASK: Fix these errors.
|
||||
CONDITIONS: Output labeled and fully fixed files only, no diffs.
|
||||
STANDARD: Respond with the files, no examples or excessive explanations.
|
||||
## Task
|
||||
1. Fix these errors.
|
||||
## Conditions
|
||||
2. Output labeled and fully fixed files only, no diffs.
|
||||
## Standard
|
||||
3. Respond with the files, no examples or excessive explanations.
|
||||
|
||||
~~~console
|
||||
|
||||
clean up your errors and put them here
|
||||
~~~
|
||||
`;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
export const customPrompts: Record<string, string> = {
|
||||
"tcs:fix:errors": taskConditionsStandard_FixErrors,
|
||||
};
|
||||
|
100
test/cli.test.ts
Normal file
100
test/cli.test.ts
Normal file
@@ -0,0 +1,100 @@
|
||||
// test/cli.test.ts
|
||||
import { describe, it, expect, beforeEach, afterEach, spyOn, mock } from 'bun:test';
|
||||
import { MarkdownGenerator, type MarkdownGeneratorOptions } from '../src/MarkdownGenerator';
|
||||
import type { PresetPrompt } from '../src/prompts';
|
||||
|
||||
// Function to process CLI arguments similar to cli.ts
|
||||
function processArgs(args: string[]): { prompt?: PresetPrompt; } & MarkdownGeneratorOptions {
|
||||
const options: { prompt?: PresetPrompt; } & MarkdownGeneratorOptions = {};
|
||||
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
if (args[i] === '--help') {
|
||||
console.log(`${Object.keys(options).map(item => "--" + item).join(', ')}`);
|
||||
continue;
|
||||
}
|
||||
if (args[i] === '--prompt') {
|
||||
options["todoPrompt"] = args[i + 1] as PresetPrompt;
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
const arg = args[i].replace(/^--/, '');
|
||||
if (['dir', 'outputFilePath', 'verbose', 'todoPrompt', 'fileTypeExclusions', 'fileExclusions', 'customPatterns', 'customSecretPatterns'].includes(arg)) {
|
||||
// @ts-ignore - dynamic property access
|
||||
options[arg] = args[i + 1];
|
||||
i++;
|
||||
} else {
|
||||
console.log(`Invalid argument specified: ${arg}`);
|
||||
console.log(`Possible arguments: ${Object.keys(options).map(item => "--" + item).join(', ')}`);
|
||||
}
|
||||
}
|
||||
|
||||
return options;
|
||||
}
|
||||
|
||||
describe('CLI', () => {
|
||||
describe('argument handling', () => {
|
||||
it('should process --prompt argument correctly', () => {
|
||||
// Set up test arguments
|
||||
const args = ['--prompt', 'tcs:fix:errors'];
|
||||
|
||||
// Process arguments
|
||||
const options = processArgs(args);
|
||||
|
||||
// Verify options
|
||||
expect(options).toEqual(expect.objectContaining({
|
||||
todoPrompt: 'tcs:fix:errors'
|
||||
}));
|
||||
});
|
||||
|
||||
it('should process other valid arguments correctly', () => {
|
||||
// Set up test arguments
|
||||
const args = ['--dir', './src', '--outputFilePath', './custom.md', '--verbose', 'false'];
|
||||
|
||||
// Process arguments
|
||||
const options = processArgs(args);
|
||||
|
||||
// Verify options
|
||||
expect(options).toEqual(expect.objectContaining({
|
||||
dir: './src',
|
||||
outputFilePath: './custom.md',
|
||||
verbose: 'false'
|
||||
}));
|
||||
});
|
||||
|
||||
it('should handle invalid arguments', () => {
|
||||
// Mock console.log to capture output
|
||||
const consoleLogSpy = spyOn(console, 'log');
|
||||
|
||||
// Set up test arguments
|
||||
const args = ['--invalidArg', 'value'];
|
||||
|
||||
// Process arguments
|
||||
processArgs(args);
|
||||
|
||||
// Verify error message was logged
|
||||
expect(consoleLogSpy).toHaveBeenCalledWith(expect.stringContaining('Invalid argument specified: invalidArg'));
|
||||
|
||||
// Restore original function
|
||||
consoleLogSpy.mockRestore();
|
||||
});
|
||||
|
||||
it('should display help when --help argument is provided', () => {
|
||||
// Mock console.log to capture output
|
||||
const consoleLogSpy = spyOn(console, 'log');
|
||||
|
||||
// Set up test arguments
|
||||
const args = ['--help'];
|
||||
|
||||
// Process arguments
|
||||
processArgs(args);
|
||||
|
||||
// Verify help message was logged
|
||||
// At this point, the options object is empty, so we just check that console.log was called
|
||||
expect(consoleLogSpy).toHaveBeenCalled();
|
||||
|
||||
// Restore original function
|
||||
consoleLogSpy.mockRestore();
|
||||
});
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user