Skip to content

Commit ed18f70

Browse files
committed
feat: add tests and update gitignore
1 parent 37292e7 commit ed18f70

File tree

7 files changed

+146
-4
lines changed

7 files changed

+146
-4
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ node_modules
66
release-it.sh
77
.cursorrules
88
.clinerules
9-
.windsuftrules
9+
.windsuftrules
10+
.cursor/*

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -266,4 +266,4 @@
266266
"zod": "^3.23.8"
267267
},
268268
"packageManager": "[email protected]"
269-
}
269+
}

src/constants/__tests__/cody.test.ts

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import * as assert from 'assert'
2+
import * as sinon from 'sinon'
3+
import * as vscode from 'vscode'
4+
import { CODY_COMMAND, CODY_CUSTOM_COMMANDS_FILE, getCodyJsonPath } from '../cody'
5+
6+
suite('Cody Constants Tests', () => {
7+
let sandbox: sinon.SinonSandbox
8+
9+
setup(() => {
10+
sandbox = sinon.createSandbox()
11+
})
12+
13+
teardown(() => {
14+
sandbox.restore()
15+
})
16+
17+
test('should have correct command constants', () => {
18+
assert.strictEqual(CODY_COMMAND.MENTION.FILE, 'cody.mention.file')
19+
assert.strictEqual(CODY_COMMAND.COMMAND.CUSTOM, 'cody.command.custom')
20+
})
21+
22+
test('should have correct custom commands file name', () => {
23+
assert.strictEqual(CODY_CUSTOM_COMMANDS_FILE, 'cody.json')
24+
})
25+
26+
test('should return correct cody.json path when workspace exists', () => {
27+
const mockWorkspaceFolder = { uri: { fsPath: '/test/path' } }
28+
sandbox.stub(vscode.workspace, 'workspaceFolders').value([mockWorkspaceFolder])
29+
30+
const result = getCodyJsonPath()
31+
assert.strictEqual(result, '/test/path/.vscode/cody.json')
32+
})
33+
34+
test('should return undefined when no workspace exists', () => {
35+
sandbox.stub(vscode.workspace, 'workspaceFolders').value(undefined)
36+
37+
const result = getCodyJsonPath()
38+
assert.strictEqual(result, undefined)
39+
})
40+
})
+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import * as assert from 'assert'
2+
import { TELEMETRY_EVENTS } from '../telemetry'
3+
4+
suite('Telemetry Constants Tests', () => {
5+
test('should have correct file event names', () => {
6+
assert.strictEqual(TELEMETRY_EVENTS.FILES.ADD_FILE, 'add_file')
7+
assert.strictEqual(TELEMETRY_EVENTS.FILES.ADD_SELECTION, 'add_selection')
8+
assert.strictEqual(TELEMETRY_EVENTS.FILES.ADD_FOLDER, 'add_folder')
9+
assert.strictEqual(TELEMETRY_EVENTS.FILES.ADD_SMART_SELECTION, 'add_smart_selection')
10+
})
11+
12+
test('should have correct custom command event names', () => {
13+
assert.strictEqual(TELEMETRY_EVENTS.CUSTOM_COMMANDS.CREATED, 'custom_command_created')
14+
assert.strictEqual(TELEMETRY_EVENTS.CUSTOM_COMMANDS.DELETED, 'custom_command_deleted')
15+
assert.strictEqual(TELEMETRY_EVENTS.CUSTOM_COMMANDS.EXECUTED, 'custom_command_executed')
16+
})
17+
})
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import * as assert from 'assert'
2+
import { COMMANDS, DEV_WEBVIEW_URL } from '../webview'
3+
4+
suite('Webview Constants Tests', () => {
5+
test('should have correct dev webview URL', () => {
6+
assert.strictEqual(DEV_WEBVIEW_URL, 'http://localhost:5173')
7+
})
8+
9+
test('should have correct command names', () => {
10+
assert.strictEqual(COMMANDS.CREATE_COMMAND, 'create_command')
11+
assert.strictEqual(COMMANDS.UPDATE_COMMAND, 'update_command')
12+
})
13+
})
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import * as assert from 'assert'
2+
import * as sinon from 'sinon'
3+
import * as vscode from 'vscode'
4+
import { executeMentionFileCommand } from '../commands'
5+
6+
suite('Cody Commands Tests', () => {
7+
let sandbox: sinon.SinonSandbox
8+
let executeCommandStub: sinon.SinonStub
9+
let showErrorMessageStub: sinon.SinonStub
10+
11+
setup(() => {
12+
sandbox = sinon.createSandbox()
13+
executeCommandStub = sandbox.stub(vscode.commands, 'executeCommand')
14+
showErrorMessageStub = sandbox.stub(vscode.window, 'showErrorMessage')
15+
})
16+
17+
teardown(() => {
18+
sandbox.restore()
19+
})
20+
21+
suite('executeMentionFileCommand', () => {
22+
test('should successfully execute mention file command', async () => {
23+
// Setup
24+
const testUri = vscode.Uri.file('/test/file.js')
25+
executeCommandStub.resolves(true)
26+
27+
// Execute
28+
const result = await executeMentionFileCommand(testUri)
29+
30+
console.log({ result })
31+
32+
// Verify
33+
assert.strictEqual(result, true)
34+
assert.strictEqual(executeCommandStub.calledOnce, true)
35+
assert.strictEqual(executeCommandStub.firstCall.args[0], 'cody.mention.file')
36+
assert.strictEqual(executeCommandStub.firstCall.args[1], testUri)
37+
})
38+
39+
test('should handle command execution failure', async () => {
40+
// Setup
41+
const testUri = vscode.Uri.file('/test/file.js')
42+
const testError = new Error('Command failed')
43+
executeCommandStub.rejects(testError)
44+
45+
// Execute
46+
const result = await executeMentionFileCommand(testUri)
47+
48+
// Verify
49+
assert.strictEqual(result, false)
50+
assert.strictEqual(executeCommandStub.calledOnce, true)
51+
assert.strictEqual(showErrorMessageStub.calledOnce, true)
52+
assert.strictEqual(
53+
showErrorMessageStub.firstCall.args[0],
54+
'Failed to trigger Cody to mention file: Command failed'
55+
)
56+
})
57+
58+
test('should handle invalid URI', async () => {
59+
// Setup
60+
const invalidUri = {} as vscode.Uri
61+
executeCommandStub.rejects(new Error('Invalid URI'))
62+
63+
// Execute
64+
const result = await executeMentionFileCommand(invalidUri)
65+
66+
// Verify
67+
assert.strictEqual(result, false)
68+
assert.strictEqual(executeCommandStub.calledOnce, true)
69+
assert.strictEqual(showErrorMessageStub.calledOnce, true)
70+
})
71+
})
72+
})

src/core/cody/commands.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import * as vscode from 'vscode'
2-
import { CODY_COMMAND } from '../../constants/cody'
32

43
/**
54
* Execute Cody command to mention a file
65
* @param uri URI of file to mention
76
*/
87
export async function executeMentionFileCommand(uri: vscode.Uri): Promise<boolean> {
98
try {
10-
await vscode.commands.executeCommand(CODY_COMMAND.MENTION.FILE, uri)
9+
await vscode.commands.executeCommand('cody.mention.file', uri)
1110
return true
1211
} catch (error: any) {
1312
vscode.window.showErrorMessage(`Failed to trigger Cody to mention file: ${error.message}`)

0 commit comments

Comments
 (0)