77 lines
4.1 KiB
JavaScript
77 lines
4.1 KiB
JavaScript
// The controller panel's model (controller-panel-model.js). Run: node --test ide/ui/js/controller-panel-model.test.mjs
|
|
import test from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import { looksLikeController, placeholders, sampleValue, fillUrl, joinUrl, requestFor, securityOf, summary, pathOfAction, routeNameOfAction, createRefresher } from './controller-panel-model.js';
|
|
|
|
const route = { name: 'blog_edit', action: 'edit', url: '/blog/{slug}/{id}', path: '/blog/{slug}/{id<\\d+>}', methods: ['GET', 'POST'],
|
|
requirements: { id: '\\d+', kind: 'a|b' }, defaults: { slug: 'hello' }, params: [{ name: 'in', kind: 'payload' }],
|
|
grants: [{ attribute: 'ROLE_USER', level: 'class', line: 5 }, { attribute: 'POST_EDIT', subject: 'post', level: 'method', line: 20 }],
|
|
access: { index: 1, path: '^/blog', roles: ['ROLE_USER'], expression: '' } };
|
|
|
|
test('only PHP files that may be controllers are sent to the PHP side', () => {
|
|
assert.equal(looksLikeController('src/Controller/PostController.php', ''), true);
|
|
assert.equal(looksLikeController('src/Web/Home.php', "#[Route('/')]"), true);
|
|
assert.equal(looksLikeController('src/Web/Home.php', 'class Home extends AbstractController {}'), true);
|
|
assert.equal(looksLikeController('src/Service/Mailer.php', 'class Mailer {}'), false);
|
|
assert.equal(looksLikeController('templates/index.html.twig', 'Controller'), false);
|
|
});
|
|
|
|
test('placeholders and their sample values', () => {
|
|
assert.deepEqual(placeholders('/a/{x}/{y}/{x}'), ['x', 'y']);
|
|
assert.equal(sampleValue(route, 'slug'), 'hello'); // the default
|
|
assert.equal(sampleValue(route, 'id'), '1'); // a digits requirement
|
|
assert.equal(sampleValue(route, 'kind'), 'a'); // the first of an alternation
|
|
assert.equal(sampleValue({}, 'userId'), '1');
|
|
assert.equal(sampleValue({}, 'slug'), 'example');
|
|
});
|
|
|
|
test('the URL is filled with the given values, else the samples, and joined to the site', () => {
|
|
assert.equal(fillUrl(route, { slug: 'a b', id: 7 }), '/blog/a%20b/7');
|
|
assert.equal(fillUrl(route), '/blog/hello/1');
|
|
assert.equal(joinUrl('http://localhost:8000/', '/blog/1'), 'http://localhost:8000/blog/1');
|
|
});
|
|
|
|
test('the HTTP Client request uses {{baseUrl}}, the first method and a JSON body for a payload', () => {
|
|
const r = requestFor(route, { slug: 'x', id: 3 });
|
|
assert.equal(r.url, '{{baseUrl}}/blog/x/3');
|
|
assert.equal(r.method, 'GET');
|
|
assert.equal(r.name, 'GET blog_edit');
|
|
assert.equal(r.headers[0].name, 'Content-Type');
|
|
assert.equal(requestFor({ ...route, params: [], methods: [] }).method, 'GET');
|
|
assert.equal(requestFor({ ...route, params: [] }).body, '');
|
|
});
|
|
|
|
test('the security line gathers the grants and the access_control rule', () => {
|
|
const s = securityOf(route);
|
|
assert.deepEqual(s.grants.map((g) => g.text), ['ROLE_USER', 'POST_EDIT ← post']);
|
|
assert.equal(s.rule.index, 1);
|
|
assert.equal(s.open, false);
|
|
assert.equal(securityOf({ grants: [] }).open, true);
|
|
});
|
|
|
|
test('the header summary and the names proposed for a new action', () => {
|
|
assert.deepEqual(summary({ routes: [1, 2], prefix: { path: '/blog', name: 'blog_' } }), { routes: 2, prefix: '/blog', namePrefix: 'blog_' });
|
|
assert.deepEqual(summary(null), { routes: 0, prefix: '', namePrefix: '' });
|
|
assert.equal(pathOfAction('showPost'), '/show-post');
|
|
assert.equal(routeNameOfAction('showPost', 'blog_'), 'blog_show_post');
|
|
});
|
|
|
|
test('an unchanged text is not asked again and a late answer is dropped', async () => {
|
|
let calls = 0;
|
|
const resolvers = [];
|
|
const r = createRefresher((path, text) => { calls++; return new Promise((res) => resolvers.push(() => res({ controller: true, text }))); });
|
|
const first = r.refresh('a.php', 'one');
|
|
resolvers[0]();
|
|
assert.equal((await first).info.text, 'one');
|
|
const again = await r.refresh('a.php', 'one');
|
|
assert.equal(again.cached, true);
|
|
assert.equal(calls, 1);
|
|
const slow = r.refresh('a.php', 'two');
|
|
const fast = r.refresh('a.php', 'three');
|
|
resolvers[2]();
|
|
assert.equal((await fast).info.text, 'three');
|
|
resolvers[1]();
|
|
const late = await slow;
|
|
assert.equal(late.stale, true);
|
|
assert.equal(r.last.text, 'three');
|
|
});
|