74 lines
4.4 KiB
JavaScript
74 lines
4.4 KiB
JavaScript
// The .env editor's helpers. Run: node --test ide/ui/js/dotenv-model.test.mjs
|
|
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { isDotenvPath, isSecretName, isSecret, parseDsn, buildDsn, maskValue, valueKind, toggleBool, diffRange, contextAt, namesIn, targetFiles, dsnPreset } from './dotenv-model.js';
|
|
|
|
test('dotenv files are recognised by their names', () => {
|
|
for (const p of ['.env', 'sub/.env.local', '.env.dev', '.env.prod.local', '.env.dist', '.env.example', 'docker/app.env']) assert.ok(isDotenvPath(p), p);
|
|
for (const p of ['.env.local.php', 'env.php', 'README.md', '.envrc']) assert.ok(!isDotenvPath(p), p);
|
|
});
|
|
|
|
test('DSNs are taken apart and built back to the same text', () => {
|
|
const url = 'postgresql://app:[email protected]:5432/app?serverVersion=16&charset=utf8';
|
|
const d = parseDsn(url);
|
|
assert.deepEqual([d.scheme, d.user, d.password, d.host, d.port, d.path], ['postgresql', 'app', '!ChangeMe!', '127.0.0.1', '5432', '/app']);
|
|
assert.deepEqual(d.query, [['serverVersion', '16'], ['charset', 'utf8']]);
|
|
assert.equal(buildDsn(d), url);
|
|
for (const u of ['sqlite:///%kernel.project_dir%/var/data_%kernel.environment%.db', 'doctrine://default?auto_setup=0', 'null://null', 'in-memory://',
|
|
'amqp://guest:guest@localhost:5672/%2f/messages', 'smtp://[email protected]']) assert.equal(buildDsn(parseDsn(u)), u);
|
|
assert.equal(parseDsn('localhost:5432'), null);
|
|
const built = buildDsn({ scheme: 'mysql', user: 'root', password: 'p@ss:w/rd', host: 'db', port: '3306', path: 'app', query: [['serverVersion', '8.0.32']] });
|
|
assert.equal(built, 'mysql://root:p%40ss%3Aw%2Frd@db:3306/app?serverVersion=8.0.32');
|
|
assert.equal(parseDsn(built).password, 'p@ss:w/rd');
|
|
});
|
|
|
|
test('secrets are recognised and masked', () => {
|
|
for (const n of ['APP_SECRET', 'DB_PASSWORD', 'GITHUB_TOKEN', 'API_KEY', 'JWT_PASSPHRASE']) assert.ok(isSecretName(n), n);
|
|
for (const n of ['APP_ENV', 'JWT_PUBLIC_KEY', 'MONKEY', 'KEYBOARD']) assert.ok(!isSecretName(n), n);
|
|
assert.ok(isSecret('DATABASE_URL', 'mysql://a:pw@db/x'));
|
|
assert.ok(!isSecret('DATABASE_URL', 'mysql://a:${DB_PASS}@db/x'));
|
|
assert.equal(maskValue('mysql://a:pw@db/x'), 'mysql://a:••••@db/x');
|
|
assert.equal(maskValue('abc'), '••••••••');
|
|
assert.equal(maskValue(''), '');
|
|
});
|
|
|
|
test('the kind of a value chooses its editor', () => {
|
|
assert.equal(valueKind('DATABASE_URL', ''), 'dsn');
|
|
assert.equal(valueKind('MAILER_DSN', 'smtp://localhost'), 'dsn');
|
|
assert.equal(valueKind('APP_DEBUG', '1'), 'bool');
|
|
assert.equal(valueKind('FEATURE', 'true'), 'bool');
|
|
assert.equal(valueKind('PORT', '8080'), 'number');
|
|
assert.equal(valueKind('KEY', 'a\nb'), 'multiline');
|
|
assert.equal(valueKind('APP_ENV', 'dev'), 'text');
|
|
assert.ok(dsnPreset('SENTRY_DSN'));
|
|
assert.equal(toggleBool('true'), 'false');
|
|
assert.equal(toggleBool('0'), '1');
|
|
assert.equal(toggleBool('ON'), 'OFF');
|
|
});
|
|
|
|
test('the smallest edit between two texts', () => {
|
|
assert.equal(diffRange('a', 'a'), null);
|
|
assert.deepEqual(diffRange('A=1\nB=2\n', 'A=1\nB=22\n'), { start: 7, end: 7, text: '2' });
|
|
assert.deepEqual(diffRange('A=1\nB=2\nC=3\n', 'A=1\nC=3\n'), { start: 4, end: 8, text: '' });
|
|
const before = 'X=1\nY=2\n';
|
|
const after = 'X=1\nY=2\nZ=3\n';
|
|
const d = diffRange(before, after);
|
|
assert.equal(before.slice(0, d.start) + d.text + before.slice(d.end), after);
|
|
});
|
|
|
|
test('completion knows where the cursor is', () => {
|
|
assert.deepEqual(contextAt('APP_', 5), { kind: 'name', prefix: 'APP_' });
|
|
assert.deepEqual(contextAt('export DA', 10), { kind: 'name', prefix: 'DA' });
|
|
assert.deepEqual(contextAt('URL=http://${HO', 16), { kind: 'ref', prefix: 'HO', braced: true });
|
|
assert.deepEqual(contextAt("A='${X", 7), { kind: 'value', name: 'A', prefix: "'${X" });
|
|
assert.deepEqual(contextAt('MAILER_DSN=sm', 14), { kind: 'value', name: 'MAILER_DSN', prefix: 'sm' });
|
|
assert.equal(contextAt('# A', 4), null);
|
|
assert.deepEqual(namesIn('A=1\n# B=2\nexport C=3\nA=4\n'), ['A', 'C']);
|
|
});
|
|
|
|
test('a variable can move to the usual files of its folder', () => {
|
|
const files = [{ name: '.env' }, { name: '.env.dev' }, { name: '.env.example' }];
|
|
const t = targetFiles(files, 'symfony', ['dev', 'prod'], '.env');
|
|
assert.deepEqual(t.map((f) => f.name), ['.env.local', '.env.dev', '.env.dev.local', '.env.prod', '.env.prod.local']);
|
|
assert.deepEqual(t.filter((f) => f.exists).map((f) => f.name), ['.env.dev']);
|
|
});
|