Skip to content

Commit b02392f

Browse files
committed
refactor: remove non-billing DI changes to keep PR focused
Revert changes to: - cli/src/utils/auth.ts (getConfigDirFromEnvironment extraction) - sdk/src/tools/code-search.ts (spawn DI) - web/src/lib/ban-conditions.ts (db/stripe DI) These DI improvements can be done in a separate PR. This PR now focuses solely on billing DI patterns.
1 parent 5acc99a commit b02392f

File tree

6 files changed

+147
-163
lines changed

6 files changed

+147
-163
lines changed

cli/src/__tests__/integration/credentials-storage.test.ts

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ import fs from 'fs'
22
import os from 'os'
33
import path from 'path'
44

5+
import {
6+
clearMockedModules,
7+
mockModule,
8+
} from '@codebuff/common/testing/mock-modules'
59
import {
610
describe,
711
test,
@@ -13,11 +17,7 @@ import {
1317
} from 'bun:test'
1418

1519
import * as authModule from '../../utils/auth'
16-
import {
17-
saveUserCredentials,
18-
getUserCredentials,
19-
getConfigDirFromEnvironment,
20-
} from '../../utils/auth'
20+
import { saveUserCredentials, getUserCredentials } from '../../utils/auth'
2121
import { setProjectRoot } from '../../project-files'
2222

2323
import type { User } from '../../utils/auth'
@@ -71,6 +71,7 @@ describe('Credentials Storage Integration', () => {
7171
}
7272

7373
mock.restore()
74+
clearMockedModules()
7475
})
7576

