rob-revisited/ide/ui/js/yaml-lang.test.mjs

42 lines
2.4 KiB
JavaScript

// The pure helpers of yaml-lang.js. Run: node --test ide/ui/js/yaml-lang.test.mjs
import test from 'node:test';
import assert from 'node:assert/strict';
import { isComposeFile, completesYaml, imageAt, withImage, YAML_LANGUAGE } from './yaml-lang.js';
test('compose files are known by their name', () => {
for (const ok of ['compose.yaml', 'docker/docker-compose.yml', 'compose.override.yaml', 'a/compose.prod.yml', '.jumphp/compose.yaml']) assert.ok(isComposeFile(ok), ok);
for (const no of ['config/services.yaml', 'composer.json', 'compose.json', 'my-compose.yaml']) assert.ok(!isComposeFile(no), no);
assert.ok(completesYaml('config/services_test.yaml'));
assert.ok(completesYaml('docker-compose.yml'));
assert.ok(!completesYaml('config/packages/doctrine.yaml'));
});
test('the image under the cursor', () => {
assert.deepEqual(imageAt(' image: nginx:1.27-alpine', 14), { name: 'nginx', tag: '1.27-alpine', start: 12, end: 29 });
assert.equal(imageAt(' image: nginx', 5), null); // on the key
assert.equal(imageAt(' - image: "Bitnami/Redis"', 15).name, 'bitnami/redis');
assert.equal(imageAt(' image: ghcr.io/owner/app:1', 15), null); // another registry
assert.equal(imageAt(' image: localhost:5000/app', 15), null);
assert.equal(imageAt(' image: ${IMAGE:-app}', 15), null);
assert.equal(imageAt(' build: .', 12), null);
assert.equal(imageAt(' image: redis', 16).tag, 'latest');
});
test('an image put on a line', () => {
assert.equal(withImage(' image: nginx # web', 'nginx:1.27'), ' image: nginx:1.27 # web');
assert.equal(withImage(" image: 'x'", 'redis:7'), ' image: redis:7');
assert.equal(withImage(' ', 'redis:7'), ' image: redis:7');
assert.equal(withImage(' ports: []', 'redis:7'), null);
});
test('the grammar compiles as regular expressions', () => {
for (const [state, rules] of Object.entries(YAML_LANGUAGE.tokenizer)) {
for (const rule of rules) if (Array.isArray(rule)) assert.ok(rule[0] instanceof RegExp, state);
}
const key = YAML_LANGUAGE.tokenizer.line.find((r) => Array.isArray(r) && Array.isArray(r[1]))[0];
assert.equal(key.exec('image: nginx:1.27')[1], 'image');
assert.equal(key.exec('"quoted key": 1')[1], '"quoted key"');
assert.equal(key.exec('App\\Controller\\: ~')[1], 'App\\Controller\\');
assert.equal(key.exec('http://example.com'), null);
assert.equal(key.exec('nginx:1.27'), null);
});