| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
 | require "./test_helper.js"
extend(global, require "../../lib/utils.js")
extend(global, require "../../background_scripts/completion.js")
extend global, require "./test_chrome_stubs.js"
global.document =
  createElement: -> {}
context "bookmark completer",
  setup ->
    @bookmark3 = { title: "bookmark3", url: "bookmark3.com" }
    @bookmark2 = { title: "bookmark2", url: "bookmark2.com" }
    @bookmark1 = { title: "bookmark1", url: "bookmark1.com", children: [@bookmark2] }
    global.chrome.bookmarks =
      getTree: (callback) => callback([@bookmark1])
    @completer = new BookmarkCompleter()
  should "flatten a list of bookmarks with inorder traversal", ->
    result = @completer.traverseBookmarks([@bookmark1, @bookmark3])
    assert.arrayEqual [@bookmark1, @bookmark2, @bookmark3], result
  should "return matching bookmarks when searching", ->
    @completer.refresh()
    results = filterCompleter(@completer, ["mark2"])
    assert.arrayEqual [@bookmark2.url], results.map (suggestion) -> suggestion.url
  should "return *no* matching bookmarks when there is no match", ->
    @completer.refresh()
    results = filterCompleter(@completer, ["does-not-match"])
    assert.arrayEqual [], results.map (suggestion) -> suggestion.url
  should "construct bookmark paths correctly", ->
    @completer.refresh()
    results = filterCompleter(@completer, ["mark2"])
    assert.equal "/bookmark1/bookmark2", @bookmark2.pathAndTitle
  should "return matching bookmark *titles* when searching *without* the folder separator character", ->
    @completer.refresh()
    results = filterCompleter(@completer, ["mark2"])
    assert.arrayEqual ["bookmark2"], results.map (suggestion) -> suggestion.title
  should "return matching bookmark *paths* when searching with the folder separator character", ->
    @completer.refresh()
    results = filterCompleter(@completer, ["/bookmark1", "mark2"])
    assert.arrayEqual ["/bookmark1/bookmark2"], results.map (suggestion) -> suggestion.title
context "HistoryCache",
  context "binary search",
    setup ->
      @compare = (a, b) -> a - b
    should "find elements to the left of the middle", ->
      assert.equal 0, HistoryCache.binarySearch(3, [3, 5, 8], @compare)
    should "find elements to the right of the middle", ->
      assert.equal 2, HistoryCache.binarySearch(8, [3, 5, 8], @compare)
    context "unfound elements",
      should "return 0 if it should be the head of the list", ->
        assert.equal 0, HistoryCache.binarySearch(1, [3, 5, 8], @compare)
      should "return length - 1 if it should be at the end of the list", ->
        assert.equal 0, HistoryCache.binarySearch(3, [3, 5, 8], @compare)
      should "return one passed end of array (so: array.length) if greater than last element in array", ->
        assert.equal 3, HistoryCache.binarySearch(10, [3, 5, 8], @compare)
      should "found return the position if it's between two elements", ->
        assert.equal 1, HistoryCache.binarySearch(4, [3, 5, 8], @compare)
        assert.equal 2, HistoryCache.binarySearch(7, [3, 5, 8], @compare)
  context "fetchHistory",
    setup ->
      @history1 = { url: "b.com", lastVisitTime: 5 }
      @history2 = { url: "a.com", lastVisitTime: 10 }
      history = [@history1, @history2]
      @onVisitedListener = null
      @onVisitRemovedListener = null
      global.chrome.history =
        search: (options, callback) -> callback(history)
        onVisited: { addListener: (@onVisitedListener) => }
        onVisitRemoved: { addListener: (@onVisitRemovedListener) => }
      HistoryCache.reset()
    should "store visits sorted by url ascending", ->
      HistoryCache.use (@results) =>
      assert.arrayEqual [@history2, @history1], @results
    should "add new visits to the history", ->
      HistoryCache.use () ->
      newSite = { url: "ab.com" }
      @onVisitedListener(newSite)
      HistoryCache.use (@results) =>
      assert.arrayEqual [@history2, newSite, @history1], @results
    should "replace new visits in the history", ->
      HistoryCache.use (@results) =>
      assert.arrayEqual [@history2, @history1], @results
      newSite = { url: "a.com", lastVisitTime: 15 }
      @onVisitedListener(newSite)
      HistoryCache.use (@results) =>
      assert.arrayEqual [newSite, @history1], @results
    should "(not) remove page from the history, when page is not in history (it should be a no-op)", ->
      HistoryCache.use (@results) =>
      assert.arrayEqual [@history2, @history1], @results
      toRemove = { urls: [ "x.com" ], allHistory: false }
      @onVisitRemovedListener(toRemove)
      HistoryCache.use (@results) =>
      assert.arrayEqual [@history2, @history1], @results
    should "remove pages from the history", ->
      HistoryCache.use (@results) =>
      assert.arrayEqual [@history2, @history1], @results
      toRemove = { urls: [ "a.com" ], allHistory: false }
      @onVisitRemovedListener(toRemove)
      HistoryCache.use (@results) =>
      assert.arrayEqual [@history1], @results
    should "remove all pages from the history", ->
      HistoryCache.use (@results) =>
      assert.arrayEqual [@history2, @history1], @results
      toRemove = { allHistory: true }
      @onVisitRemovedListener(toRemove)
      HistoryCache.use (@results) =>
      assert.arrayEqual [], @results
