|
| 1 | +const { test } = require('node:test'); |
| 2 | +const assert = require('node:assert/strict'); |
| 3 | + |
| 4 | +// stub browser globals required by editor.js at module load time |
| 5 | +global.addEventListener = () => {}; |
| 6 | +global.id = () => null; |
| 7 | + |
| 8 | +const { fzfMatchToken, fzfScore, fzfFilter, buildDefinitionParams } = require('../static/js/editor.js'); |
| 9 | + |
| 10 | +test('fzfMatchToken - exact match', () => { |
| 11 | + const r = fzfMatchToken('foobar', 'foo'); |
| 12 | + assert.ok(r !== null); |
| 13 | + assert.ok(r.score > 0); |
| 14 | +}); |
| 15 | + |
| 16 | +test('fzfMatchToken - no match', () => { |
| 17 | + assert.equal(fzfMatchToken('foobar', 'xyz'), null); |
| 18 | +}); |
| 19 | + |
| 20 | +test('fzfMatchToken - consecutive chars score higher', () => { |
| 21 | + const consecutive = fzfMatchToken('foobar', 'foo'); |
| 22 | + const scattered = fzfMatchToken('fxoxo', 'foo'); |
| 23 | + assert.ok(consecutive.score > scattered.score); |
| 24 | +}); |
| 25 | + |
| 26 | +test('fzfScore - single token match', () => { |
| 27 | + assert.ok(fzfScore('src/main.c', 'main') > 0); |
| 28 | +}); |
| 29 | + |
| 30 | +test('fzfScore - multi token AND', () => { |
| 31 | + assert.ok(fzfScore('src/main.c', 'src main') > 0); |
| 32 | +}); |
| 33 | + |
| 34 | +test('fzfScore - token not found returns -1', () => { |
| 35 | + assert.equal(fzfScore('src/main.c', 'xyz'), -1); |
| 36 | +}); |
| 37 | + |
| 38 | +test('fzfScore - empty query returns 0', () => { |
| 39 | + assert.equal(fzfScore('src/main.c', ''), 0); |
| 40 | +}); |
| 41 | + |
| 42 | +test('fzfFilter - returns top N results', () => { |
| 43 | + const files = ['a.c', 'b.c', 'c.c', 'd.c', 'e.c']; |
| 44 | + const result = fzfFilter(files, '', 3); |
| 45 | + assert.equal(result.length, 3); |
| 46 | +}); |
| 47 | + |
| 48 | +test('fzfFilter - filters and sorts by score', () => { |
| 49 | + const files = ['openssl/bio.c', 'openssl/ssl.c', 'curl/easy.c']; |
| 50 | + const result = fzfFilter(files, 'ssl', 10); |
| 51 | + assert.ok(result.every(f => f.includes('ssl'))); |
| 52 | +}); |
| 53 | + |
| 54 | +test('fzfFilter - no match returns empty', () => { |
| 55 | + const result = fzfFilter(['a.c', 'b.c'], 'xyz', 10); |
| 56 | + assert.equal(result.length, 0); |
| 57 | +}); |
| 58 | + |
| 59 | +test('buildDefinitionParams - basic', () => { |
| 60 | + const p = buildDefinitionParams('foo', '', '', false); |
| 61 | + assert.ok(p.get('q').includes('foo')); |
| 62 | + assert.equal(p.get('regex'), '1'); |
| 63 | + assert.equal(p.get('case'), '0'); |
| 64 | +}); |
| 65 | + |
| 66 | +test('buildDefinitionParams - case sensitive', () => { |
| 67 | + const p = buildDefinitionParams('Foo', '', '', true); |
| 68 | + assert.equal(p.get('case'), '1'); |
| 69 | +}); |
| 70 | + |
| 71 | +test('buildDefinitionParams - with dir and glob', () => { |
| 72 | + const p = buildDefinitionParams('bar', 'src', '*.h', false); |
| 73 | + assert.equal(p.get('dir'), 'src'); |
| 74 | + assert.equal(p.get('glob'), '*.h'); |
| 75 | +}); |
| 76 | + |
| 77 | +test('buildDefinitionParams - escapes regex special chars', () => { |
| 78 | + const p = buildDefinitionParams('foo.bar', '', '', false); |
| 79 | + assert.ok(p.get('q').includes('foo\\.bar')); |
| 80 | +}); |
0 commit comments