Skip to content

Commit 60c5555

Browse files
authored
Merge pull request #39 from MetabobProject/testing-extension
Testing for extension
2 parents 90f15e5 + 4ea89c5 commit 60c5555

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+16913
-10571
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ node_modules
44
*.vsix
55
.DS_Store
66
.history
7-
build
7+
build
8+
coverage

.vscodeignore

+2
Original file line numberDiff line numberDiff line change
@@ -39,3 +39,5 @@ scripts/**
3939
nodemon.json
4040
commitlint.config.js
4141
lint-staged.config.js
42+
coverage
43+
jest.config.ts

ext-src/__mocks__/vscode.ts

+122
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
2+
// @ts-nocheck
3+
// Mocked Uri interface
4+
export interface Uri {
5+
scheme: string;
6+
path: string;
7+
}
8+
9+
// Mocked Position class
10+
export class Position {
11+
constructor(public line: number, public character: number) { }
12+
13+
isBefore(other: Position): boolean {
14+
return this.line < other.line || (this.line === other.line && this.character < other.character);
15+
}
16+
17+
// Mocked methods
18+
isBeforeOrEqual = jest.fn((other: Position) => this.isBefore(other) || this.isEqual(other));
19+
isAfter = jest.fn((other: Position) => !this.isBeforeOrEqual(other));
20+
isAfterOrEqual = jest.fn((other: Position) => this.isAfter(other) || this.isEqual(other));
21+
isEqual = jest.fn((other: Position) => this.line === other.line && this.character === other.character);
22+
compareTo = jest.fn((other: Position) => {
23+
if (this.isBefore(other)) return -1;
24+
if (this.isAfter(other)) return 1;
25+
return 0;
26+
});
27+
translate = jest.fn((lineDelta?: number, characterDelta?: number) => {
28+
const line = typeof lineDelta === 'number' ? this.line + lineDelta : this.line;
29+
const character = typeof characterDelta === 'number' ? this.character + characterDelta : this.character;
30+
return new Position(line, character);
31+
});
32+
with = jest.fn((line?: number, character?: number) => new Position(line ?? this.line, character ?? this.character));
33+
}
34+
35+
// Mocked Range class
36+
export class Range {
37+
constructor(public start: Position, public end: Position) { }
38+
39+
isEmpty = jest.fn(() => this.start.isEqual(this.end));
40+
isSingleLine = jest.fn(() => this.start.line === this.end.line);
41+
contains = jest.fn((positionOrRange: Position | Range) => {
42+
if (positionOrRange instanceof Position) {
43+
return (
44+
(positionOrRange.isAfterOrEqual(this.start) && positionOrRange.isBefore(this.end)) ||
45+
positionOrRange.isEqual(this.start) ||
46+
positionOrRange.isEqual(this.end)
47+
);
48+
}
49+
return (
50+
this.contains(positionOrRange.start) ||
51+
this.contains(positionOrRange.end) ||
52+
(positionOrRange.start.isBeforeOrEqual(this.start) && positionOrRange.end.isAfterOrEqual(this.end))
53+
);
54+
});
55+
isEqual = jest.fn((other: Range) => this.start.isEqual(other.start) && this.end.isEqual(other.end));
56+
intersection = jest.fn((range: Range) => {
57+
const start = this.start.isBefore(range.start) ? range.start : this.start;
58+
const end = this.end.isAfter(range.end) ? range.end : this.end;
59+
return start.isBefore(end) ? new Range(start, end) : undefined;
60+
});
61+
union = jest.fn((other: Range) => {
62+
const start = this.start.isBefore(other.start) ? this.start : other.start;
63+
const end = this.end.isAfter(other.end) ? this.end : other.end;
64+
return new Range(start, end);
65+
});
66+
with = jest.fn((start?: Position, end?: Position) => new Range(start ?? this.start, end ?? this.end));
67+
}
68+
69+
// Mocked TextDocument interface
70+
export interface TextDocument {
71+
uri: Uri;
72+
fileName: string;
73+
languageId: string;
74+
version: number;
75+
isDirty: boolean;
76+
isClosed: boolean;
77+
78+
// Implement other properties and methods
79+
}
80+
81+
export class ExtensionContextMock {
82+
// Add any necessary properties and methods here for your tests
83+
subscriptions: { dispose: jest.Mock }[] = [];
84+
85+
// Mock method to subscribe to events
86+
subscribe(subscription: { dispose: jest.Mock }): void {
87+
this.subscriptions.push(subscription);
88+
}
89+
90+
// Mock method to trigger disposal of subscriptions
91+
dispose(): void {
92+
this.subscriptions.forEach(sub => sub.dispose());
93+
this.subscriptions = [];
94+
}
95+
}
96+
97+
// Mocked vscode module
98+
const vscode = {
99+
version: '1.0.0',
100+
Position,
101+
Range,
102+
Uri: { scheme: '', path: '' },
103+
window: {
104+
activeTextEditor: {
105+
document: null as TextDocument | null,
106+
},
107+
},
108+
commands: {
109+
executeCommand: jest.fn(),
110+
},
111+
languages: {
112+
getTextDocument: jest.fn(),
113+
},
114+
env: {
115+
isTelemetryEnabled: true,
116+
machineId: '123456789',
117+
},
118+
};
119+
120+
vscode.ExtensionContext = ExtensionContextMock;
121+
122+
export default vscode;

ext-src/commands/AnalyzeDocument.ts

+2-6
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import * as vscode from 'vscode';
22
import { SubmitRepresentationResponse } from '../services';
33
import { handleDocumentAnalyze } from '../helpers';
44
import { Analyze, Session } from '../state';
5-
import debugChannel from '../debug';
65
import CONSTANTS from '../constants';
76
import Util from '../utils';
87
import { getExtensionEventEmitter } from '../events';
98

10-
119
export function activateAnalyzeCommand(context: vscode.ExtensionContext): void {
1210
const command = CONSTANTS.analyzeDocumentCommand;
1311

@@ -44,6 +42,7 @@ export function activateAnalyzeCommand(context: vscode.ExtensionContext): void {
4442

4543
// If the user session is not available then we can't request file analysis.
4644
if (!sessionToken) {
45+
vscode.window.showErrorMessage(CONSTANTS.sessionTokenUndefined);
4746
extensionEventEmitter.fire({
4847
type: 'Analysis_Error',
4948
data: 'Session Token is undefined',
@@ -53,9 +52,6 @@ export function activateAnalyzeCommand(context: vscode.ExtensionContext): void {
5352

5453
const documentMetaData = Util.extractMetaDataFromDocument(editor.document);
5554

56-
debugChannel.appendLine(`Metabob: Starting Analysis for ${documentMetaData.filePath}`);
57-
58-
5955
Util.withProgress<SubmitRepresentationResponse>(
6056
handleDocumentAnalyze(documentMetaData, sessionToken, analyzeState, context),
6157
CONSTANTS.analyzeCommandProgressMessage,
@@ -71,7 +67,7 @@ export function activateAnalyzeCommand(context: vscode.ExtensionContext): void {
7167
}
7268

7369
Util.withProgress<SubmitRepresentationResponse>(
74-
handleDocumentAnalyze(documentMetaData, sessionToken, analyzeState, context, inflightJobId,),
70+
handleDocumentAnalyze(documentMetaData, sessionToken, analyzeState, context, inflightJobId),
7571
CONSTANTS.analyzeCommandQueueMessage,
7672
).then(response => {
7773
switch (response.status) {

ext-src/commands/DetailDocument.ts

+6-11
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import * as vscode from 'vscode';
22
import CONSTANTS from '../constants';
3-
import _debug from '../debug';
4-
import { Analyze, CurrentQuestion, CurrentQuestionState, Session } from '../state';
3+
import { Analyze, Session } from '../state';
54
import { Problem } from '../types';
65
import { getExtensionEventEmitter } from '../events';
76
import { FeedbackSuggestionPayload, feedbackService } from '../services';
@@ -23,20 +22,18 @@ export function activateDetailSuggestionCommand(context: vscode.ExtensionContext
2322
const sessionToken = new Session(context).get()?.value;
2423
const extensionEventEmitter = getExtensionEventEmitter();
2524

26-
_debug.appendLine(`Detail initiated for ${args.path} with Problem ${args.id} `);
27-
2825
const documentMetaData = Utils.getFileNameFromCurrentEditor();
2926

3027
if (!documentMetaData) {
3128
vscode.window.showErrorMessage(CONSTANTS.editorNotSelectorError);
32-
return
29+
return;
3330
}
3431

3532
if (!sessionToken || !analyzeStateValue) {
3633
getExtensionEventEmitter().fire({
3734
type: 'CURRENT_PROJECT',
3835
data: {
39-
name: currentWorkSpaceFolder
36+
name: currentWorkSpaceFolder,
4037
},
4138
});
4239

@@ -76,7 +73,7 @@ export function activateDetailSuggestionCommand(context: vscode.ExtensionContext
7673
getExtensionEventEmitter().fire({
7774
type: 'Analysis_Completed',
7875
data: {
79-
shouldResetRecomendation: true,
76+
shouldResetRecomendation: false,
8077
shouldMoveToAnalyzePage: false,
8178
...copiedAnalyzeValue,
8279
},
@@ -90,16 +87,14 @@ export function activateDetailSuggestionCommand(context: vscode.ExtensionContext
9087
getExtensionEventEmitter().fire({
9188
type: 'CURRENT_PROJECT',
9289
data: {
93-
name: currentWorkSpaceFolder
90+
name: currentWorkSpaceFolder,
9491
},
9592
});
9693
}, 500);
9794

9895
await setAnalyzeState.set(copiedAnalyzeValue);
9996

100-
await Promise.allSettled([
101-
feedbackService.readSuggestion(readSuggestionPayload, sessionToken),
102-
]);
97+
await Promise.allSettled([feedbackService.readSuggestion(readSuggestionPayload, sessionToken)]);
10398
};
10499

105100
context.subscriptions.push(vscode.commands.registerCommand(command, commandHandler));

ext-src/commands/DiscardSuggestion.ts

+2-8
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { getExtensionEventEmitter } from '../events';
55
import CONSTANTS from '../constants';
66
import Utils from '../utils';
77
import { Problem } from '../types';
8-
import _debug from '../debug';
98

109
export type DiscardCommandHandler = { id: string; path: string };
1110

@@ -18,7 +17,6 @@ export function activateDiscardCommand(context: vscode.ExtensionContext): void {
1817
const problems = analyzeState.get()?.value;
1918

2019
if (!problems) {
21-
_debug?.appendLine('Metabob: Problems is undefined in Discard Suggestion');
2220
vscode.window.showErrorMessage(CONSTANTS.discardCommandErrorMessage);
2321
return;
2422
}
@@ -30,7 +28,6 @@ export function activateDiscardCommand(context: vscode.ExtensionContext): void {
3028

3129
const session = new Session(context).get()?.value;
3230
if (!session) {
33-
_debug?.appendLine('Metabob: Session is undefined in Discard Suggestion');
3431
vscode.window.showErrorMessage(CONSTANTS.discardCommandErrorMessage);
3532
return;
3633
}
@@ -66,10 +63,7 @@ export function activateDiscardCommand(context: vscode.ExtensionContext): void {
6663
}
6764

6865
if (isUserOnProblemFile) {
69-
Utils.decorateCurrentEditorWithHighlights(
70-
results,
71-
documentMetaData.editor,
72-
);
66+
Utils.decorateCurrentEditorWithHighlights(results, documentMetaData.editor);
7367
}
7468

7569
extensionEventEmitter.fire({
@@ -90,7 +84,7 @@ export function activateDiscardCommand(context: vscode.ExtensionContext): void {
9084
getExtensionEventEmitter().fire({
9185
type: 'CURRENT_PROJECT',
9286
data: {
93-
name: currentWorkSpaceFolder
87+
name: currentWorkSpaceFolder,
9488
},
9589
});
9690

ext-src/commands/FixSuggestion.ts

+1-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import * as vscode from 'vscode';
22
import CONSTANTS from '../constants';
33
import { Problem } from '../types';
44
import { CurrentQuestion } from '../state';
5-
import debugChannel from '../debug';
65
import { getExtensionEventEmitter } from '../events';
76

87
export type FixSuggestionCommandHandler = {
@@ -36,9 +35,7 @@ export function activateFixSuggestionCommand(context: vscode.ExtensionContext):
3635
isReset: false,
3736
},
3837
});
39-
} catch (error) {
40-
debugChannel.appendLine(`Metabob: Error while fixing suggestion ${JSON.stringify(error)}`);
41-
}
38+
} catch (error) { }
4239

4340
vscode.commands.executeCommand('recommendation-panel-webview.focus');
4441
};

ext-src/commands/FocusRecommendation.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import * as vscode from 'vscode';
22
import { Problem } from '../types';
3-
import _debug from '../debug';
43
import { getExtensionEventEmitter } from '../events';
54
import { Analyze } from '../state';
65

@@ -11,7 +10,6 @@ export function activateFocusRecommendCommand(context: vscode.ExtensionContext):
1110
const commandHandler = async (args: FocusCommandHandler) => {
1211
const { id, vuln, path } = args;
1312
const key = `${path}@@${id}`;
14-
_debug?.appendLine(`Current Detection Set for ${path} with Problem ${id} `);
1513

1614
const analyzeState = new Analyze(context);
1715
const analyzeStateValue = analyzeState.get()?.value;
@@ -23,7 +21,7 @@ export function activateFocusRecommendCommand(context: vscode.ExtensionContext):
2321
copyProblems[key].isEndorsed = false;
2422
copyProblems[key].isViewed = true;
2523

26-
await analyzeState.set({ ...copyProblems })
24+
await analyzeState.set({ ...copyProblems });
2725
getExtensionEventEmitter().fire({
2826
type: 'FIX_SUGGESTION',
2927
data: {

0 commit comments

Comments
 (0)