context "history completer",
  setup ->
    @history1 = { title: "history1", url: "history1.com", lastVisitTime: hours(1) }
    @history2 = { title: "history2", url: "history2.com", lastVisitTime: hours(5) }
    global.chrome.history =
      search: (options, callback) => callback([@history1, @history2])
      onVisited: { addListener: -> }
      onVisitRemoved: { addListener: -> }
    @completer = new HistoryCompleter()
  should "return matching history entries when searching", ->
    assert.arrayEqual [@history1.url], filterCompleter(@completer, ["story1"]).map (entry) -> entry.url
  should "rank recent results higher than nonrecent results", ->
    stub(Date, "now", returns(hours(24)))
    results = filterCompleter(@completer, ["hist"])
    results.forEach (result) -> result.computeRelevancy()
    results.sort (a, b) -> b.relevancy - a.relevancy
    assert.arrayEqual [@history2.url, @history1.url], results.map (result) -> result.url
context "domain completer",
  setup ->
    @history1 = { title: "history1", url: "http://history1.com", lastVisitTime: hours(1) }
    @history2 = { title: "history2", url: "http://history2.com", lastVisitTime: hours(1) }
    stub(HistoryCache, "use", (onComplete) => onComplete([@history1, @history2]))
    global.chrome.history =
      onVisited: { addListener: -> }
      onVisitRemoved: { addListener: -> }
    stub(Date, "now", returns(hours(24)))
    @completer = new DomainCompleter()
  should "return only a single matching domain", ->
    results = filterCompleter(@completer, ["story"])
    assert.arrayEqual ["http://history1.com"], results.map (result) -> result.url
  should "pick domains which are more recent", ->
    # These domains are the same except for their last visited time.
    assert.equal "http://history1.com", filterCompleter(@completer, ["story"])[0].url
    @history2.lastVisitTime = hours(3)
    assert.equal "http://history2.com", filterCompleter(@completer, ["story"])[0].url
  should "returns no results when there's more than one query term, because clearly it's not a domain", ->
    assert.arrayEqual [], filterCompleter(@completer, ["his", "tory"])
context "domain completer (removing entries)",
  setup ->
    @history1 = { title: "history1", url: "http://history1.com", lastVisitTime: hours(2) }
    @history2 = { title: "history2", url: "http://history2.com", lastVisitTime: hours(1) }
    @history3 = { title: "history2something", url: "http://history2.com/something", lastVisitTime: hours(0) }
    stub(HistoryCache, "use", (onComplete) => onComplete([@history1, @history2, @history3]))
    @onVisitedListener = null
    @onVisitRemovedListener = null
    global.chrome.history =
      onVisited: { addListener: (@onVisitedListener) => }
      onVisitRemoved: { addListener: (@onVisitRemovedListener) => }
    stub(Date, "now", returns(hours(24)))
    @completer = new DomainCompleter()
    # Force installation of listeners.
    filterCompleter(@completer, ["story"])
  should "remove 1 entry for domain with reference count of 1", ->
    @onVisitRemovedListener { allHistory: false, urls: [@history1.url] }
    assert.equal "http://history2.com", filterCompleter(@completer, ["story"])[0].url
    assert.equal 0, filterCompleter(@completer, ["story1"]).length
  should "remove 2 entries for domain with reference count of 2", ->
    @onVisitRemovedListener { allHistory: false, urls: [@history2.url] }
    assert.equal "http://history2.com", filterCompleter(@completer, ["story2"])[0].url
    @onVisitRemovedListener { allHistory: false, urls: [@history3.url] }
    assert.equal 0, filterCompleter(@completer, ["story2"]).length
    assert.equal "http://history1.com", filterCompleter(@completer, ["story"])[0].url
  should "remove 3 (all) matching domain entries", ->
    @onVisitRemovedListener { allHistory: false, urls: [@history2.url] }
    @onVisitRemovedListener { allHistory: false, urls: [@history1.url] }
    @onVisitRemovedListener { allHistory: false, urls: [@history3.url] }
    assert.equal 0, filterCompleter(@completer, ["story"]).length
  should "remove 3 (all) matching domain entries, and do it all at once", ->
    @onVisitRemovedListener { allHistory: false, urls: [ @history2.url, @history1.url, @history3.url ] }
    assert.equal 0, filterCompleter(@completer, ["story"]).length
  should "remove *all* domain entries", ->
    @onVisitRemovedListener { allHistory: true }
    assert.equal 0, filterCompleter(@completer, ["story"]).length