7677
describe('P0: File System Operations', () => {
@@ -157,22 +158,47 @@ describe('Credentials Storage Integration', () => {
157158
expect(keys[0]).toBe('default')
158159
})
159160

160-
test('should use manicode-test directory in test environment', () => {
161-
const configDir = getConfigDirFromEnvironment('test')
161+
test('should use manicode-test directory in test environment', async () => {
162+
// Restore getConfigDir to use real implementation for this test
163+
mock.restore()
164+
165+
await mockModule('@codebuff/common/env', () => ({
166+
env: { NEXT_PUBLIC_CB_ENVIRONMENT: 'test' },
167+
}))
168+
169+
// Call real getConfigDir to verify it includes '-dev'
170+
const configDir = authModule.getConfigDir()
162171
expect(configDir).toEqual(
163172
path.join(os.homedir(), '.config', 'manicode-test'),
164173
)
165174
})
166175

167-
test('should use manicode-dev directory in development environment', () => {
168-
const configDir = getConfigDirFromEnvironment('dev')
176+
test('should use manicode-dev directory in development environment', async () => {
177+
// Restore getConfigDir to use real implementation for this test
178+
mock.restore()
179+
180+
await mockModule('@codebuff/common/env', () => ({
181+
env: { NEXT_PUBLIC_CB_ENVIRONMENT: 'dev' },
182+
}))
183+
184+
// Call real getConfigDir to verify it includes '-dev'
185+
const configDir = authModule.getConfigDir()
169186
expect(configDir).toEqual(
170187
path.join(os.homedir(), '.config', 'manicode-dev'),
171188
)
172189
})
173190

174-
test('should use manicode directory in production environment', () => {
175-
const configDir = getConfigDirFromEnvironment('prod')
191+
test('should use manicode directory in production environment', async () => {
192+
// Restore getConfigDir to use real implementation
193+
mock.restore()
194+
195+
// Set environment to prod (or unset it)
196+
await mockModule('@codebuff/common/env', () => ({
197+
env: { NEXT_PUBLIC_CB_ENVIRONMENT: 'prod' },
198+
}))
199+
200+
// Call real getConfigDir to verify it doesn't include '-dev'
201+
const configDir = authModule.getConfigDir()
176202
expect(configDir).toEqual(path.join(os.homedir(), '.config', 'manicode'))
177203
})
178204

cli/src/utils/auth.ts

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,19 @@ const credentialsSchema = z
4141
})
4242
.catchall(z.unknown())
4343

44-
// Get the config directory path from a specific environment value
45-
export const getConfigDirFromEnvironment = (
46-
environment: string | undefined,
47-
): string => {
44+
// Get the config directory path
45+
export const getConfigDir = (): string => {
4846
return path.join(
4947
os.homedir(),
5048
'.config',
5149
'manicode' +
52-
(environment && environment !== 'prod' ? `-${environment}` : ''),
50+
// on a development stack?
51+
(env.NEXT_PUBLIC_CB_ENVIRONMENT !== 'prod'
52+
? `-${env.NEXT_PUBLIC_CB_ENVIRONMENT}`
53+
: ''),
5354
)
5455
}
5556

56-
// Get the config directory path
57-
export const getConfigDir = (): string => {
58-
return getConfigDirFromEnvironment(env.NEXT_PUBLIC_CB_ENVIRONMENT)
59-
}
60-
6157
// Get the credentials file path
6258
export const getCredentialsPath = (): string => {
6359
return path.join(getConfigDir(), 'credentials.json')

sdk/src/__tests__/code-search.test.ts

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
import { EventEmitter } from 'events'
22

3+
import {
4+
clearMockedModules,
5+
mockModule,
6+
} from '@codebuff/common/testing/mock-modules'
37
import { describe, expect, it, mock, beforeEach, afterEach } from 'bun:test'
48

59
import { codeSearch } from '../tools/code-search'
610

711
import type { ChildProcess } from 'child_process'
8-
import type { CodeSearchDeps } from '../tools/code-search'
912

1013
// Helper to create a mock child process
1114
function createMockChildProcess() {
@@ -53,24 +56,25 @@ function createRgJsonContext(
5356
describe('codeSearch', () => {
5457
let mockSpawn: ReturnType<typeof mock>
5558
let mockProcess: ReturnType<typeof createMockChildProcess>
56-
let deps: CodeSearchDeps
5759

58-
beforeEach(() => {
60+
beforeEach(async () => {
5961
mockProcess = createMockChildProcess()
6062
mockSpawn = mock(() => mockProcess)
61-
deps = { spawn: mockSpawn as CodeSearchDeps['spawn'] }
63+
await mockModule('child_process', () => ({
64+
spawn: mockSpawn,
65+
}))
6266
})
6367

6468
afterEach(() => {
6569
mock.restore()
70+
clearMockedModules()
6671
})
6772

6873
describe('basic search', () => {
6974
it('should parse standard ripgrep output without context flags', async () => {
7075
const searchPromise = codeSearch({
7176
projectPath: '/test/project',
7277
pattern: 'import',
73-
deps,
7478
})
7579

7680
// Simulate ripgrep JSON output
@@ -97,7 +101,6 @@ describe('codeSearch', () => {
97101
projectPath: '/test/project',
98102
pattern: 'import.*env',
99103
flags: '-A 2',
100-
deps,
101104
})
102105

103106
// Ripgrep JSON output with -A 2 includes match + 2 context lines after
@@ -133,7 +136,6 @@ describe('codeSearch', () => {
133136
projectPath: '/test/project',
134137
pattern: 'export',
135138
flags: '-B 2',
136-
deps,
137139
})
138140

139141
// Ripgrep JSON output with -B 2 includes 2 context lines before + match
@@ -167,7 +169,6 @@ describe('codeSearch', () => {
167169
projectPath: '/test/project',
168170
pattern: 'TODO',
169171
flags: '-C 1',
170-
deps,
171172
})
172173

173174
// Ripgrep JSON output with -C 1 includes 1 line before + match + 1 line after
@@ -196,7 +197,6 @@ describe('codeSearch', () => {
196197
projectPath: '/test/project',
197198
pattern: 'import',
198199
flags: '-A 1',
199-
deps,
200200
})
201201

202202
const output = [
@@ -225,7 +225,6 @@ describe('codeSearch', () => {
225225
projectPath: '/test/project',
226226
pattern: 'import',
227227
flags: '-B 2',
228-
deps,
229228
})
230229

231230
// First line match has no before context
@@ -246,7 +245,6 @@ describe('codeSearch', () => {
246245
projectPath: '/test/project',
247246
pattern: 'test',
248247
flags: '-A 1',
249-
deps,
250248
})
251249

252250
const output = [
@@ -271,7 +269,6 @@ describe('codeSearch', () => {
271269
projectPath: '/test/project',
272270
pattern: 'import',
273271
flags: '-A 1',
274-
deps,
275272
})
276273

277274
const output = [
@@ -297,7 +294,6 @@ describe('codeSearch', () => {
297294
projectPath: '/test/project',
298295
pattern: 'test',
299296
flags: '-A 1',
300-
deps,
301297
})
302298

303299
const output = createRgJsonMatch(
@@ -323,7 +319,6 @@ describe('codeSearch', () => {
323319
pattern: 'import.*env',
324320
flags: '-A 2',
325321
maxOutputStringLength: 20000,
326-
deps,
327322
})
328323

329324
const output = [
@@ -353,7 +348,6 @@ describe('codeSearch', () => {
353348
pattern: 'test',
354349
flags: '-A 1',
355350
maxResults: 2,
356-
deps,
357351
})
358352

359353
const output = [
@@ -394,7 +388,6 @@ describe('codeSearch', () => {
394388
pattern: 'test',
395389
flags: '-A 1',
396390
globalMaxResults: 3,
397-
deps,
398391
})
399392

400393
const output = [
@@ -430,7 +423,6 @@ describe('codeSearch', () => {
430423
pattern: 'match',
431424
flags: '-A 2 -B 2',
432425
maxResults: 1,
433-
deps,
434426
})
435427

436428
const output = [
@@ -463,7 +455,6 @@ describe('codeSearch', () => {
463455
const searchPromise = codeSearch({
464456
projectPath: '/test/project',
465457
pattern: 'test',
466-
deps,
467458
})
468459

469460
const output = [
@@ -487,7 +478,6 @@ describe('codeSearch', () => {
487478
const searchPromise = codeSearch({
488479
projectPath: '/test/project',
489480
pattern: 'nonexistent',
490-
deps,
491481
})
492482

493483
mockProcess.stdout.emit('data', Buffer.from(''))
@@ -508,7 +498,6 @@ describe('codeSearch', () => {
508498
const searchPromise = codeSearch({
509499
projectPath: '/test/project',
510500
pattern: '-foo',
511-
deps,
512501
})
513502

514503
const output = createRgJsonMatch('file.ts', 1, 'const x = -foo')
@@ -529,7 +518,6 @@ describe('codeSearch', () => {
529518
const searchPromise = codeSearch({
530519
projectPath: '/test/project',
531520
pattern: 'import',
532-
deps,
533521
})
534522

535523
// Simulate ripgrep JSON with trailing newlines in lineText
@@ -559,7 +547,6 @@ describe('codeSearch', () => {
559547
const searchPromise = codeSearch({
560548
projectPath: '/test/project',
561549
pattern: 'test',
562-
deps,
563550
})
564551

565552
// Send partial JSON chunks that will be completed in remainder
@@ -589,7 +576,6 @@ describe('codeSearch', () => {
589576
projectPath: '/test/project',
590577
pattern: 'test',
591578
maxOutputStringLength: 500, // Small limit
592-
deps,
593579
})
594580

595581
// Generate many matches that would exceed the limit
@@ -617,7 +603,6 @@ describe('codeSearch', () => {
617603
const searchPromise = codeSearch({
618604
projectPath: '/test/project',
619605
pattern: 'test',
620-
deps,
621606
})
622607

623608
// Simulate ripgrep JSON with path.bytes instead of path.text
@@ -648,7 +633,6 @@ describe('codeSearch', () => {
648633
projectPath: '/test/project',
649634
pattern: 'import',
650635
flags: '-g *.ts',
651-
deps,
652636
})
653637

654638
const output = [
@@ -676,7 +660,6 @@ describe('codeSearch', () => {
676660
projectPath: '/test/project',
677661
pattern: 'import',
678662
flags: '-g *.ts -g *.tsx',
679-
deps,
680663
})
681664

682665
const output = createRgJsonMatch('file.tsx', 1, 'import React from "react"')
@@ -703,7 +686,6 @@ describe('codeSearch', () => {
703686
projectPath: '/test/project',
704687
pattern: 'import',
705688
flags: '-g *.ts -i -g *.tsx',
706-
deps,
707689
})
708690

709691
const output = createRgJsonMatch('file.tsx', 1, 'import React from "react"')
@@ -733,7 +715,6 @@ describe('codeSearch', () => {
733715
projectPath: '/test/project',
734716
pattern: 'test',
735717
timeoutSeconds: 1,
736-
deps,
737718
})
738719

739720
// Don't emit any data or close event to simulate hanging
@@ -756,7 +737,6 @@ describe('codeSearch', () => {
756737
projectPath: '/test/project',
757738
pattern: 'test',
758739
cwd: '.',
759-
deps,
760740
})
761741

762742
const output = createRgJsonMatch('file.ts', 1, 'test content')
@@ -784,7 +764,6 @@ describe('codeSearch', () => {
784764
projectPath: '/test/project',
785765
pattern: 'test',
786766
cwd: 'subdir',
787-
deps,
788767
})
789768

790769
const output = createRgJsonMatch('file.ts', 1, 'test content')
@@ -809,7 +788,6 @@ describe('codeSearch', () => {
809788
projectPath: '/test/project',
810789
pattern: 'test',
811790
cwd: '../outside',
812-
deps,
813791
})
814792

815793
const result = await searchPromise

0 commit comments

Comments
 (0)