Skip to content

Commit c6cb6e4

Browse files
committed
Remove VSCode mock and refactor diff processing by moving file operations to handler layer
1 parent 0bb0ba5 commit c6cb6e4

4 files changed

Lines changed: 34 additions & 118 deletions

File tree

‎apps/editor/__mocks__/vscode.js‎

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

‎apps/editor/jest.config.js‎

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,10 @@
22
module.exports = {
33
preset: 'ts-jest',
44
testEnvironment: 'node',
5-
// Automatically clear mock calls, instances, contexts and results before every test
65
clearMocks: true,
7-
// Indicates whether the coverage information should be collected while executing the test
86
testMatch: ['**/src/**/*.@(spec|test).ts?(x)'],
9-
// Module name mapper for path aliases from tsconfig.json
107
moduleNameMapper: {
118
'^@/(.*)$': '<rootDir>/src/$1',
12-
'^@shared/(.*)$': '<rootDir>/../../packages/shared/src/$1',
13-
'^vscode$': '<rootDir>/__mocks__/vscode.js'
14-
},
15-
// Transform files with ts-jest
16-
transform: {
17-
'^.+\\.tsx?$': [
18-
'ts-jest',
19-
{
20-
tsconfig: 'tsconfig.json' // Ensure it uses the correct tsconfig
21-
}
22-
]
23-
},
24-
// Ignore transforms for node_modules
25-
transformIgnorePatterns: ['/node_modules/']
26-
// Setup files can be added here if needed, e.g., for global mocks or environment setup
27-
// setupFilesAfterEnv: ['<rootDir>/jest.setup.js'],
28-
// Note: For tests involving actual VS Code APIs, a different setup might be needed
29-
// using vscode-test or similar, but for unit tests like clipboard-parser, 'node' is fine.
30-
}
9+
'^@shared/(.*)$': '<rootDir>/../../packages/shared/src/$1'
10+
}
11+
};

‎apps/editor/src/commands/apply-chat-response-command/handlers/diff-handler.ts‎

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Logger } from '@shared/utils/logger'
66
import { promisify } from 'util'
77
import { OriginalFileState } from '../types/original-file-state'
88
import { create_safe_path } from '@/utils/path-sanitizer'
9-
import { apply_diff, process_diff } from '../utils/diff-processor'
9+
import { apply_diff } from '../utils/diff-processor'
1010
import { remove_directory_if_empty } from '../utils/file-operations'
1111

1212
const execAsync = promisify(exec)
@@ -398,6 +398,36 @@ const handle_deleted_file_patch = async (
398398
}
399399
}
400400

401+
const process_diff = async (params: {
402+
file_path: string
403+
diff_patch_path: string
404+
use_strict_whitespace?: boolean
405+
}): Promise<string> => {
406+
const file_content = fs.readFileSync(params.file_path, 'utf8')
407+
const diff_patch_content = fs.readFileSync(params.diff_patch_path, 'utf8')
408+
409+
const result = apply_diff({
410+
original_code: file_content,
411+
diff_patch: diff_patch_content,
412+
use_strict_whitespace: params.use_strict_whitespace
413+
})
414+
415+
try {
416+
await vscode.workspace.fs.writeFile(
417+
vscode.Uri.file(params.file_path),
418+
Buffer.from(result, 'utf8')
419+
)
420+
return result
421+
} catch (error) {
422+
Logger.error({
423+
function_name: 'process_diff',
424+
message: 'Failed to save file after applying diff patch',
425+
data: { error, file_path: params.file_path }
426+
})
427+
throw new Error('Failed to save file after applying diff patch')
428+
}
429+
}
430+
401431
export const apply_git_patch = async (
402432
patch_content: string,
403433
workspace_path: string

‎apps/editor/src/commands/apply-chat-response-command/utils/diff-processor/diff-processor.ts‎

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
import * as vscode from 'vscode'
2-
import * as fs from 'fs'
3-
41
type ReplaceLine = {
52
content: string
63
search_index: number | null
@@ -14,31 +11,6 @@ type SearchBlock = {
1411
search_to_original_map: Map<number, number>
1512
}
1613

17-
export const process_diff = async (params: {
18-
file_path: string
19-
diff_patch_path: string
20-
use_strict_whitespace?: boolean
21-
}): Promise<string> => {
22-
const file_content = fs.readFileSync(params.file_path, 'utf8')
23-
const diff_patch_content = fs.readFileSync(params.diff_patch_path, 'utf8')
24-
25-
const result = apply_diff({
26-
original_code: file_content,
27-
diff_patch: diff_patch_content,
28-
use_strict_whitespace: params.use_strict_whitespace
29-
})
30-
31-
try {
32-
await vscode.workspace.fs.writeFile(
33-
vscode.Uri.file(params.file_path),
34-
Buffer.from(result, 'utf8')
35-
)
36-
return result
37-
} catch (error) {
38-
throw new Error('Failed to save file after applying diff patch')
39-
}
40-
}
41-
4214
export const apply_diff = (params: {
4315
original_code: string
4416
diff_patch: string

0 commit comments

Comments
 (0)