Skip to content

Commit 7b7f96f

Browse files
committed
refactor(code): reorganize test structure to test/ directory
- Moved all tests from src/__tests__/ to test/unit/ and test/integration/ - Created new utils package with extracted utilities: - parseArgs function with bug fix for flag value parsing - Platform detection utilities - Fixed CLI -v/--version and -h/--help flag handling - Updated package.json test script to bun test ./test - Reorganized test structure: unit tests in test/unit/, integration tests in test/integration/ - All 87 tests passing with new structure
1 parent cb7f71a commit 7b7f96f

11 files changed

Lines changed: 1161 additions & 188 deletions

File tree

packages/code/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
"dev": "bun --watch run src/cli.ts",
2929
"build": "echo 'Build handled by root build:npm'",
3030
"typecheck": "tsc -p tsconfig.json --noEmit",
31-
"test": "bun test ./src"
31+
"test": "bun test ./test"
3232
},
3333
"dependencies": {
3434
"@pleaseai/code-format": "workspace:*",

packages/code/src/__tests__/cli.test.ts

Lines changed: 0 additions & 150 deletions
This file was deleted.

packages/code/src/cli.ts

Lines changed: 11 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import process from 'node:process'
1717
import { Format } from '@pleaseai/code-format'
1818
import pkg from '../package.json'
1919
import { runLSPDiagnostics } from './hooks/lsp'
20+
import { parseArgs } from './utils'
2021

2122
const VERSION = pkg.version
2223

@@ -31,38 +32,6 @@ interface HookInput {
3132
tool_use_id: string
3233
}
3334

34-
interface ParsedArgs {
35-
command: string
36-
args: string[]
37-
flags: Record<string, string | boolean>
38-
}
39-
40-
function parseArgs(argv: string[]): ParsedArgs {
41-
const args = argv.slice(2)
42-
const flags: Record<string, string | boolean> = {}
43-
const positional: string[] = []
44-
45-
for (let i = 0; i < args.length; i++) {
46-
const arg = args[i]!
47-
if (arg.startsWith('--')) {
48-
const [key, value] = arg.slice(2).split('=')
49-
flags[key!] = value ?? true
50-
}
51-
else if (arg.startsWith('-')) {
52-
flags[arg.slice(1)] = true
53-
}
54-
else {
55-
positional.push(arg)
56-
}
57-
}
58-
59-
return {
60-
command: positional[0] ?? 'help',
61-
args: positional.slice(1),
62-
flags,
63-
}
64-
}
65-
6635
async function readStdinJson(): Promise<HookInput> {
6736
const chunks: Buffer[] = []
6837
for await (const chunk of Bun.stdin.stream()) {
@@ -157,6 +126,16 @@ Environment:
157126
async function main(): Promise<void> {
158127
const { command, args, flags } = parseArgs(process.argv)
159128

129+
// Check for version/help flags first (before command routing)
130+
if (flags.v || flags.version) {
131+
versionCommand()
132+
return
133+
}
134+
if (flags.h || flags.help) {
135+
helpCommand()
136+
return
137+
}
138+
160139
const projectDir
161140
= (flags.project as string)
162141
?? process.env.CODE_PROJECT_PATH
@@ -208,14 +187,10 @@ async function main(): Promise<void> {
208187
}
209188

210189
case 'version':
211-
case '-v':
212-
case '--version':
213190
versionCommand()
214191
break
215192

216193
case 'help':
217-
case '-h':
218-
case '--help':
219194
helpCommand()
220195
break
221196

packages/code/src/utils/args.ts

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/**
2+
* CLI argument parsing utilities
3+
*/
4+
5+
export interface ParsedArgs {
6+
command: string
7+
args: string[]
8+
flags: Record<string, string | boolean>
9+
}
10+
11+
/**
12+
* Parse command-line arguments into structured format.
13+
*
14+
* Supports:
15+
* - --flag=value (equals syntax)
16+
* - --flag (boolean flag)
17+
* - -f (short boolean flag)
18+
* - Positional arguments
19+
*
20+
* @param argv - Process argv array (first 2 elements are node/bun path and script path)
21+
* @returns Parsed arguments with command, positional args, and flags
22+
*/
23+
export function parseArgs(argv: string[]): ParsedArgs {
24+
const args = argv.slice(2)
25+
const flags: Record<string, string | boolean> = {}
26+
const positional: string[] = []
27+
28+
for (let i = 0; i < args.length; i++) {
29+
const arg = args[i]!
30+
if (arg.startsWith('--')) {
31+
const eqIndex = arg.indexOf('=')
32+
if (eqIndex > -1) {
33+
const key = arg.slice(2, eqIndex)
34+
const value = arg.slice(eqIndex + 1)
35+
flags[key] = value
36+
}
37+
else {
38+
flags[arg.slice(2)] = true
39+
}
40+
}
41+
else if (arg.startsWith('-')) {
42+
flags[arg.slice(1)] = true
43+
}
44+
else {
45+
positional.push(arg)
46+
}
47+
}
48+
49+
return {
50+
command: positional[0] ?? 'help',
51+
args: positional.slice(1),
52+
flags,
53+
}
54+
}

packages/code/src/utils/index.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
/**
2+
* Utility modules for @pleaseai/code
3+
*/
4+
5+
export { parseArgs, type ParsedArgs } from './args'
6+
export { getPotentialBinaryPaths, getTarget, isMusl } from './platform'

0 commit comments

Comments
 (0)