rob-revisited/ide/ui/js/api-install.test.mjs

63 lines
3.1 KiB
JavaScript

// The install flows with a fake runner (no DOM). Run: node --test ide/ui/js/api-install.test.mjs
import test from 'node:test';
import assert from 'node:assert/strict';
import { installPackage, undefinedClass, missingClasses, requireLine, validPackage } from './api-install-model.js';
const fake = (over = {}) => {
const log = [];
const deps = {
plan: async (what) => (log.push(['plan', what]), { installed: false, package: 'api', constraint: '', command: 'composer require api' }),
confirm: async (plan) => (log.push(['confirm', plan.command]), true),
run: async (p) => (log.push(['run', p]), { code: 0 }),
enable: async () => log.push(['enable']),
...over,
};
return { deps, log };
};
test('an installed API Platform is left alone', async () => {
const { deps, log } = fake({ plan: async () => ({ installed: true, package: 'api', command: '' }) });
assert.equal((await installPackage(deps)).status, 'present');
assert.deepEqual(log, []);
});
test('plan, confirm, composer require, then the capabilities are refreshed', async () => {
const { deps, log } = fake();
const r = await installPackage(deps);
assert.equal(r.status, 'installed');
assert.deepEqual(log.map((l) => l[0]), ['plan', 'confirm', 'run', 'enable']);
assert.deepEqual(log[2][1], { action: 'require', package: 'api', constraint: '', dev: false });
});
test('a declined confirmation runs nothing', async () => {
const { deps, log } = fake({ confirm: async () => false });
assert.equal((await installPackage(deps)).status, 'declined');
assert.ok(!log.some((l) => l[0] === 'run'));
});
test('a failed composer run is reported and the window does not appear', async () => {
const { deps, log } = fake({ run: async () => ({ code: 2 }) });
assert.equal((await installPackage(deps)).status, 'failed');
assert.ok(!log.some((l) => l[0] === 'enable'));
});
test('the JWT bundle needs no window refresh', async () => {
const { deps, log } = fake({ plan: async () => ({ installed: false, package: 'lexik/jwt-authentication-bundle', constraint: '', command: 'x' }) });
assert.equal((await installPackage(deps, 'jwt')).status, 'installed');
assert.ok(!log.some((l) => l[0] === 'enable'));
});
test('the undefined classes of the markers of a quick fix request', () => {
assert.equal(undefinedClass("Undefined class 'ApiPlatform\\Metadata\\ApiResource'"), 'ApiPlatform\\Metadata\\ApiResource');
assert.equal(undefinedClass("Undefined function 'foo'"), null);
const found = missingClasses([{ message: "Undefined class 'A\\B'" }, { message: "Undefined class 'A\\B'" }, { message: 'Syntax error' }]);
assert.equal(found.length, 1);
assert.equal(found[0].name, 'A\\B');
});
test('the require line and the package name check', () => {
assert.equal(requireLine({ package: 'doctrine/orm', constraint: '^3.0', dev: true }), 'composer require doctrine/orm:^3.0 --dev');
assert.equal(requireLine({ package: 'api' }), 'composer require api');
assert.ok(validPackage('symfony/orm-pack') && validPackage('api'));
assert.ok(!validPackage('--dev') && !validPackage('no spaces/x') && !validPackage(''));
});