context "tab completer",
  setup ->
    @tabs = [
      { url: "tab1.com", title: "tab1", id: 1 }
      { url: "tab2.com", title: "tab2", id: 2 }]
    chrome.tabs = { query: (args, onComplete) => onComplete(@tabs) }
    @completer = new TabCompleter()
  should "return matching tabs", ->
    results = filterCompleter(@completer, ["tab2"])
    assert.arrayEqual ["tab2.com"], results.map (tab) -> tab.url
    assert.arrayEqual [2], results.map (tab) -> tab.tabId
context "search engines",
  setup ->
    searchEngines = "foo: bar?q=%s\n# comment\nbaz: qux?q=%s"
    Settings.set 'searchEngines', searchEngines
    @completer = new SearchEngineCompleter()
    # note, I couldn't just call @completer.refresh() here as I couldn't set root.Settings without errors
    # workaround is below, would be good for someone that understands the testing system better than me to improve
    @completer.searchEngines = Settings.getSearchEngines()
  should "return search engine suggestion", ->
    results = filterCompleter(@completer, ["foo", "hello"])
    assert.arrayEqual ["bar?q=hello"], results.map (result) -> result.url
    assert.arrayEqual ["foo: hello"], results.map (result) -> result.title
context "suggestions",
  should "escape html in page titles", ->
    suggestion = new Suggestion(["queryterm"], "tab", "url", "title <span>", returns(1))
    assert.isTrue suggestion.generateHtml().indexOf("title <span>") >= 0
  should "highlight query words", ->
    suggestion = new Suggestion(["ninj", "words"], "tab", "url", "ninjawords", returns(1))
    expected = "<span class='vomnibarMatch'>ninj</span>a<span class='vomnibarMatch'>words</span>"
    assert.isTrue suggestion.generateHtml().indexOf(expected) >= 0
  should "highlight query words correctly when whey they overlap", ->
    suggestion = new Suggestion(["ninj", "jaword"], "tab", "url", "ninjawords", returns(1))
    expected = "<span class='vomnibarMatch'>ninjaword</span>s"
    assert.isTrue suggestion.generateHtml().indexOf(expected) >= 0
  should "shorten urls", ->
    suggestion = new Suggestion(["queryterm"], "tab", "http://ninjawords.com", "ninjawords", returns(1))
    assert.equal -1, suggestion.generateHtml().indexOf("http://ninjawords.com")
