rob-revisited/ide/ui/js/path-links.test.mjs

77 lines
6.2 KiB
JavaScript

// node --test ide/ui/js/path-links.test.mjs (or: docker run --rm -v $PWD:/w -w /w node:22-alpine node --test ide/ui/js/path-links.test.mjs)
import test from 'node:test';
import assert from 'node:assert/strict';
import { refsInLine, refAt, pathTypedAt, rankEntries, formatSize, fitIn, importsOf, kindBefore } from './path-links.js';
const refs = (lang, line) => refsInLine(lang, line).map((r) => [line.slice(r.start, r.end), r.kind]);
test('Twig: every tag and function that names a template is one whole link', () => {
assert.deepEqual(refs('twig', "{% extends 'base.html.twig' %}"), [['base.html.twig', 'template']]);
assert.deepEqual(refs('twig', '{% include "partials/_nav.html.twig" with {a: 1} %}'), [['partials/_nav.html.twig', 'template']]);
assert.deepEqual(refs('twig', "{%- embed 'card.html.twig' -%}"), [['card.html.twig', 'template']]);
assert.deepEqual(refs('twig', "{% import 'macros.html.twig' as m %}"), [['macros.html.twig', 'template']]);
assert.deepEqual(refs('twig', "{% from 'macros.html.twig' import field %}"), [['macros.html.twig', 'template']]);
assert.deepEqual(refs('twig', "{% use 'blocks.html.twig' %}"), [['blocks.html.twig', 'template']]);
assert.deepEqual(refs('twig', "{% form_theme form 'form/theme.html.twig' %}"), [['form/theme.html.twig', 'template']]);
assert.deepEqual(refs('twig', "{{ include('@App/x.html.twig') }} {{ source('svg/logo.svg') }}"), [['@App/x.html.twig', 'template'], ['svg/logo.svg', 'template']]);
assert.deepEqual(refs('twig', '<img src="{{ asset(\'images/logo.png\') }}" alt="">'), [['images/logo.png', 'asset']]);
assert.deepEqual(refs('twig', '<link href="/css/app.css" rel="stylesheet">'), [['/css/app.css', 'relative']]);
assert.deepEqual(refs('twig', "{{ 'not a path'|trans }} {% set x = 'base.html.twig' %}"), []);
});
test('HTML, CSS, JavaScript and Markdown paths', () => {
assert.deepEqual(refs('html', '<img src="img/a b.png"> <a href="#top">x</a> <a href="mailto:x@y">m</a> <script src="./app.js"></script>'), [['./app.js', 'relative']]);
assert.deepEqual(refs('html', '<img src="https://example.com/x.png?v=1"> <a href="https://example.com/">site</a>'), [['https://example.com/x.png?v=1', 'url']]);
assert.deepEqual(refs('css', "@import 'base.css'; .a { background: url(../img/bg.png) } .b { background: url(\"x.svg\") }"),
[['../img/bg.png', 'relative'], ['x.svg', 'relative'], ['base.css', 'relative']]);
assert.deepEqual(refs('javascript', "import './bootstrap.js'; import { Controller } from '@hotwired/stimulus';"), [['./bootstrap.js', 'module'], ['@hotwired/stimulus', 'module']]);
assert.deepEqual(refs('javascript', "const m = await import('./lazy.js'); const x = require('lodash'); new URL('./w.js', import.meta.url); console.log('hi.js')"),
[['./lazy.js', 'module'], ['lodash', 'module'], ['./w.js', 'relative']]);
assert.deepEqual(refs('markdown', 'See [the guide](docs/guide.md "Guide") and ![shot](images/a.png) or [web](https://example.com).'),
[['docs/guide.md', 'relative'], ['images/a.png', 'relative']]);
assert.deepEqual(refs('php', "return $this->render('home/index.html.twig', ['logo' => 'public/logo.png', 'name' => 'Ada']);"),
[['home/index.html.twig', 'template'], ['public/logo.png', 'any']]);
});
test('the link under the cursor', () => {
const line = "{% extends 'base.html.twig' %}";
assert.equal(refAt('twig', line, 13).ref, 'base.html.twig');
assert.equal(refAt('twig', line, 2), null);
});
test('the path being typed and its kind', () => {
assert.deepEqual(pathTypedAt('javascript', "import './"), { typed: './', kind: 'module' });
assert.deepEqual(pathTypedAt('javascript', "import { Controller } from '@hot"), { typed: '@hot', kind: 'module' });
assert.deepEqual(pathTypedAt('javascript', "import x from \"../lib/"), { typed: '../lib/', kind: 'module' });
assert.deepEqual(pathTypedAt('javascript', "require('lod"), { typed: 'lod', kind: 'module' });
assert.equal(pathTypedAt('javascript', "console.log('./"), null);
assert.equal(pathTypedAt('javascript', "import './a.js'; const s = '"), null);
assert.deepEqual(pathTypedAt('css', '.a { background: url(../img/'), { typed: '../img/', kind: 'relative' });
assert.deepEqual(pathTypedAt('css', "@import 'par"), { typed: 'par', kind: 'relative' });
assert.deepEqual(pathTypedAt('html', '<img src="images/'), { typed: 'images/', kind: 'relative' });
assert.deepEqual(pathTypedAt('twig', "{% extends 'ba"), { typed: 'ba', kind: 'template' });
assert.deepEqual(pathTypedAt('twig', "{{ include('partials/"), { typed: 'partials/', kind: 'template' });
assert.deepEqual(pathTypedAt('twig', "<img src=\"{{ asset('images/"), { typed: 'images/', kind: 'asset' });
assert.deepEqual(pathTypedAt('markdown', 'See ![x](images/'), { typed: 'images/', kind: 'relative' });
assert.deepEqual(pathTypedAt('php', "$this->render('home/"), { typed: 'home/', kind: 'template' });
assert.deepEqual(pathTypedAt('php', "$path = 'public/img"), { typed: 'public/img', kind: 'any' });
assert.equal(pathTypedAt('php', "$name = 'Ada"), null, 'a plain PHP string is not a path');
assert.equal(pathTypedAt('yaml', "file: './"), null, 'YAML has its own');
assert.equal(kindBefore('twig', '{% set x = '), null);
});
test('ranking: folders, then packages, then files; a prefix before a match inside', () => {
const items = [{ label: 'app.js', kind: 'file' }, { label: 'styles/', kind: 'folder' }, { label: 'bootstrap.js', kind: 'file' }, { label: 'images/', kind: 'folder' }, { label: 'map.js', kind: 'file' }];
assert.deepEqual(rankEntries(items, '').map((i) => i.label), ['images/', 'styles/', 'app.js', 'bootstrap.js', 'map.js']);
assert.deepEqual(rankEntries(items, 'ap').map((i) => i.label), ['app.js', 'bootstrap.js', 'map.js']);
assert.deepEqual(rankEntries(items, 'sty').map((i) => i.label), ['styles/']);
});
test('sizes, thumbnails and imports', () => {
assert.equal(formatSize(512), '512 B');
assert.equal(formatSize(12595), '12.3 KB');
assert.equal(formatSize(3 * 1048576), '3.0 MB');
assert.deepEqual(fitIn(1920, 1080, 240), { width: 240, height: 135 });
assert.deepEqual(fitIn(16, 16, 240), { width: 16, height: 16 });
assert.deepEqual(importsOf("import a from './a.js';\nimport('b');\nrequire(\"c\");\nimport 'a';"), ['./a.js', 'a', 'b', 'c']);
});