@@ -22,11 +22,17 @@ describe('SearchService', () => {
2222 } ) ;
2323
2424 beforeEach ( ( ) => {
25+ global . base_url = '/' ;
2526 searchService = new SearchService ( 'search-service' , null ) ;
27+ searchService . render_container = {
28+ append : jest . fn ( ) ,
29+ html : jest . fn ( ) ,
30+ } ;
2631 } ) ;
2732
2833 afterEach ( async ( ) => {
2934 searchService = null ;
35+ delete global . base_url ;
3036 } ) ;
3137
3238 it ( 'Access data from mock-index.json' , ( ) => {
@@ -111,4 +117,93 @@ describe('SearchService', () => {
111117 } ) ;
112118
113119 } ) ;
120+
121+ test ( 'Keeps only exact ATT&CK ID matches and references, with the object first' , async ( ) => {
122+ const documents = {
123+ 1 : {
124+ id : 1 ,
125+ title : 'TA577, Group G1037' ,
126+ path : '/groups/G1037/index.html' ,
127+ content : 'A group with no reference to the queried technique.' ,
128+ attackId : 'G1037' ,
129+ } ,
130+ 2 : {
131+ id : 2 ,
132+ title : 'Ingress Tool Transfer, Technique T1105 - Enterprise' ,
133+ path : '/techniques/T1105/index.html' ,
134+ content : 'The T1105 technique.' ,
135+ attackId : 'T1105' ,
136+ } ,
137+ 3 : {
138+ id : 3 ,
139+ title : 'A valid reference' ,
140+ path : '/resources/reference/index.html' ,
141+ content : 'This page references T1105.' ,
142+ } ,
143+ } ;
144+ searchService . attackIndex . search = jest . fn ( ) . mockResolvedValue ( [ { field : 'title' , result : [ 1 , 2 , 3 ] } ] ) ;
145+ searchService . resolveSearchResults = jest . fn ( async positions => positions . map ( position => documents [ position ] ) ) ;
146+
147+ await searchService . query ( 't1105' ) ;
148+
149+ expect ( searchService . allSearchResults . map ( result => result . id ) ) . toEqual ( [ 2 , 3 ] ) ;
150+ } ) ;
151+
152+ test ( 'Treats a four-digit query as an exact ATT&CK ID suffix search' , async ( ) => {
153+ const documents = {
154+ 1 : {
155+ id : 1 ,
156+ title : 'TA577, Group G1037' ,
157+ path : '/groups/G1037/index.html' ,
158+ content : 'A group with no reference to the queried technique.' ,
159+ attackId : 'G1037' ,
160+ } ,
161+ 2 : {
162+ id : 2 ,
163+ title : 'Data from Local System, Technique T1005 - Enterprise' ,
164+ path : '/techniques/T1005/index.html' ,
165+ content : 'The T1005 technique.' ,
166+ attackId : 'T1005' ,
167+ } ,
168+ 3 : {
169+ id : 3 ,
170+ title : 'Matching software, Software S1005' ,
171+ path : '/software/S1005/index.html' ,
172+ content : 'The S1005 software.' ,
173+ attackId : 'S1005' ,
174+ } ,
175+ 4 : {
176+ id : 4 ,
177+ title : 'A valid reference' ,
178+ path : '/resources/reference/index.html' ,
179+ content : 'This page references T1005.' ,
180+ } ,
181+ 5 : {
182+ id : 5 ,
183+ title : 'Data from Local System: Archive Collected Data, Sub-technique T1005.001' ,
184+ path : '/techniques/T1005/001/index.html' ,
185+ content : 'The T1005.001 sub-technique.' ,
186+ attackId : 'T1005.001' ,
187+ } ,
188+ } ;
189+ searchService . attackIndex . search = jest . fn ( ) . mockResolvedValue ( [ { field : 'title' , result : [ 1 , 3 , 4 , 2 , 5 ] } ] ) ;
190+ searchService . resolveSearchResults = jest . fn ( async positions => positions . map ( position => documents [ position ] ) ) ;
191+
192+ await searchService . query ( '1005' ) ;
193+
194+ expect ( searchService . allSearchResults . map ( result => result . id ) ) . toEqual ( [ 2 , 5 , 3 , 4 ] ) ;
195+ } ) ;
196+
197+ test ( 'Preserves result ordering for non-ID queries' , async ( ) => {
198+ const documents = {
199+ 1 : { id : 1 , title : 'First result' , path : '/resources/faq/index.html' , content : 'Resources' } ,
200+ 2 : { id : 2 , title : 'Second result' , path : '/resources/attackcon/index.html' , content : 'Resources' } ,
201+ } ;
202+ searchService . attackIndex . search = jest . fn ( ) . mockResolvedValue ( [ { field : 'title' , result : [ 1 , 2 ] } ] ) ;
203+ searchService . resolveSearchResults = jest . fn ( async positions => positions . map ( position => documents [ position ] ) ) ;
204+
205+ await searchService . query ( 'Resources' ) ;
206+
207+ expect ( searchService . allSearchResults . map ( result => result . id ) ) . toEqual ( [ 1 , 2 ] ) ;
208+ } ) ;
114209} ) ;
0 commit comments