context "RankingUtils.wordRelevancy",
  should "score higher in shorter URLs", ->
    highScore = RankingUtils.wordRelevancy(["stack"], "http://stackoverflow.com/short",  "a-title")
    lowScore  = RankingUtils.wordRelevancy(["stack"], "http://stackoverflow.com/longer", "a-title")
    assert.isTrue highScore > lowScore
  should "score higher in shorter titles", ->
    highScore = RankingUtils.wordRelevancy(["coffee"], "a-url", "Coffeescript")
    lowScore  = RankingUtils.wordRelevancy(["coffee"], "a-url", "Coffeescript rocks")
    assert.isTrue highScore > lowScore
  should "score higher for matching the start of a word (in a URL)", ->
    lowScore  = RankingUtils.wordRelevancy(["stack"], "http://Xstackoverflow.com/same", "a-title")
    highScore = RankingUtils.wordRelevancy(["stack"], "http://stackoverflowX.com/same", "a-title")
    assert.isTrue highScore > lowScore
  should "score higher for matching the start of a word (in a title)", ->
    lowScore  = RankingUtils.wordRelevancy(["te"], "a-url", "Dist racted")
    highScore = RankingUtils.wordRelevancy(["te"], "a-url", "Distrac ted")
    assert.isTrue highScore > lowScore
  should "score higher for matching a whole word (in a URL)", ->
    lowScore  = RankingUtils.wordRelevancy(["com"], "http://stackoverflow.comX/same", "a-title")
    highScore = RankingUtils.wordRelevancy(["com"], "http://stackoverflowX.com/same", "a-title")
    assert.isTrue highScore > lowScore
  should "score higher for matching a whole word (in a title)", ->
    lowScore  = RankingUtils.wordRelevancy(["com"], "a-url", "abc comX")
    highScore = RankingUtils.wordRelevancy(["com"], "a-url", "abcX com")
    assert.isTrue highScore > lowScore
  # # TODO: (smblott)
  # #       Word relevancy should take into account the number of matches (it doesn't currently).
  # should "score higher for multiple matches (in a URL)", ->
  #   lowScore  = RankingUtils.wordRelevancy(["stack"], "http://stackoverflow.com/Xxxxxx", "a-title")
  #   highScore = RankingUtils.wordRelevancy(["stack"], "http://stackoverflow.com/Xstack", "a-title")
  #   assert.isTrue highScore > lowScore
  # should "score higher for multiple matches (in a title)", ->
  #   lowScore  = RankingUtils.wordRelevancy(["bbc"], "http://stackoverflow.com/same", "BBC Radio 4 (XBCr4)")
  #   highScore = RankingUtils.wordRelevancy(["bbc"], "http://stackoverflow.com/same", "BBC Radio 4 (BBCr4)")
  #   assert.isTrue highScore > lowScore
context "Suggestion.pushMatchingRanges",
  should "extract ranges matching term (simple case, two matches)", ->
    ranges = []
    [ one, two, three ] = [ "one", "two", "three" ]
    suggestion = new Suggestion([], "", "", "", returns(1))
    suggestion.pushMatchingRanges("#{one}#{two}#{three}#{two}#{one}", two, ranges)
    assert.equal 2, Utils.zip([ ranges, [ [3,6], [11,14] ] ]).filter((pair) -> pair[0][0] == pair[1][0] and pair[0][1] == pair[1][1]).length
  should "extract ranges matching term (two matches, one at start of string)", ->
    ranges = []
    [ one, two, three ] = [ "one", "two", "three" ]
    suggestion = new Suggestion([], "", "", "", returns(1))
    suggestion.pushMatchingRanges("#{two}#{three}#{two}#{one}", two, ranges)
    assert.equal 2, Utils.zip([ ranges, [ [0,3], [8,11] ] ]).filter((pair) -> pair[0][0] == pair[1][0] and pair[0][1] == pair[1][1]).length
  should "extract ranges matching term (two matches, one at end of string)", ->
    ranges = []
    [ one, two, three ] = [ "one", "two", "three" ]
    suggestion = new Suggestion([], "", "", "", returns(1))
    suggestion.pushMatchingRanges("#{one}#{two}#{three}#{two}", two, ranges)
    assert.equal 2, Utils.zip([ ranges, [ [3,6], [11,14] ] ]).filter((pair) -> pair[0][0] == pair[1][0] and pair[0][1] == pair[1][1]).length
  should "extract ranges matching term (no matches)", ->
    ranges = []
    [ one, two, three ] = [ "one", "two", "three" ]
    suggestion = new Suggestion([], "", "", "", returns(1))
    suggestion.pushMatchingRanges("#{one}#{two}#{three}#{two}#{one}", "does-not-match", ranges)
    assert.equal 0, ranges.length
context "RankingUtils",
  should "do a case insensitive match", ->
    assert.isTrue RankingUtils.matches(["ari"], "maRio")
  should "do a case insensitive match on full term", ->
    assert.isTrue RankingUtils.matches(["mario"], "MARio")
  should "do a case insensitive match on several terms", ->
    assert.isTrue RankingUtils.matches(["ari"], "DOES_NOT_MATCH", "DOES_NOT_MATCH_EITHER", "MARio")
  should "do a smartcase match (positive)", ->
    assert.isTrue RankingUtils.matches(["Mar"], "Mario")
  should "do a smartcase match (negative)", ->
    assert.isFalse RankingUtils.matches(["Mar"], "mario")
  should "do a match with regexp meta-characters (positive)", ->
    assert.isTrue RankingUtils.matches(["ma.io"], "ma.io")
  should "do a match with regexp meta-characters (negative)", ->
    assert.isFalse RankingUtils.matches(["ma.io"], "mario")
  should "do a smartcase match on full term", ->
    assert.isTrue RankingUtils.matches(["Mario"], "Mario")
    assert.isFalse RankingUtils.matches(["Mario"], "mario")
  should "do case insensitive word relevancy (matching)", ->
    assert.isTrue RankingUtils.wordRelevancy(["ari"], "MARIO", "MARio") > 0.0
  should "do case insensitive word relevancy (not matching)", ->
    assert.isTrue RankingUtils.wordRelevancy(["DOES_NOT_MATCH"], "MARIO", "MARio") == 0.0
  should "every query term must match at least one thing (matching)", ->
    assert.isTrue RankingUtils.matches(["cat", "dog"], "catapult", "hound dog")
  should "every query term must match at least one thing (not matching)", ->
    assert.isTrue not RankingUtils.matches(["cat", "dog", "wolf"], "catapult", "hound dog")
