1+ // Create mock vscode module before importing anything
2+ const createMockUri = ( scheme : string , path : string ) => ( {
3+ scheme,
4+ authority : '' ,
5+ path,
6+ query : '' ,
7+ fragment : '' ,
8+ fsPath : path ,
9+ with : jest . fn ( ) ,
10+ toString : ( ) => path ,
11+ toJSON : ( ) => ( {
12+ scheme,
13+ authority : '' ,
14+ path,
15+ query : '' ,
16+ fragment : ''
17+ } )
18+ } )
19+
20+ const mockExecuteCommand = jest . fn ( )
21+ const mockOpenExternal = jest . fn ( )
22+ const mockShowErrorMessage = jest . fn ( )
23+
24+ const mockVscode = {
25+ workspace : {
26+ workspaceFolders : [ {
27+ uri : { fsPath : "/test/workspace" }
28+ } ]
29+ } ,
30+ window : {
31+ showErrorMessage : mockShowErrorMessage ,
32+ showInformationMessage : jest . fn ( ) ,
33+ showWarningMessage : jest . fn ( ) ,
34+ createTextEditorDecorationType : jest . fn ( ) ,
35+ createOutputChannel : jest . fn ( ) ,
36+ createWebviewPanel : jest . fn ( ) ,
37+ activeTextEditor : undefined
38+ } ,
39+ commands : {
40+ executeCommand : mockExecuteCommand
41+ } ,
42+ env : {
43+ openExternal : mockOpenExternal
44+ } ,
45+ Uri : {
46+ parse : jest . fn ( ( url : string ) => createMockUri ( 'https' , url ) ) ,
47+ file : jest . fn ( ( path : string ) => createMockUri ( 'file' , path ) )
48+ } ,
49+ Position : jest . fn ( ) ,
50+ Range : jest . fn ( ) ,
51+ TextEdit : jest . fn ( ) ,
52+ WorkspaceEdit : jest . fn ( ) ,
53+ DiagnosticSeverity : {
54+ Error : 0 ,
55+ Warning : 1 ,
56+ Information : 2 ,
57+ Hint : 3
58+ }
59+ }
60+
61+ // Mock modules
62+ jest . mock ( 'vscode' , ( ) => mockVscode )
63+ jest . mock ( "../../../services/browser/UrlContentFetcher" )
64+ jest . mock ( "../../../utils/git" )
65+
66+ // Now import the modules that use the mocks
67+ import { parseMentions , openMention } from "../index"
68+ import { UrlContentFetcher } from "../../../services/browser/UrlContentFetcher"
69+ import * as git from "../../../utils/git"
70+
71+ describe ( "mentions" , ( ) => {
72+ const mockCwd = "/test/workspace"
73+ let mockUrlContentFetcher : UrlContentFetcher
74+
75+ beforeEach ( ( ) => {
76+ jest . clearAllMocks ( )
77+
78+ // Create a mock instance with just the methods we need
79+ mockUrlContentFetcher = {
80+ launchBrowser : jest . fn ( ) . mockResolvedValue ( undefined ) ,
81+ closeBrowser : jest . fn ( ) . mockResolvedValue ( undefined ) ,
82+ urlToMarkdown : jest . fn ( ) . mockResolvedValue ( "" ) ,
83+ } as unknown as UrlContentFetcher
84+ } )
85+
86+ describe ( "parseMentions" , ( ) => {
87+ it ( "should parse git commit mentions" , async ( ) => {
88+ const commitHash = "abc1234"
89+ const commitInfo = `abc1234 Fix bug in parser
90+
91+ Author: John Doe
92+ Date: Mon Jan 5 23:50:06 2025 -0500
93+
94+ Detailed commit message with multiple lines
95+ - Fixed parsing issue
96+ - Added tests`
97+
98+ jest . mocked ( git . getCommitInfo ) . mockResolvedValue ( commitInfo )
99+
100+ const result = await parseMentions (
101+ `Check out this commit @${ commitHash } ` ,
102+ mockCwd ,
103+ mockUrlContentFetcher
104+ )
105+
106+ expect ( result ) . toContain ( `'${ commitHash } ' (see below for commit info)` )
107+ expect ( result ) . toContain ( `<git_commit hash="${ commitHash } ">` )
108+ expect ( result ) . toContain ( commitInfo )
109+ } )
110+
111+ it ( "should handle errors fetching git info" , async ( ) => {
112+ const commitHash = "abc1234"
113+ const errorMessage = "Failed to get commit info"
114+
115+ jest . mocked ( git . getCommitInfo ) . mockRejectedValue ( new Error ( errorMessage ) )
116+
117+ const result = await parseMentions (
118+ `Check out this commit @${ commitHash } ` ,
119+ mockCwd ,
120+ mockUrlContentFetcher
121+ )
122+
123+ expect ( result ) . toContain ( `'${ commitHash } ' (see below for commit info)` )
124+ expect ( result ) . toContain ( `<git_commit hash="${ commitHash } ">` )
125+ expect ( result ) . toContain ( `Error fetching commit info: ${ errorMessage } ` )
126+ } )
127+ } )
128+
129+ describe ( "openMention" , ( ) => {
130+ it ( "should handle file paths and problems" , async ( ) => {
131+ await openMention ( "/path/to/file" )
132+ expect ( mockExecuteCommand ) . not . toHaveBeenCalled ( )
133+ expect ( mockOpenExternal ) . not . toHaveBeenCalled ( )
134+ expect ( mockShowErrorMessage ) . toHaveBeenCalledWith ( "Could not open file!" )
135+
136+ await openMention ( "problems" )
137+ expect ( mockExecuteCommand ) . toHaveBeenCalledWith ( "workbench.actions.view.problems" )
138+ } )
139+
140+ it ( "should handle URLs" , async ( ) => {
141+ const url = "https://example.com"
142+ await openMention ( url )
143+ const mockUri = mockVscode . Uri . parse ( url )
144+ expect ( mockOpenExternal ) . toHaveBeenCalled ( )
145+ const calledArg = mockOpenExternal . mock . calls [ 0 ] [ 0 ]
146+ expect ( calledArg ) . toEqual ( expect . objectContaining ( {
147+ scheme : mockUri . scheme ,
148+ authority : mockUri . authority ,
149+ path : mockUri . path ,
150+ query : mockUri . query ,
151+ fragment : mockUri . fragment
152+ } ) )
153+ } )
154+ } )
155+ } )
0 commit comments