rob-revisited/ide/ui/js/page-preview-model.test.mjs

108 lines
6.7 KiB
JavaScript

import test from 'node:test';
import assert from 'node:assert/strict';
import { STATIC, groupRoutes, routeLabel, pickRoute, inputsOf, missingValues, suggestSql, firstColumn, loginLike, followRedirects, statusOf,
absoluteOf, inlineCss, choiceOf } from './page-preview-model.js';
const routes = [
{ name: 'app_home', path: '/', methods: [], renders: true, get: true, params: [] },
{ name: 'app_post', path: '/post', methods: ['POST'], renders: false, get: false, params: [] },
{ name: 'app_book', path: '/book/{id}', methods: ['GET'], renders: false, get: true,
params: [{ name: 'id', requirement: '\\d+', default: null, optional: false, entity: 'App\\Entity\\Book', table: 'book', column: 'id' }] },
{ name: 'app_blog', path: '/blog/{page}', methods: ['GET', 'HEAD'], renders: false, get: true, params: [{ name: 'page', requirement: '', default: '1', optional: true }] },
];
test('the selector groups: the routes of this template, the GET ones, the others', () => {
const g = groupRoutes(routes);
assert.deepEqual(g.renders.map((r) => r.name), ['app_home']);
assert.deepEqual(g.get.map((r) => r.name), ['app_book', 'app_blog']);
assert.deepEqual(g.other.map((r) => r.name), ['app_post']);
assert.deepEqual(groupRoutes(null), { renders: [], get: [], other: [] });
assert.equal(routeLabel(routes[0]), 'app_home · ANY /');
assert.equal(routeLabel(routes[3]), 'app_blog · GET|HEAD /blog/{page}');
});
test('the first choice: the remembered route, else the one that renders the template, else static', () => {
assert.equal(pickRoute(routes, { route: 'app_book' }), 'app_book');
assert.equal(pickRoute(routes, { route: 'gone' }), 'app_home');
assert.equal(pickRoute(routes, null), 'app_home');
assert.equal(pickRoute(routes, { static: true, route: 'app_book' }), STATIC);
assert.equal(pickRoute(routes.filter((r) => !r.renders), null), STATIC);
assert.equal(pickRoute([], null), STATIC);
assert.deepEqual(choiceOf('app_book', { id: '3' }, true), { route: 'app_book', values: { id: '3' }, session: true, static: false });
assert.equal(choiceOf(STATIC, {}, false).static, true);
});
test('the parameters become inputs with what was typed last time', () => {
const inputs = inputsOf(routes[2], { id: '12' });
assert.equal(inputs.length, 1);
assert.deepEqual([inputs[0].name, inputs[0].value, inputs[0].required, inputs[0].placeholder, inputs[0].table], ['id', '12', true, '\\d+', 'book']);
assert.equal(inputsOf(routes[3])[0].placeholder, '1');
assert.equal(inputsOf(routes[3])[0].required, false);
assert.deepEqual(inputsOf(routes[0]), []);
assert.deepEqual(missingValues(inputsOf(routes[2])), ['id']);
assert.deepEqual(missingValues(inputsOf(routes[2], { id: ' ' })), ['id']);
assert.deepEqual(missingValues(inputsOf(routes[2], { id: '5' })), []);
assert.deepEqual(missingValues(inputsOf(routes[3])), []);
});
test('ids are suggested from the entity\'s table, never from a name that is not an identifier', () => {
assert.equal(suggestSql(inputsOf(routes[2])[0]), 'SELECT id FROM book ORDER BY 1 LIMIT 8');
assert.equal(suggestSql({ table: 'x; DROP TABLE y', column: 'id' }), '');
assert.equal(suggestSql({ table: '', column: 'id' }), '');
assert.deepEqual(firstColumn({ results: [{ rows: [[1, 'a'], [2, 'b'], [null, 'c']] }] }), ['1', '2']);
assert.deepEqual(firstColumn({ rows: [{ id: 7 }] }), ['7']);
assert.deepEqual(firstColumn(null), []);
});
test('sign-in pages are recognised', () => {
assert.equal(loginLike('http://localhost:8100/login'), true);
assert.equal(loginLike('/fr/connexion?next=/admin'), true);
assert.equal(loginLike('/login_check'), true);
assert.equal(loginLike('/blog/login-tips'), false);
assert.equal(loginLike('/book/3'), false);
});
test('redirects are followed from the site, a login among them is reported', async () => {
const asked = [];
const pages = { 'http://s/admin': { status: 302, url: 'http://s/admin', location: '/login' }, 'http://s/login': { status: 200, url: 'http://s/login', body: '<form>' } };
const r = await followRedirects(async () => pages['http://s/admin'], async (u) => { asked.push(u); return pages[u]; });
assert.deepEqual(asked, ['http://s/login']);
assert.equal(r.response.status, 200);
assert.equal(r.login, true);
assert.deepEqual(r.chain, [{ status: 302, url: 'http://s/admin', to: 'http://s/login' }]);
const loop = await followRedirects(async () => ({ status: 302, url: 'http://s/a', location: '/a' }), async () => ({ status: 302, url: 'http://s/a', location: '/a' }), 3);
assert.equal(loop.loop, true);
assert.equal(loop.chain.length, 3);
const missing = await followRedirects(async () => ({ missing: ['id'] }), async () => { throw new Error('no'); });
assert.deepEqual(statusOf(missing), { kind: 'missing', text: 'id' });
});
test('the status of a fetch: a 500 is an error to read, not to hide', () => {
const s = statusOf({ response: { status: 500, reason: 'Internal Server Error', url: 'http://s/', time: 40 }, chain: [], login: false });
assert.equal(s.kind, 'error');
assert.equal(s.text, '500 Internal Server Error');
assert.equal(statusOf({ response: { status: 200, reason: 'OK' }, chain: [{}] }).hops, 1);
assert.equal(statusOf(null).kind, 'none');
});
test('only addresses of the site are inlined', () => {
assert.equal(absoluteOf('http://s:8100/blog/', '../build/app.css?v=2#x', 'http://s:8100'), 'http://s:8100/build/app.css?v=2');
assert.equal(absoluteOf('http://s:8100/', 'https://cdn.example.com/a.css', 'http://s:8100'), '');
assert.equal(absoluteOf('http://s:8100/', 'data:image/png;base64,AA', 'http://s:8100'), '');
assert.equal(absoluteOf('http://s:8100/', '#top', 'http://s:8100'), '');
assert.equal(absoluteOf('http://s:8100/', '/a.png'), 'http://s:8100/a.png');
});
test('a stylesheet of the page: @import inlined, pictures and fonts as data URLs, other sites left alone', async () => {
const files = { 'http://s/css/base.css': 'body { background: url(../img/bg.png) } @import "extra.css";', 'http://s/css/extra.css': 'p { color: red }' };
const io = { text: async (u) => files[u] ?? null, dataUrl: async (u) => (u.endsWith('.png') ? 'data:image/png;base64,AAAA' : null) };
const out = await inlineCss('@import url("/css/base.css") screen; h1 { background: url("https://cdn.example.com/x.png") } i { background: url(/img/gone.gif) }', 'http://s/', io, 'http://s');
assert.match(out, /@media screen \{/);
assert.match(out, /url\("data:image\/png;base64,AAAA"\)/);
assert.match(out, /p \{ color: red \}/);
assert.match(out, /https:\/\/cdn\.example\.com\/x\.png/);
assert.match(out, /url\(\/img\/gone\.gif\)/);
// an @import cycle ends
const cyc = { text: async () => '@import "/a.css"; a { top: 0 }', dataUrl: async () => null };
assert.match(await inlineCss('@import "/a.css";', 'http://s/', cyc, 'http://s'), /a \{ top: 0 \}/);
});