context "RegexpCache",
  should "RegexpCache is in fact caching (positive case)", ->
    assert.isTrue RegexpCache.get("this") is RegexpCache.get("this")
  should "RegexpCache is in fact caching (negative case)", ->
    assert.isTrue RegexpCache.get("this") isnt RegexpCache.get("that")
  should "RegexpCache prefix/suffix wrapping is working (positive case)", ->
    assert.isTrue RegexpCache.get("this", "(", ")") is RegexpCache.get("this", "(", ")")
  should "RegexpCache prefix/suffix wrapping is working (negative case)", ->
    assert.isTrue RegexpCache.get("this", "(", ")") isnt RegexpCache.get("this")
  should "search for a string", ->
    assert.isTrue "hound dog".search(RegexpCache.get("dog")) == 6
  should "search for a string which isn't there", ->
    assert.isTrue "hound dog".search(RegexpCache.get("cat")) == -1
  should "search for a string with a prefix/suffix (positive case)", ->
    assert.isTrue "hound dog".search(RegexpCache.get("dog", "\\b", "\\b")) == 6
  should "search for a string with a prefix/suffix (negative case)", ->
    assert.isTrue "hound dog".search(RegexpCache.get("do", "\\b", "\\b")) == -1
context "TabRecency",
  setup ->
    @tabRecency = new TabRecency()
    @tabRecency.add 3
    @tabRecency.add 2
    @tabRecency.add 9
    @tabRecency.add 1
    @tabRecency.remove 9
    @tabRecency.add 4
  should "have entries for active tabs", ->
    assert.isTrue @tabRecency.cache[1]
    assert.isTrue @tabRecency.cache[2]
    assert.isTrue @tabRecency.cache[3]
    assert.isTrue @tabRecency.cache[4]
  should "not have entries for removed tabs", ->
    assert.isFalse @tabRecency.cache[9]
  should "give a high score to the most recent tab", ->
    assert.isTrue @tabRecency.recencyScore(4) < @tabRecency.recencyScore 1
    assert.isTrue @tabRecency.recencyScore(3) < @tabRecency.recencyScore 1
    assert.isTrue @tabRecency.recencyScore(2) < @tabRecency.recencyScore 1
  should "give a low score to the current tab", ->
    assert.isTrue @tabRecency.recencyScore(1) > @tabRecency.recencyScore 4
    assert.isTrue @tabRecency.recencyScore(2) > @tabRecency.recencyScore 4
    assert.isTrue @tabRecency.recencyScore(3) > @tabRecency.recencyScore 4
  should "rank tabs by recency", ->
    assert.isTrue @tabRecency.recencyScore(3) < @tabRecency.recencyScore 2
    assert.isTrue @tabRecency.recencyScore(2) < @tabRecency.recencyScore 1
    @tabRecency.add 3
    @tabRecency.add 4 # Making 3 the most recent tab which isn't the current tab.
    assert.isTrue @tabRecency.recencyScore(1) < @tabRecency.recencyScore 3
    assert.isTrue @tabRecency.recencyScore(2) < @tabRecency.recencyScore 3
    assert.isTrue @tabRecency.recencyScore(4) < @tabRecency.recencyScore 3
    assert.isTrue @tabRecency.recencyScore(4) < @tabRecency.recencyScore 1
    assert.isTrue @tabRecency.recencyScore(4) < @tabRecency.recencyScore 2
# A convenience wrapper around completer.filter() so it can be called synchronously in tests.
filterCompleter = (completer, queryTerms) ->
  results = []
  completer.filter(queryTerms, (completionResults) -> results = completionResults)
  results
hours = (n) -> 1000 * 60 * 60 * n
 |