Add a search bar: case-insensitive, steps through matches with Tab/Shift+Tab (Refs #27)
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
29
test/search.test.js
Normal file
29
test/search.test.js
Normal file
@@ -0,0 +1,29 @@
|
||||
import { test } from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import { searchRanges, stepSearchIndex } from '../src/search.js';
|
||||
|
||||
test('searchRanges is case-insensitive and finds every place', () => {
|
||||
assert.deepEqual(searchRanges('Bitwarden, BITWARDEN and bitwarden.', 'bitwarden'),
|
||||
[{ start: 0, end: 9 }, { start: 11, end: 20 }, { start: 25, end: 34 }]);
|
||||
assert.deepEqual(searchRanges('Santander', 'BitWarden'), []);
|
||||
});
|
||||
|
||||
test('searchRanges treats the query as plain text, ignores outer spaces, and blank matches nothing', () => {
|
||||
assert.deepEqual(searchRanges('a.b (c) a.b', 'a.b'), [{ start: 0, end: 3 }, { start: 8, end: 11 }]);
|
||||
assert.deepEqual(searchRanges('axb', 'a.b'), []); // "." is not a wildcard
|
||||
assert.deepEqual(searchRanges('(c) [d] $5 ^ \\', '(c)'), [{ start: 0, end: 3 }]);
|
||||
assert.equal(searchRanges('x', '[').length, 0); // a stray bracket must not throw
|
||||
assert.deepEqual(searchRanges('go bitwarden go', ' bitwarden '), [{ start: 3, end: 12 }]);
|
||||
assert.deepEqual(searchRanges('anything', ''), []);
|
||||
assert.deepEqual(searchRanges('anything', ' '), []);
|
||||
assert.deepEqual(searchRanges('aaaa', 'aa'), [{ start: 0, end: 2 }, { start: 2, end: 4 }]); // no overlap
|
||||
});
|
||||
|
||||
test('stepSearchIndex wraps both ways and handles no results', () => {
|
||||
assert.equal(stepSearchIndex(0, 1, 3), 1);
|
||||
assert.equal(stepSearchIndex(2, 1, 3), 0);
|
||||
assert.equal(stepSearchIndex(0, -1, 3), 2);
|
||||
assert.equal(stepSearchIndex(0, 1, 1), 0);
|
||||
assert.equal(stepSearchIndex(-1, 1, 3), 0); // nothing current yet: forward goes to the first
|
||||
assert.equal(stepSearchIndex(0, 1, 0), -1);
|
||||
});
|
||||
Reference in New Issue
Block a user