|
| 1 | +/* Tests for formatQn() in codegraph/web/static/app.js |
| 2 | + * |
| 3 | + * app.js is a classic browser script. Extract the `formatQn` function source |
| 4 | + * (along with its `esc` dependency) and evaluate them in a Node vm sandbox. |
| 5 | + * |
| 6 | + * Run with: node --test tests/test_app_format.js |
| 7 | + */ |
| 8 | +'use strict'; |
| 9 | + |
| 10 | +const test = require('node:test'); |
| 11 | +const assert = require('node:assert/strict'); |
| 12 | +const fs = require('node:fs'); |
| 13 | +const path = require('node:path'); |
| 14 | +const vm = require('node:vm'); |
| 15 | + |
| 16 | +const APP_JS = path.join( |
| 17 | + __dirname, '..', 'codegraph', 'web', 'static', 'app.js' |
| 18 | +); |
| 19 | + |
| 20 | +function loadFormatQn() { |
| 21 | + const source = fs.readFileSync(APP_JS, 'utf-8'); |
| 22 | + const escMatch = source.match(/function esc\(s\)\s*\{[\s\S]*?\n\}/); |
| 23 | + if (!escMatch) throw new Error('esc() not found in app.js'); |
| 24 | + const fmtMatch = source.match(/function formatQn\(qn,\s*opts\)\s*\{[\s\S]*?\n\}/); |
| 25 | + if (!fmtMatch) throw new Error('formatQn() not found in app.js'); |
| 26 | + const sandbox = {}; |
| 27 | + vm.createContext(sandbox); |
| 28 | + vm.runInContext( |
| 29 | + `${escMatch[0]}\n${fmtMatch[0]}\nthis.formatQn = formatQn;`, |
| 30 | + sandbox, |
| 31 | + ); |
| 32 | + return sandbox.formatQn; |
| 33 | +} |
| 34 | + |
| 35 | +const formatQn = loadFormatQn(); |
| 36 | + |
| 37 | +test('formatQn renders single-segment as leaf', () => { |
| 38 | + const out = formatQn('foo', { maxParts: 3 }); |
| 39 | + assert.match(out, /qn-key/); |
| 40 | + assert.match(out, />foo</); |
| 41 | +}); |
| 42 | + |
| 43 | +test('formatQn dims parent path', () => { |
| 44 | + const out = formatQn('a.b.c', { maxParts: 3 }); |
| 45 | + assert.match(out, /qn-dim/); |
| 46 | + assert.match(out, /a\.b\./); |
| 47 | + assert.match(out, />c</); |
| 48 | +}); |
| 49 | + |
| 50 | +test('formatQn truncates long paths with ellipsis', () => { |
| 51 | + const out = formatQn('a.b.c.d.e', { maxParts: 2 }); |
| 52 | + assert.match(out, /…/); |
| 53 | + assert.match(out, />e</); |
| 54 | +}); |
| 55 | + |
| 56 | +test('formatQn escapes HTML in qn segments', () => { |
| 57 | + const out = formatQn('a.<b>', { maxParts: 3 }); |
| 58 | + assert.match(out, /<b>/); |
| 59 | + assert.doesNotMatch(out, /<b>/); |
| 60 | +}); |
| 61 | + |
| 62 | +test('formatQn handles null gracefully', () => { |
| 63 | + const out = formatQn(null, { maxParts: 3 }); |
| 64 | + // null is coalesced to '' before splitting; one empty leaf, no head. |
| 65 | + assert.match(out, /qn-key/); |
| 66 | + assert.doesNotMatch(out, /null/); |
| 67 | +}); |
| 68 | + |
| 69 | +test('formatQn defaults maxParts when opts omitted', () => { |
| 70 | + const out = formatQn('a.b.c.d.e'); |
| 71 | + // default maxParts = 3 -> visible head = last 2 segments |
| 72 | + assert.match(out, /…/); |
| 73 | +}); |
0 commit comments