Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
124 changes: 121 additions & 3 deletions .github/scripts/docker_saved_places_test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
// node .github/scripts/docker_saved_places_test.js — a deleted folder or removed container still named in userprefs.cfg no longer
// moves a saved folder on the Docker page (npm ci in this folder first)
// node .github/scripts/docker_saved_places_test.js — where the Docker page puts saved folders: stale userprefs.cfg entries do not move
// them, and after sync_order.php (asked for once per load) regroups the file, the page draws its rows in its order (npm ci here first)
'use strict';
const { drawDocker, check, done } = require('./preview_pages_harness.js');
const fs = require('fs');
const path = require('path');
const { drawDocker, phpServer, check, done } = require('./preview_pages_harness.js');

const same = (a, b) => JSON.stringify(a) === JSON.stringify(b);
const folder = (name, containers = [], regex = '') => ({ name, containers, regex });
Expand Down Expand Up @@ -65,5 +67,121 @@ const folder = (name, containers = [], regex = '') => ({ name, containers, regex
check('the generated orders do place folders, with and without unsaved rows above them', shapes.folders > 50 && shapes.unsaved > 50 && shapes.both > 20, shapes);
check('200 generated orders with nothing stale place every folder where they did before', differ.length === 0, differ.slice(0, 2));
}
// The real sync_order.php on a saved userprefs.cfg: its answers, the file after one sync, and after a second
const synced = (folders, containers, prefs) => {
const php = phpServer('userprefs-sync', { containers });
try {
const dockerMan = path.join(path.dirname(php.config), 'dockerMan');
const file = path.join(dockerMan, 'userprefs.cfg');
fs.writeFileSync(path.join(php.config, 'docker.json'), JSON.stringify(folders));
fs.writeFileSync(path.join(dockerMan, 'dockerMan.cfg'), `autostart-file="${path.join(dockerMan, 'unraid-autostart')}"\n`);
fs.writeFileSync(file, prefs.map((n, i) => `${i}="${n}"\n`).join(''));
const read = () => fs.readFileSync(file, 'utf8').split('\n').filter(Boolean).map((l) => l.replace(/^\d+="(.*)"$/, '$1'));
const first = php.server('sync_order.php', 'POST', { type: 'docker' });
const after = read();
return { answers: [first, php.server('sync_order.php', 'POST', { type: 'docker' })], after, again: read() };
} finally { php.cleanup(); }
};
const drawnFrom = async (folders, containers, prefs) => { const page = await drawDocker({ folders, containers, prefs }); await page.draw(); return page; };
for (const [what, folders, containers, prefs] of [
['a folder member it lacks', { f: folder('F', ['x', 'y']) }, ['a', 'b', 'x', 'y'], ['a', 'folder-f', 'x', 'b']],
['a member saved away from its folder', { f: folder('F', ['x', 'y']) }, ['a', 'b', 'x', 'y'], ['y', 'a', 'folder-f', 'x', 'b']],
['a member moved to another folder', { f: folder('F', ['x']), g: folder('G', ['w', 'y']) }, ['w', 'x', 'y'], ['folder-f', 'x', 'y', 'folder-g', 'w']],
['members saved before their folder', { f: folder('F', ['x', 'y']) }, ['a', 'x', 'y'], ['x', 'folder-f', 'a']],
['a regex folder', { r: folder('Rx', [], '^web') }, ['web1', 'web2', 'db'], ['web2', 'folder-r', 'db', 'web1']],
]) {
const r = synced(folders, containers, prefs);
const [was, now] = [(await drawnFrom(folders, containers, prefs)).drawn(), (await drawnFrom(folders, containers, r.after)).drawn()];
check(`${what}: the sync regroups userprefs.cfg, and the page draws the same from it`, r.answers[0] === '{"success":true}' && !same(r.after, prefs) && same(was, now), { answers: r.answers, after: r.after, was, now });
}
{
// A regex folder saved while empty; web1, installed since, joins it by regex
const r = synced({ f: folder('F', [], '^web') }, ['a', 'web1', 'b'], ['a', 'folder-f', 'b']);
check('a folder with none of its members saved yet gets them after its entry', same(r.after, ['a', 'folder-f', 'web1', 'b']), r);
}
// The real read_membership.php: container => folder name. `json` is docker.json as written, its key order kept
const membershipOf = (folders, containers, prefs, json = JSON.stringify(folders)) => {
const php = phpServer('membership', { containers });
try {
fs.writeFileSync(path.join(php.config, 'docker.json'), json);
if (prefs.length) fs.writeFileSync(path.join(path.dirname(php.config), 'dockerMan', 'userprefs.cfg'), prefs.map((n, i) => `${i}="${n}"\n`).join(''));
return JSON.parse(php.server('read_membership.php', 'GET', { type: 'docker' })).membership;
} finally { php.cleanup(); }
};
{
const web = (name) => folder(name, [], '^web');
for (const [what, folders, containers, prefs, owner, json] of [
['the folder userprefs.cfg names first', { a: web('A'), b: web('B') }, ['web1', 'loose'], ['folder-b', 'loose', 'folder-a'], 'b'],
['of folders it does not name, an index-like id first', { x: web('X'), 7: web('Seven') }, ['web1', 'loose'], ['loose'], '7',
`{"x":${JSON.stringify(web('X'))},"7":${JSON.stringify(web('Seven'))}}`],
['with nothing saved, the folder first A-Z', { z: web('Zulu'), a: web('Alpha') }, ['web1'], [], 'a'],
]) {
const page = await drawnFrom(folders, containers, prefs);
const map = membershipOf(folders, containers, prefs, json);
check(`two folders' regexes match a container: ${what} gets it, on the page and in read_membership.php`,
same(page.members(owner), ['web1']) && map?.web1 === folders[owner].name, { members: Object.keys(folders).map((id) => [id, page.members(id)]), map });
}
const r = synced({ a: web('A'), b: web('B') }, ['web1', 'loose'], ['folder-b', 'loose', 'folder-a', 'web1']);
check('and the sync moves it beside the folder the page draws it in', same(r.after, ['folder-b', 'web1', 'loose', 'folder-a']), r);
}
{
// Generated folders (a container listed in one folder at most) and saved orders, with stale and unsaved entries
let seed = 20261116;
const rnd = (n) => { seed = (Math.imul(seed, 1103515245) + 12345) & 0x7fffffff; return (seed >>> 8) % n; };
const shuffle = (a) => { for (let i = a.length - 1; i > 0; i--) { const j = rnd(i + 1); [a[i], a[j]] = [a[j], a[i]]; } return a; };
const off = [], apart = [], unstable = [], refused = [];
let changed = 0, overlaps = 0;
for (let n = 0; n < 120; n++) {
const containers = shuffle(['a', 'b', 'c', 'web1', 'web2', 'x', 'y']).slice(0, 3 + rnd(5));
const nf = 1 + rnd(3);
const folders = Object.fromEntries(Array.from({ length: nf }, (_, i) => [`f${i}`, folder(`F${i}`, [], rnd(3) === 0 ? '^web' : '')]));
for (const c of containers) { const i = rnd(nf * 2); if (i < nf) folders[`f${i}`].containers.push(c); }
const entries = containers.concat(Object.keys(folders).map((k) => `folder-${k}`));
if (rnd(4) === 0) entries.push('ghost');
const prefs = shuffle(entries).filter(() => rnd(5) > 0);
if (!prefs.length) continue;
overlaps += Object.values(folders).filter((f) => f.regex).length > 1 && containers.some((c) => c.startsWith('web'));
const r = synced(folders, containers, prefs);
if (r.answers.some((a) => a !== '{"success":true}')) { refused.push({ folders, prefs, r }); continue; }
changed += !same(r.after, prefs);
if (!same(r.after, r.again)) unstable.push({ folders, containers, prefs, r });
const page = await drawnFrom(folders, containers, r.after);
const inFolder = new Set(Object.keys(folders).flatMap((id) => page.members(id)));
const shown = (e) => (e.startsWith('folder-') && folders[e.slice(7)]) || (containers.includes(e) && !inFolder.has(e));
// Unraid puts a name the file lacks first; docker.js puts each folder it lacks on top, the last in docker.json first
const want = Object.keys(folders).map((id) => `folder-${id}`).filter((e) => !r.after.includes(e)).reverse()
.concat(containers.filter((c) => !r.after.includes(c) && !inFolder.has(c)), r.after.filter(shown));
if (!same(page.rows(), want)) off.push({ folders, containers, after: r.after, rows: page.rows(), want });
// Members of a folder the file has no entry for stay where they were saved
const unsaved = new Set(Object.keys(folders).filter((id) => !r.after.includes(`folder-${id}`)).flatMap((id) => page.members(id)));
const named = r.after.filter((e) => containers.includes(e) && !unsaved.has(e));
if (!same(named, page.drawn().filter((e) => named.includes(e)))) apart.push({ folders, containers, after: r.after, drawn: page.drawn() });
}
check('the sync regroups over a quarter of the generated orders, and every sync answers success', changed > 30 && refused.length === 0, { changed, refused: refused.slice(0, 2) });
check('a second sync leaves userprefs.cfg as the first wrote it', unstable.length === 0, unstable.slice(0, 2));
check('after the sync the page draws its rows in the order userprefs.cfg names them', off.length === 0, { count: off.length, first: off.slice(0, 2) });
check('and userprefs.cfg names its containers in the order the page draws them, folders whose regexes match one container among them',
apart.length === 0 && overlaps > 10, { overlaps, count: apart.length, first: apart.slice(0, 2) });
}
{
const syncs = (page) => page.posts.filter((r) => r.url.endsWith('/sync_order.php')).map((r) => r.data);
const saved = { folders: { f: folder('F', ['m']) }, containers: ['a', 'm'], prefs: ['folder-f', 'a'] };
const page = await drawDocker(saved);
await page.draw();
await page.draw();
check('a page with folders and a saved order asks for one sync, not one per list rebuild', same(syncs(page), [{ type: 'docker' }]), page.posts);
for (const [what, setup] of [['nothing saved', { ...saved, prefs: [] }], ['no folders', { ...saved, folders: {} }],
['no folder holds a container', { ...saved, containers: ['a'] }]]) {
const quiet = await drawDocker(setup);
await quiet.draw();
check(`${what}: no sync is asked for`, syncs(quiet).length === 0, quiet.posts);
}
const refused = await drawDocker(saved);
const banners = [];
refused.p.w.fv3ShowBanner = (text) => { banners.push(text); };
refused.failing.add('sync_order.php');
await refused.draw();
check('a refused sync shows the start-order banner with the reason', same(banners, ['The Docker start order could not be updated: refused by the test']), banners);
}
done();
})();
Loading
Loading