diff --git a/.coderabbit.yaml b/.coderabbit.yaml index 24006ece..ad04c22e 100644 --- a/.coderabbit.yaml +++ b/.coderabbit.yaml @@ -206,6 +206,54 @@ reviews: GraphQL checks in schema.py; flag a second copy of either. - TESTS use synthetic versions, shas and handles only; a value captured from a live system in a fixture is a finding. + - path: ".github/unraid-compat/**" + instructions: | + The Unraid compatibility matrix; CI only, nothing here ships. + - install_test.sh puts the .plg through each Unraid version's own + plugin installer, hooks included, in a fresh Docker container with + no network; it must refuse to run anywhere else (lib.sh's guard). + Only Slackware's upgradepkg and removepkg are stubbed, keeping their + file-list records: flag any other stand-in for Unraid's code. + - fake_unraid.sh serves one Unraid version's real webGUI with php -S + and pages_test.php drives FV3 through it. Stand-ins are what the OS + image adds around the webGUI, nothing of the webGUI itself: + router.php (nginx's locations), prepend.php (php.ini's prepend), + docker.php (dockerd's socket and the docker CLI), libvirt.php (the + php-libvirt extension), rc_service.sh (rc.docker and rc.libvirt), + dmidecode.sh and ip.sh (those commands), connect_state.php (the + Connect plugin), the dockerd and libvirtd + processes, and emhttp's state files (var.ini, network.ini, the + docker and domain cfg). A call a stand-in does not answer fails + the run: flag an answer that hides one, and any stand-in for code + the webGUI repo has. + - browser_test.sh starts fake_unraid.sh in a container with no network + and runs the Playwright image in a second container that joins its + network. browser_test.js drives FV3's Docker, VMs and Dashboard + pages in Chromium and Firefox and checks what the user sees (rows, + tiles, an opened folder, the advanced preview's layout) against + values written by hand. A failed request or console error passes + only for a service the fake does not run (ABSENT) or for Unraid's + own unload handler. Clicks are a user's; only where an element of + Unraid's page frame named in FRAME covers the target is the click + dispatched instead. + fake_logs.php holds the log checks both tests run. package.json + pins playwright-core to the image's version, which + browser_test.sh checks. + - browser_test.sh runs an Unraid-alone pass first, FV3's three + page hooks moved out of its plugin folder. It checks hooks.js, a + hand-written list of Unraid's DOM hooks, functions, stylesheet + classes and endpoints FV3 relies on, each naming the FV3 file that + still mentions it and what the user loses; any/since only where + FV3 itself branches. + layout.js holds the layout rules FV3's pass checks against that + pass and a loose container on the same page, each from a fix FV3 + has shipped. + - matrix_check.py keeps ci.yml's Unraid rows at one per minor from the + .plg's min to the newest release; Renovate only moves rows. + - Each step asserts what the user is left with (live files, the + registered manifest, saved folders, Unraid's orders) right after it, + against values written by hand. A failed step fails the job. + - Fixtures hold synthetic names and ids only. - path: ".github/scripts/**" instructions: | CI tests and helpers; nothing here ships in the plugin. @@ -224,6 +272,9 @@ reviews: failed read or download as "nothing to report". - package.json here is test-only (jquery, jsdom), pinned to exact versions. + - php_tests.sh is the one list of PHP checks for Lint + build and the + PHP matrix: php -l on every .php and .page file, then every + *_test.php here; a deprecation fails like an error. - path: "**" instructions: |- Applies to every file, whatever the language: diff --git a/.github/scripts/action_refusal_test.js b/.github/scripts/action_refusal_test.js new file mode 100644 index 00000000..6f546c51 --- /dev/null +++ b/.github/scripts/action_refusal_test.js @@ -0,0 +1,114 @@ +// node .github/scripts/action_refusal_test.js — a folder action Unraid refuses shows Unraid's own reason: the real docker.js and vm.js +// folder actions and shared.js, answered as Unraid's Events.php ({success: true|reason}) and VMajax.php ({success: true}|{error: reason}) answer +'use strict'; +const { drawDocker, drawVms, check, done, FOLDER_SETTINGS } = require('./preview_pages_harness.js'); + +const folder = (name, containers = [], regex = '') => ({ name, containers, regex }); +// A VM folder as vm.json holds one +const vmFolder = (name, vms) => ({ name, icon: '', regex: '', containers: vms, settings: { ...FOLDER_SETTINGS }, hidden_preview: [], actions: [] }); + +// Each action post answered by `answer(data)` (or failed when it returns an Error); the dialogs as the user reads them +const stub = (p, answer) => { + const posts = []; + const dialogs = []; + p.$.post = (url, data) => { + posts.push({ url, data }); + const a = answer(data); + return a instanceof Error ? p.$.Deferred().reject({ status: 500, statusText: a.message }) : p.$.Deferred().resolve(a); + }; + p.w.swal = (o) => { + const box = p.w.document.createElement('div'); + box.innerHTML = String(o.text); + dialogs.push({ title: o.title, lines: box.innerHTML.split('
').map((part) => { box.innerHTML = part; return box.textContent; }) }); + }; + p.w.swal.close = () => {}; + return { posts, dialogs }; +}; + +// Runs a folder action to the end on the page's manual clock +const act = async (p, call) => { + let finished = false; + p.g(call).then(() => { finished = true; }, (e) => { p.errors.push(`${call}: ${e && e.stack || e}`); finished = true; }); + for (let i = 0; i < 50 && !finished; i++) await p.tick(100); + return finished; +}; + +(async () => { + const REFUSED = 'Error response from daemon: cannot stop container: web: permission denied'; + { + const page = await drawDocker({ folders: { f: folder('Apps', ['web', 'db']) }, containers: ['web', 'db'], prefs: ['folder-f', 'web', 'db'] }); + await page.draw(); + const { posts, dialogs } = stub(page.p, (data) => (data.container === page.p.g("globalFolders['f'].containers['web'].id") ? { success: REFUSED } : { success: true })); + const finished = await act(page.p, "actionFolder('f', 'stop')"); + check("a container Unraid refuses to stop: the dialog shows Unraid's reason, not its JSON answer", + finished && posts.length === 2 && dialogs.length === 1 && dialogs[0].title === 'exec-error' && JSON.stringify(dialogs[0].lines) === JSON.stringify([REFUSED]), + { finished, posts: posts.length, dialogs, errors: page.p.errors }); + } + { + const page = await drawDocker({ folders: { f: folder('Apps', ['web', 'db']) }, containers: ['web', 'db'], prefs: ['folder-f', 'web', 'db'] }); + await page.draw(); + const { posts, dialogs } = stub(page.p, () => ({ success: true })); + const finished = await act(page.p, "actionFolder('f', 'stop')"); + check('every container accepted: no dialog', finished && posts.length === 2 && dialogs.length === 0, { finished, posts: posts.length, dialogs }); + } + { + const page = await drawDocker({ folders: { f: folder('Apps', ['web']) }, containers: ['web'], prefs: ['folder-f', 'web'] }); + await page.draw(); + const { dialogs } = stub(page.p, () => new Error('Internal Server Error')); + const finished = await act(page.p, "actionFolder('f', 'stop')"); + check('a request that fails outright is still in the dialog, named by its failure', + finished && dialogs.length === 1 && JSON.stringify(dialogs[0].lines) === JSON.stringify(['Internal Server Error']), { finished, dialogs }); + } + { + const NO_NETWORK = "Requested operation is not valid: network 'br0' is not active"; + const p = await drawVms({ folders: { v: vmFolder('VMs', ['vm-a', 'vm-b']) }, names: ['vm-a', 'vm-b'], prefs: ['folder-v', 'vm-a', 'vm-b'] }); + const { posts, dialogs } = stub(p, (data) => (data.uuid === 'u0' ? { error: NO_NETWORK } : { success: true, state: 'running' })); + const finished = await act(p, "actionFolder('v', 'domain-start')"); + check("a VM Unraid refuses to start: the dialog shows libvirt's reason, not the JSON answer", + finished && posts.length === 2 && dialogs.length === 1 && JSON.stringify(dialogs[0].lines) === JSON.stringify([NO_NETWORK]), + { finished, posts: posts.length, dialogs, errors: p.errors }); + } + { + // A success that also carries an `error` field (a warning) is still a success + const p = await drawVms({ folders: { v: vmFolder('VMs', ['vm-a']) }, names: ['vm-a'], prefs: ['folder-v', 'vm-a'] }); + const { dialogs } = stub(p, () => ({ success: true, state: 'running', error: 'Waiting for the guest agent' })); + const finished = await act(p, "actionFolder('v', 'domain-start')"); + check('an answer that says success and also carries an error field is a success: no dialog', finished && dialogs.length === 0, { finished, dialogs }); + } + { + // json_encode of nothing: the action still ends with a dialog instead of throwing with the spinner up + const page = await drawDocker({ folders: { f: folder('Apps', ['web']) }, containers: ['web'], prefs: ['folder-f', 'web'] }); + await page.draw(); + const { dialogs } = stub(page.p, () => null); + const finished = await act(page.p, "actionFolder('f', 'stop')"); + check('a null answer ends the action with a dialog that says the request failed', + finished && !page.p.errors.length && dialogs.length === 1 && JSON.stringify(dialogs[0].lines) === JSON.stringify(['Request failed']), { finished, dialogs, errors: page.p.errors }); + } + { + // With the Unraid API (7.2 and later) the action goes to GraphQL first; when that errors, the PHP endpoint's refusal is shown + const page = await drawDocker({ folders: { f: folder('Apps', ['web']) }, containers: ['web'], prefs: ['folder-f', 'web'], api: true }); + await page.draw(); + const realFetch = page.p.w.fetch; + let mutations = 0; + page.p.w.fetch = async (url, init) => { + if (!String(url).includes('/graphql') || !/mutation/.test(String(init && init.body))) return realFetch(url, init); + mutations++; + return { ok: true, json: async () => ({ errors: [{ message: 'container web: permission denied' }] }) }; + }; + const { posts, dialogs } = stub(page.p, () => ({ success: REFUSED })); + const finished = await act(page.p, "actionFolder('f', 'stop')"); + check("with the API, an action GraphQL refuses goes to Unraid's PHP endpoint once, and its reason is shown", + finished && mutations === 1 && posts.length === 1 && dialogs.length === 1 && JSON.stringify(dialogs[0].lines) === JSON.stringify([REFUSED]), + { finished, mutations, posts: posts.length, dialogs, errors: page.p.errors }); + } + { + // An answer with no reason in it is still a failure, shown as Unraid sent it + const page = await drawDocker({ folders: { f: folder('Apps', ['web']) }, containers: ['web'], prefs: ['folder-f', 'web'] }); + await page.draw(); + const { dialogs } = stub(page.p, () => ({ success: '' })); + const finished = await act(page.p, "actionFolder('f', 'stop')"); + check('an Events.php answer with an empty reason is a failure, not a success', + finished && dialogs.length === 1 && JSON.stringify(dialogs[0].lines) === JSON.stringify(['{"success":""}']), { finished, dialogs }); + } + done(); +})().catch((e) => { console.error(e); process.exit(1); }); diff --git a/.github/scripts/docker_saved_places_test.js b/.github/scripts/docker_saved_places_test.js index 1a28436a..96ec7187 100644 --- a/.github/scripts/docker_saved_places_test.js +++ b/.github/scripts/docker_saved_places_test.js @@ -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 }); @@ -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(); })(); diff --git a/.github/scripts/error_handler_test.php b/.github/scripts/error_handler_test.php new file mode 100644 index 00000000..a7ed48e5 --- /dev/null +++ b/.github/scripts/error_handler_test.php @@ -0,0 +1,52 @@ +&1', $out, $rc); +check('the plugin was copied', $rc === 0 && is_file("$docroot/plugins/folder.view3/server/lib.php"), $out); +// Each warns on its line 2: an FV3 file, Unraid's VM code, and a folder whose name only starts like FV3's +$warns = [ + 'fv3-own' => "$docroot/plugins/folder.view3/server/fv3t_warn.php", + 'unraid-own' => "$docroot/plugins/dynamix.vm.manager/include/fv3t_warn.php", + 'name-prefix' => "$docroot/plugins/folder.view3-copy/fv3t_warn.php", +]; +// lib.php requires these two Unraid host files at load time +$files = ['webGui/include/Helpers.php' => ' ' $path) $files[substr($path, strlen("$docroot/"))] = " $src) { + @mkdir(dirname("$docroot/$rel"), 0777, true); + file_put_contents("$docroot/$rel", $src); +} + +// A process of its own, as an endpoint request runs: lib.php's handler and the log settings stay out of this test's PHP +$request = '$_SERVER["DOCUMENT_ROOT"] = ' . var_export($docroot, true) . '; require ' . var_export("$docroot/plugins/folder.view3/server/lib.php", true) . ';' + . ' $configDir = ' . var_export("$fv3tTmp/config", true) . '; foreach (' . var_export(array_values($warns), true) . ' as $f) include $f;'; +$out = []; +exec(escapeshellarg(PHP_BINARY) . ' -d error_reporting=-1 -d display_errors=0 -d log_errors=1 -d error_log=' . escapeshellarg("$fv3tTmp/php.log") + . ' -r ' . escapeshellarg($request) . ' 2>&1', $out, $rc); +check('the request ran with no output', $rc === 0 && $out === [], ['rc' => $rc, 'out' => $out]); + +$logged = @file("$fv3tTmp/config/error.log", FILE_IGNORE_NEW_LINES) ?: []; +check("error.log holds the FV3 file's warning and nothing else", + count($logged) === 1 && preg_match('/^\[\d{4}-\d\d-\d\d \d\d:\d\d:\d\d\] php-error: Undefined array key "fv3-own" at fv3t_warn\.php:2$/', $logged[0]), $logged); +$phplog = (string)@file_get_contents("$fv3tTmp/php.log"); +foreach ($warns as $key => $path) { + check("PHP's own log still has the $key warning", str_contains($phplog, "PHP Warning: Undefined array key \"$key\" in $path on line 2"), $phplog); +} + +exec('rm -rf ' . escapeshellarg($fv3tTmp)); +echo $fv3tFailed ? "\n$fv3tFailed FAILED\n" : "\nall passed\n"; +exit($fv3tFailed ? 1 : 0); diff --git a/.github/scripts/important_check.py b/.github/scripts/important_check.py new file mode 100644 index 00000000..489c0fd1 --- /dev/null +++ b/.github/scripts/important_check.py @@ -0,0 +1,132 @@ +#!/usr/bin/env python3 +"""Each !important in the plugin's own CSS, JS, PHP and .page files needs a `!important: ` comment.""" +import pathlib +import re +import sys + +import vendor + +REASON = re.compile(r'!important:\s*\w', re.I) +KINDS = {'.css': 'css', '.js': 'js', '.php': 'php', '.page': 'page'} +# A `/` after one of these, or first on its line, opens a JS regex literal rather than dividing; not `<`, as `

` has it +REGEX_AFTER = set('(,=:[!&|?{};+-*%>~^') + + +def _blank(code, text, i, j): + code[i:j] = [ch if ch == '\n' else ' ' for ch in text[i:j]] + + +def _regex_end(text, i): + """End of the JS regex literal opening at `i`, or None when the `/` divides.""" + before = text[:i].rstrip(' \t') + if before and not before.endswith('\n') and before[-1] not in REGEX_AFTER and not re.search(r'\b(return|typeof)$', before): + return None + j, in_class = i + 1, False + while j < len(text) and text[j] != '\n': + if text[j] == '\\': + j += 1 + elif text[j] == '[': + in_class = True + elif text[j] == ']': + in_class = False + elif text[j] == '/' and not in_class: + return j + 1 + j += 1 + return None + + +def _in_script(spans, i): + return any(start <= i < end for start, end in spans) + + +def scan(text, kind): + """(code, comments): the text with comments, CSS strings and JS regexes blanked, and [(start, end, comment text)].""" + code, comments, i, n = list(text), [], 0, len(text) + script = kind != 'css' + # A .page holds CSS and HTML too: only its ', text, re.I | re.S)] if kind == 'page' else [] + slashes = [(0, n)] if kind in ('js', 'php') else js + [m.span(1) for m in re.finditer(r'<\?(.*?)(?:\?>|\Z)', text, re.S)] if kind == 'page' else [] + while i < n: + c, two = text[i], text[i:i + 2] + html = kind == 'page' and text.startswith('' if html else '\n' + found = text.find(close, i + 2) + end = n if found == -1 else found + (0 if close == '\n' else len(close)) + comments.append((i, end, text[i:end])) + _blank(code, text, i, end) + i = end + elif c == '/' and _in_script(js, i) and (end := _regex_end(text, i)): + _blank(code, text, i, end) + i = end + elif c in '"\'' or (c == '`' and script): + j = i + 1 + while j < n and text[j] != c and text[j] != '\n': + j += 2 if text[j] == '\\' else 1 + # A script's string is where inline styles live: keep it as code; a CSS string (content: "...") is text + if kind == 'css': + _blank(code, text, i, j + 1) + i = j + 1 + else: + i += 1 + return ''.join(code), comments + + +def _line(text, at): + return text.count('\n', 0, at) + + +def _own_line(text, start): + return text[text.rfind('\n', 0, start) + 1:start].strip() == '' + + +def _reason(comments, text, line, alone): + """A reason comment ending on `line`; with `alone`, only one with no code before it on its line.""" + return any(REASON.search(body) and _line(text, end - 1) == line and (not alone or _own_line(text, start)) + for start, end, body in comments) + + +def _rule_start(code, at): + """Where the selector of the rule holding the declaration at `at` begins, past any nested rules; None outside a rule.""" + depth, opened = 0, at - 1 + while opened >= 0 and not (code[opened] == '{' and depth == 0): + depth += (code[opened] == '}') - (code[opened] == '{') + opened -= 1 + if opened < 0: + return None + start = max(code.rfind(ch, 0, opened) for ch in '{};') + 1 + while start < opened and code[start].isspace(): + start += 1 + return start + + +def problems_in(path, text, kind): + code, comments = scan(text, kind) + out = [] + for m in re.finditer(r'!\s*important\b', code, re.I): + line = _line(text, m.start()) + ok = _reason(comments, text, line, alone=False) or _reason(comments, text, line - 1, alone=True) + start = _rule_start(code, m.start()) if kind == 'css' else None + if not ok and start is not None: + ok = _reason(comments, text, _line(text, start) - 1, alone=True) or _reason(comments, text, _line(text, start), alone=True) + if not ok: + out.append(f'{path}:{line + 1}: !important without a "!important: " comment on its line, the line above, or above its rule') + return out + + +def problems(repo): + repo = repo.resolve() + manifest, root = vendor.plugin_root(repo) + bundled = {vendor.destination(manifest, root, dest) for lib in manifest['libraries'] for dest in lib['files'].values()} + files = [p for p in sorted(root.rglob('*')) if p.suffix in KINDS and p.is_file() and p.resolve() not in bundled] + if not files: + return [f'{manifest["root"]}: no CSS, JS, PHP or .page files to check'] + return [problem for p in files for problem in problems_in(p.relative_to(repo), p.read_text(encoding='utf-8'), KINDS[p.suffix])] + + +if __name__ == '__main__': + found = problems(pathlib.Path(__file__).resolve().parents[2]) + print('\n'.join(found) or 'Every !important has its reason') + sys.exit(1 if found else 0) diff --git a/.github/scripts/important_check_test.py b/.github/scripts/important_check_test.py new file mode 100644 index 00000000..ad451f49 --- /dev/null +++ b/.github/scripts/important_check_test.py @@ -0,0 +1,98 @@ +import json +import pathlib +import tempfile +import unittest + +import important_check + +ROOT = 'src/plugin' + + +class ImportantCheckTest(unittest.TestCase): + def check(self, files, bundled=()): + tmp = tempfile.TemporaryDirectory() + self.addCleanup(tmp.cleanup) + repo = pathlib.Path(tmp.name) + (repo / 'vendor.json').write_text(json.dumps({'root': ROOT, 'libraries': [{'files': {f'x/{b}': b for b in bundled}}]}), encoding='utf-8') + for name, text in files.items(): + (repo / ROOT / name).parent.mkdir(parents=True, exist_ok=True) + (repo / ROOT / name).write_text(text, encoding='utf-8') + return important_check.problems(repo) + + def test_a_reason_above_the_rule_or_on_the_line_passes(self): + sheet = ('.a { top: 0 }\n\n/* !important: Unraid inlines this padding */\n#t .x,\n#t .y {\n padding-left: 20px !important;\n}\n' + '.b { margin: 0 !important; /* !important: a theme sets it inline */ }\n') + self.assertEqual(self.check({'styles/a.css': sheet}), []) + + def test_an_important_without_its_reason_is_named_with_its_line(self): + sheet = '/* spacing */\n.a {\n top: 0;\n left: 1px!important;\n}\n' + self.assertEqual(self.check({'styles/a.css': sheet}), + [f'{ROOT}/styles/a.css:4: !important without a "!important: " comment on its line, the line above, or above its rule']) + + def test_a_reason_belongs_to_the_rule_under_it_only(self): + sheet = '/* !important: first */\n.a { top: 0 !important }\n.b { top: 0 !important }\n' + (problem,) = self.check({'styles/a.css': sheet}) + self.assertIn('a.css:3:', problem) + + def test_a_reason_at_the_end_of_a_line_covers_that_line_only(self): + rules = '.a { top: 0 !important } /* !important: r */\n.b { top: 0 !important }\n' + decls = '.c {\n top: 0 !important; /* !important: r */\n left: 0 !important;\n}\n' + self.assertEqual([p.split(': !')[0] for p in self.check({'styles/a.css': rules, 'styles/b.css': decls})], + [f'{ROOT}/styles/a.css:2', f'{ROOT}/styles/b.css:3']) + + def test_a_nested_rule_does_not_move_a_declaration_to_its_selector(self): + outer = '/* !important: r */\n.a {\n .b { color: red }\n top: 0 !important;\n}\n' + inner = '.a {\n /* !important: r */\n .b { color: red }\n top: 0 !important;\n}\n' + self.assertEqual(self.check({'styles/a.css': outer}), []) + (problem,) = self.check({'styles/b.css': inner}) + self.assertIn('b.css:4:', problem) + + def test_a_quote_in_a_regex_literal_or_a_php_hash_comment_is_read_as_code_does(self): + js = "s = s.replace(/'/g, ''); el.style.cssText = 'x:1 !important'; // !important: r\n" + php = "See https://example.test

\n\n\n'}) + self.assertEqual([p.split(': !')[0] for p in found], [f'{ROOT}/Folder.page:1', f'{ROOT}/styles/a.css:1']) + + def test_a_url_in_page_css_is_no_regex_but_one_in_its_script_is(self): + page = ('\n' + "\n") + (problem,) = self.check({'Folder.page': page}) + self.assertIn('Folder.page:1:', problem) + + def test_a_protocol_relative_url_in_page_css_is_no_comment_but_one_in_its_php_is(self): + page = '\n"; ?>\n' + (problem,) = self.check({'Folder.page': page}) + self.assertIn('Folder.page:1:', problem) + + def test_no_files_to_check_fails_closed(self): + self.assertEqual(self.check({'README.md': 'x'}), [f'{ROOT}: no CSS, JS, PHP or .page files to check']) + + def test_a_reason_comment_must_give_a_reason(self): + self.assertEqual(len(self.check({'styles/a.css': '/* !important: */\n.a { top: 0 !important }\n'})), 1) + + def test_a_comment_or_a_content_string_that_mentions_it_is_not_a_use(self): + sheet = '/* never use !important here */\n.a::after { content: "!important"; }\n' + self.assertEqual(self.check({'styles/a.css': sheet}), []) + + def test_a_script_needs_the_reason_on_its_line_or_the_line_above(self): + good = "// beats swal's !important rules\n// !important: no stylesheet may show a row while it is measured\nrow.style.cssText += ';visibility:hidden !important;';\n" + bad = "// hide the row\nrow.style.cssText += ';visibility:hidden !important;';\n" + self.assertEqual(self.check({'scripts/a.js': good}), []) + (problem,) = self.check({'scripts/b.js': bad}) + self.assertIn('scripts/b.js:2:', problem) + + def test_page_and_php_files_are_checked_too(self): + found = self.check({'Folder.page': 'Menu="x"\n---\n\n', 'server/a.php': " JSON.parse(execFileSync('php', ['-r', `${fn} $l = json_decode(stream_get_contents(STDIN), true); usort($l, 'fv3_name_cmp'); echo json_encode($l);`], +// The shipped lib.order.php, required whole: it defines functions only +const phpSort = (names) => JSON.parse(execFileSync('php', ['-r', `require $argv[1]; $l = json_decode(stream_get_contents(STDIN), true); usort($l, 'fv3_name_cmp'); echo json_encode($l);`, + path.join(plugin, 'server/lib.order.php')], { input: JSON.stringify(names), encoding: 'utf8' })); const browserSort = (names) => names.slice().sort((a, b) => a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' })); diff --git a/.github/scripts/package-lock.json b/.github/scripts/package-lock.json index dc7d31db..5338d543 100644 --- a/.github/scripts/package-lock.json +++ b/.github/scripts/package-lock.json @@ -7,7 +7,7 @@ "name": "folder-view3-ci-tests", "devDependencies": { "jquery": "3.7.1", - "jsdom": "31.0.0" + "jsdom": "31.0.1" } }, "node_modules/@exodus/bytes": { @@ -154,9 +154,9 @@ "license": "MIT" }, "node_modules/jsdom": { - "version": "31.0.0", - "resolved": "https://registry.npmjs.org/jsdom/-/jsdom-31.0.0.tgz", - "integrity": "sha512-xAkhD9z1eyFC5+QUi9K24CaZJgdCQ2GOGLW1GdVDoQvkp/AxEH9TQ0pAplPOlIiV92hfcgnFOADZypbjgbBT6Q==", + "version": "31.0.1", + "resolved": "https://registry.npmjs.org/jsdom/-/jsdom-31.0.1.tgz", + "integrity": "sha512-CbhJL8ZQg3eKswxSZth4QO9tW0jPIhmR0vtSXQSbKSfWbwIT6hoRc17wdHIVTQ43KI0mdV5iTJr3281IDgVZ5g==", "dev": true, "license": "MIT", "dependencies": { @@ -202,9 +202,9 @@ } }, "node_modules/parse5": { - "version": "8.1.0", - "resolved": "https://registry.npmjs.org/parse5/-/parse5-8.1.0.tgz", - "integrity": "sha512-sq3qBFpee5jeocbFbO48VHlGr8sT7mbXS0vxlKN6sottBtJzrOG3998sr0llSZcQAGgfpdmEYAIZE0P9z0dE8w==", + "version": "8.1.1", + "resolved": "https://registry.npmjs.org/parse5/-/parse5-8.1.1.tgz", + "integrity": "sha512-EcR/6WDcU7b2N3vQSxKALCk9lXGtUMQxXio008OEMBE0kavu/7JLfBEeFPzi43nMqVDSbEn3QCK+BMCc9zO//w==", "dev": true, "license": "MIT", "dependencies": { @@ -215,13 +215,13 @@ } }, "node_modules/parse5-parser-stream": { - "version": "8.1.0", - "resolved": "https://registry.npmjs.org/parse5-parser-stream/-/parse5-parser-stream-8.1.0.tgz", - "integrity": "sha512-58MPfNTA+DsIzro41rU94xghnu/h2N4yEPdPdGPlZVHM/QvA5jlVGYVo7IzlHbFLHYx3cZUnr9s9HBrA0tEYyw==", + "version": "8.1.1", + "resolved": "https://registry.npmjs.org/parse5-parser-stream/-/parse5-parser-stream-8.1.1.tgz", + "integrity": "sha512-hMcVbbUJcQmP8rURWUdYrT1flRiBiBVjq/RYGguJpDUgIXSqdYKlE0CxthmdMWoSlBAuL5bUBa73g0pttSy7Hw==", "dev": true, "license": "MIT", "dependencies": { - "parse5": "^8.0.0" + "parse5": "^8.1.1" }, "funding": { "url": "https://github.com/inikulin/parse5?sponsor=1" @@ -251,29 +251,29 @@ } }, "node_modules/tldts": { - "version": "7.4.19", - "resolved": "https://registry.npmjs.org/tldts/-/tldts-7.4.19.tgz", - "integrity": "sha512-48dH6aSpQnpbzDUc4vIQZ1VjB8tkK+e8c1yx05/SZK+rkssgjEOlxDd/yU8/a4vGYQ9vRgJr9dwMq9TJ0b3X2Q==", + "version": "7.5.4", + "resolved": "https://registry.npmjs.org/tldts/-/tldts-7.5.4.tgz", + "integrity": "sha512-Rn2ABgjHZrA+jzqom5zhxA0hLohzYrIgfCPyhpTUxqsUrcsBYuab49LseR9waB9Jr8/MxdodvMHA91oRiGLO3Q==", "dev": true, "license": "MIT", "dependencies": { - "tldts-core": "^7.4.19" + "tldts-core": "^7.5.4" }, "bin": { "tldts": "bin/cli.js" } }, "node_modules/tldts-core": { - "version": "7.4.20", - "resolved": "https://registry.npmjs.org/tldts-core/-/tldts-core-7.4.20.tgz", - "integrity": "sha512-C630TWXZCZmLq0F2bW4QqwnaaxCQ/svpAqvJwnC4JvgEM5ohhBnGsEzTLSOXcN+xjz+kEaHS5uoWIw2klnqd0A==", + "version": "7.5.4", + "resolved": "https://registry.npmjs.org/tldts-core/-/tldts-core-7.5.4.tgz", + "integrity": "sha512-9+4zWtKEDZ3V3FoZ1B97rv+5z5oqp7yeXGVze3CxgQ+CJro9do9DCPD00wxstYZDXDMXsurkBerFfiB6GYtPbA==", "dev": true, "license": "MIT" }, "node_modules/tough-cookie": { - "version": "6.0.2", - "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-6.0.2.tgz", - "integrity": "sha512-exgYmnmL/sJpR3upZfXG5PoatXQii55xAiXGXzY+sROLZ/Y+SLcp9PgJNI9Vz37HpQ74WvDcLT8eqm+kV3FzrA==", + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/tough-cookie/-/tough-cookie-6.1.0.tgz", + "integrity": "sha512-FTxc8u8Kl2tz0Lat1u8ksvcy8bwHRyEMawwTWybo51JqaC6O7SvfuhAKg+Ld6VqyIe2QRJBzY9HA7y2WyFRT2w==", "dev": true, "license": "BSD-3-Clause", "dependencies": { @@ -297,9 +297,9 @@ } }, "node_modules/undici": { - "version": "8.12.3", - "resolved": "https://registry.npmjs.org/undici/-/undici-8.12.3.tgz", - "integrity": "sha512-+fdndUTwBP86cVjihTp21hZHo7d6oj1c/47XPA3lL8mM0XHs+C4EkcjmgRsFnFk2f/UONGDy9ItBItmXsLOQHw==", + "version": "8.12.4", + "resolved": "https://registry.npmjs.org/undici/-/undici-8.12.4.tgz", + "integrity": "sha512-8Mx+uNKKER2bA4AdtedkqRbwUCIJFr2nJckXRe5yrDqzePqwLP7VNcctN8/XdmDSbF7mS+jJKIszlL+vbam1VA==", "dev": true, "license": "MIT", "engines": { diff --git a/.github/scripts/package.json b/.github/scripts/package.json index cab4cf12..3548b360 100644 --- a/.github/scripts/package.json +++ b/.github/scripts/package.json @@ -4,6 +4,6 @@ "description": "Test-only dependencies for the scripts in this folder; nothing here ships in the plugin", "devDependencies": { "jquery": "3.7.1", - "jsdom": "31.0.0" + "jsdom": "31.0.1" } } diff --git a/.github/scripts/page_order_test.php b/.github/scripts/page_order_test.php index 58b8ce4b..edab0144 100644 --- a/.github/scripts/page_order_test.php +++ b/.github/scripts/page_order_test.php @@ -75,6 +75,7 @@ class DockerUpdate {} $GLOBALS['fv3tDown'] = $names === null; $setPrefs($cfg); @unlink("$configDir/docker.json"); + @unlink("$configDir/order-docker.json"); @unlink($fv3tAutostart); if ($folders !== null) file_put_contents("$configDir/docker.json", json_encode((object)$folders)); if ($autostart !== null) file_put_contents($fv3tAutostart, is_array($autostart) ? implode("\n", $autostart) . "\n" : $autostart); @@ -90,6 +91,12 @@ class DockerUpdate {} $GLOBALS['fv3tDown'] = false; } }; +// $sync's run, answering with userprefs.cfg as the run left it (null = no file) in place of the autostart file +$syncPrefs = function (?array $folders, array $names, ?string $cfg, ?callable $break = null) use ($sync, $fv3DockerManDir): array { + [$answer] = $sync($folders, $names, $cfg, null, $break); + $file = "$fv3DockerManDir/userprefs.cfg"; + return [$answer, file_exists($file) ? file_get_contents($file) : null]; +}; // updateAutostartConfig() as the Autostart tab calls it, with the folder config as given $autostartSave = function (string $mode, string $foldersJson, array $waits = [], bool $readOnly = false) use ($setPrefs, $configDir, $fv3tAutostart): array { $GLOBALS['fv3tNames'] = ['a', 'b']; @@ -204,11 +211,18 @@ class DockerUpdate {} ['folder-a', 'folder-b', 'folder-m', 'plex', 'folder-z']], ['no userprefs.cfg: a look-alike of a folder member keeps its own A-Z place', $order(['f' => $folder('F', ['10'])], ['10', '1e1', '0first'], []), ['0first', '1e1', '10', 'folder-f']], - ['read_membership.php: an explicit entry beats a label (s1), a label places a loose container (mid), the first folder whose regex matches wins (r1, y1)', - $membership(null), [1, '200', ['alpha' => 'Dup', 'mid' => 'Zulu', 'n1' => 'Numbers', 'o1' => 'Old', 'r1' => 'Rex', 'r2' => 'Rex', 's1' => 'Seven', 'y1' => 'Yankee', 'y2' => 'Yankee', 'z1' => 'Zulu', 'z2' => 'Zulu']]], - ['and its answer does not depend on userprefs.cfg: the saved order, a reversed one, a file cut off mid-save', + ['two folders\' regexes match a container: the folder userprefs.cfg names first takes it, as the page draws it', + $order(['a' => $folder('A', [], '^web'), 'b' => $folder('B', [], '^web')], ['web1', 'loose'], ['folder-b', 'loose', 'folder-a']), + ['web1', 'folder-b', 'loose', 'folder-a']], + ['folders userprefs.cfg does not name take theirs in JS key order, an index-like id first', + $order(['x' => $folder('X', [], '^web'), '7' => $folder('Seven', [], '^web')], ['web1', 'loose'], ['loose']), ['folder-x', 'web1', 'folder-7', 'loose']], + ['no userprefs.cfg: the folder first A-Z takes a container two regexes match', + $order(['z' => $folder('Zulu', [], '^web'), 'a' => $folder('Alpha', [], '^web')], ['web1'], []), ['web1', 'folder-a', 'folder-z']], + ['read_membership.php: an explicit entry beats a label (s1), a label places a loose container (mid); with nothing saved the folder first A-Z whose regex matches wins (Dup: r1, y1)', + $membership(null), [1, '200', ['alpha' => 'Dup', 'mid' => 'Zulu', 'n1' => 'Numbers', 'o1' => 'Old', 'r1' => 'Dup', 'r2' => 'Dup', 's1' => 'Seven', 'y1' => 'Dup', 'y2' => 'Dup', 'z1' => 'Zulu', 'z2' => 'Zulu']]], + ['with a saved order, the folders it names win first, then the rest (Rex, Yankee before Dup); a file cut off mid-save reads as nothing saved, as on the page', [$membership($userprefs($fixture['prefs'])), $membership($userprefs(array_reverse($fixture['containers']))), $membership($cutOff)], - array_fill(0, 3, $membership(null))], + [...array_fill(0, 2, [1, '200', ['alpha' => 'Dup', 'mid' => 'Zulu', 'n1' => 'Numbers', 'o1' => 'Old', 'r1' => 'Rex', 'r2' => 'Rex', 's1' => 'Seven', 'y1' => 'Yankee', 'y2' => 'Yankee', 'z1' => 'Zulu', 'z2' => 'Zulu']]), $membership(null)]], ['autostart lines follow the order and keep their wait value', fv3_reorder_autostart(['a 5', 'b', 'c 10'], ['c', 'folder-f', 'a', 'a']), ['c 10', 'a 5', 'b']], ['a line the order does not name stays after the others, in file order', @@ -248,23 +262,68 @@ class DockerUpdate {} $sync([], ['b', 'a', 'c'], $cutOff, ['c', 'ghost', 'b 5', 'a']), ["Unraid's saved Docker order (userprefs.cfg) could not be read", ['c', 'ghost', 'b 5', 'a']]], ['an empty userprefs.cfg writes A-Z, as no userprefs.cfg does', $sync([], ['b', 'a', 'c'], '', ['c', 'b 5', 'a']), [null, ['a', 'b 5', 'c']]], + ['userprefs.cfg: a folder member it lacks is saved right after its folder, in the folder\'s order', + $syncPrefs(['f' => $folder('F', ['x', 'y'])], ['a', 'b', 'x', 'y'], $userprefs(['a', 'folder-f', 'x', 'b'])), [null, $userprefs(['a', 'folder-f', 'x', 'y', 'b'])]], + ['userprefs.cfg: a member saved away from its folder moves to it', + $syncPrefs(['f' => $folder('F', ['x', 'y'])], ['a', 'b', 'x', 'y'], $userprefs(['y', 'a', 'folder-f', 'x', 'b'])), [null, $userprefs(['a', 'folder-f', 'x', 'y', 'b'])]], + ['userprefs.cfg: a member moved to another folder follows it', + $syncPrefs(['f' => $folder('F', ['x']), 'g' => $folder('G', ['w', 'y'])], ['w', 'x', 'y'], $userprefs(['folder-f', 'x', 'y', 'folder-g', 'w'])), [null, $userprefs(['folder-f', 'x', 'folder-g', 'w', 'y'])]], + ['userprefs.cfg: members saved before their folder, as FV3\'s own writers save them, stay before it', + $syncPrefs(['f' => $folder('F', ['x', 'y'])], ['a', 'x', 'y'], $userprefs(['x', 'folder-f', 'a'])), [null, $userprefs(['x', 'y', 'folder-f', 'a'])]], + ['userprefs.cfg: a regex folder\'s members keep the saved order, on their side of its entry', + $syncPrefs(['r' => $folder('Rx', [], '^web')], ['web1', 'web2', 'db'], $userprefs(['web2', 'folder-r', 'db', 'web1'])), [null, $userprefs(['web2', 'web1', 'folder-r', 'db'])]], + ['userprefs.cfg: a folder with none of its members saved yet, as after an install, gets them after its entry', + $syncPrefs(['f' => $folder('F', ['x'])], ['a', 'x'], $userprefs(['folder-f', 'a'])), [null, $userprefs(['folder-f', 'x', 'a'])]], + ['userprefs.cfg: a folder it has no entry for leaves its members where they are', + $syncPrefs(['f' => $folder('F', ['x', 'y'])], ['a', 'x', 'y'], $userprefs(['x', 'a', 'y'])), [null, $userprefs(['x', 'a', 'y'])]], + ['userprefs.cfg: an entry Docker does not have keeps its place', + $syncPrefs(['f' => $folder('F', ['x'])], ['a', 'x'], $userprefs(['ghost', 'folder-f', 'a', 'x'])), [null, $userprefs(['ghost', 'folder-f', 'x', 'a'])]], + ['userprefs.cfg: none is not created, so Unraid keeps its A-Z order', + $syncPrefs(['f' => $folder('F', ['x'])], ['a', 'x'], null), [null, null]], + ['userprefs.cfg: an empty one stays empty', + $syncPrefs(['f' => $folder('F', ['x'])], ['a', 'x'], ''), [null, '']], + ['userprefs.cfg: one already in place is not written again, its numbering kept', + $syncPrefs(['f' => $folder('F', ['x', 'y'])], ['x', 'y'], "0=\"folder-f\"\n2=\"x\"\n5=\"y\"\n"), [null, "0=\"folder-f\"\n2=\"x\"\n5=\"y\"\n"]], + ['userprefs.cfg: the fixture\'s saved order is already in place', + $syncPrefs($fixture['folders'], $fixture['containers'], $userprefs($fixture['prefs'])), [null, $userprefs($fixture['prefs'])]], + ['userprefs.cfg: custom and off modes regroup it too, and leave the autostart file to their own rule', + array_map(fn($mode) => [...$sync(['f' => $folder('F', ['x'])], ['a', 'x'], $userprefs(['x', 'a', 'folder-f']), ['x', 'a'], + fn() => file_put_contents("$configDir/autostart.json", json_encode(['mode' => $mode, 'sequence' => ['a', 'x']]))), file_get_contents("$fv3DockerManDir/userprefs.cfg")], ['custom', 'off']), + [[null, ['a', 'x'], $userprefs(['a', 'folder-f', 'x'])], [null, ['x', 'a'], $userprefs(['a', 'folder-f', 'x'])]]], + ['userprefs.cfg: a name that cannot be written back (a quote another tool saved) leaves it as it is, and is reported', + $syncPrefs(['f' => $folder('F', ['x'])], ['x'], "0=\"x\"\n1=\"a\\\"b\"\n2=\"folder-f\"\n"), + ["Unraid's saved Docker order (userprefs.cfg) holds a name that cannot be written back", "0=\"x\"\n1=\"a\\\"b\"\n2=\"folder-f\"\n"]], + ['userprefs.cfg: an order saved while the sync worked it out is kept as it is', + (function () use ($setPrefs, $userprefs, $folder, $fv3DockerManDir) { + $read = ['x', 'b', 'folder-f']; + $setPrefs($userprefs(['folder-f', 'x', 'b'])); + return [fv3_sync_user_prefs($read, fv3_compute_folder_membership(['f' => $folder('F', ['x'])], ['b', 'x'], [], $read)), file_get_contents("$fv3DockerManDir/userprefs.cfg")]; + })(), [null, $userprefs(['folder-f', 'x', 'b'])]], + ['userprefs.cfg: FV3\'s order snapshot, which the heal restores from after a reinstall, is saved with it', + [$syncPrefs(['f' => $folder('F', ['x'])], ['a', 'x'], $userprefs(['x', 'a', 'folder-f']))[1], fv3_export_order_snapshot('docker')['entries'] ?? null], + [$userprefs(['a', 'folder-f', 'x']), ['a', 'folder-f', 'x']]], + ['userprefs.cfg: a cut-off one is left as it is, and reported', + $syncPrefs(['f' => $folder('F', ['x'])], ['a', 'x'], $cutOff), ["Unraid's saved Docker order (userprefs.cfg) could not be read", $cutOff]], + ['userprefs.cfg: the start order worked out from it is the same before and after the update (the page: docker_saved_places_test.js)', + $order(['f' => $folder('F', ['x', 'y'])], ['a', 'b', 'x', 'y'], fv3_parse_user_prefs($syncPrefs(['f' => $folder('F', ['x', 'y'])], ['a', 'b', 'x', 'y'], $userprefs(['y', 'a', 'folder-f', 'b']))[1])), + $order(['f' => $folder('F', ['x', 'y'])], ['a', 'b', 'x', 'y'], ['y', 'a', 'folder-f', 'b'])], ['no containers, or Docker not running (Unraid\'s client answers an empty list): nothing is written, nothing is reported', $sync([], [], null, ['b', 'a']), [null, ['b', 'a']]], ['a container list with an unnamed entry: nothing is written, and it is reported', $sync([], ['a', ''], null, ['b', 'a']), ["Docker's container list could not be read", ['b', 'a']]], ['a failed label read: nothing is written, and it is reported', $sync([], ['a', 'b'], null, ['b', 'a'], function () { $GLOBALS['fv3tLabelsDown'] = true; }), ['the container labels could not be read', ['b', 'a']]], - ['custom mode does not read the folder config, so a damaged one does not stop it', + ['custom mode still writes its sequence when a damaged folder config stops the userprefs.cfg update, and says so', $sync([], ['a', 'b'], null, ['b', 'a'], function () use ($configDir) { file_put_contents("$configDir/docker.json", '{'); file_put_contents("$configDir/autostart.json", '{"mode":"custom","sequence":["a","b"]}'); - }), [null, ['a', 'b']]], + }), ['the folder config could not be read', ['a', 'b']]], ['a missing plugin config folder (deleted by hand under an open tab) is "no folders", not a failure: the sync runs and reports nothing', [$syncWithoutConfigDir(['b', 'a']), $syncWithoutConfigDir([]), is_dir($configDir)], [[null, ['a', 'b']], [null, ['b', 'a']], true]], ['a damaged folder config is reported', $sync([], ['a', 'b'], null, ['b', 'a'], fn() => file_put_contents("$configDir/docker.json", '{')), ['the folder config could not be read', ['b', 'a']]], - ['autostart mode off reads nothing and reports nothing', - $sync([], ['a', 'b'], $cutOff, ['b', 'a'], fn() => file_put_contents("$configDir/autostart.json", '{"mode":"off"}')), [null, ['b', 'a']]], + ['autostart mode off leaves the autostart file as it is, and reports a userprefs.cfg it cannot read', + $sync([], ['a', 'b'], $cutOff, ['b', 'a'], fn() => file_put_contents("$configDir/autostart.json", '{"mode":"off"}')), ["Unraid's saved Docker order (userprefs.cfg) could not be read", ['b', 'a']]], ['custom mode writes its saved sequence, the rest after it in file order', $sync([], ['a', 'b', 'c'], null, ['b 5', 'a', 'ghost', 'c'], fn() => file_put_contents("$configDir/autostart.json", '{"mode":"custom","sequence":["c","a"]}')), [null, ['c', 'a', 'b 5']]], ['custom mode: a file already in its sequence is not written again', @@ -319,6 +378,16 @@ class DockerUpdate {} [['success' => true, 'mode' => 'off', 'sequence' => [], 'warning' => 'the wait edits could not be written to the autostart file'], ['success' => true, 'mode' => 'folder', 'sequence' => [], 'warning' => 'the wait edits could not be written to the autostart file; the autostart file could not be written'], ['success' => true, 'mode' => 'off', 'sequence' => []], ['b', 'a 30']]]; + $cases[] = ['userprefs.cfg: a failed write is reported, and the autostart file is still put in order', + (function () use ($sync, $folder, $userprefs, $fv3DockerManDir) { + try { return $sync(['f' => $folder('F', ['x'])], ['a', 'b', 'x'], $userprefs(['b', 'folder-f', 'a', 'x']), ['a', 'x', 'b'], fn() => chmod($fv3DockerManDir, 0555)); } + finally { chmod($fv3DockerManDir, 0755); } + })(), ["Unraid's saved Docker order (userprefs.cfg) could not be updated", ['b', 'x', 'a']]]; + $cases[] = ['userprefs.cfg and the autostart file both not written: both are reported', + (function () use ($sync, $folder, $userprefs, $fv3DockerManDir, $fv3tAutostart) { + try { return $sync(['f' => $folder('F', ['x'])], ['a', 'b', 'x'], $userprefs(['b', 'folder-f', 'a', 'x']), ['a', 'x', 'b'], function () use ($fv3DockerManDir, $fv3tAutostart) { chmod($fv3DockerManDir, 0555); chmod($fv3tAutostart, 0444); }); } + finally { chmod($fv3DockerManDir, 0755); } + })(), ["Unraid's saved Docker order (userprefs.cfg) could not be updated; the autostart file could not be written", ['a', 'x', 'b']]]; $cases[] = ['custom mode reports it too', $sync([], ['a', 'b'], null, ['b', 'a'], function () use ($configDir, $fv3tAutostart) { file_put_contents("$configDir/autostart.json", '{"mode":"custom","sequence":["a","b"]}'); chmod($fv3tAutostart, 0444); }), ['the autostart file could not be written', ['b', 'a']]]; } @@ -398,10 +467,10 @@ class DockerUpdate {} }; $cases[] = ['folder import, no saved order: the new folders first in the file\'s order, each after its members, then the rest A-Z', $importOrder(null, ['n2', 'n1'], ['a', 'b', 'c']), ['200', true, null, null, $userprefs(['folder-n2', 'b', 'folder-n1', 'c', 'a', 'folder-old'])]]; -$cases[] = ['folder import, a saved order: the new folders go on top in the file\'s order, the saved entries after them as they were', - $importOrder($keep, ['n2', 'n1'], ['a', 'b', 'c']), ['200', true, null, null, $userprefs(['folder-n2', 'folder-n1', 'alpha', 'zed'])]]; -$cases[] = ['folder import, a saved order that already places one of them: that one keeps its place, an id with no folder is not written', - $importOrder($userprefs(['alpha', 'folder-n1', 'zed']), ['n2', 'n1', 'ghost'], ['a', 'b', 'c']), ['200', true, null, null, $userprefs(['folder-n2', 'alpha', 'folder-n1', 'zed'])]]; +$cases[] = ['folder import, a saved order: the new folders go on top in the file\'s order with their members, the saved entries after them as they were', + $importOrder($keep, ['n2', 'n1'], ['a', 'b', 'c']), ['200', true, null, null, $userprefs(['folder-n2', 'folder-n1', 'b', 'alpha', 'zed'])]]; +$cases[] = ['folder import, a saved order that already places one of them: that one keeps its place with its member, an id with no folder is not written', + $importOrder($userprefs(['alpha', 'folder-n1', 'zed']), ['n2', 'n1', 'ghost'], ['a', 'b', 'c']), ['200', true, null, null, $userprefs(['folder-n2', 'alpha', 'folder-n1', 'b', 'zed'])]]; $cases[] = ['folder import, a saved order holding a list line: said, and left as it was', $importOrder("0=\"a\"\n1[]=\"b\"\n", ['n1'], ['a', 'b']), ['200', true, "Unraid's saved Docker order (userprefs.cfg) could not be read", "Unraid's saved Docker order (userprefs.cfg) could not be read", "0=\"a\"\n1[]=\"b\"\n"]]; $cases[] = ['folder import, a saved order and a folder config that will not parse: said, and the order left as it was', diff --git a/.github/scripts/php_tests.sh b/.github/scripts/php_tests.sh new file mode 100755 index 00000000..53457d56 --- /dev/null +++ b/.github/scripts/php_tests.sh @@ -0,0 +1,37 @@ +#!/bin/bash +# php -l over every plugin .php and .page file, then every .github/scripts/*_test.php, with every error level on. +# Every PHP process logs to one file, the endpoints the tests start included; a deprecation there fails like an error. +set -u +cd "$(dirname "$0")/../.." || exit 2 +status=0 +ini=$(mktemp -d) +trap 'rm -rf "$ini"' EXIT +log=$ini/errors.log +: > "$log" +printf 'error_reporting=-1\nlog_errors=1\nerror_log=%s\n' "$log" > "$ini/zz-fv3-tests.ini" +# An empty entry keeps PHP's own scan folder; this file loads after it, in every child process too +export PHP_INI_SCAN_DIR="${PHP_INI_SCAN_DIR:-}:$ini" +php -v | head -1 +php -r 'exit((error_reporting() & E_ALL) === E_ALL && ini_get("log_errors") ? 0 : 1);' || { echo 'FAILED: the error-log settings did not load'; exit 1; } + +deprecated() { # deprecated : fails the run when logged a deprecation + local seen; seen=$(grep -c 'Deprecated:' "$log") + if [ "$seen" -gt "${logged:-0}" ]; then + echo "FAILED: $1 ran into a deprecation:" + grep 'Deprecated:' "$log" | tail -n "$((seen - ${logged:-0}))" + status=1 + fi + logged=$seen +} + +while IFS= read -r -d '' f; do + php -l "$f" >/dev/null 2>&1 || { php -l "$f"; status=1; } +done < <(find src \( -name '*.php' -o -name '*.page' \) -print0) +deprecated 'php -l' + +for t in .github/scripts/*_test.php; do + echo "== $t" + php -d display_errors=stderr "$t" || status=1 + deprecated "$t" +done +exit $status diff --git a/.github/scripts/preview_pages_harness.js b/.github/scripts/preview_pages_harness.js index 0ad69e6f..0f094b0e 100644 --- a/.github/scripts/preview_pages_harness.js +++ b/.github/scripts/preview_pages_harness.js @@ -252,7 +252,7 @@ const dockerRow = (name, sid) => ``; // The Docker page drawn by the real createFolders() from docker.json's folders, Docker's container names and userprefs.cfg's values -const drawDocker = async ({ folders, containers, prefs, listed }) => { +const drawDocker = async ({ folders, containers, prefs, listed, api = false }) => { const sid = (i) => String(i).padStart(12, 'a'); const defs = Object.fromEntries(Object.entries(folders).map(([id, f]) => [id, { ...f, icon: '', settings: { ...FOLDER_SETTINGS }, hidden_preview: [], actions: [] }])); const saved = prefs.filter((n) => containers.includes(n)); @@ -261,7 +261,7 @@ const drawDocker = async ({ folders, containers, prefs, listed }) => { // false = 0); how it orders several missing ones among themselves is its own tie-break, so they stay as given const shown = missing.concat(saved); const info = {}; - const p = await makePage({ page: 'docker', api: false, answers: { + const p = await makePage({ page: 'docker', api, answers: { 'read.php': () => JSON.stringify(defs), 'read_order.php': () => JSON.stringify(prefs), 'read_info.php': () => JSON.stringify(info), @@ -305,6 +305,27 @@ const drawDocker = async ({ folders, containers, prefs, listed }) => { return { p, posts, failing, dropped, render, draw, rows, members, drawn }; }; +// Unraid's row for one VM (VMMachines.php), stopped, with its hidden detail row +const vmRow = (name, i) => `` + + `${name}
stopped
` + + ``; + +// The VMs page drawn by the real vm.js from vm.json's folders as given, the VMs (all shut off, uuid u0, u1, …) and userprefs.cfg's values +const drawVms = async ({ folders, names, prefs }) => { + const info = Object.fromEntries(names.map((n, i) => [n, { uuid: `u${i}`, state: 'shutoff', autostart: false }])); + const p = await makePage({ page: 'vm', api: false, answers: { + 'read.php': () => JSON.stringify(folders), 'read_order.php': () => JSON.stringify(prefs), + 'read_unraid_order.php': () => JSON.stringify(names), 'read_info.php': () => JSON.stringify(info), + } }); + fakeXhr(p.w, () => ({ status: 200, body: '' })); + p.w.loadlist_original = () => p.$('#kvm_list').html(names.map(vmRow).join('')); + p.g('loadlist()'); + p.g('fv3DrawList()'); + for (let i = 0; i < 50 && !(p.w.fv3DrawLog || []).some((d) => d.outcome === 'drawn') && !p.errors.length; i++) await p.tick(100); + if (p.errors.length || !(p.w.fv3DrawLog || []).some((d) => d.outcome === 'drawn')) throw new Error(`VMs page not drawn: ${p.errors.join('; ')}`); + return p; +}; + let failed = 0; const check = (label, ok, detail) => { if (ok) { console.log(`ok ${label}`); return; } @@ -347,4 +368,4 @@ const phpServer = (label, { containers = [] } = {}) => { return { config, server, cleanup: () => fs.rmSync(tmp, { recursive: true, force: true }) }; }; -module.exports = { makePage, drawDocker, fakeXhr, scriptHelpers, check, done, flush, read, phpServer }; +module.exports = { makePage, drawDocker, drawVms, fakeXhr, scriptHelpers, check, done, flush, read, phpServer, plugin, FOLDER_SETTINGS }; diff --git a/.github/scripts/settings_order_test.js b/.github/scripts/settings_order_test.js index 597f64da..91231ae8 100644 --- a/.github/scripts/settings_order_test.js +++ b/.github/scripts/settings_order_test.js @@ -4,7 +4,7 @@ const fs = require('fs'); const vm = require('vm'); const { JSDOM, VirtualConsole } = require('jsdom'); -const { makePage, drawDocker, fakeXhr, scriptHelpers, check, done, read, phpServer } = require('./preview_pages_harness.js'); +const { drawDocker, drawVms, scriptHelpers, check, done, read, phpServer } = require('./preview_pages_harness.js'); // The settings page's backup panel (the two folder tables) with the shipped code that fills them const part = (rel, from, to) => { @@ -50,22 +50,9 @@ const dockerPage = async ({ folders, names, prefs }) => { await page.draw(); return page.rows().filter((r) => r.startsWith('folder-')).map((r) => folders[r.slice(7)].name.trim()); }; -// The VMs page's folder order, by name as shown (trimmed), top-down: Unraid's rows (VMMachines.php) for the names as VMs -const vmRow = (name, i) => `` + - `${name}
stopped
` + - ``; +// The VMs page's folder order, by name as shown (trimmed), top-down const vmPage = async ({ folders, names, prefs }) => { - const info = Object.fromEntries(names.map((n, i) => [n, { uuid: `u${i}`, state: 'shutoff', autostart: false }])); - const p = await makePage({ page: 'vm', api: false, answers: { - 'read.php': () => JSON.stringify(folders), 'read_order.php': () => JSON.stringify(prefs), - 'read_unraid_order.php': () => JSON.stringify(names), 'read_info.php': () => JSON.stringify(info), - } }); - fakeXhr(p.w, () => ({ status: 200, body: '' })); - p.w.loadlist_original = () => p.$('#kvm_list').html(names.map(vmRow).join('')); - p.g('loadlist()'); - p.g('fv3DrawList()'); - for (let i = 0; i < 50 && !(p.w.fv3DrawLog || []).some((d) => d.outcome === 'drawn') && !p.errors.length; i++) await p.tick(100); - if (p.errors.length || !(p.w.fv3DrawLog || []).some((d) => d.outcome === 'drawn')) throw new Error(`VMs page not drawn: ${p.errors.join('; ')}`); + const p = await drawVms({ folders, names, prefs }); return p.$('#kvm_list > tr.folder').toArray().map((tr) => folders[tr.className.match(/folder-id-(\S+)/)[1]].name.trim()); }; diff --git a/.github/scripts/vendor.py b/.github/scripts/vendor.py index ef2e1f91..0437defb 100755 --- a/.github/scripts/vendor.py +++ b/.github/scripts/vendor.py @@ -95,12 +95,25 @@ def lf(data): return data if b'\0' in data[:8000] else data.replace(b'\r\n', b'\n') +def plugin_root(repo=ROOT): + """(vendor.json, the plugin folder its root names).""" + manifest = json.loads((repo / 'vendor.json').read_text()) + return manifest, (repo / manifest['root']).resolve() + + +def destination(manifest, root, rel): + """Where a bundled file lands; vendor.json may not point outside the plugin folder.""" + dest = (root / rel).resolve() + if not dest.is_relative_to(root): + raise SystemExit(f'vendor.json: {rel} is outside {manifest["root"]}') + return dest + + def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('mode', choices=('check', 'sync')) mode = parser.parse_args().mode - manifest = json.loads((ROOT / 'vendor.json').read_text()) - root = (ROOT / manifest['root']).resolve() + manifest, root = plugin_root() stale = [] for lib in manifest['libraries']: if 'npm' in lib: @@ -110,9 +123,7 @@ def main(): else: raise SystemExit(f'vendor.json: a library needs an npm or github source: {lib}') for src, data in files.items(): - dest = (root / lib['files'][src]).resolve() - if not dest.is_relative_to(root): - raise SystemExit(f'vendor.json: {lib["files"][src]} is outside {manifest["root"]}') + dest = destination(manifest, root, lib['files'][src]) data = lf(data) if dest.is_file() and dest.read_bytes() == data: continue diff --git a/.github/unraid-compat/.dockerignore b/.github/unraid-compat/.dockerignore new file mode 100644 index 00000000..3c3629e6 --- /dev/null +++ b/.github/unraid-compat/.dockerignore @@ -0,0 +1 @@ +node_modules diff --git a/.github/unraid-compat/Dockerfile b/.github/unraid-compat/Dockerfile new file mode 100644 index 00000000..1fae0965 --- /dev/null +++ b/.github/unraid-compat/Dockerfile @@ -0,0 +1,7 @@ +# The PHP an Unraid version ships, plus Unraid's gawk (the remove script uses RT), xz (packages), procps (pages call pgrep) +# and wget (the installer's downloader, so a download fails on the network, not a missing tool) +ARG PHP +FROM php:${PHP}-cli +# Unraid's /bin/sh is bash: its exec() strings use &> +RUN apt-get update && apt-get install -y --no-install-recommends gawk procps wget xz-utils && rm -rf /var/lib/apt/lists/* \ + && ln -sf bash /bin/sh diff --git a/.github/unraid-compat/browser_test.js b/.github/unraid-compat/browser_test.js new file mode 100644 index 00000000..113bc3b3 --- /dev/null +++ b/.github/unraid-compat/browser_test.js @@ -0,0 +1,422 @@ +// FV3's pages in Chromium and Firefox on the fake Unraid browser_test.sh started, 4 themes at 3 widths, screenshots kept. +// Usage: node browser_test.js [unraid-alone] +'use strict'; +const fs = require('fs'); +const path = require('path'); +const { chromium, firefox } = require('playwright-core'); +const { HOOKS, tokens, mentions, describe, applies, forView, missingHooks } = require('./hooks'); +const { widths, layoutState } = require('./layout'); + +const [version, shots, fakeToken, tree, mode = 'fv3'] = process.argv.slice(2); +// The Unraid-alone pass runs with FV3's page hooks moved away: it checks Unraid's hooks and measures what FV3's pass compares with +const ALONE = mode === 'unraid-alone'; +const MEASURED = `${shots}/unraid-alone.json`; +const base = 'http://127.0.0.1'; +const failures = []; +let checks = 0; +// Each failure prints as it happens, so a run the step's time limit kills still shows what failed; a missing hook prints once the +// views end, as one line for all its views +const check = (ok, what) => { checks++; if (!ok) { failures.push(what); console.log(`- ${what}`); } return ok; }; +const same = (got, want) => JSON.stringify(got) === JSON.stringify(want); + +const MEDIA = 'MediaAbCdEfGhIjKlMnO'; +const LAB = 'LabFoldAbCdEfGhIjKlM'; +const SPARE = 'Spare & '; +const THEMES = ['black', 'white', 'azure', 'gray']; +const SIZES = [[1440, 900], [768, 1024], [400, 860]]; + +// Unraid's services the fake does not run. A failed request, or a console error naming one, passes only on its path here. +const ABSENT = [ + [/^\/sub\//, "Unraid's nchan push channels: the fake runs no nchan, so the page's live updates never connect"], + [/^\/graphql$/, "the Unraid API: the fake has none, so FV3's fv3DetectApi takes the PHP path"], +]; +const pathOf = (url) => { try { return new URL(url).pathname; } catch { return ''; } }; +const absent = (url) => ABSENT.some(([re]) => re.test(pathOf(url))); +// Chromium refuses every unload handler: Unraid's dynamix.js adds some, and 7.0's inline page scripts. From dynamix.js or the +// page itself the refusal is Unraid's while FV3's shipped files add none, which fv3Unload checks +const plugin = `${__dirname}/../../src/folder.view3/usr/local/emhttp/plugins/folder.view3`; +const fv3Unload = fs.readdirSync(plugin, { recursive: true }).filter((f) => /\.(js|page|php)$/.test(f)) + .filter((f) => /(? { + if (fv3Unload.length || m.text() !== 'Permissions policy violation: unload is not allowed in this document.') return false; + return ['/webGui/javascript/dynamix.js', `/${page}`].includes(pathOf(m.location()?.url)); +}; +// A console error from FV3's files is never excused; elsewhere, by its source or a URL in its text (trailing punctuation dropped) +const excused = (m, page) => { + const from = m.location()?.url || ''; + if (pathOf(from).startsWith('/plugins/folder.view3/')) return false; + const urls = [from, ...(m.text().match(/\b(?:https?|wss?):\/\/[^\s'"]+/g) || []).map((u) => u.replace(/[.,;:)]+$/, ''))]; + return urls.some(absent) || unraidUnload(m, page); +}; + +// Each page's own rows, tiles and folders as the user sees them, read in the page: a folder as "name [members it shows]", a +// container or VM by its name +const READ = { + Docker: () => window.fv3Test.tableRows('docker_list', 'ct-name'), + VMs: () => { + // Each VM's detail row stays right after its VM, wherever FV3 moved the pair + const details = [...document.querySelectorAll('#kvm_list tr[child-id]')]; + const paired = details.filter((d) => d.previousElementSibling?.getAttribute('parent-id') === d.getAttribute('child-id')).length; + return { rows: window.fv3Test.tableRows('kvm_list', 'vm-name'), details: `${paired} of ${details.length} detail rows after their VM` }; + }, + Dashboard: () => { + const { seen, name, members } = window.fv3Test; + const tiles = (view) => [...document.querySelectorAll(`tbody#${view} > tr.updated > td > :not(.fv3-fullwidth-panel)`)].filter(seen).map((tile) => { + if (!tile.classList.contains('folder-showcase-outer')) return name(tile); + const mine = members(tile); + return `${tile.querySelector('.fv3-folder-appname').innerText.trim()} [${mine.map(name).join(', ')}] shows [${mine.filter(seen).map(name).join(', ')}]`; + }); + return { docker: tiles('docker_view'), vms: tiles('vm_view') }; + }, +}; + +// fake_unraid.sh's setup in Unraid's saved order: Media holds app-a by hand and app-b by its label, Tools takes tool-c by regex, +// app-d is loose, Lab holds Debian 12. The empty folder made below is in no saved order, so FV3 draws it first +const WANT = { + Docker: { + shut: [`${SPARE} []`, 'Media [app-a, app-b]', 'Tools [tool-c]', 'app-d'], + open: [`${SPARE} []`, 'Media [app-a, app-b]', 'app-a', 'app-b', 'Tools [tool-c]', 'app-d'], + }, + VMs: { + shut: { rows: ['Lab [Debian 12]', 'Home Assistant', 'Windows 11'], details: '3 of 3 detail rows after their VM' }, + open: { rows: ['Lab [Debian 12]', 'Debian 12', 'Home Assistant', 'Windows 11'], details: '3 of 3 detail rows after their VM' }, + }, + Dashboard: { + shut: { docker: [`${SPARE} [] shows []`, 'Media [app-a, app-b] shows []', 'Tools [tool-c] shows []', 'app-d'], vms: ['Lab [Debian 12] shows []', 'Home Assistant', 'Windows 11'] }, + open: { docker: [`${SPARE} [] shows []`, 'Media [app-a, app-b] shows [app-a, app-b]', 'Tools [tool-c] shows []', 'app-d'], vms: ['Lab [Debian 12] shows [Debian 12]', 'Home Assistant', 'Windows 11'] }, + }, +}; +// FV3's events for a finished draw; the folders each page opens, their events once open, and how a user opens them +const DRAWN = { Docker: ['docker-post-folders-creation'], VMs: ['vm-post-folders-creation'], Dashboard: ['docker-post-folders-creation', 'vm-post-folders-creation'] }; +const OPEN = { + Docker: ['Media', ['docker-post-folder-expansion'], (p) => press(p.locator(`button.dropDown-${MEDIA}`), "Media's chevron")], + VMs: ['Lab', ['vm-post-folder-expansion'], (p) => press(p.locator(`button.dropDown-${LAB}`), "Lab's chevron")], + // Each by its tile's menu, the item FV3's lang pack labels 'expand'. Unraid fades a used menu out and hides every menu as each + // fade ends, so a user's next menu waits for the fade (jQuery's :animated): one opened during it closes at once + Dashboard: ['Media and Lab', ['docker-post-folder-expansion', 'vm-post-folder-expansion'], async (p) => { + const expand = await p.evaluate(() => $.i18n('expand')); + for (const [id, folder] of [[MEDIA, 'Media'], [LAB, 'Lab']]) { + await press(p.locator(`#folder-id-${id}`), `${folder}'s tile`); + await press(p.locator('ul.dropdown-context:visible li > a', { hasText: expand }), `${folder}'s menu's Expand`); + await p.waitForFunction(() => !$('ul.dropdown-context:animated').length && [...document.querySelectorAll('ul.dropdown-context')].every((u) => !u.offsetParent), null, { timeout: 5000 }) + .catch(() => { throw new Error(`${folder}'s menu did not close within 5 s`); }); + } + }], +}; +// FV3's Dashboard layouts for the Docker and VM cards in each theme, so every layout is drawn at every width +const LAYOUTS = [['classic', 'embossed'], ['fullwidth', 'classic'], ['accordion', 'fullwidth'], ['inset', 'accordion']]; +// Unraid's own list on each page once its rows load: a loading placeholder row is not one +const LISTED = { Docker: '#docker_list > tr.sortable', VMs: '#kvm_list > tr.sortable', Dashboard: 'tbody#docker_view > tr.updated' }; +// The first member of Media, and where a click opens its advanced preview once Media is open (fullwidth: in the panel after its row) +const PREVIEWED = 'app-a'; +const PREVIEW = { Docker: `tr.folder-id-${MEDIA} .folder-preview span.hand[id^="folder-preview-"]`, Dashboard: `.folder-showcase-${MEDIA} > span.outer > span.hand, .fv3-fullwidth-panel[data-folder-name="Media"] > span.outer > span.hand` }; + +// Before the page's scripts: records FV3's draw and expansion events as they fire, and the readers' helpers. A display: contents +// element (FV3's Dashboard folders) is seen when a child is; a name is .inner's text before its first line break, drawn or not +function testHelpers() { + const events = []; + const dispatch = EventTarget.prototype.dispatchEvent; + EventTarget.prototype.dispatchEvent = function (event) { + if (/-post-folders?-(creation|expansion)$/.test(event.type)) events.push(event.type); + return dispatch.call(this, event); + }; + // Seen: drawn, not transparent or hidden, and not clipped away by an overflow: hidden/clip ancestor (scrolling ones are reachable) + const seen = (el) => { + if (getComputedStyle(el).display === 'contents') return [...el.children].some(seen); + if (!el.checkVisibility({ opacityProperty: true, visibilityProperty: true })) return false; + let { left, right, top, bottom } = el.getBoundingClientRect(); + for (let a = el.parentElement; a && right > left && bottom > top; a = a.parentElement) { + const style = getComputedStyle(a), box = a.getBoundingClientRect(); + if (/hidden|clip/.test(style.overflowX)) { left = Math.max(left, box.left); right = Math.min(right, box.right); } + if (/hidden|clip/.test(style.overflowY)) { top = Math.max(top, box.top); bottom = Math.min(bottom, box.bottom); } + } + return right > left && bottom > top; + }; + // Every FV3 banner shown, kept after the page removes it + const banners = []; + new MutationObserver((changes) => changes.forEach((c) => c.addedNodes.forEach((n) => { + if (n.nodeType === 1 && n.matches('.fv3-banner, :has(.fv3-banner)')) banners.push(n.textContent.trim()); + }))).observe(document, { childList: true, subtree: true }); + const name = (outer) => { + const before = []; + for (const node of outer.querySelector('.inner').childNodes) { if (node.nodeName === 'BR') break; before.push(node.textContent); } + return before.join('').trim(); + }; + // A Dashboard folder's member tiles wherever its layout puts them: in the folder, or the fullwidth layout's panel in its row + const members = (folder) => { + const named = folder.querySelector('.folder-showcase')?.dataset.folderName; + const panel = [...folder.parentElement.querySelectorAll(':scope > .fv3-fullwidth-panel')].find((p) => p.dataset.folderName === named); + return [...folder.querySelectorAll('.folder-storage > span.outer, .folder-showcase > span.outer'), ...(panel?.querySelectorAll(':scope > span.outer') || [])]; + }; + const tableRows = (list, nameCell) => [...document.querySelectorAll(`#${list} > tr`)].filter(seen).map((row) => row.classList.contains('folder') + ? `${row.querySelector('.folder-appname').innerText.trim()} [${[...row.querySelectorAll('.folder-preview span.outer')].filter(seen).map(name).join(', ')}]` + : name(row.querySelector(`td.${nameCell} span.outer`))); + window.fv3Test = { events, seen, name, members, tableRows, banners }; +} + +// The page's reading once two in a row agree: FV3 animates an opening and Unraid may reload a list after the first draw +async function settled(page, read, arg) { + let last = null; + for (let i = 0; i < 40; i++) { + const now = await page.evaluate(read, arg); + if (last !== null && same(now, last)) return now; + last = now; + await page.waitForTimeout(250); + } + throw new Error(`still changing after 10 s: ${JSON.stringify(last)}`); +} + +// Unraid's own page frame. A click it covers is dispatched to the target; any other cover fails the view +const FRAME = [ + ['#menu', "7.0's azure and gray sidebar: fixed at the left edge, it lies over the page below desktop width"], +]; + +// A user's click, the target scrolled to the middle until it stays there: a folder opening above it can still move it +async function press(locator, what) { + const middle = () => locator.evaluate((el) => { + el.scrollIntoView({ block: 'center', inline: 'center' }); + const r = el.getBoundingClientRect(); + return `${Math.round(r.top)},${Math.round(r.left)}`; + }, null, { timeout: 10000 }).catch(() => { throw new Error(`${what} is not on the page within 10 s`); }); + for (let at = await middle(), i = 0; i < 20; i++) { + await locator.page().waitForTimeout(100); + if (at === (at = await middle())) break; + } + try { + await locator.click({ timeout: 5000 }); + } catch (e) { + // Playwright's own retries scroll the target elsewhere: what covers it is read back in the middle, beside Playwright's reason + const why = e.message.split('\n').find((l) => l.includes('intercepts pointer events'))?.trim().replace(/^- /, '') || e.message.split('\n')[0]; + await middle().catch(() => {}); + const cover = await locator.evaluate((el, frame) => { + if (!window.fv3Test.seen(el)) return null; + const r = el.getBoundingClientRect(); + const top = document.elementFromPoint(r.left + r.width / 2, r.top + r.height / 2); + if (!top || el.contains(top)) return null; + if (frame.some((selector) => top.closest(selector))) return ''; + const n = top.closest('[id], [class]') || top; + return `${n.tagName.toLowerCase()}${n.id ? `#${n.id}` : ''}${[...n.classList].map((c) => `.${c}`).join('')}`; + }, FRAME.map(([selector]) => selector), { timeout: 5000 }).catch(() => undefined); + if (cover === undefined) throw new Error(`${what} could not be clicked, then left the page: ${why}`); + if (cover === null) throw new Error(`${what} could not be clicked: ${why}`); + if (cover) throw new Error(`${what} is covered by ${cover} (${why})`); + await locator.dispatchEvent('click'); + } +} + +const waitEvents = (page, names) => page.waitForFunction((names) => names.every((n) => window.fv3Test.events.includes(n)), names, { timeout: 20000 }); + +// The popup's markup is indented HTML: with each blank text node set to one space nothing may move, as it does where Unraid's +// tooltip box keeps line breaks (7.4's .tooltipster-content white-space: pre-line) and FV3 does not reset it +function popupState(name) { + const base = [...document.querySelectorAll('.tooltipster-base')].find((b) => b.getClientRects().length && b.querySelector('.preview-outbox')); + if (!base) return { error: 'no advanced preview open' }; + const box = base.querySelector('.preview-outbox'); + const elements = [box, ...box.querySelectorAll('*')]; + const boxes = () => elements.map((e) => { const r = e.getBoundingClientRect(); return [r.top, r.left, r.width, r.height].map(Math.round).join(','); }); + const before = boxes(); + const blanks = []; + const walker = document.createTreeWalker(box, NodeFilter.SHOW_TEXT); + for (let node; (node = walker.nextNode());) if (node.data.trim() === '' && node.data !== ' ') blanks.push([node, node.data]); + blanks.forEach(([node]) => { node.data = ' '; }); + const after = boxes(); + blanks.forEach(([node, data]) => { node.data = data; }); + const moved = elements.filter((e, i) => before[i] !== after[i]); + // Within the window's width, and nowhere the page cannot scroll to: a clicked popup stays open and in place while the page scrolls + const r = base.getBoundingClientRect(), top = r.top + scrollY, bottom = r.bottom + scrollY, page = document.documentElement.scrollHeight; + const fits = r.left >= 0 && r.right <= innerWidth + 1 && top >= 0 && bottom <= page + 1; + return { + seen: window.fv3Test.seen(box), + named: box.innerText.includes(name), + blanks: blanks.length, + moved: moved.length ? `${moved.length} elements, first ${moved.slice(0, 3).map((e) => `${e.tagName.toLowerCase()}.${[...e.classList].join('.')}`).join(', ')}` : '', + inside: fits ? '' : `left ${Math.round(r.left)}, right ${Math.round(r.right)} of the window's ${innerWidth}; top ${Math.round(top)}, bottom ${Math.round(bottom)} of the page's ${page}`, + }; +} + +// A page whose script never yields stalls page.evaluate, which has no timeout: past this the view fails, not the step's time limit +const VIEW_LIMIT = 90000; + +// One page in one browser, theme and width, through one pass's steps; false when it ended before the page drew +async function view(browser, kind, theme, [width, height], page, steps) { + const v = { where: `${kind} ${theme} ${width}px ${page}`, file: `${shots}/${kind}/${theme}-${width}-${page}${ALONE ? '-unraid-alone' : ''}`, theme, width, page, drew: false }; + const { where, file } = v; + const errors = []; + let context, tab, timer; + try { + context = await browser.newContext({ viewport: { width, height } }); + await context.addInitScript(testHelpers); + tab = await context.newPage(); + tab.on('pageerror', (e) => errors.push(`page error: ${e.message}`)); + tab.on('console', (m) => { if (m.type() === 'error' && !excused(m, page)) errors.push(`console error: ${m.text()} (${m.location()?.url || 'no source'})`); }); + tab.on('response', (r) => { if (r.status() >= 400 && !absent(r.url())) errors.push(`${r.status()} for ${r.url()}`); }); + tab.on('requestfailed', (r) => { if (!absent(r.url())) errors.push(`request failed: ${r.url()} ${r.failure()?.errorText}`); }); + await Promise.race([steps(tab, v), new Promise((_, reject) => { timer = setTimeout(() => reject(new Error(`the view took over ${VIEW_LIMIT / 1000} s`)), VIEW_LIMIT); })]); + } catch (e) { + check(false, `${where}: ${e.message.split('\n')[0]}`); + await tab?.screenshot({ path: `${file}-failed.png`, fullPage: true, timeout: 10000 }).catch(() => {}); + } finally { + clearTimeout(timer); + } + // Unraid alone must run clean too: an error there is Unraid's or the fake's, and its readings would not hold + check(!errors.length, `${where}:\n ${[...new Set(errors)].join('\n ')}`); + await context?.close().catch(() => {}); + return v.drew; +} + +// The page as the theme's stylesheet draws it, or the view fails +async function load(tab, { where, page, theme }) { + const loaded = await tab.goto(`${base}/${page}`); + if (loaded?.status() !== 200) throw new Error(`/${page} answered ${loaded?.status()}`); + check(await tab.evaluate((t) => [...document.querySelectorAll('link[rel=stylesheet]')] + .some((l) => new RegExp(`/webGui/styles/(?:themes/|default-)${t}\\.css`).test(l.getAttribute('href'))), theme), `${where}: the ${theme} theme's stylesheet is not on the page`); +} + +// Unraid alone: its list drawn, the widths FV3's pass compares with, and which of FV3's hooks it lacks +const measured = {}, lacking = new Map(); +async function aloneSteps(tab, v) { + await load(tab, v); + // An FV3 page hook left in place would compare FV3 with itself + const fv3 = await tab.$('script[src*="/plugins/folder.view3/"], link[href*="/plugins/folder.view3/"]'); + if (!check(!fv3, `${v.where}: FV3 is on the page, so this is not Unraid alone and nothing was read`)) return; + v.drew = await tab.waitForSelector(LISTED[v.page], { timeout: 20000 }).then(() => true, () => false); + if (!check(v.drew, `${v.where}: Unraid alone drew no ${LISTED[v.page]} within 20 s, so its hooks and widths were not read`)) return; + measured[v.where] = await settled(tab, widths); + const hooks = forView(v.page, version); + for (const i of await settled(tab, missingHooks, hooks)) lacking.set(hooks[i], [...(lacking.get(hooks[i]) || []), v.where]); +} + +// FV3's pass: drawn in the saved order, folders opened as a user opens them, the layout rules, the advanced preview, no banners +async function fv3Steps(tab, v) { + const { where, file, page } = v; + await load(tab, v); + await waitEvents(tab, DRAWN[page]).catch(() => { throw new Error(`FV3 did not finish drawing within 20 s (${DRAWN[page].join(', ')})`); }); + v.drew = true; + if (page === 'Dashboard') { + const want = LAYOUTS[THEMES.indexOf(v.theme)]; + const drawn = await tab.evaluate(() => ['docker_view', 'vm_view'].map((t) => [...(document.querySelector(`tbody#${t} > tr.updated > td`)?.classList || [])] + .find((c) => c.startsWith('fv3-layout-'))?.slice('fv3-layout-'.length) || 'no layout')); + check(same(drawn, want), `${where}: the Docker and VM cards are drawn in ${drawn.join(' and ')}, not ${want.join(' and ')}`); + } + let got = await settled(tab, READ[page]); + check(same(got, WANT[page].shut), `${where}: drawn as ${JSON.stringify(got)}, not ${JSON.stringify(WANT[page].shut)}`); + const [folder, opened, open] = OPEN[page]; + await open(tab); + await waitEvents(tab, opened).catch(() => { throw new Error(`${folder} did not open within 20 s (${opened.join(', ')})`); }); + got = await settled(tab, READ[page]); + check(same(got, WANT[page].open), `${where}: with ${folder} open, drawn as ${JSON.stringify(got)}, not ${JSON.stringify(WANT[page].open)}`); + const layout = await settled(tab, layoutState, page); + if (layout.error) throw new Error(layout.error); + for (const problem of Object.values(layout.rules)) check(!problem, `${where}: ${problem}`); + // Only where Unraid alone fits: where it already scrolls sideways, FV3 may be wider + const now = await tab.evaluate(widths), was = measured[where]; + if (was) check((!was.page || now.page) && (was.table !== true || now.table), `${where}: FV3 adds sideways scrolling Unraid alone does not have: ${now.sizes} (Unraid alone ${was.sizes})`); + else if (Object.keys(measured).length) check(false, `${where}: widths not compared, the Unraid-alone pass did not measure this view`); + await tab.screenshot({ path: `${file}.png`, fullPage: true }); + if (PREVIEW[page]) { + await press(tab.locator(PREVIEW[page]).first(), `${PREVIEWED}'s preview icon`); + await tab.waitForSelector('.tooltipster-base .preview-outbox', { state: 'visible', timeout: 10000 }) + .catch(() => { throw new Error('the advanced preview did not open within 10 s'); }); + // The popup fades in and draws its graph after it opens + const popup = await settled(tab, popupState, PREVIEWED); + if (popup.error) throw new Error(popup.error); + // Playwright's visible ignores opacity: a popup tooltipster leaves transparent opens to nothing + if (!popup.seen) throw new Error('the advanced preview opened but cannot be seen'); + check(popup.named, `${where}: the advanced preview is not ${PREVIEWED}'s`); + check(!popup.moved, `${where}: the advanced preview draws its markup's line breaks: ${popup.moved} moved when ${popup.blanks} blank text nodes became one space`); + check(!popup.inside, `${where}: the advanced preview is out of reach: ${popup.inside}`); + await tab.screenshot({ path: `${file}-preview.png` }); + } + const banners = await tab.evaluate(() => window.fv3Test.banners); + check(!banners.length, `${where}: FV3 showed a banner: ${banners.join(' | ')}`); +} + +// A save as the page posts it; anything but a 200, or an FV3 answer without FV3's header, stops the run +async function post(request, route, form, what) { + const r = await request.post(`${base}${route}`, { form }); + if (r.status() !== 200 || (route.includes('folder.view3') && !r.headers()['x-fv3-answer'])) throw new Error(`${what}: status ${r.status()}, ${(await r.text()).slice(0, 200)}`); +} + +// FV3's pass starts from: Media and the Dashboard with the advanced preview on, and an empty folder +async function fv3Setup(request, token) { + // Media as fake_unraid.sh saved it + const saved = await request.get(`${base}/plugins/folder.view3/server/read.php?type=docker`); + const media = saved.ok() ? (await saved.json())[MEDIA] : undefined; + if (!media) throw new Error(`no Media folder to turn the advanced preview on: status ${saved.status()}`); + await post(request, '/plugins/folder.view3/server/update.php', { type: 'docker', id: MEDIA, csrf_token: token, + content: JSON.stringify({ ...media, settings: { ...media.settings, context: 2 } }) }, "Media's advanced preview"); + await post(request, '/plugins/folder.view3/server/update_settings.php', { key: 'dashboard_context', value: '2', csrf_token: token }, "the Dashboard's advanced preview"); + // Named in characters a page must escape + await post(request, '/plugins/folder.view3/server/create.php', { type: 'docker', csrf_token: token, + content: JSON.stringify({ name: SPARE, icon: '', regex: '', containers: [], settings: { preview: 1 }, actions: [] }) }, 'the empty folder'); +} + +// Before the views: each hook's FV3 file still mentions it, and each endpoint is a webGUI file, called from Unraid's pages +// where FV3 intercepts it +function hookFiles(webgui) { + for (const h of HOOKS) { + const text = fs.existsSync(`${plugin}/${h.file}`) ? fs.readFileSync(`${plugin}/${h.file}`, 'utf8') : null; + const gone = text === null ? [h.file] : tokens(h).filter((t) => !mentions(text, t)); + check(!gone.length, `hooks.js names ${h.file} for ${describe(h)}, but FV3 no longer has ${gone.join(', ')} there`); + } + const plugins = `${webgui}/emhttp/plugins`; + const pages = fs.readdirSync(plugins, { recursive: true }).filter((f) => /\.(page|php|js)$/.test(f)).map((f) => [`/plugins/${f}`, fs.readFileSync(`${plugins}/${f}`, 'utf8')]); + for (const h of HOOKS.filter((x) => x.url)) { + const file = h.url.replace(/^\/webGui\//, '/plugins/dynamix/'); + if (!check(fs.existsSync(`${webgui}/emhttp${file}`), `Unraid ${version} lacks ${h.url}, which ${h.file} needs: ${h.loses}`) || !h.intercepts) continue; + check(pages.some(([f, text]) => f !== file && text.includes(h.url)), `Unraid ${version}'s pages no longer call ${h.url}, which ${h.file} intercepts: ${h.loses}`); + } +} + +(async () => { + const webgui = path.resolve(__dirname, '../..', tree || ''); + if (!fs.existsSync('/.dockerenv') || !fakeToken || !fs.existsSync(`${webgui}/emhttp`)) { console.error('Run browser_test.sh: this needs the fake Unraid it starts and its webGUI tree'); process.exit(2); } + if (!ALONE) check(!fv3Unload.length, `FV3 adds an unload handler, which Chromium never runs: ${fv3Unload.join(', ')}`); + const browsers = { chromium: await chromium.launch(), firefox: await firefox.launch() }; + const setup = await browsers.chromium.newContext(); + const tab = await setup.newPage(); + const loaded = await tab.goto(`${base}/Docker`); + if (loaded?.status() !== 200) throw new Error(`/Docker answered ${loaded?.status()}, so the setup could not read Unraid's token`); + // lib.sh gives every fake this token and a real Unraid never has it, so no real server on this loopback is written to + const token = await tab.evaluate(() => typeof csrf_token === 'string' ? csrf_token : ''); + if (token !== fakeToken) { console.error(`127.0.0.1 is not the fake Unraid browser_test.sh started (token "${token}")`); process.exit(2); } + if (!ALONE) { + // Without them only the width comparison is lost; FV3's pass still runs + try { Object.assign(measured, JSON.parse(fs.readFileSync(MEASURED, 'utf8'))); } catch (e) { check(false, `the Unraid-alone pass left no measurements, so no widths are compared: ${e.message}`); } + await fv3Setup(setup.request, token); + } + if (ALONE) hookFiles(webgui); + let undrawn = 0; + try { + for (const [i, theme] of THEMES.entries()) { + // As Unraid's Display Settings saves it + await post(setup.request, '/update.php', { '#file': 'dynamix/dynamix.cfg', '#section': 'display', theme, csrf_token: token }, `the ${theme} theme`); + if (!ALONE) for (const [card, layout] of [['docker', LAYOUTS[i][0]], ['vm', LAYOUTS[i][1]]]) { + await post(setup.request, '/plugins/folder.view3/server/update_settings.php', { key: `dashboard_${card}_layout`, value: layout, csrf_token: token }, `the Dashboard's ${layout} ${card} card`); + } + for (const [kind, browser] of Object.entries(browsers)) { + fs.mkdirSync(`${shots}/${kind}`, { recursive: true }); + for (const size of SIZES) for (const page of ['Docker', 'VMs', 'Dashboard']) { + undrawn = (await view(browser, kind, theme, size, page, ALONE ? aloneSteps : fv3Steps)) ? 0 : undrawn + 1; + if (undrawn === 3) throw new Error(`3 views in a row ended before ${ALONE ? 'Unraid' : 'FV3'} drew, so the rest were not run`); + } + } + } + } finally { + // Each missing hook once, with the views it was missing from, also when the run stops early + for (const [h, where] of lacking) check(false, `Unraid ${version} lacks ${describe(h)} on ${h.page} (${where.length} views, first ${where[0]}), which ${h.file} needs: ${h.loses}`); + } + await setup.close(); + for (const browser of Object.values(browsers)) await browser.close(); + if (ALONE) fs.writeFileSync(MEASURED, JSON.stringify(measured)); + const what = ALONE ? `Unraid alone has the ${HOOKS.filter((h) => applies(h, version)).length} hooks FV3 relies on, ${THEMES.length} themes at ${SIZES.length} widths in Chromium and Firefox` + : `FV3's pages in Chromium and Firefox, ${THEMES.length} themes at ${SIZES.length} widths`; + if (failures.length) { + console.log(`FAILED [Unraid ${version}]: ${failures.length} of ${checks} checks (${ALONE ? 'Unraid alone' : "FV3's pages"}), each listed above`); + process.exit(1); + } + console.log(`Unraid ${version}: ${what}, passed (${checks} checks)`); +})().catch((e) => { + console.log(`FAILED [Unraid ${version}]: ${e.message.split('\n')[0]}${failures.length ? ` (after ${failures.length} failed checks, listed above)` : ''}`); + process.exit(1); +}); diff --git a/.github/unraid-compat/browser_test.sh b/.github/unraid-compat/browser_test.sh new file mode 100644 index 00000000..5624e2c0 --- /dev/null +++ b/.github/unraid-compat/browser_test.sh @@ -0,0 +1,40 @@ +#!/bin/bash +# FV3's pages in Chromium and Firefox: the fake Unraid in a container with no network, the browsers in one that joins its network. +# From the repo root, after npm ci here: browser_test.sh +set -u +image=$1; tree=$2; version=$3; shots=$4 +# Renovate moves this with playwright-core in package.json: the driver only runs the browsers built for its own version +browsers=mcr.microsoft.com/playwright:v1.65.1-noble@sha256:1507cbfc550a63a6b7396791ca4f00df17beb56342d2447b3f106c306f525579 +fail() { echo "FAILED [Unraid $version]: $*"; exit 1; } +# Removes only the container this run made; one a killed run leaves stops within the hour (its sleep 3600) +fake= +trap '[ -z "$fake" ] || docker rm -f "$fake" >/dev/null 2>&1' EXIT + +driver=$(node -p "require('./.github/unraid-compat/node_modules/playwright-core/package.json').version" 2>/dev/null) +[[ $browsers == *":v$driver-"* ]] || fail "playwright-core ${driver:-is not installed (npm ci in .github/unraid-compat)}, but the browsers are ${browsers%@*}" +fake=$(docker run -d --network none -v "$PWD:/repo" -w /repo "$image" \ + bash -c 'bash .github/unraid-compat/fake_unraid.sh "$0" "$1" && touch /run/fv3-fake-up && exec sleep 3600' "$tree" "$version") \ + || fail 'the fake Unraid container did not start' +end=$((SECONDS + 120)) +until docker exec "$fake" test -f /run/fv3-fake-up 2>/dev/null; do + [ "$(docker inspect -f '{{.State.Running}}' "$fake" 2>/dev/null)" = true ] || fail "the fake Unraid stopped: $(docker logs "$fake" 2>&1 | tail -20)" + [ $SECONDS -lt $end ] || fail 'the fake Unraid was not up within 2 minutes' + sleep 0.5 +done +# browser_test.js writes only once the page it reaches carries this token +token=$(docker exec "$fake" php -d auto_prepend_file= -r 'echo parse_ini_file("/var/local/emhttp/var.ini")["csrf_token"] ?? "";') +[ -n "$token" ] || fail "no csrf_token in the fake's var.ini" + +mkdir -p "$shots" || fail "cannot make $shots" +browse() { docker run --rm --network container:"$fake" --shm-size=1g --user "$(id -u):$(id -g)" -e HOME=/tmp -v "$PWD:/repo:ro" \ + -v "$(cd "$shots" && pwd):/shots" -w /repo/.github/unraid-compat "$browsers" node browser_test.js "$version" /shots "$token" "$tree" "$@"; } +# Unraid alone first, with FV3's page hooks (folder.view3..page) out of its plugin folder; FV3's pass then compares with it +docker exec "$fake" bash -c 'mkdir -p /run/fv3-off && mv /usr/local/emhttp/plugins/folder.view3/folder.view3.*.page /run/fv3-off/' || fail "FV3's pages could not be moved aside" +# FV3's pass must never compare with an earlier run's measurements +rm -f "$shots/unraid-alone.json" || fail "the last run's measurements could not be removed from $shots" +browse unraid-alone; status=$? +docker exec "$fake" bash -c 'mv /run/fv3-off/* /usr/local/emhttp/plugins/folder.view3/' || fail "FV3's pages could not be put back" +browse || status=1 +# The logs hold what the pages asked of Unraid's PHP and the fakes, for every run above +docker exec "$fake" php -d auto_prepend_file= -d display_errors=stderr .github/unraid-compat/fake_logs.php "$version" || status=1 +exit $status diff --git a/.github/unraid-compat/fake/connect_state.php b/.github/unraid-compat/fake/connect_state.php new file mode 100644 index 00000000..6ed92c36 --- /dev/null +++ b/.github/unraid-compat/fake/connect_state.php @@ -0,0 +1,3 @@ +> /var/log/fv3-fakes.log; exit 1 ;; +esac diff --git a/.github/unraid-compat/fake/docker.php b/.github/unraid-compat/fake/docker.php new file mode 100644 index 00000000..108a1514 --- /dev/null +++ b/.github/unraid-compat/fake/docker.php @@ -0,0 +1,91 @@ +` is the docker CLI. A request it does not know is logged as UNKNOWN and refused. +$d = json_decode((string)@file_get_contents(__DIR__ . '/../fixtures/docker.json'), true); +if (!is_array($d)) { fwrite(STDERR, "fixtures/docker.json is unreadable\n"); exit(2); } + +function fv3d_log(string $line): void { file_put_contents('/var/log/fv3-fakes.log', "docker $line\n", FILE_APPEND | LOCK_EX); } + +function fv3d_container(array $d, string $ref): ?array { + foreach ($d['inspect'] as $ct) { + if (ltrim($ct['Name'], '/') === ltrim($ref, '/') || (strlen($ref) >= 12 && str_starts_with($ct['Id'], $ref))) return $ct; + } + return null; +} + +function fv3d_running(array $d): array { return array_values(array_filter($d['containers'], fn($c) => $c['State'] === 'running')); } + +function fv3d_answer(array $d, string $method, string $target): array { + $path = (string)parse_url($target, PHP_URL_PATH); + parse_str((string)parse_url($target, PHP_URL_QUERY), $query); + // Only `all` on the two lists is answered; a filter, any other parameter or all[] (Docker's key `all[]`, not `all`) is a + // request this fake does not know + $lists = in_array($path, ['/containers/json', '/images/json'], true); + if ($method !== 'GET' || array_diff(array_keys($query), $lists ? ['all'] : []) || array_filter($query, 'is_array')) return [null, null]; + // Docker's httputils.BoolValue: empty, 0, no, false and none are false, any other value true + $all = !in_array(strtolower(trim($query['all'] ?? '')), ['', '0', 'no', 'false', 'none'], true); + if ($path === '/containers/json') return [200, $all ? $d['containers'] : fv3d_running($d)]; + if (preg_match('#^/containers/([^/]+)/json$#', $path, $m)) { + $ct = fv3d_container($d, $m[1]); + return $ct ? [200, $ct] : [404, ['message' => "No such container: {$m[1]}"]]; + } + if ($path === '/images/json') return [200, $d['images']]; + if (preg_match('#^/images/(.+)/json$#', $path, $m)) { + foreach ($d['image_inspect'] as $id => $img) { + if ($m[1] === $id || in_array($m[1], $img['RepoTags'] ?? [], true)) return [200, $img]; + } + return [404, ['message' => "No such image: {$m[1]}"]]; + } + return [null, null]; +} + +// The docker CLI lines Unraid and FV3 run, matched argument by argument as the shell split them (--format too), and answered +// in the CLI's own output format +function fv3d_cli(array $d, array $args): ?array { + if ($args === ['network', 'ls', '--format={{.Name}}={{.Driver}}']) return array_map(fn($n) => "{$n['Name']}={$n['Driver']}", $d['networks']); + if ($args === ['ps', '-q', '--no-trunc']) return array_column(fv3d_running($d), 'Id'); + // A label's presence only, as Unraid filters: label=key=value is a form this fake does not answer + if (count($args) === 5 && [$args[0], $args[1], $args[3], $args[4]] === ['ps', '--filter', '--format', '{{.Names}}'] && preg_match('/^label=([^\s=]+)$/', $args[2], $m)) { + return array_map(fn($c) => ltrim($c['Names'][0], '/'), array_values(array_filter(fv3d_running($d), fn($c) => isset($c['Labels'][$m[1]])))); + } + if ($args === ['stats', '--no-stream', '--format={{.ID}};{{.CPUPerc}};{{.MemUsage}}']) { + return array_map(fn($c) => substr($c['Id'], 0, 12) . ';0.50%;12.5MiB / 7.7GiB', fv3d_running($d)); + } + return null; +} + +if (($argv[1] ?? '') === 'cli') { + $args = array_slice($argv, 2); + $out = fv3d_cli($d, $args); + fv3d_log(($out === null ? 'UNKNOWN ' : '') . 'cli ' . implode(' ', $args) . ($out === null ? ' (' . count($args) . ' arguments)' : '')); + if ($out === null) exit(1); + echo $out ? implode("\n", $out) . "\n" : ''; + exit(0); +} + +if (($argv[1] ?? '') === 'serve') { + // It replaces the host's Docker socket: only inside fake_unraid.sh's container + if (!is_file('/.dockerenv') || !is_file('/var/run/fv3-fake-unraid')) { fwrite(STDERR, "docker.php serve runs only under fake_unraid.sh\n"); exit(2); } + $sock = '/var/run/docker.sock'; + @unlink($sock); + $server = stream_socket_server("unix://$sock", $errno, $errstr); + if (!$server) { fwrite(STDERR, "docker.sock: $errstr\n"); exit(2); } + chmod($sock, 0666); + file_put_contents('/var/run/dockerd.pid', getmypid()); + while (true) { + $conn = @stream_socket_accept($server, -1); + if (!$conn) continue; + $request = (string)fgets($conn); + while (($line = fgets($conn)) !== false && rtrim($line) !== ''); + [$method, $target] = array_pad(explode(' ', trim($request)), 2, ''); + [$status, $body] = fv3d_answer($d, $method, $target); + fv3d_log(($status === null ? 'UNKNOWN ' : '') . "socket $method $target"); + if ($status === null) [$status, $body] = [404, ['message' => 'page not found']]; + // One line of JSON with a length: DockerClient decodes the body line by line + $json = json_encode($body, JSON_UNESCAPED_SLASHES) . "\n"; + fwrite($conn, "HTTP/1.1 $status " . ($status === 200 ? 'OK' : 'Not Found') . "\r\nContent-Type: application/json\r\nContent-Length: " . strlen($json) . "\r\nConnection: close\r\n\r\n$json"); + fclose($conn); + } +} +fwrite(STDERR, "usage: docker.php serve | cli \n"); +exit(2); diff --git a/.github/unraid-compat/fake/ip.sh b/.github/unraid-compat/fake/ip.sh new file mode 100644 index 00000000..38682d6c --- /dev/null +++ b/.github/unraid-compat/fake/ip.sh @@ -0,0 +1,9 @@ +#!/bin/bash +# Stand-in for the OS image's ip: eth0's IPv4 address from network.ini, the one form Unraid's ipaddr() asks from 7.1 +addr=$(sed -n 's/^IPADDR:0="\(.*\)"$/\1/p' /var/local/emhttp/network.ini 2>/dev/null) +# Matched on the argument count as well as the words, so the same words split another way are not answered +case "$#:$*" in +'7:-4 -br addr show eth0 scope global') [ -n "$addr" ] && echo "eth0 UP $addr/24 " && exit 0 ;; +esac +echo "ip UNKNOWN $* ($# arguments; network.ini address: ${addr:-none})" >> /var/log/fv3-fakes.log +exit 1 diff --git a/.github/unraid-compat/fake/libvirt.php b/.github/unraid-compat/fake/libvirt.php new file mode 100644 index 00000000..41f15ba5 --- /dev/null +++ b/.github/unraid-compat/fake/libvirt.php @@ -0,0 +1,154 @@ + $d) if (is_resource($dom) && fv3l_handle('domain', $name) === $dom) return $d; + $GLOBALS['fv3l_error'] = 'invalid domain handle'; + return null; +} + +function fv3l_lookup(string $field, $value) { + foreach (fv3l_domains() as $name => $d) if ($d[$field] === $value) return fv3l_handle('domain', $name); + $GLOBALS['fv3l_error'] = "Domain not found: no domain with matching $field '$value'"; + return false; +} + +// Unraid's Libvirt class connects read-write to qemu:///system; another URI or a read-only connection is not this fake +function libvirt_connect($uri = null, $readonly = true, $credentials = []) { + if ($uri !== 'qemu:///system' || $readonly !== false) { fv3l_log('UNKNOWN libvirt_connect to ' . var_export($uri, true) . ($readonly === false ? '' : ' read-only')); return false; } + return fv3l_handle('connection', 'qemu:///system'); +} +function libvirt_get_last_error() { return $GLOBALS['fv3l_error'] ?? null; } +function libvirt_logfile_set($filename, $maxsize = 0) { return true; } +function libvirt_connect_get_uri($conn) { return 'qemu:///system'; } +function libvirt_connect_get_hostname($conn) { return 'Tower'; } +function libvirt_list_domains($conn) { return array_keys(fv3l_domains()); } +function libvirt_list_active_domain_ids($conn) { return array_values(array_map(fn($d) => $d['id'], array_filter(fv3l_domains(), fn($d) => $d['id'] > 0))); } +function libvirt_domain_lookup_by_name($conn, $name) { return fv3l_lookup('name', (string)$name); } +function libvirt_domain_lookup_by_uuid_string($conn, $uuid) { return fv3l_lookup('uuid', (string)$uuid); } +function libvirt_domain_get_name($dom) { return fv3l_domain($dom)['name'] ?? false; } +function libvirt_domain_get_uuid_string($dom) { return fv3l_domain($dom)['uuid'] ?? false; } +function libvirt_domain_get_id($dom) { $d = fv3l_domain($dom); return $d ? $d['id'] : false; } +function libvirt_domain_is_active($dom) { $d = fv3l_domain($dom); return $d ? $d['id'] > 0 : false; } +function libvirt_domain_get_autostart($dom) { $d = fv3l_domain($dom); return $d ? (int)$d['autostart'] : false; } +// The live XML only, as Unraid asks for it ($dom, 0): an xpath or flags would be a form this fake does not answer +function libvirt_domain_get_xml_desc($dom, $xpath = null, $flags = 0) { + if (!in_array($xpath, [null, 0, '0', ''], true) || $flags !== 0) { fv3l_log('UNKNOWN libvirt_domain_get_xml_desc with an xpath or flags'); return false; } + return fv3l_domain($dom)['xml'] ?? false; +} +function libvirt_list_domain_snapshots($dom) { return fv3l_domain($dom) ? [] : false; } + +function libvirt_domain_get_info($dom) { + $d = fv3l_domain($dom); + if (!$d) return false; + $xml = simplexml_load_string($d['xml']); + return ['maxMem' => (int)$xml->memory, 'memory' => (int)$xml->currentMemory, 'state' => $d['state'], 'nrVirtCpu' => (int)$xml->vcpu, 'cpuUsed' => $d['id'] > 0 ? 12.5 : 0]; +} + +function libvirt_node_get_info($conn) { + return ['model' => 'x86_64', 'memory' => 16777216, 'cpus' => 8, 'nodes' => 1, 'sockets' => 1, 'cores' => 4, 'threads' => 2, 'mhz' => 3600]; +} + +// Each disk is a 20 GiB image at the path its XML names +function libvirt_domain_get_block_info($dom, $dev) { + $d = fv3l_domain($dom); + if (!$d) return false; + foreach (simplexml_load_string($d['xml'])->devices->disk as $disk) { + if ((string)$disk->target['dev'] !== (string)$dev) continue; + $size = 21474836480; + return ['device' => (string)$dev, 'file' => (string)$disk->source['file'], 'type' => (string)$disk->driver['type'], 'capacity' => $size, 'allocation' => $size, 'physical' => $size]; + } + $GLOBALS['fv3l_error'] = "invalid argument: no disk with target $dev"; + return false; +} + +// Unraid's VM usage poller asks for every stat of every domain ($conn alone) and reads these keys; a stats or flags selection is +// a form this fake does not answer. An active domain (running or paused) has used some CPU time and holds all its memory +// (balloon.rss, which libvirt reports only for an active domain) +function libvirt_connect_get_all_domain_stats($conn, $stats = 0, $flags = 0) { + if ($stats !== 0 || $flags !== 0) { fv3l_log('UNKNOWN libvirt_connect_get_all_domain_stats with a stats or flags selection'); return false; } + $all = []; + foreach (fv3l_domains() as $name => $d) { + $info = libvirt_domain_get_info(fv3l_handle('domain', $name)); + $all[$name] = ['state.state' => $d['state'], 'state.reason' => 1, 'cpu.time' => $d['id'] > 0 ? 125000000000 : 0, + 'vcpu.current' => $info['nrVirtCpu'], 'vcpu.maximum' => $info['nrVirtCpu'], 'balloon.current' => $info['memory'], + 'balloon.maximum' => $info['maxMem'], 'net.count' => 0, 'block.count' => 0] + ($d['id'] > 0 ? ['balloon.rss' => $info['memory']] : []); + } + return $all; +} + +function libvirt_domain_get_counts($conn) { + $active = count(array_filter(fv3l_domains(), fn($d) => $d['id'] > 0)); + return ['total' => count(fv3l_domains()), 'active' => $active, 'inactive' => count(fv3l_domains()) - $active]; +} + +// php-libvirt's answer: the matches' text in order, their count under 'num', and false when nothing matches +function libvirt_domain_xml_xpath($dom, $xpath, $flags = 0) { + if ($flags !== 0) { fv3l_log('UNKNOWN libvirt_domain_xml_xpath on the inactive XML'); return false; } + $d = fv3l_domain($dom); + if (!$d) return false; + $doc = new DOMDocument(); + $doc->loadXML($d['xml']); + $nodes = @(new DOMXPath($doc))->query($xpath); + if ($nodes === false) { $GLOBALS['fv3l_error'] = "invalid xpath $xpath"; return false; } + if ($nodes->length === 0) return false; + $out = ['xpath' => $xpath, 'num' => $nodes->length]; + foreach ($nodes as $i => $node) $out[$i] = $node->nodeValue; + return $out; +} + +// Every other libvirt_* function in the webGUI's code at 7.0.1-7.4.0: none has an answer here yet +foreach (['libvirt_connect_get_information', 'libvirt_connect_get_capabilities', 'libvirt_connect_get_machine_types', + 'libvirt_connect_get_domain_capabilities', 'libvirt_has_feature', + 'libvirt_list_nodedevs', 'libvirt_nodedev_get', 'libvirt_nodedev_get_xml_desc', 'libvirt_nodedev_get_information', + 'libvirt_nodedev_capabilities', 'libvirt_list_networks', 'libvirt_network_get', 'libvirt_network_get_xml_desc', 'libvirt_get_net_res', + 'libvirt_get_net_list', 'libvirt_get_net_xml', 'libvirt_domain_interface_addresses', + 'libvirt_domain_interface_stats', 'libvirt_domain_get_interface_devices', 'libvirt_domain_memory_stats', + 'libvirt_domain_get_screen_dimensions', 'libvirt_domain_qemu_agent_command', 'libvirt_domain_define_xml', 'libvirt_domain_create_xml', + 'libvirt_domain_undefine', 'libvirt_domain_update_device', 'libvirt_domain_create', 'libvirt_domain_destroy', 'libvirt_domain_shutdown', + 'libvirt_domain_reboot', 'libvirt_domain_suspend', 'libvirt_domain_resume', 'libvirt_domain_managedsave', 'libvirt_domain_set_autostart', + 'libvirt_domain_change_boot_devices', 'libvirt_domain_disk_remove', 'libvirt_domain_send_keys', 'libvirt_domain_send_pointer_event', + 'libvirt_domain_snapshot_create', 'libvirt_domain_snapshot_delete', 'libvirt_domain_snapshot_get_xml', 'libvirt_domain_snapshot_lookup_by_name', + 'libvirt_domain_snapshot_revert', 'libvirt_storagevolume_create_xml', 'libvirt_version'] as $fv3lName) { + eval("function $fv3lName(...\$args) { fv3l_log('UNKNOWN $fv3lName'); \$GLOBALS['fv3l_error'] = 'not in the fake'; return false; }"); +} +unset($fv3lName); diff --git a/.github/unraid-compat/fake/prepend.php b/.github/unraid-compat/fake/prepend.php new file mode 100644 index 00000000..785ca16f --- /dev/null +++ b/.github/unraid-compat/fake/prepend.php @@ -0,0 +1,5 @@ +> /var/log/fv3-fakes.log +exit 1 diff --git a/.github/unraid-compat/fake/router.php b/.github/unraid-compat/fake/router.php new file mode 100644 index 00000000..63fd04fa --- /dev/null +++ b/.github/unraid-compat/fake/router.php @@ -0,0 +1,19 @@ +/dev/null that is missing goes unseen: only errors that reach php -S's log are caught + +// FV3's flash folder in the fake, as lib.sh's $flash names it +const FV3T_FLASH = '/boot/config/plugins/folder.view3'; + +function fv3_fake_log_checks(): array { + $checks = []; + // fake_unraid.sh makes each log, FV3 its error.log on its first line: one that cannot be read fails, never "nothing logged" + $read = function (string $file, bool $mayBeAbsent = false) use (&$checks): array { + if ($mayBeAbsent && !file_exists($file)) return []; + $lines = @file($file, FILE_IGNORE_NEW_LINES); + $checks[] = [$lines !== false, "$file could not be read"]; + return $lines ?: []; + }; + // From 7.2 Unraid logs a .page's inline PHP as "in plugins/folder.view3/.page", with no leading slash + $phplog = $read('/var/log/phplog'); + $fv3Lines = preg_grep('#plugins/folder\.view3/#', $phplog); + $checks[] = [!$fv3Lines, "PHP logged from FV3's files:\n " . implode("\n ", array_slice($fv3Lines, 0, 20))]; + // No script died or failed an include: in Unraid's own code, a sign of state the fakes lack + $broken = preg_grep('/PHP Fatal error|Failed opening .* for inclusion/', $phplog); + $checks[] = [!$broken, "PHP died or missed a file:\n " . implode("\n ", array_slice(array_unique($broken), 0, 20))]; + // Any line in FV3's error log fails: a clean run logs nothing there, and Unraid's own warnings stay in its phplog + // No error.log means nothing logged only while its folder is there + $checks[] = [is_dir(FV3T_FLASH), FV3T_FLASH . ' is missing, so its error.log was never read']; + $reported = $read(FV3T_FLASH . '/error.log', true); + $checks[] = [!$reported, "FV3's error.log:\n " . implode("\n ", $reported)]; + $unknown = preg_grep('/UNKNOWN/', $read('/var/log/fv3-fakes.log')); + $checks[] = [!$unknown, "the fakes were asked what they do not answer:\n " . implode("\n ", array_unique($unknown))]; + $missing = preg_grep('/command not found/', $read('/var/log/php-server.log')); + $checks[] = [!$missing, "the webGUI ran commands this image lacks:\n " . implode("\n ", array_unique($missing))]; + return $checks; +} + +// Run on its own (browser_test.sh): print what failed, exit 1 on any +if (PHP_SAPI === 'cli' && realpath($argv[0] ?? '') === __FILE__) { + if (!is_file('/.dockerenv') || !is_file('/var/run/fv3-fake-unraid')) { fwrite(STDERR, "Run in the container fake_unraid.sh set up\n"); exit(2); } + $version = $argv[1] ?? '?'; + $failed = array_column(array_filter(fv3_fake_log_checks(), fn($c) => !$c[0]), 1); + if ($failed) { + echo "FAILED [Unraid $version]: the fake Unraid's logs\n- " . implode("\n- ", $failed) . "\n"; + exit(1); + } + echo "Unraid $version: the fake Unraid's logs are clean\n"; +} diff --git a/.github/unraid-compat/fake_unraid.sh b/.github/unraid-compat/fake_unraid.sh new file mode 100644 index 00000000..6daf410f --- /dev/null +++ b/.github/unraid-compat/fake_unraid.sh @@ -0,0 +1,105 @@ +#!/bin/bash +# One Unraid version running in this container, FV3 installed and Docker and libvirt faked, served on 127.0.0.1:80. +# Run from the repo root. Usage: fake_unraid.sh +set -u +version=$2; repo=$(pwd); here=$repo/.github/unraid-compat +. "$here/lib.sh" +# Past lib.sh's guard: fake/docker.php and pages_test.php refuse to run without this +mkdir -p /var/run && touch /var/run/fv3-fake-unraid || fail 'no marker' +fv3_docroot "$1" || fail "no Unraid webGUI under $1" + +# FV3 as upgradepkg leaves it, and the config folder its .plg makes +fv3_build "$repo" 2099.01.01 beta || fail 'package build' +tar -xJf "$work/2099.01.01/dist/$name-2099.01.01-x86_64-1.txz" -C / || fail 'package unpack' +mkdir -p "$flash/styles" "$flash/scripts" && printf '2099.01.01' > "$flash/version" + +# Unraid's own state in emhttp's files, the array started: a synthetic licence and flash, root alone, and no devices, disks +# or user shares +printf 'version="%s"\n' "$version" > /etc/unraid-version +cat >> /var/local/emhttp/var.ini < /var/local/emhttp/$ini.ini; done +printf '["root"]\nidx="0"\nname="root"\ndesc="Console and webGui login account"\npasswd="yes"\n' > /var/local/emhttp/users.ini +printf '[eth0]\nBONDING="no"\nBRIDGING="no"\nIPADDR:0="192.0.2.10"\nNETMASK:0="255.255.255.0"\nUSE_DHCP:0="no"\n' > /var/local/emhttp/network.ini +printf 'DOCKER_ENABLED="yes"\n' > /boot/config/docker.cfg +# DEBUG keeps PHP warnings on: without it Unraid's libvirt_helpers.php, which FV3 loads, turns error reporting off +printf 'SERVICE="enable"\nDEBUG="yes"\n' > /boot/config/domain.cfg +# Unraid's own writers add every other key with its default +for cfg in dynamix.docker.manager/scripts/dockerconfig dynamix.vm.manager/scripts/libvirtconfig; do + php -d short_open_tag=On $docroot/plugins/$cfg || fail "$cfg" +done +# The OS image adds the Connect plugin, which the webGUI repo lacks from 7.2 on: the header includes its myservers files, and +# from 7.4 requires its state class +if [ ! -f $docroot/plugins/dynamix.my.servers/include/state.php ]; then + mkdir -p $docroot/plugins/dynamix.my.servers/include + cp "$here/fake/connect_state.php" $docroot/plugins/dynamix.my.servers/include/state.php + : > $docroot/plugins/dynamix.my.servers/include/myservers1.php + : > $docroot/plugins/dynamix.my.servers/include/myservers2.php +fi +ln -sf $docroot/plugins/dynamix.plugin.manager/scripts/plugin /usr/local/sbin/plugin + +# A saved setup: Media holds app-a by hand and app-b by its label, Tools takes tool-c by regex, app-d is loose; Lab holds a VM +mkdir -p /boot/config/plugins/dockerMan/templates-user /boot/config/plugins/dynamix.vm.manager /var/lib/docker +cat > "$flash/docker.json" <<'EOF' +{"MediaAbCdEfGhIjKlMnO":{"name":"Media","icon":"","regex":"","containers":["app-a"],"settings":{"preview":1},"actions":[]},"ToolsPqRsTuVwXyZaBcD":{"name":"Tools","icon":"","regex":"^tool-","containers":[],"settings":{"preview":1},"actions":[]}} +EOF +cat > "$flash/vm.json" <<'EOF' +{"LabFoldAbCdEfGhIjKlM":{"name":"Lab","icon":"","regex":"","containers":["Debian 12"],"settings":{"preview":1},"actions":[]}} +EOF +printf '0="folder-MediaAbCdEfGhIjKlMnO"\n1="app-a"\n2="app-b"\n3="folder-ToolsPqRsTuVwXyZaBcD"\n4="tool-c"\n5="app-d"\n' > /boot/config/plugins/dockerMan/userprefs.cfg +printf '0="folder-LabFoldAbCdEfGhIjKlM"\n1="Debian 12"\n2="Home Assistant"\n3="Windows 11"\n' > /boot/config/plugins/dynamix.vm.manager/userprefs.cfg +printf 'app-a\napp-d 10\n' > /var/lib/docker/unraid-autostart +cat > /boot/config/plugins/dockerMan/templates-user/my-app-a.xml <<'EOF' + + + app-a + alpine:3.20 + bridge + sh + http://[IP]:[PORT:8080]/ + 18080 + +EOF + +# PHP as Unraid's php.ini sets it: short tags, the prepend, errors to a log and never into a page +cat > "$PHP_INI_DIR/conf.d/zz-fake-unraid.ini" < /var/log/phplog; : > /var/log/fv3-fakes.log + +# Docker and libvirtd under their own names and rc scripts, so Unraid's pgrep(), /proc/ and status checks find them running +mkdir -p /etc/rc.d && for rc in rc.docker rc.libvirt; do cp "$here/fake/rc_service.sh" /etc/rc.d/$rc && chmod +x /etc/rc.d/$rc; done +# The OS image's dmidecode and ip: the forms Unraid's Dashboard and ipaddr() run, anything else UNKNOWN +for cmd in dmidecode ip; do cp "$here/fake/$cmd.sh" /usr/sbin/$cmd && chmod +x /usr/sbin/$cmd; done +printf '#!/bin/sh\nexec php %s/fake/docker.php cli "$@"\n' "$here" > /usr/local/bin/docker && chmod +x /usr/local/bin/docker +ln -sf "$(command -v php)" /usr/local/sbin/dockerd; ln -sf "$(command -v sleep)" /usr/local/sbin/libvirtd +dockerd "$here/fake/docker.php" serve > /var/log/dockerd.log 2>&1 & +mkdir -p /var/run/libvirt; libvirtd infinity & echo $! > /var/run/libvirt/libvirtd.pid +php -S 127.0.0.1:80 -t $docroot "$here/fake/router.php" > /var/log/php-server.log 2>&1 & +for _ in $(seq 300); do + [ -S /var/run/docker.sock ] && [ -s /var/run/dockerd.pid ] && curl -sf -o /dev/null http://127.0.0.1/robots.txt && exit 0 + sleep 0.1 +done +fail "the fake Unraid did not start: $(cat /var/log/dockerd.log /var/log/php-server.log)" diff --git a/.github/unraid-compat/fixtures/docker.json b/.github/unraid-compat/fixtures/docker.json new file mode 100644 index 00000000..b10c3b1b --- /dev/null +++ b/.github/unraid-compat/fixtures/docker.json @@ -0,0 +1,25 @@ +{ + "containers": [ + {"Id":"c02cabde5b2bae8bbe0ed4a53597fc7f6a95317322f9df9f415d49837824bb6f","Names":["/app-d"],"Image":"alpine:3.20","ImageID":"sha256:1ff3b73d4cc7e7dcaff2ff9071a710eabe41b5da61ebd2dabc9febe5d974b615","ImageManifestDescriptor":{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:704f8823e322c9ae8c92aa3a330e9df3a626ca80158f016ed3d12903a28d9e39","size":1026,"annotations":{"com.docker.official-images.bashbrew.arch":"amd64","org.opencontainers.image.base.name":"scratch","org.opencontainers.image.created":"2026-04-16T23:53:21Z","org.opencontainers.image.revision":"0db70ae354ee747109ce0b9a0cfbcd3c907bc822","org.opencontainers.image.source":"https://github.com/alpinelinux/docker-alpine.git#0db70ae354ee747109ce0b9a0cfbcd3c907bc822:x86_64","org.opencontainers.image.url":"https://hub.docker.com/_/alpine","org.opencontainers.image.version":"3.20.10"},"platform":{"architecture":"amd64","os":"linux"}},"Command":"sleep 3600","Created":1794654654,"Ports":[],"Labels":{"net.unraid.docker.managed":"dockerman"},"State":"running","Status":"Up Less than a second","HostConfig":{"NetworkMode":"host"},"Health":{"Status":"none","FailingStreak":0},"NetworkSettings":{"Networks":{"host":{"IPAMConfig":null,"Links":null,"Aliases":null,"DriverOpts":null,"GwPriority":0,"NetworkID":"d76277a5b0783027956af1e58f401a4a9018b9e63f2c0bde7babe8c8bdc23378","EndpointID":"51578f75b98400841232ddda91450f8b4bd6fc805fea58ba6f0b864e27b3d39a","Gateway":"","IPAddress":"","MacAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"DNSNames":null}}},"Mounts":[]}, + {"Id":"6f0a162e593d88facb5c669bb52959572ca3571de86575e7fcc2a69f1eddd4d8","Names":["/tool-c"],"Image":"alpine:3.20","ImageID":"sha256:1ff3b73d4cc7e7dcaff2ff9071a710eabe41b5da61ebd2dabc9febe5d974b615","ImageManifestDescriptor":{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:704f8823e322c9ae8c92aa3a330e9df3a626ca80158f016ed3d12903a28d9e39","size":1026,"annotations":{"com.docker.official-images.bashbrew.arch":"amd64","org.opencontainers.image.base.name":"scratch","org.opencontainers.image.created":"2026-04-16T23:53:21Z","org.opencontainers.image.revision":"0db70ae354ee747109ce0b9a0cfbcd3c907bc822","org.opencontainers.image.source":"https://github.com/alpinelinux/docker-alpine.git#0db70ae354ee747109ce0b9a0cfbcd3c907bc822:x86_64","org.opencontainers.image.url":"https://hub.docker.com/_/alpine","org.opencontainers.image.version":"3.20.10"},"platform":{"architecture":"amd64","os":"linux"}},"Command":"sleep 3600","Created":1794654654,"Ports":[],"Labels":{"net.unraid.docker.managed":"dockerman"},"State":"created","Status":"Created","HostConfig":{"NetworkMode":"bridge"},"Health":{"Status":"none","FailingStreak":0},"NetworkSettings":{"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"DriverOpts":null,"GwPriority":0,"NetworkID":"","EndpointID":"","Gateway":"","IPAddress":"","MacAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"DNSNames":null}}},"Mounts":[]}, + {"Id":"74f3b261653ad172efc0f1f7cc23c596605e85fafe84cbcb9f39a17480935df7","Names":["/app-b"],"Image":"alpine:3.20","ImageID":"sha256:1ff3b73d4cc7e7dcaff2ff9071a710eabe41b5da61ebd2dabc9febe5d974b615","ImageManifestDescriptor":{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:704f8823e322c9ae8c92aa3a330e9df3a626ca80158f016ed3d12903a28d9e39","size":1026,"annotations":{"com.docker.official-images.bashbrew.arch":"amd64","org.opencontainers.image.base.name":"scratch","org.opencontainers.image.created":"2026-04-16T23:53:21Z","org.opencontainers.image.revision":"0db70ae354ee747109ce0b9a0cfbcd3c907bc822","org.opencontainers.image.source":"https://github.com/alpinelinux/docker-alpine.git#0db70ae354ee747109ce0b9a0cfbcd3c907bc822:x86_64","org.opencontainers.image.url":"https://hub.docker.com/_/alpine","org.opencontainers.image.version":"3.20.10"},"platform":{"architecture":"amd64","os":"linux"}},"Command":"sleep 3600","Created":1794654654,"Ports":[],"Labels":{"folder.view3":"Media","net.unraid.docker.managed":"dockerman"},"State":"running","Status":"Up Less than a second","HostConfig":{"NetworkMode":"bridge"},"Health":{"Status":"none","FailingStreak":0},"NetworkSettings":{"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"DriverOpts":null,"GwPriority":0,"NetworkID":"9a33407be38f647fe4c00b47444159cf8cb2484ea0e716778a059c23c62cfcf4","EndpointID":"997b20e28c8074ea75a32f93fe786be4221aa760c27900338354945b216b6794","Gateway":"172.17.0.1","IPAddress":"172.17.0.3","MacAddress":"02:42:ac:11:00:02","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"DNSNames":null}}},"Mounts":[]}, + {"Id":"7da32cf55ebd070122ced08ab094712770b1d5e3f0a2ba68e84809e6a0e67104","Names":["/app-a"],"Image":"alpine:3.20","ImageID":"sha256:1ff3b73d4cc7e7dcaff2ff9071a710eabe41b5da61ebd2dabc9febe5d974b615","ImageManifestDescriptor":{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:704f8823e322c9ae8c92aa3a330e9df3a626ca80158f016ed3d12903a28d9e39","size":1026,"annotations":{"com.docker.official-images.bashbrew.arch":"amd64","org.opencontainers.image.base.name":"scratch","org.opencontainers.image.created":"2026-04-16T23:53:21Z","org.opencontainers.image.revision":"0db70ae354ee747109ce0b9a0cfbcd3c907bc822","org.opencontainers.image.source":"https://github.com/alpinelinux/docker-alpine.git#0db70ae354ee747109ce0b9a0cfbcd3c907bc822:x86_64","org.opencontainers.image.url":"https://hub.docker.com/_/alpine","org.opencontainers.image.version":"3.20.10"},"platform":{"architecture":"amd64","os":"linux"}},"Command":"sleep 3600","Created":1794654653,"Ports":[{"IP":"0.0.0.0","PrivatePort":8080,"PublicPort":18080,"Type":"tcp"},{"IP":"::","PrivatePort":8080,"PublicPort":18080,"Type":"tcp"}],"Labels":{"net.unraid.docker.icon":"/plugins/dynamix.docker.manager/images/question.png","net.unraid.docker.managed":"dockerman","net.unraid.docker.webui":"http://[IP]:[PORT:8080]/"},"State":"running","Status":"Up Less than a second","HostConfig":{"NetworkMode":"bridge"},"Health":{"Status":"none","FailingStreak":0},"NetworkSettings":{"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"DriverOpts":null,"GwPriority":0,"NetworkID":"9a33407be38f647fe4c00b47444159cf8cb2484ea0e716778a059c23c62cfcf4","EndpointID":"b659ee595f07b05268a63cb7b00a429891b16b62726c04320af96a0ad14c2fab","Gateway":"172.17.0.1","IPAddress":"172.17.0.2","MacAddress":"02:42:ac:11:00:03","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"DNSNames":null}}},"Mounts":[]} + ], + "inspect": { + "app-a": {"Id":"7da32cf55ebd070122ced08ab094712770b1d5e3f0a2ba68e84809e6a0e67104","Created":"2026-11-14T11:10:53.865989285Z","Path":"sleep","Args":["3600"],"State":{"Status":"running","Running":true,"Paused":false,"Restarting":false,"OOMKilled":false,"Dead":false,"Pid":1001,"ExitCode":0,"Error":"","StartedAt":"2026-11-14T11:10:53.881644066Z","FinishedAt":"0001-01-01T00:00:00Z"},"Image":"sha256:1ff3b73d4cc7e7dcaff2ff9071a710eabe41b5da61ebd2dabc9febe5d974b615","ResolvConfPath":"/var/lib/docker/containers/7da32cf55ebd070122ced08ab094712770b1d5e3f0a2ba68e84809e6a0e67104/resolv.conf","HostnamePath":"/var/lib/docker/containers/7da32cf55ebd070122ced08ab094712770b1d5e3f0a2ba68e84809e6a0e67104/hostname","HostsPath":"/var/lib/docker/containers/7da32cf55ebd070122ced08ab094712770b1d5e3f0a2ba68e84809e6a0e67104/hosts","LogPath":"/var/lib/docker/containers/7da32cf55ebd070122ced08ab094712770b1d5e3f0a2ba68e84809e6a0e67104/7da32cf55ebd070122ced08ab094712770b1d5e3f0a2ba68e84809e6a0e67104-json.log","Name":"/app-a","RestartCount":0,"Driver":"overlayfs","Platform":"linux","MountLabel":"","ProcessLabel":"","AppArmorProfile":"docker-default","ExecIDs":null,"HostConfig":{"Binds":null,"ContainerIDFile":"","LogConfig":{"Type":"json-file","Config":{}},"NetworkMode":"bridge","PortBindings":{"8080/tcp":[{"HostIp":"","HostPort":"18080"}]},"RestartPolicy":{"Name":"no","MaximumRetryCount":0},"AutoRemove":false,"VolumeDriver":"","VolumesFrom":null,"ConsoleSize":[0,0],"CapAdd":null,"CapDrop":null,"CgroupnsMode":"private","Dns":null,"DnsOptions":[],"DnsSearch":[],"ExtraHosts":null,"GroupAdd":null,"IpcMode":"private","Cgroup":"","Links":null,"OomScoreAdj":0,"PidMode":"","Privileged":false,"PublishAllPorts":false,"ReadonlyRootfs":false,"SecurityOpt":null,"UTSMode":"","UsernsMode":"","ShmSize":67108864,"Runtime":"runc","Isolation":"","CpuShares":0,"Memory":0,"NanoCpus":0,"CgroupParent":"","BlkioWeight":0,"BlkioWeightDevice":[],"BlkioDeviceReadBps":[],"BlkioDeviceWriteBps":[],"BlkioDeviceReadIOps":[],"BlkioDeviceWriteIOps":[],"CpuPeriod":0,"CpuQuota":0,"CpuRealtimePeriod":0,"CpuRealtimeRuntime":0,"CpusetCpus":"","CpusetMems":"","Devices":[],"DeviceCgroupRules":null,"DeviceRequests":null,"MemoryReservation":0,"MemorySwap":0,"MemorySwappiness":null,"OomKillDisable":null,"PidsLimit":null,"Ulimits":null,"CpuCount":0,"CpuPercent":0,"IOMaximumIOps":0,"IOMaximumBandwidth":0,"MaskedPaths":["/proc/acpi","/proc/asound","/proc/interrupts","/proc/kcore","/proc/keys","/proc/latency_stats","/proc/sched_debug","/proc/scsi","/proc/timer_list","/proc/timer_stats","/sys/devices/virtual/powercap","/sys/firmware"],"ReadonlyPaths":["/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger"]},"Storage":{"RootFS":{"Snapshot":{"Name":"overlayfs"}}},"Mounts":[],"Config":{"Hostname":"7da32cf55ebd","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"ExposedPorts":{"8080/tcp":{}},"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Cmd":["sleep","3600"],"Image":"alpine:3.20","Volumes":null,"WorkingDir":"/","Entrypoint":null,"Labels":{"net.unraid.docker.icon":"/plugins/dynamix.docker.manager/images/question.png","net.unraid.docker.managed":"dockerman","net.unraid.docker.webui":"http://[IP]:[PORT:8080]/"}},"NetworkSettings":{"SandboxID":"427490050ddf210917ec5cf9bdb658d51e340d522578968f45f2c54f637961c7","SandboxKey":"/var/run/docker/netns/0123456789ab","Ports":{"8080/tcp":[{"HostIp":"0.0.0.0","HostPort":"18080"},{"HostIp":"::","HostPort":"18080"}]},"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"DriverOpts":null,"GwPriority":0,"NetworkID":"9a33407be38f647fe4c00b47444159cf8cb2484ea0e716778a059c23c62cfcf4","EndpointID":"b659ee595f07b05268a63cb7b00a429891b16b62726c04320af96a0ad14c2fab","Gateway":"172.17.0.1","IPAddress":"172.17.0.2","MacAddress":"02:42:ac:11:00:03","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"DNSNames":null}}},"ImageManifestDescriptor":{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:704f8823e322c9ae8c92aa3a330e9df3a626ca80158f016ed3d12903a28d9e39","size":1026,"annotations":{"com.docker.official-images.bashbrew.arch":"amd64","org.opencontainers.image.base.name":"scratch","org.opencontainers.image.created":"2026-04-16T23:53:21Z","org.opencontainers.image.revision":"0db70ae354ee747109ce0b9a0cfbcd3c907bc822","org.opencontainers.image.source":"https://github.com/alpinelinux/docker-alpine.git#0db70ae354ee747109ce0b9a0cfbcd3c907bc822:x86_64","org.opencontainers.image.url":"https://hub.docker.com/_/alpine","org.opencontainers.image.version":"3.20.10"},"platform":{"architecture":"amd64","os":"linux"}}}, + "app-b": {"Id":"74f3b261653ad172efc0f1f7cc23c596605e85fafe84cbcb9f39a17480935df7","Created":"2026-11-14T11:10:54.013835226Z","Path":"sleep","Args":["3600"],"State":{"Status":"running","Running":true,"Paused":false,"Restarting":false,"OOMKilled":false,"Dead":false,"Pid":1002,"ExitCode":0,"Error":"","StartedAt":"2026-11-14T11:10:54.04418883Z","FinishedAt":"0001-01-01T00:00:00Z"},"Image":"sha256:1ff3b73d4cc7e7dcaff2ff9071a710eabe41b5da61ebd2dabc9febe5d974b615","ResolvConfPath":"/var/lib/docker/containers/74f3b261653ad172efc0f1f7cc23c596605e85fafe84cbcb9f39a17480935df7/resolv.conf","HostnamePath":"/var/lib/docker/containers/74f3b261653ad172efc0f1f7cc23c596605e85fafe84cbcb9f39a17480935df7/hostname","HostsPath":"/var/lib/docker/containers/74f3b261653ad172efc0f1f7cc23c596605e85fafe84cbcb9f39a17480935df7/hosts","LogPath":"/var/lib/docker/containers/74f3b261653ad172efc0f1f7cc23c596605e85fafe84cbcb9f39a17480935df7/74f3b261653ad172efc0f1f7cc23c596605e85fafe84cbcb9f39a17480935df7-json.log","Name":"/app-b","RestartCount":0,"Driver":"overlayfs","Platform":"linux","MountLabel":"","ProcessLabel":"","AppArmorProfile":"docker-default","ExecIDs":null,"HostConfig":{"Binds":null,"ContainerIDFile":"","LogConfig":{"Type":"json-file","Config":{}},"NetworkMode":"bridge","PortBindings":{},"RestartPolicy":{"Name":"no","MaximumRetryCount":0},"AutoRemove":false,"VolumeDriver":"","VolumesFrom":null,"ConsoleSize":[0,0],"CapAdd":null,"CapDrop":null,"CgroupnsMode":"private","Dns":null,"DnsOptions":[],"DnsSearch":[],"ExtraHosts":null,"GroupAdd":null,"IpcMode":"private","Cgroup":"","Links":null,"OomScoreAdj":0,"PidMode":"","Privileged":false,"PublishAllPorts":false,"ReadonlyRootfs":false,"SecurityOpt":null,"UTSMode":"","UsernsMode":"","ShmSize":67108864,"Runtime":"runc","Isolation":"","CpuShares":0,"Memory":0,"NanoCpus":0,"CgroupParent":"","BlkioWeight":0,"BlkioWeightDevice":[],"BlkioDeviceReadBps":[],"BlkioDeviceWriteBps":[],"BlkioDeviceReadIOps":[],"BlkioDeviceWriteIOps":[],"CpuPeriod":0,"CpuQuota":0,"CpuRealtimePeriod":0,"CpuRealtimeRuntime":0,"CpusetCpus":"","CpusetMems":"","Devices":[],"DeviceCgroupRules":null,"DeviceRequests":null,"MemoryReservation":0,"MemorySwap":0,"MemorySwappiness":null,"OomKillDisable":null,"PidsLimit":null,"Ulimits":null,"CpuCount":0,"CpuPercent":0,"IOMaximumIOps":0,"IOMaximumBandwidth":0,"MaskedPaths":["/proc/acpi","/proc/asound","/proc/interrupts","/proc/kcore","/proc/keys","/proc/latency_stats","/proc/sched_debug","/proc/scsi","/proc/timer_list","/proc/timer_stats","/sys/devices/virtual/powercap","/sys/firmware"],"ReadonlyPaths":["/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger"]},"Storage":{"RootFS":{"Snapshot":{"Name":"overlayfs"}}},"Mounts":[],"Config":{"Hostname":"74f3b261653a","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Cmd":["sleep","3600"],"Image":"alpine:3.20","Volumes":null,"WorkingDir":"/","Entrypoint":null,"Labels":{"folder.view3":"Media","net.unraid.docker.managed":"dockerman"}},"NetworkSettings":{"SandboxID":"32bdb90ab4c38d97d285fc4330f4d1c810e095b1f6a0e77c1e2fe8e654ead58c","SandboxKey":"/var/run/docker/netns/0123456789ab","Ports":{},"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"DriverOpts":null,"GwPriority":0,"NetworkID":"9a33407be38f647fe4c00b47444159cf8cb2484ea0e716778a059c23c62cfcf4","EndpointID":"997b20e28c8074ea75a32f93fe786be4221aa760c27900338354945b216b6794","Gateway":"172.17.0.1","IPAddress":"172.17.0.3","MacAddress":"02:42:ac:11:00:02","IPPrefixLen":16,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"DNSNames":null}}},"ImageManifestDescriptor":{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:704f8823e322c9ae8c92aa3a330e9df3a626ca80158f016ed3d12903a28d9e39","size":1026,"annotations":{"com.docker.official-images.bashbrew.arch":"amd64","org.opencontainers.image.base.name":"scratch","org.opencontainers.image.created":"2026-04-16T23:53:21Z","org.opencontainers.image.revision":"0db70ae354ee747109ce0b9a0cfbcd3c907bc822","org.opencontainers.image.source":"https://github.com/alpinelinux/docker-alpine.git#0db70ae354ee747109ce0b9a0cfbcd3c907bc822:x86_64","org.opencontainers.image.url":"https://hub.docker.com/_/alpine","org.opencontainers.image.version":"3.20.10"},"platform":{"architecture":"amd64","os":"linux"}}}, + "tool-c": {"Id":"6f0a162e593d88facb5c669bb52959572ca3571de86575e7fcc2a69f1eddd4d8","Created":"2026-11-14T11:10:54.108306911Z","Path":"sleep","Args":["3600"],"State":{"Status":"created","Running":false,"Paused":false,"Restarting":false,"OOMKilled":false,"Dead":false,"Pid":0,"ExitCode":0,"Error":"","StartedAt":"0001-01-01T00:00:00Z","FinishedAt":"0001-01-01T00:00:00Z"},"Image":"sha256:1ff3b73d4cc7e7dcaff2ff9071a710eabe41b5da61ebd2dabc9febe5d974b615","ResolvConfPath":"","HostnamePath":"","HostsPath":"","LogPath":"","Name":"/tool-c","RestartCount":0,"Driver":"overlayfs","Platform":"linux","MountLabel":"","ProcessLabel":"","AppArmorProfile":"","ExecIDs":null,"HostConfig":{"Binds":null,"ContainerIDFile":"","LogConfig":{"Type":"json-file","Config":{}},"NetworkMode":"bridge","PortBindings":{},"RestartPolicy":{"Name":"no","MaximumRetryCount":0},"AutoRemove":false,"VolumeDriver":"","VolumesFrom":null,"ConsoleSize":[0,0],"CapAdd":null,"CapDrop":null,"CgroupnsMode":"private","Dns":null,"DnsOptions":[],"DnsSearch":[],"ExtraHosts":null,"GroupAdd":null,"IpcMode":"private","Cgroup":"","Links":null,"OomScoreAdj":0,"PidMode":"","Privileged":false,"PublishAllPorts":false,"ReadonlyRootfs":false,"SecurityOpt":null,"UTSMode":"","UsernsMode":"","ShmSize":67108864,"Runtime":"runc","Isolation":"","CpuShares":0,"Memory":0,"NanoCpus":0,"CgroupParent":"","BlkioWeight":0,"BlkioWeightDevice":[],"BlkioDeviceReadBps":[],"BlkioDeviceWriteBps":[],"BlkioDeviceReadIOps":[],"BlkioDeviceWriteIOps":[],"CpuPeriod":0,"CpuQuota":0,"CpuRealtimePeriod":0,"CpuRealtimeRuntime":0,"CpusetCpus":"","CpusetMems":"","Devices":[],"DeviceCgroupRules":null,"DeviceRequests":null,"MemoryReservation":0,"MemorySwap":0,"MemorySwappiness":null,"OomKillDisable":false,"PidsLimit":null,"Ulimits":null,"CpuCount":0,"CpuPercent":0,"IOMaximumIOps":0,"IOMaximumBandwidth":0,"MaskedPaths":["/proc/acpi","/proc/asound","/proc/interrupts","/proc/kcore","/proc/keys","/proc/latency_stats","/proc/sched_debug","/proc/scsi","/proc/timer_list","/proc/timer_stats","/sys/devices/virtual/powercap","/sys/firmware"],"ReadonlyPaths":["/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger"]},"Storage":{"RootFS":{"Snapshot":{"Name":"overlayfs"}}},"Mounts":[],"Config":{"Hostname":"6f0a162e593d","Domainname":"","User":"","AttachStdin":false,"AttachStdout":true,"AttachStderr":true,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Cmd":["sleep","3600"],"Image":"alpine:3.20","Volumes":null,"WorkingDir":"/","Entrypoint":null,"Labels":{"net.unraid.docker.managed":"dockerman"}},"NetworkSettings":{"SandboxID":"","SandboxKey":"","Ports":{},"Networks":{"bridge":{"IPAMConfig":null,"Links":null,"Aliases":null,"DriverOpts":null,"GwPriority":0,"NetworkID":"","EndpointID":"","Gateway":"","IPAddress":"","MacAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"DNSNames":null}}},"ImageManifestDescriptor":{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:704f8823e322c9ae8c92aa3a330e9df3a626ca80158f016ed3d12903a28d9e39","size":1026,"annotations":{"com.docker.official-images.bashbrew.arch":"amd64","org.opencontainers.image.base.name":"scratch","org.opencontainers.image.created":"2026-04-16T23:53:21Z","org.opencontainers.image.revision":"0db70ae354ee747109ce0b9a0cfbcd3c907bc822","org.opencontainers.image.source":"https://github.com/alpinelinux/docker-alpine.git#0db70ae354ee747109ce0b9a0cfbcd3c907bc822:x86_64","org.opencontainers.image.url":"https://hub.docker.com/_/alpine","org.opencontainers.image.version":"3.20.10"},"platform":{"architecture":"amd64","os":"linux"}}}, + "app-d": {"Id":"c02cabde5b2bae8bbe0ed4a53597fc7f6a95317322f9df9f415d49837824bb6f","Created":"2026-11-14T11:10:54.137826016Z","Path":"sleep","Args":["3600"],"State":{"Status":"running","Running":true,"Paused":false,"Restarting":false,"OOMKilled":false,"Dead":false,"Pid":1003,"ExitCode":0,"Error":"","StartedAt":"2026-11-14T11:10:54.155919462Z","FinishedAt":"0001-01-01T00:00:00Z"},"Image":"sha256:1ff3b73d4cc7e7dcaff2ff9071a710eabe41b5da61ebd2dabc9febe5d974b615","ResolvConfPath":"/var/lib/docker/containers/c02cabde5b2bae8bbe0ed4a53597fc7f6a95317322f9df9f415d49837824bb6f/resolv.conf","HostnamePath":"/var/lib/docker/containers/c02cabde5b2bae8bbe0ed4a53597fc7f6a95317322f9df9f415d49837824bb6f/hostname","HostsPath":"/var/lib/docker/containers/c02cabde5b2bae8bbe0ed4a53597fc7f6a95317322f9df9f415d49837824bb6f/hosts","LogPath":"/var/lib/docker/containers/c02cabde5b2bae8bbe0ed4a53597fc7f6a95317322f9df9f415d49837824bb6f/c02cabde5b2bae8bbe0ed4a53597fc7f6a95317322f9df9f415d49837824bb6f-json.log","Name":"/app-d","RestartCount":0,"Driver":"overlayfs","Platform":"linux","MountLabel":"","ProcessLabel":"","AppArmorProfile":"docker-default","ExecIDs":null,"HostConfig":{"Binds":null,"ContainerIDFile":"","LogConfig":{"Type":"json-file","Config":{}},"NetworkMode":"host","PortBindings":{},"RestartPolicy":{"Name":"no","MaximumRetryCount":0},"AutoRemove":false,"VolumeDriver":"","VolumesFrom":null,"ConsoleSize":[0,0],"CapAdd":null,"CapDrop":null,"CgroupnsMode":"private","Dns":null,"DnsOptions":[],"DnsSearch":[],"ExtraHosts":null,"GroupAdd":null,"IpcMode":"private","Cgroup":"","Links":null,"OomScoreAdj":0,"PidMode":"","Privileged":false,"PublishAllPorts":false,"ReadonlyRootfs":false,"SecurityOpt":null,"UTSMode":"","UsernsMode":"","ShmSize":67108864,"Runtime":"runc","Isolation":"","CpuShares":0,"Memory":0,"NanoCpus":0,"CgroupParent":"","BlkioWeight":0,"BlkioWeightDevice":[],"BlkioDeviceReadBps":[],"BlkioDeviceWriteBps":[],"BlkioDeviceReadIOps":[],"BlkioDeviceWriteIOps":[],"CpuPeriod":0,"CpuQuota":0,"CpuRealtimePeriod":0,"CpuRealtimeRuntime":0,"CpusetCpus":"","CpusetMems":"","Devices":[],"DeviceCgroupRules":null,"DeviceRequests":null,"MemoryReservation":0,"MemorySwap":0,"MemorySwappiness":null,"OomKillDisable":null,"PidsLimit":null,"Ulimits":null,"CpuCount":0,"CpuPercent":0,"IOMaximumIOps":0,"IOMaximumBandwidth":0,"MaskedPaths":["/proc/acpi","/proc/asound","/proc/interrupts","/proc/kcore","/proc/keys","/proc/latency_stats","/proc/sched_debug","/proc/scsi","/proc/timer_list","/proc/timer_stats","/sys/devices/virtual/powercap","/sys/firmware"],"ReadonlyPaths":["/proc/bus","/proc/fs","/proc/irq","/proc/sys","/proc/sysrq-trigger"]},"Storage":{"RootFS":{"Snapshot":{"Name":"overlayfs"}}},"Mounts":[],"Config":{"Hostname":"Tower","Domainname":"","User":"","AttachStdin":false,"AttachStdout":false,"AttachStderr":false,"Tty":false,"OpenStdin":false,"StdinOnce":false,"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Cmd":["sleep","3600"],"Image":"alpine:3.20","Volumes":null,"WorkingDir":"/","Entrypoint":null,"Labels":{"net.unraid.docker.managed":"dockerman"}},"NetworkSettings":{"SandboxID":"7352ddb38c6519326c97324ee5da104c419573030e2111f1a3422e0f5b45eae8","SandboxKey":"/var/run/docker/netns/default","Ports":{},"Networks":{"host":{"IPAMConfig":null,"Links":null,"Aliases":null,"DriverOpts":null,"GwPriority":0,"NetworkID":"d76277a5b0783027956af1e58f401a4a9018b9e63f2c0bde7babe8c8bdc23378","EndpointID":"51578f75b98400841232ddda91450f8b4bd6fc805fea58ba6f0b864e27b3d39a","Gateway":"","IPAddress":"","MacAddress":"","IPPrefixLen":0,"IPv6Gateway":"","GlobalIPv6Address":"","GlobalIPv6PrefixLen":0,"DNSNames":null}}},"ImageManifestDescriptor":{"mediaType":"application/vnd.oci.image.manifest.v1+json","digest":"sha256:704f8823e322c9ae8c92aa3a330e9df3a626ca80158f016ed3d12903a28d9e39","size":1026,"annotations":{"com.docker.official-images.bashbrew.arch":"amd64","org.opencontainers.image.base.name":"scratch","org.opencontainers.image.created":"2026-04-16T23:53:21Z","org.opencontainers.image.revision":"0db70ae354ee747109ce0b9a0cfbcd3c907bc822","org.opencontainers.image.source":"https://github.com/alpinelinux/docker-alpine.git#0db70ae354ee747109ce0b9a0cfbcd3c907bc822:x86_64","org.opencontainers.image.url":"https://hub.docker.com/_/alpine","org.opencontainers.image.version":"3.20.10"},"platform":{"architecture":"amd64","os":"linux"}}} + }, + "images": [ + {"Containers":4,"Created":1776383604,"Id":"sha256:1ff3b73d4cc7e7dcaff2ff9071a710eabe41b5da61ebd2dabc9febe5d974b615","Labels":{},"ParentId":"","Descriptor":{"mediaType":"application/vnd.oci.image.index.v1+json","digest":"sha256:1ff3b73d4cc7e7dcaff2ff9071a710eabe41b5da61ebd2dabc9febe5d974b615","size":9226},"RepoDigests":["alpine@sha256:1ff3b73d4cc7e7dcaff2ff9071a710eabe41b5da61ebd2dabc9febe5d974b615"],"RepoTags":["alpine:3.20"],"SharedSize":-1,"Size":13660215} + ], + "image_inspect": { + "sha256:1ff3b73d4cc7e7dcaff2ff9071a710eabe41b5da61ebd2dabc9febe5d974b615": {"Id":"sha256:1ff3b73d4cc7e7dcaff2ff9071a710eabe41b5da61ebd2dabc9febe5d974b615","RepoTags":["alpine:3.20"],"RepoDigests":["alpine@sha256:1ff3b73d4cc7e7dcaff2ff9071a710eabe41b5da61ebd2dabc9febe5d974b615"],"Comment":"buildkit.dockerfile.v0","Created":"2026-04-16T23:53:24.896953537Z","Config":{"Env":["PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"],"Cmd":["/bin/sh"],"WorkingDir":"/"},"Architecture":"amd64","Os":"linux","Size":4103199,"RootFS":{"Type":"layers","Layers":["sha256:17fde15de9bf75f013a6219775fcb946f09ebb3c5f19ef7305ad8599b1cee239"]},"Metadata":{"LastTagTime":"2026-10-30T23:32:14.282085809Z"},"Descriptor":{"mediaType":"application/vnd.oci.image.index.v1+json","digest":"sha256:1ff3b73d4cc7e7dcaff2ff9071a710eabe41b5da61ebd2dabc9febe5d974b615","size":9226},"Identity":{"Pull":[{"Repository":"docker.io/library/alpine"}]}} + }, + "networks": [ + {"Name":"host","Driver":"host"}, + {"Name":"none","Driver":"null"}, + {"Name":"bridge","Driver":"bridge"} + ] +} diff --git a/.github/unraid-compat/fixtures/vms.json b/.github/unraid-compat/fixtures/vms.json new file mode 100644 index 00000000..f6be4c74 --- /dev/null +++ b/.github/unraid-compat/fixtures/vms.json @@ -0,0 +1,28 @@ +{ + "domains": [ + { + "name": "Debian 12", + "uuid": "803bd917-84b0-490f-ba3c-5e21b4cd6f07", + "state": 1, + "id": 1, + "autostart": true, + "xml": "\n Debian 12\n 803bd917-84b0-490f-ba3c-5e21b4cd6f07\n Debian 12 lab server\n \n \n \n 2097152\n 2097152\n \n \n \n 2\n \n hvm\n \n \n \n \n \n \n \n /usr/local/sbin/qemu\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n" + }, + { + "name": "Windows 11", + "uuid": "9fd1c657-7f2b-41b4-9ff3-5b42706315d5", + "state": 5, + "id": -1, + "autostart": false, + "xml": "\n Windows 11\n 9fd1c657-7f2b-41b4-9ff3-5b42706315d5\n \n \n \n \n 8388608\n 8388608\n \n \n \n 4\n \n hvm\n \n \n \n \n \n \n /usr/local/sbin/qemu\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n" + }, + { + "name": "Home Assistant", + "uuid": "39ec878e-ce44-4414-8dd1-b8fb3e57f9b2", + "state": 3, + "id": 2, + "autostart": true, + "xml": "\n Home Assistant\n 39ec878e-ce44-4414-8dd1-b8fb3e57f9b2\n \n \n \n \n 4194304\n 4194304\n \n \n \n 3\n \n hvm\n \n \n \n \n \n \n \n /usr/local/sbin/qemu\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n" + } + ] +} diff --git a/.github/unraid-compat/hooks.js b/.github/unraid-compat/hooks.js new file mode 100644 index 00000000..ee7e7470 --- /dev/null +++ b/.github/unraid-compat/hooks.js @@ -0,0 +1,105 @@ +// Unraid's own parts FV3 relies on, each with the FV3 file that uses it and what the user loses without it. browser_test.js checks +// them on Unraid alone (endpoints as webGUI files), each file still naming its hook; any/since only where FV3 itself branches +'use strict'; +const D = 'Docker', V = 'VMs', B = 'Dashboard', ALL = 'any page'; +const HOOKS = [ + // Docker's list and its rows + { page: D, dom: '#docker_list > tr.sortable', file: 'scripts/docker.js', loses: 'folders never draw' }, + { page: D, dom: '#docker_containers > thead > tr > th', file: 'scripts/docker.js', loses: 'folder rows lose their column spans and widths' }, + { page: D, dom: '#docker_list td.ct-name > span.outer > span.hand > img.img', file: 'scripts/docker.js', loses: "folder previews show no containers' icons" }, + { page: D, dom: '#docker_list td.ct-name span.inner > span.appname > a.exec', file: 'scripts/docker.js', loses: 'containers are not read into their folders' }, + { page: D, dom: '#docker_list td.ct-name span.outer i[id^="load-"]', file: 'scripts/docker.js', loses: "folder icons stop showing their containers' state" }, + { page: D, dom: 'td.updatecolumn', file: 'scripts/shared.js', loses: 'incognito leaves version text showing' }, + { page: D, dom: '#docker_list td.advanced', file: 'scripts/docker.js', loses: "the folder autostart switch cannot find its containers' switches" }, + { page: D, dom: '#docker_list input.autostart', file: 'scripts/docker.js', loses: 'the folder autostart switch does nothing' }, + { page: D, dom: '.switch-button-background', file: 'scripts/docker.js', loses: "the folder autostart switch leaves its members' switches unchanged" }, + { page: D, dom: '#docker_containers th.nine', file: 'scripts/shared.js', loses: 'the autostart column is sized wrong' }, + { page: D, dom: '.advancedview', file: 'scripts/docker.js', loses: 'folders are not redrawn on the basic/advanced toggle' }, + { page: D, any: ['.js-actions', 'input[value="Add Container"]'], file: 'folder.view3.Docker.page', loses: 'no Add Folder button' }, + { page: D, dom: 'div.spinner.fixed', file: 'scripts/docker.js', loses: 'no spinner during folder actions' }, + // VMs' list and its rows + { page: V, dom: '#kvm_list > tr.sortable', file: 'scripts/vm.js', loses: 'folders never draw' }, + { page: V, dom: '#kvm_table > thead > tr', file: 'scripts/vm.js', loses: 'folder rows lose their column spans' }, + { page: V, dom: '#kvm_list td.vm-name span.outer span.inner a', file: 'scripts/vm.js', loses: 'VMs are not read into their folders' }, + { page: V, dom: '#kvm_list td.vm-name span.hand > img.img', file: 'scripts/vm.js', loses: "folder previews show no VMs' icons" }, + { page: V, dom: '#kvm_list tr[id^="name-"]', file: 'scripts/vm.js', loses: 'VM detail rows escape their folders' }, + { page: V, dom: '#kvm_list a[onclick*="toggle_id"]', file: 'scripts/vm.js', loses: 'VM detail rows escape their folders' }, + { page: V, dom: '#kvm_table th.th1', file: 'scripts/vm.js', loses: 'the name column is not sized' }, + { page: V, dom: '#kvm_list input.autostart', file: 'scripts/vm.js', loses: 'the folder autostart switch throws' }, + { page: V, any: ['.js-actions', 'input[value="Add VM"]'], file: 'folder.view3.VMs.page', loses: 'no Add Folder button' }, + // The Dashboard's tiles + { page: B, dom: 'tbody#docker_view > tr.updated > td', file: 'scripts/dashboard.js', loses: 'Docker folders never draw' }, + { page: B, dom: 'tbody#vm_view > tr.updated > td', file: 'scripts/dashboard.js', loses: 'VM folders never draw' }, + { page: B, dom: 'tbody#docker_view span.outer.solid.apps > span.hand', file: 'scripts/dashboard.js', loses: 'containers are not moved into folder tiles' }, + { page: B, dom: 'tbody#docker_view span.outer.apps > span.inner', file: 'scripts/dashboard.js', loses: "containers' names are not read" }, + { page: B, dom: 'tbody#vm_view span.outer.vms > span.hand', file: 'scripts/dashboard.js', loses: 'VMs are not moved into folder tiles' }, + { page: B, dom: 'input#apps', file: 'scripts/dashboard.js', loses: "hide-stopped ignores Docker folders" }, + { page: B, dom: 'input#vms', file: 'scripts/dashboard.js', loses: 'hide-stopped ignores VM folders' }, + { page: B, dom: 'table.dashboard', file: 'styles/dashboard.css', loses: 'folder tiles lose their padding' }, + // Every page + { page: ALL, dom: 'link[href*="default-base"]', since: '7.1', file: 'scripts/include/customEvents.js', loses: "FV3 styles the page as 7.0's layout" }, + // Classes FV3 puts on its own markup for Unraid's stylesheet to colour + { page: ALL, css: 'green-text', file: 'scripts/docker.js', loses: 'a started folder loses its green state' }, + { page: ALL, css: 'red-text', file: 'scripts/docker.js', loses: 'a stopped folder loses its red state' }, + { page: ALL, css: 'orange-text', file: 'scripts/docker.js', loses: "a folder's drag handle and update mark lose their colour" }, + // Unraid's functions and jQuery plugins, each callable, and its CSRF token, a non-empty string + { page: D, js: 'loadlist', file: 'scripts/docker.js', loses: 'folders never draw' }, + { page: D, js: 'listview', file: 'scripts/docker.js', loses: 'folders are not redrawn after a view toggle' }, + { page: D, js: 'openDocker', file: 'scripts/docker.js', loses: "a folder's Update does nothing" }, + { page: D, js: 'openTerminal', file: 'scripts/docker.js', loses: 'the console and log buttons do nothing' }, + { page: D, js: 'dockerload.addEventListener', file: 'scripts/docker.js', loses: 'no CPU and memory stats without the Unraid API' }, + { page: V, js: 'loadlist', file: 'scripts/vm.js', loses: 'folders never draw' }, + { page: ALL, js: 'context.attach', file: 'scripts/docker.js', loses: 'folders have no menus' }, + { page: ALL, js: 'swal', file: 'scripts/shared.js', loses: 'no confirm or error dialogs' }, + { page: ALL, js: 'csrf_token', type: 'string', file: 'scripts/include/customEvents.js', loses: "every FV3 save is refused" }, + { page: ALL, js: '$.fn.tooltipster', file: 'scripts/advanced-preview.js', loses: 'no advanced preview' }, + { page: D, js: '$.fn.switchButton', file: 'scripts/docker.js', loses: 'the folder autostart switch never draws' }, + { page: D, js: '$.fn.sortable', file: 'scripts/docker.js', loses: 'folders cannot be dragged' }, + { page: D, js: '$.cookie', file: 'scripts/docker.js', loses: 'docker.js stops at load, so folders never draw' }, + { page: D, js: '$.fn.readmore', file: 'scripts/shared.js', loses: "an opened folder's long descriptions are not folded" }, + { page: ALL, js: 'openBox', file: 'scripts/shared.js', loses: "a folder's user script actions do nothing" }, + // Unraid's endpoints FV3 calls, as files in the webGUI; one it intercepts must also still be called from Unraid's pages + { url: '/plugins/dynamix.docker.manager/include/UserPrefs.php', intercepts: true, file: 'scripts/docker.js', loses: "Unraid saves an order without FV3's folders" }, + { url: '/plugins/dynamix.vm.manager/include/UserPrefs.php', intercepts: true, file: 'scripts/vm.js', loses: "Unraid saves an order without FV3's folders" }, + { url: '/plugins/dynamix.vm.manager/include/VMMachines.php', intercepts: true, file: 'scripts/vm.js', loses: 'folders are not redrawn when the VM list reloads' }, + { url: '/webGui/include/DashboardApps.php', intercepts: true, file: 'scripts/dashboard.js', loses: 'Dashboard folders are not redrawn when the tiles reload' }, + { url: '/plugins/dynamix.docker.manager/include/UpdateConfig.php', intercepts: true, file: 'scripts/docker.js', loses: 'folder autostart falls out of step' }, + { url: '/plugins/dynamix.docker.manager/include/DockerUpdate.php', intercepts: true, file: 'scripts/docker.js', loses: 'folders are not redrawn after an update' }, + { url: '/plugins/dynamix.vm.manager/include/VMajax.php', file: 'scripts/shared.js', loses: 'VM folder actions fail' }, + { url: '/plugins/dynamix.vm.manager/vnc.html', file: 'scripts/vm.js', loses: "a folder member's VNC button does nothing" }, + { url: '/plugins/dynamix.docker.manager/images/question.png', file: 'scripts/docker.js', loses: 'folders without an icon show a broken image' }, +]; + +// What a hook's FV3 file must still mention: a selector's #ids, .classes and quoted values, a function's name, a URL's file +const tokens = (h) => (h.js ? [h.js.startsWith('$.fn.') ? h.js.slice(4) : h.js] : h.url ? [h.url.split('/').pop()] : h.css ? [h.css] + : (h.any || [h.dom]).flatMap((s) => [...s.matchAll(/([#.][\w-]+)|"([^"]+)"/g)].map((m) => m[1] || m[2]))); +const esc = (t) => t.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); +// As a whole token (a #id or .class starts at its mark); a class may also be named in a class attribute or as a quoted string +const mentions = (text, t) => new RegExp(`${/^[#.]/.test(t) ? '' : '(? h.dom || (h.any && `any of ${h.any.join(', ')}`) || (h.css && `a stylesheet rule for .${h.css}`) || h.url + || (h.type === 'string' ? `${h.js} as a non-empty string` : `the function ${h.js}`); +const older = (a, b) => { const [x, y] = [a, b].map((v) => v.split('.').map(Number)); const i = x.findIndex((n, k) => n !== y[k]); return i >= 0 && x[i] < y[i]; }; +const applies = (h, version) => !h.since || !older(version, h.since); +// The hooks one view checks: its page's and every page's that apply to this version (endpoints are checked as files) +const forView = (page, version) => HOOKS.filter((h) => !h.url && [page, ALL].includes(h.page) && applies(h, version)); + +// In the page: the indexes of the hooks it lacks. Rules in @media and @import count; a sheet that cannot be read counts as none +function missingHooks(hooks) { + const own = (sheet) => { try { return [...sheet.cssRules]; } catch { return []; } }; + const flat = (rules) => rules.flatMap((r) => (r.styleSheet ? flat(own(r.styleSheet)) : r.cssRules ? [r, ...flat([...r.cssRules])] : [r])); + const selectors = flat([...document.styleSheets].flatMap(own)) + .map((r) => r.selectorText || '').join('\n'); + const present = (h) => { + if (h.dom) return !!document.querySelector(h.dom); + if (h.any) return h.any.some((s) => document.querySelector(s)); + if (h.css) return new RegExp(`\\.${h.css}(?![\\w-])`).test(selectors); + try { + const value = (0, eval)(h.js); + return typeof value === (h.type || 'function') && value !== ''; + } catch { return false; } + }; + return hooks.map((h, i) => (present(h) ? -1 : i)).filter((i) => i >= 0); +} + +module.exports = { HOOKS, tokens, mentions, describe, applies, forView, missingHooks }; diff --git a/.github/unraid-compat/install_test.sh b/.github/unraid-compat/install_test.sh new file mode 100755 index 00000000..64281244 --- /dev/null +++ b/.github/unraid-compat/install_test.sh @@ -0,0 +1,134 @@ +#!/bin/bash +# FolderView3 through a real Unraid webGUI's plugin installer: install, upgrade over the last stable, remove, failures, min. +# Disposable container only, as root, with no network. Usage: install_test.sh +set -u +version=$2; repo=$(pwd) +. "$repo/.github/unraid-compat/lib.sh" +stable=$(cd "$3" 2>/dev/null && pwd) && [ -f "$stable/pkg_build.sh" ] || fail "no last-stable checkout with pkg_build.sh at $3" +awk --version 2>/dev/null | grep -q 'GNU Awk' || fail 'awk is not gawk, as it is on Unraid: the remove script needs RT' +fv3_docroot "$1" || fail "no Unraid plugin installer under $1" + +live=$docroot/plugins/$name +manifest=/boot/config/plugins/$name.plg +mkdir -p /boot/config/plugins-error /var/log/plugins /var/log/packages +# Unraid's pre- and post-install hooks keep their state under /tmp/plugins +mkdir -p /tmp/plugins +plugin() { php "$docroot/plugins/dynamix.plugin.manager/scripts/plugin" "$@"; } + +# Slackware's package tools, as they work: the record lists the package's files, and removepkg takes those and the folders +# they leave empty. Everything else is Unraid's own code. +cat > /sbin/upgradepkg <<'STUB' +#!/bin/bash +for pkg; do :; done +tar -xJf "$pkg" -C / || exit 1 +tar -tJf "$pkg" > "/var/log/packages/$(basename "$pkg" .txz)" +STUB +cat > /sbin/removepkg <<'STUB' +#!/bin/bash +record=/var/log/packages/$1 +[ -f "$record" ] || exit 1 +grep -v '/$' "$record" | while IFS= read -r f; do rm -f "/$f"; done +grep '/$' "$record" | sort -r | while IFS= read -r d; do rmdir "/$d" 2>/dev/null; done +rm -f "$record" +STUB +chmod +x /sbin/upgradepkg /sbin/removepkg + +build() { fv3_build "$@" || fail "package build of $2"; } +old=2026.01.01; new=2099.01.01 +build "$stable" $old main; build "$repo" $new beta +min=$(grep -o ']*min="[^"]*"' "$work/$new/$name.plg" | sed 's/.*min="//; s/"$//') +[ -n "$min" ] || fail 'the .plg names no min version' + +set_version() { printf 'version="%s"\n' "$1" > /etc/unraid-version; } +reset() { + rm -rf "$flash" "$live" "$manifest" /var/log/plugins/$name.plg /var/log/packages/$name-* /boot/config/plugins/dockerMan /boot/config/plugins/dynamix.vm.manager + mkdir -p "$flash" +} +install() { # install : the .plg with its package already on the flash, as a boot or a cached download has it + cp "$work/$1/dist/$name-$1-x86_64-1.txz" "$flash/" + plugin install "$work/$1/$name.plg" > "$work/$2.log" 2>&1 || { cat "$work/$2.log"; fail "$2: install of $1 refused"; } +} +installed() { # installed : its files are live, its package is recorded and its manifest registered + local pkg=$name-$1-x86_64-1 x=$work/unpacked + rm -rf "$x" && mkdir "$x" && tar -xJf "$work/$1/dist/$pkg.txz" -C "$x" || fail "$2: unpack $1" + local differ; differ=$(cd "$x" && find . -type f | while read -r f; do cmp -s "$f" "/$f" || echo "${f#.}"; done) + [ -z "$differ" ] || fail "$2: files not as $1 ships them: $(echo $differ | head -c 300)" + [ -e "/var/log/packages/$pkg" ] || fail "$2: no package record for $1" + [ -L /var/log/plugins/$name.plg ] && grep -q "ENTITY version \"$1\"" "$manifest" || fail "$2: $1 is not the registered manifest" +} +saved_config() { # what a user has saved: folders, settings, a theme, and Unraid's own orders holding folder entries + mkdir -p "$flash/styles" "$flash/scripts" /boot/config/plugins/dockerMan /boot/config/plugins/dynamix.vm.manager + printf '{"AbCdEfGhIjKlMnOpQrSt":{"name":"Media","icon":"","regex":"","containers":["alpha"],"settings":{"preview":1},"actions":[]}}' > "$flash/docker.json" + printf '{"ZyXwVuTsRqPoNmLkJiHg":{"name":"Lab","icon":"","regex":"","containers":["Debian 12"],"settings":{"preview":1},"actions":[]}}' > "$flash/vm.json" + printf '{"docker":{"preview":1}}' > "$flash/settings.json" + printf '.folder-appname { color: #abc; }\n' > "$flash/styles/custom.css" + # folder-sync and folder-nightlybackups are containers: too short, or no capital as a folder id has + printf '0="folder-AbCdEfGhIjKlMnOpQrSt"\n1="alpha"\n2="folder-sync"\n3="folder-nightlybackups"\n4="beta"\n' > /boot/config/plugins/dockerMan/userprefs.cfg + printf '0="folder-ZyXwVuTsRqPoNmLkJiHg"\r\n1="Debian 12"\r\n2="Windows"' > /boot/config/plugins/dynamix.vm.manager/userprefs.cfg +} +config_sums() { (cd /boot/config/plugins && find $name dockerMan dynamix.vm.manager -type f ! -name "$name-*.txz" ! -path "$name/version" -print0 | sort -z | xargs -0 md5sum); } +boot() { # boot : what a reboot does: RAM starts empty, and the registered manifest installs again from the flash alone + rm -rf "$live" /var/log/packages/$name-* /var/log/plugins/$name.plg + plugin install "$manifest" > "$work/$1.log" 2>&1 || { cat "$work/$1.log"; fail "$1: reinstall at boot refused"; } +} +set_version "$version" + +# A fresh install +reset; install $new fresh; installed $new fresh +[ "$(cat "$flash/version")" = $new ] || fail 'fresh: version file not written' +[ -d "$flash/styles" ] && [ -d "$flash/scripts" ] || fail 'fresh: styles and scripts folders not made' + +# An upgrade over the last stable keeps what the user saved, byte for byte, and drops the previous package +reset; install $old stable; installed $old stable; saved_config; before=$(config_sums) +install $new upgrade; installed $new upgrade +[ "$(config_sums)" = "$before" ] || fail 'upgrade: saved folders, settings, theme or Unraid orders changed' +[ ! -e "$flash/$name-$old-x86_64-1.txz" ] || fail 'upgrade: previous archive left on the flash' +[ ! -e "/var/log/packages/$name-$old-x86_64-1" ] || fail 'upgrade: previous package record left' + +# A reboot after the upgrade brings back the new version from its archive +boot boot; installed $new boot +[ "$(config_sums)" = "$before" ] || fail 'boot: saved files changed' + +# Removal leaves nothing, a file the package did not ship included, and takes only FolderView3's entries out of Unraid's +# orders, line endings kept +printf 'x' > "$live/not-from-the-package" +plugin remove $name.plg > "$work/remove.log" 2>&1 || { cat "$work/remove.log"; fail 'remove: refused'; } +[ ! -e "$live" ] && [ ! -e "$flash" ] && [ ! -e /var/log/plugins/$name.plg ] && [ ! -e "$manifest" ] || fail 'remove: files left behind' +ls /var/log/packages/$name-* >/dev/null 2>&1 && fail 'remove: package record left' +cmp -s /boot/config/plugins/dockerMan/userprefs.cfg <(printf '0="alpha"\n1="folder-sync"\n2="folder-nightlybackups"\n3="beta"\n') \ + || fail "remove: Docker order not as expected: $(od -c /boot/config/plugins/dockerMan/userprefs.cfg | head -5)" +cmp -s /boot/config/plugins/dynamix.vm.manager/userprefs.cfg <(printf '0="Debian 12"\r\n1="Windows"') \ + || fail "remove: VM order not as expected: $(od -c /boot/config/plugins/dynamix.vm.manager/userprefs.cfg | head -5)" + +# A download that fails (no network here) leaves the running install, what the user saved, and a next boot that brings it back +reset; install $old stable2; saved_config; before=$(config_sums) +plugin install "$work/$new/$name.plg" > "$work/download.log" 2>&1 && fail 'download: install reported success without its package' +grep -q 'download failure' "$work/download.log" || { cat "$work/download.log"; fail 'download: refused for a reason other than the download'; } +installed $old download +[ "$(config_sums)" = "$before" ] || fail 'download: saved files changed' +[ -f "$flash/$name-$old-x86_64-1.txz" ] || fail 'download: deleted the archive the next boot installs from' +boot download-boot; installed $old download-boot +[ "$(config_sums)" = "$before" ] || fail 'download-boot: saved files changed' + +# A failed upgradepkg does the same +reset; install $old stable3; saved_config; before=$(config_sums) +cp "$work/$new/dist/$name-$new-x86_64-1.txz" "$flash/" +mv /sbin/upgradepkg "$work/upgradepkg"; printf '#!/bin/bash\nexit 1\n' > /sbin/upgradepkg; chmod +x /sbin/upgradepkg +plugin install "$work/$new/$name.plg" > "$work/upgradepkg.log" 2>&1; status=$? +mv "$work/upgradepkg" /sbin/upgradepkg +[ $status -ne 0 ] || fail 'upgradepkg: install reported success though upgradepkg failed' +grep -q "run failed: 'upgradepkg" "$work/upgradepkg.log" || { cat "$work/upgradepkg.log"; fail 'upgradepkg: refused before upgradepkg ran'; } +installed $old upgradepkg +[ "$(config_sums)" = "$before" ] || fail 'upgradepkg: saved files changed' +[ -f "$flash/$name-$old-x86_64-1.txz" ] || fail 'upgradepkg: deleted the archive the next boot installs from' +boot upgradepkg-boot; installed $old upgradepkg-boot +[ "$(config_sums)" = "$before" ] || fail 'upgradepkg-boot: saved files changed' + +# The installer itself refuses an Unraid below the .plg's min, and says why +reset; set_version 6.12.15; cp "$work/$new/dist/$name-$new-x86_64-1.txz" "$flash/" +plugin install "$work/$new/$name.plg" > "$work/min.log" 2>&1 && fail "min: installed on 6.12.15 though min is $min" +grep -q "installed Unraid version is too low, require at least version $min" "$work/min.log" || { cat "$work/min.log"; fail 'min: no reason given'; } +[ ! -e "$live" ] || fail 'min: files installed below min' +set_version "$version" + +echo "Unraid $version: install, upgrade, boot, remove, failed download, failed upgradepkg and min passed" diff --git a/.github/unraid-compat/layout.js b/.github/unraid-compat/layout.js new file mode 100644 index 00000000..39310694 --- /dev/null +++ b/.github/unraid-compat/layout.js @@ -0,0 +1,55 @@ +// The layout rules browser_test.js checks once FV3 has opened a folder, each from a fix FV3 has shipped, read in the page +'use strict'; + +// Whether the page, and the table wrapper where only the table scrolls (7.2's .TableContainer), fit without scrolling sideways +function widths() { + const page = document.documentElement, table = document.querySelector('.TableContainer'); + const fits = (el) => el.scrollWidth <= el.clientWidth + 1; + const size = (el) => `${el.scrollWidth} of ${el.clientWidth} px`; + return { page: fits(page), table: table ? fits(table) : null, sizes: `page ${size(page)}${table ? `, table ${size(table)}` : ''}` }; +} + +// Each rule's failure, '' when it holds. A folder against a loose container on its page (Unraid's name element, a link only +// with a template): name and row no taller than 1.5x theirs. A preview item clipped wholly out of sight drops out of the readout +function layoutState(page) { + const { seen, name, members } = window.fv3Test; + const box = (el) => el.getBoundingClientRect(); + const tall = (node) => { const r = document.createRange(); r.selectNodeContents(node); return Math.round(r.getBoundingClientRect().height); }; + const shown = (label, nameEl, icon, other, loose) => [ + !nameEl || !seen(nameEl) ? `${label}'s name cannot be seen` : tall(nameEl) > 1.5 * tall(other) ? `${label}'s name is ${tall(nameEl)} px tall, ${loose}'s ${tall(other)} px` : '', + !icon || !seen(icon) ? `${label}'s icon cannot be seen` : !icon.complete || !icon.naturalWidth ? `${label}'s icon did not load` : '', + ].filter(Boolean); + // A folder by its name's text, even where that text cannot be seen + const nameOf = (el) => el?.textContent.trim() || 'a folder'; + const rules = { name: [] }; + + if (page === 'Dashboard') { + const tile = [...document.querySelectorAll('tbody#docker_view span.outer')].find((o) => !o.closest('.folder-showcase-outer') && seen(o) && name(o) === 'app-d'); + const otherName = tile?.querySelector('.inner > *'); + if (!otherName) return { error: "no app-d tile to compare folders' tiles with" }; + rules.card = []; + for (const folder of document.querySelectorAll('.folder-showcase-outer')) { + const label = nameOf(folder.querySelector('.fv3-folder-appname')); + rules.name.push(...shown(label, folder.querySelector('.fv3-folder-appname'), folder.querySelector('img.fv3-folder-icon'), otherName, 'app-d')); + const card = box(folder.closest('tbody')); + const own = folder.querySelector(':scope > span.outer'); + const out = [own, ...members(folder)].filter((o) => o && seen(o)).filter((o) => box(o).left < card.left - 1 || box(o).right > card.right + 1); + if (out.length) rules.card.push(`${label}'s ${out.map((o) => (o === own ? 'tile' : name(o))).join(', ')} outside the card (${Math.round(card.left)} to ${Math.round(card.right)} px)`); + } + } else { + const [list, cell, loose] = page === 'Docker' ? ['docker_list', 'ct-name', 'app-d'] : ['kvm_list', 'vm-name', 'Home Assistant']; + const rows = [...document.querySelectorAll(`#${list} > tr`)].filter(seen); + const other = rows.find((r) => !r.classList.contains('folder') && r.querySelector(`td.${cell} span.outer`) && name(r.querySelector(`td.${cell} span.outer`)) === loose); + const otherName = other?.querySelector(`td.${cell} span.inner > *`); + if (!otherName) return { error: `no ${loose} row to compare folders with` }; + rules.height = []; + for (const row of rows.filter((r) => r.classList.contains('folder'))) { + const label = nameOf(row.querySelector('a.folder-appname')); + rules.name.push(...shown(label, row.querySelector('a.folder-appname'), row.querySelector('img.folder-img'), otherName, loose)); + if (box(row).height > 1.5 * box(other).height) rules.height.push(`${label}'s row is ${Math.round(box(row).height)} px tall, ${loose}'s ${Math.round(box(other).height)} px`); + } + } + return { rules: Object.fromEntries(Object.entries(rules).map(([rule, problems]) => [rule, problems.join('; ')])) }; +} + +module.exports = { widths, layoutState }; diff --git a/.github/unraid-compat/lib.sh b/.github/unraid-compat/lib.sh new file mode 100644 index 00000000..ad48d599 --- /dev/null +++ b/.github/unraid-compat/lib.sh @@ -0,0 +1,24 @@ +#!/bin/bash +# Shared by install_test.sh and fake_unraid.sh: source it from the repo root with $version set. These replace system files and +# a download must fail, so they run only in a fresh Docker container with no network (loopback alone), never on a host. +if [ ! -f /.dockerenv ] || [ -e /etc/unraid-version ] || [ -d /boot/config ] || [ "$(ls /sys/class/net 2>/dev/null)" != lo ]; then + echo 'Refusing to run outside a fresh Docker container with no network' >&2; exit 2 +fi +name=folder.view3 +docroot=/usr/local/emhttp +flash=/boot/config/plugins/$name +work=$(mktemp -d) +fail() { echo "FAILED [Unraid $version]: $*"; exit 1; } + +fv3_docroot() { # fv3_docroot : Unraid's /usr/local/emhttp from that webGUI checkout, its symlinks included + [ -f "$1/emhttp/plugins/dynamix.plugin.manager/scripts/plugin" ] || return 1 + mkdir -p $docroot /var/local/emhttp /boot/config/plugins && cp -a "$1/emhttp/." $docroot/ || return 1 + printf 'csrf_token="compat-test-token"\n' > /var/local/emhttp/var.ini || return 1 + # Unraid's hooks and scripts run as #!/usr/bin/php + ln -sf "$(command -v php)" /usr/bin/php +} + +fv3_build() { # fv3_build : the .plg and package as the release workflow makes them, in $work/ + local dir="$work/$2"; mkdir -p "$dir"; cp -R "$1/src" "$1/$name.plg" "$1/pkg_build.sh" "$dir/" + (cd "$dir" && bash pkg_build.sh --version "$2" --branch "$3" --out "$dir/dist" >/dev/null) +} diff --git a/.github/unraid-compat/matrix_check.py b/.github/unraid-compat/matrix_check.py new file mode 100644 index 00000000..23c1b1e0 --- /dev/null +++ b/.github/unraid-compat/matrix_check.py @@ -0,0 +1,51 @@ +#!/usr/bin/env python3 +"""ci.yml's Unraid matrix has one release row per minor from the .plg's min to the newest, and one for any newer release.""" +import pathlib +import re +import sys + +# Only a row that YAML reads: first on its line, so a commented-out row is not counted as tested +ROW = re.compile(r"^[ \t]*- \{unraid: '([^']+)', php: '([^']+)'(?:, row: '([^']+)')?\}", re.M) +RELEASE = re.compile(r'(\d+)\.(\d+)\.\d+') + + +def problems(ci, plg): + floor = re.search(r']*\bmin="(\d+)\.(\d+)\.\d+"', plg) + if not floor: + return ['folder.view3.plg names no min="x.y.z"'] + floor = (int(floor[1]), int(floor[2])) + rows = ROW.findall(ci) + releases = [version for version, _, row in rows if not row] + tags = [version for version, _, row in rows if row == 'newest tag'] + out = [] if len(tags) == 1 else [f"ci.yml needs exactly one 'newest tag' row, not {len(tags)}"] + minors = {} + for version in releases: + m = RELEASE.fullmatch(version) + if not m: + out.append(f'release row {version} is not a release version x.y.z') + else: + minors.setdefault((int(m[1]), int(m[2])), []).append(version) + if not minors: + return out + ['ci.yml has no Unraid release rows'] + for (major, minor), versions in sorted(minors.items()): + if len(versions) > 1: + out.append(f'Unraid {major}.{minor} has {len(versions)} release rows: {", ".join(versions)}') + if (major, minor) < floor: + out.append(f'release row {versions[0]} is below the .plg\'s min {floor[0]}.{floor[1]}') + # From the min's own minor: a major with no rows at all still needs its first minor + for major in range(floor[0], max(minors)[0] + 1): + first = floor[1] if major == floor[0] else 0 + present = [n for m, n in minors if m == major] + missing = [f'{major}.{n}' for n in range(first, max(present, default=first + 1)) if n not in present] + out += [f'no release row for Unraid {v}, between the .plg\'s min and the newest row' for v in missing] + # The 'newest tag' row runs only betas; a release it reaches needs a row of its own and the PHP that release ships + out += [f'Unraid {tag} is out: give it a release row with the PHP its release notes name' for tag in tags + if RELEASE.fullmatch(tag) and tag not in releases] + return out + + +if __name__ == '__main__': + repo = pathlib.Path(__file__).resolve().parents[2] + found = problems((repo / '.github/workflows/ci.yml').read_text(encoding='utf-8'), (repo / 'folder.view3.plg').read_text(encoding='utf-8')) + print('\n'.join(found) or 'The Unraid matrix covers every minor from min to the newest release') + sys.exit(1 if found else 0) diff --git a/.github/unraid-compat/matrix_check_test.py b/.github/unraid-compat/matrix_check_test.py new file mode 100644 index 00000000..5f63c9f6 --- /dev/null +++ b/.github/unraid-compat/matrix_check_test.py @@ -0,0 +1,43 @@ +import unittest + +import matrix_check + +PLG = '' + + +def ci(*rows, tag="'7.4.0', php: '8.4'"): + lines = [f" - {{unraid: '{v}', php: '8.4'}}" for v in rows] + return '\n'.join(lines + [f" - {{unraid: {tag}, row: 'newest tag'}}"]) + + +class MatrixCheckTest(unittest.TestCase): + def test_one_row_per_minor_up_to_the_newest_passes_with_a_beta_or_that_release_as_newest_tag(self): + self.assertEqual(matrix_check.problems(ci('7.0.1', '7.1.4', '7.2.7', '7.3.5', '7.4.0'), PLG), []) + self.assertEqual(matrix_check.problems(ci('7.0.1', '7.1.4', '7.2.7', '7.3.5', '7.4.0', tag="'7.5.0-beta.1', php: '8.4'"), PLG), []) + + def test_a_release_the_newest_tag_reached_needs_its_own_row(self): + self.assertEqual(matrix_check.problems(ci('7.0.1', '7.1.4', '7.2.7', '7.3.5', '7.4.0', tag="'7.5.0', php: '8.4'"), PLG), + ['Unraid 7.5.0 is out: give it a release row with the PHP its release notes name']) + + def test_a_skipped_minor_a_second_row_for_one_and_a_row_below_min_are_named(self): + self.assertEqual(matrix_check.problems(ci('6.12.15', '7.0.1', '7.0.0', '7.2.7', '7.3.5', '7.4.0'), PLG), [ + 'release row 6.12.15 is below the .plg\'s min 7.0', + 'Unraid 7.0 has 2 release rows: 7.0.1, 7.0.0', + 'no release row for Unraid 7.1, between the .plg\'s min and the newest row']) + + def test_a_commented_out_row_is_not_tested_so_not_counted(self): + text = ci('7.0.1', '7.1.4', '7.3.5', '7.4.0').replace(" - {unraid: '7.3.5'", " # - {unraid: '7.2.9', php: '8.4'}\n - {unraid: '7.3.5'") + self.assertEqual(matrix_check.problems(text, PLG), ["no release row for Unraid 7.2, between the .plg's min and the newest row"]) + + def test_the_mins_own_major_needs_rows_when_only_a_later_major_has_them(self): + self.assertEqual(matrix_check.problems(ci('8.0.0', tag="'8.0.0', php: '8.4'"), PLG), + ["no release row for Unraid 7.0, between the .plg's min and the newest row"]) + + def test_a_missing_min_newest_tag_or_release_row_fails_closed(self): + self.assertEqual(matrix_check.problems(ci('7.0.1'), ''), ['folder.view3.plg names no min="x.y.z"']) + self.assertEqual(matrix_check.problems("- {unraid: '7.0.1', php: '8.3'}", PLG), ["ci.yml needs exactly one 'newest tag' row, not 0"]) + self.assertEqual(matrix_check.problems(ci(), PLG), ['ci.yml has no Unraid release rows']) + + +if __name__ == '__main__': + unittest.main() diff --git a/.github/unraid-compat/package-lock.json b/.github/unraid-compat/package-lock.json new file mode 100644 index 00000000..702bb3fa --- /dev/null +++ b/.github/unraid-compat/package-lock.json @@ -0,0 +1,26 @@ +{ + "name": "folder-view3-unraid-browser-test", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "name": "folder-view3-unraid-browser-test", + "devDependencies": { + "playwright-core": "1.65.1" + } + }, + "node_modules/playwright-core": { + "version": "1.65.1", + "resolved": "https://registry.npmjs.org/playwright-core/-/playwright-core-1.65.1.tgz", + "integrity": "sha512-hqPpz7blSMPxWVdoeE1yiBSzXsKiVOd2k+kjCvmUywD2GPO2Dd9HfcWnq9NN0GHbNZDFgZ9JfBFr+X5/BLo0qw==", + "dev": true, + "license": "Apache-2.0", + "bin": { + "playwright-core": "cli.js" + }, + "engines": { + "node": ">=20" + } + } + } +} diff --git a/.github/unraid-compat/package.json b/.github/unraid-compat/package.json new file mode 100644 index 00000000..2a7b95a1 --- /dev/null +++ b/.github/unraid-compat/package.json @@ -0,0 +1,8 @@ +{ + "name": "folder-view3-unraid-browser-test", + "private": true, + "description": "browser_test.js's driver; nothing here ships in the plugin. Its version must equal the Playwright image's in browser_test.sh", + "devDependencies": { + "playwright-core": "1.65.1" + } +} diff --git a/.github/unraid-compat/pages_test.php b/.github/unraid-compat/pages_test.php new file mode 100644 index 00000000..93c7c332 --- /dev/null +++ b/.github/unraid-compat/pages_test.php @@ -0,0 +1,250 @@ + true, CURLOPT_TIMEOUT => 60, CURLOPT_CUSTOMREQUEST => $method, + CURLOPT_HEADERFUNCTION => function ($ch, $line) use (&$headers) { + if (str_contains($line, ':')) { [$k, $v] = explode(':', $line, 2); $headers[strtolower(trim($k))] = trim($v); } + return strlen($line); + }]); + if ($method === 'POST') curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields + ($token === null ? [] : ['csrf_token' => $token]))); + $body = curl_exec($ch); + $status = curl_getinfo($ch, CURLINFO_RESPONSE_CODE); + curl_close($ch); + return [$status, (string)$body, $headers]; +} + +// What one .page file put on the page: Unraid opens each file's part, in the head and again in the body, with a comment naming +// it; a part ends at the next one, at or at the footer +function fv3t_page_part(string $html, string $file): string { + preg_match_all('//', $html, $marks, PREG_OFFSET_CAPTURE); + preg_match_all('#|id="footer"#', $html, $ends, PREG_OFFSET_CAPTURE); + $bounds = array_merge(array_column($marks[0], 1), array_column($ends[0], 1), [strlen($html)]); + sort($bounds); + $part = ''; + foreach ($marks[0] as $i => [$mark, $at]) { + if ($marks[1][$i][0] !== $file) continue; + $from = $at + strlen($mark); + $to = current(array_filter($bounds, fn($b) => $b >= $from)); + $part .= substr($html, $from, $to - $from); + } + return $part; +} + +function get_json(string $path, ?int &$status = null) { + [$status, $body] = req('GET', "/plugins/folder.view3/server/$path"); + return json_decode($body, true); +} + +require __DIR__ . '/fake_logs.php'; +$flash = FV3T_FLASH; +$media = 'MediaAbCdEfGhIjKlMnO'; +$tools = 'ToolsPqRsTuVwXyZaBcD'; +$lab = 'LabFoldAbCdEfGhIjKlM'; +$containers = ['app-a', 'app-b', 'app-d', 'tool-c']; +$vms = ['Debian 12', 'Home Assistant', 'Windows 11']; +$fixture = json_decode(file_get_contents(__DIR__ . '/fixtures/docker.json'), true); +$shortId = fn(string $name) => substr($fixture['inspect'][$name]['Id'], 0, 12); +$sorted = function (array $a): array { sort($a); return $a; }; +// The saved setup fake_unraid.sh writes, as FV3 stores a folder +$folder = fn(string $name, array $members, string $regex = '') => ['name' => $name, 'icon' => '', 'regex' => $regex, 'containers' => $members, 'settings' => ['preview' => 1], 'actions' => []]; +$mediaFolder = $folder('Media', ['app-a']); +$toolsFolder = $folder('Tools', [], '^tool-'); +$labFolder = $folder('Lab', ['Debian 12']); +// read.php adds the fields every reader dereferences (fv3_normalize_folders), without writing them +$read = fn(array $f) => $f + ['hidden_preview' => []]; + +// FV3's saved folders and settings and Unraid's orders, as fake_unraid.sh wrote them: FV3's reads, its pages and each write below +// leave them exactly so, bar what the write is meant to change; $leaves checks all six after the reads, the pages and each write +$dockerPrefs = '/boot/config/plugins/dockerMan/userprefs.cfg'; +$vmPrefs = '/boot/config/plugins/dynamix.vm.manager/userprefs.cfg'; +$autostartFile = '/var/lib/docker/unraid-autostart'; +$saved = fn(string $type) => is_file("$flash/$type.json") ? json_decode(file_get_contents("$flash/$type.json"), true) ?? 'not JSON' : 'absent'; +$state = fn() => ['docker' => $saved('docker'), 'vm' => $saved('vm'), 'settings' => $saved('settings'), 'Docker order' => (string)@file_get_contents($dockerPrefs), + 'VM order' => (string)@file_get_contents($vmPrefs), 'autostart' => (string)@file_get_contents($autostartFile)]; +$seeded = ['docker' => [$media => $mediaFolder, $tools => $toolsFolder], 'vm' => [$lab => $labFolder], 'settings' => 'absent', + 'Docker order' => "0=\"folder-$media\"\n1=\"app-a\"\n2=\"app-b\"\n3=\"folder-$tools\"\n4=\"tool-c\"\n5=\"app-d\"\n", + 'VM order' => "0=\"folder-$lab\"\n1=\"Debian 12\"\n2=\"Home Assistant\"\n3=\"Windows 11\"\n", 'autostart' => "app-a\napp-d 10\n"]; +$leaves = function (string $step, array $changed = []) use ($seeded, $state): void { + static $want = null; + $want = array_replace($want ?? $seeded, $changed); + foreach ($state() as $what => $now) check($now === $want[$what], "$step left $what as " . json_encode($now)); +}; + +// Unraid's own lists, which FV3's pages fold into folders +[$status, $body] = req('GET', '/plugins/dynamix.docker.manager/include/DockerContainers.php'); +check($status === 200 && substr_count($body, "") === 4, "Docker list: status $status, " . substr_count($body, "") . ' container rows, not 4'); +foreach ($containers as $ct) check(str_contains($body, $ct), "Docker list has no row for $ct"); +[$status, $body] = req('GET', '/plugins/dynamix.vm.manager/include/VMMachines.php'); +check($status === 200 && preg_match_all("//", $body) === 3, "VM list: status $status, not 3 VM rows"); +foreach ($vms as $vm) check(str_contains($body, $vm), "VM list has no row for $vm"); +[$status, $body] = req('POST', '/webGui/include/DashboardApps.php', ['docker' => 1, 'vms' => 1, 'vmusage' => 'N']); +foreach (array_merge($containers, $vms) as $name) check($status === 200 && str_contains($body, $name), "Dashboard tiles: status $status, no tile for $name"); + +// FV3's reads; the Docker and VM ones answer from what Unraid's own code got from the fakes +$info = get_json('read_info.php?type=docker', $status); +check($status === 200 && is_array($info) && $sorted(array_keys($info)) === $containers, "read_info docker: status $status, containers " . json_encode(is_array($info) ? array_keys($info) : $info)); +if (is_array($info) && $sorted(array_keys($info)) === $containers) { + $seen = array_map(fn($c) => [$c['info']['State']['Autostart'], $c['info']['State']['Running'], $c['info']['State']['WebUi'], $c['info']['template']['path'] ?? null, $c['shortId']], $info); + ksort($seen); + // [IP] is ipaddr() on 7.0 (network.ini's address); from 7.1 DockerUtil::host() needs a LAN port, which a container with no network lacks + $webui = version_compare($version, '7.1', '<') ? 'http://192.0.2.10:18080/' : 'http://:18080/'; + check($seen === [ + 'app-a' => [true, true, $webui, '/boot/config/plugins/dockerMan/templates-user/my-app-a.xml', $shortId('app-a')], + 'app-b' => [false, true, '', null, $shortId('app-b')], + 'app-d' => [true, true, '', null, $shortId('app-d')], + 'tool-c' => [false, false, '', null, $shortId('tool-c')], + ], 'read_info docker: autostart, running, WebUI, template or short id wrong: ' . json_encode($seen)); +} +$info = get_json('read_info.php?type=vm', $status); +check($status === 200 && is_array($info) && $sorted(array_keys($info)) === $vms, "read_info vm: status $status, VMs " . json_encode(is_array($info) ? array_keys($info) : $info)); +if (is_array($info) && $sorted(array_keys($info)) === $vms) { + $seen = array_map(fn($v) => [$v['state'], $v['autostart'], $v['icon'], $v['vnc_port']], $info); + check($seen === [ + 'Debian 12' => ['running', 1, '/plugins/dynamix.vm.manager/templates/images/debian.png', '5700'], + 'Windows 11' => ['shutoff', false, '/plugins/dynamix.vm.manager/templates/images/windows11.png', ''], + 'Home Assistant' => ['paused', 1, '/plugins/dynamix.vm.manager/templates/images/linux.png', '5701'], + ], 'read_info vm: state, autostart, icon or VNC port wrong: ' . json_encode($seen)); + check($info['Debian 12']['description'] === 'Debian 12 lab server', 'read_info vm: description lost'); +} +check(get_json('read_unraid_order.php?type=docker', $status) === ['app-a', 'app-b', 'tool-c', 'app-d'] && $status === 200, 'read_unraid_order docker: not the saved order'); +check(get_json('read_unraid_order.php?type=vm', $status) === ['Debian 12', 'Home Assistant', 'Windows 11'] && $status === 200, 'read_unraid_order vm: not the saved order'); +$membership = get_json('read_membership.php?type=docker', $status); +check($status === 200 && ($membership['membership'] ?? null) === ['app-a' => 'Media', 'app-b' => 'Media', 'tool-c' => 'Tools'], + "read_membership: status $status, " . json_encode($membership)); +[$status, $body] = req('GET', '/plugins/folder.view3/server/docker_load.php'); +$load = explode("\n", trim($body)); +sort($load); +$want = array_map(fn($n) => $shortId($n) . ';0.50%;12.5MiB / 7.7GiB', ['app-a', 'app-b', 'app-d']); +sort($want); +check($status === 200 && $load === $want, "docker_load: status $status, " . json_encode($load)); +$image = array_key_first($fixture['image_inspect']); +$env = get_json('read_image_env.php?image=' . urlencode($image), $status); +check($status === 200 && $env === ['env' => ['PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin']], "read_image_env: status $status, " . json_encode($env)); +$folders = get_json('read.php?type=docker', $status); +check($status === 200 && $folders === [$media => $read($mediaFolder), $tools => $read($toolsFolder)], 'read docker: ' . json_encode($folders)); +$folders = get_json('read.php?type=vm', $status); +check($status === 200 && $folders === [$lab => $read($labFolder)], 'read vm: ' . json_encode($folders)); +$autostart = get_json('read_autostart.php', $status); +check($status === 200 && ($autostart['autostart'] ?? null) === [['name' => 'app-a', 'wait' => 0], ['name' => 'app-d', 'wait' => 10]], 'read_autostart: ' . json_encode($autostart)); +// Unraid's saved orders, and no FV3 settings, themes, styles or organizer folders yet; re-encoded, so {} and [] stay apart +$exact = [ + 'read_order.php?type=docker' => json_encode(["folder-$media", 'app-a', 'app-b', "folder-$tools", 'tool-c', 'app-d']), + 'read_order.php?type=vm' => json_encode(["folder-$lab", 'Debian 12', 'Home Assistant', 'Windows 11']), + 'read_settings.php' => '{}', 'list_themes.php' => '[]', 'read_css_config.php' => '{}', 'read_organizer_registry.php' => '{"folders":[]}', +]; +foreach ($exact as $path => $want) { + [$status, $body] = req('GET', "/plugins/folder.view3/server/$path"); + check($status === 200 && json_encode(json_decode($body)) === $want, "$path: status $status, " . substr($body, 0, 200)); +} +// All 49 of the CSS tool's defaults, two of them by value +$defaults = get_json('read_css_defaults.php', $status); +check($status === 200 && is_array($defaults) && count($defaults) === 49 && ($defaults['folder-view3-graph-cpu'] ?? null) === '#2b8da3' + && ($defaults['fv3-folder-name-min-width'] ?? null) === '220px', "read_css_defaults: status $status, " . substr(json_encode($defaults), 0, 200)); +// The backup: the seeded folders and nothing else of FV3's yet, stamped with this Unraid, this FV3 and the time +$export = get_json('export_all.php', $status); +$stamp = strtotime((string)($export['exported'] ?? '')); +unset($export['exported']); +check($status === 200 && $export === ['fv3_export_version' => 1, 'plugin_version' => '2099.01.01', 'unraid_version' => $version, + 'docker' => [$media => $mediaFolder, $tools => $toolsFolder], 'vm' => [$lab => $labFolder], 'settings' => [], 'autostart' => [], + 'css_config' => [], 'custom_styles' => [], 'order_docker' => [], 'order_vm' => []], "export_all: status $status, " . json_encode($export)); +check($stamp !== false && abs($stamp - time()) < 300, 'export_all: not stamped with the time'); +// FV3's debug panel shows the error log's last 200 lines; what those lines may hold is checked with the logs below +$raw = trim((string)@file_get_contents("$flash/error.log")); +$log = get_json('read_error_log.php', $status); +check($status === 200 && $log === ['log' => implode("\n", array_slice($raw === '' ? [] : explode("\n", $raw), -200))], "read_error_log: status $status, not the log's tail"); +[$status, $body] = req('GET', '/plugins/folder.view3/server/version.php'); +check($status === 200 && $body === '2099.01.01', "version: status $status, $body"); +// Logical CPUs, as this container's /proc/cpuinfo lists them +$cpus = count(preg_grep('/^processor\s*:/', file('/proc/cpuinfo'))); +[$status, $body] = req('GET', '/plugins/folder.view3/server/cpu.php'); +check($status === 200 && trim($body) === (string)$cpus, "cpu: status $status, $body, not $cpus"); +$leaves("FV3's reads"); + +// FV3's pages in every theme: rendered whole, Unraid's lists that FV3's scripts fold present, FV3's own part present, and every +// file that part links served by this Unraid +$pages = [ + 'Docker' => ['folder.view3.Docker.page', ['/plugins/folder.view3/scripts/docker.js', '/plugins/folder.view3/styles/docker.css'], ['docker_list', 'loadlist()']], + 'VMs' => ['folder.view3.VMs.page', ['/plugins/folder.view3/scripts/vm.js', '/plugins/folder.view3/styles/vm.css'], ['kvm_table', 'kvm_list', 'loadlist()']], + 'Dashboard' => ['folder.view3.Dashboard.page', ['/plugins/folder.view3/scripts/dashboard.js', '/plugins/folder.view3/styles/dashboard.css'], ['docker_view', 'vm_view', 'loadlist()']], + 'Settings/FolderView3' => ['FolderView3.page', ['/plugins/folder.view3/scripts/folderview3.js', '/plugins/folder.view3/styles/folderview3.css'], []], + "Folder?type=docker&id=$media" => ['Folder.page', ['/plugins/folder.view3/scripts/folder.js', '/plugins/folder.view3/styles/folder.css'], []], +]; +$served = []; +@mkdir('/boot/config/plugins/dynamix', 0777, true); +foreach (['black', 'white', 'azure', 'gray'] as $theme) { + file_put_contents('/boot/config/plugins/dynamix/dynamix.cfg', "[display]\ntheme=\"$theme\"\n"); + foreach ($pages as $page => [$file, $needs, $unraid]) { + [$status, $html] = req('GET', "/$page"); + $where = "$page ($theme)"; + check($status === 200 && str_contains($html, ''), "$where: status $status, page cut short"); + // An element id, or a function FV3's script wraps when written as name() + foreach ($unraid as $hook) { + $found = str_ends_with($hook, '()') ? preg_match('/function\s+' . substr($hook, 0, -2) . '\s*\(/', $html) : preg_match("/id=[\"']{$hook}[\"']/", $html); + check($found === 1, "$where: Unraid's page has no $hook for FV3's script"); + } + check(!preg_match('#Error evaluating content in plugins\\\\?/folder\.view3#', $html), "$where: Unraid caught an error in FV3's page"); + $part = fv3t_page_part($html, "plugins/folder.view3/$file"); + if ($part === '') { + check(false, "$where: no part from $file"); + continue; + } + check(!str_contains($part, '{"error":"Internal error"}'), "$where: FV3's error answer inside the page"); + check(preg_match("#/webGui/styles/(?:themes/|default-)$theme\.css#", $html) === 1, "$where: Unraid's $theme stylesheet missing, so the theme was not applied"); + preg_match_all('#(?:src|href)="(/[^"?]+\.(?:js|css))(?:\?[^"]*)?"#', $part, $links); + foreach ($needs as $need) check(in_array($need, $links[1], true), "$where: does not load $need"); + foreach (array_unique($links[1]) as $link) { + $served[$link] ??= req('GET', $link)[0]; + check($served[$link] === 200, "$where: links $link, which this Unraid answers {$served[$link]}"); + } + } +} +@unlink('/boot/config/plugins/dynamix/dynamix.cfg'); +$leaves("FV3's pages"); + +// Writes go through Unraid's CSRF check: a create without the right token saves nothing and FV3 never answers +$create = ['type' => 'docker', 'content' => json_encode($folder('New', ['app-d']))]; +foreach (['missing' => null, 'wrong' => 'not-the-token'] as $case => $token) { + [$status, $body, $headers] = req('POST', '/plugins/folder.view3/server/create.php', $create, $token); + check(!isset($headers['x-fv3-answer']), "create with the token $case: answered by FV3 (status $status)"); + $leaves("create with the token $case"); +} +[$status, $body, $headers] = req('POST', '/plugins/folder.view3/server/create.php', $create); +$new = (string)(json_decode($body, true)['id'] ?? ''); +check($status === 200 && isset($headers['x-fv3-answer']) && $new !== '', "create: status $status, $body"); +$leaves('create', ['docker' => [$media => $mediaFolder, $tools => $toolsFolder, $new => $folder('New', ['app-d'])]]); +[$status] = req('POST', '/plugins/folder.view3/server/update.php', ['type' => 'docker', 'id' => $media, 'content' => json_encode($folder('Media 2', ['app-a']))]); +check($status === 200, "update: status $status"); +$leaves('update', ['docker' => [$media => $folder('Media 2', ['app-a']), $tools => $toolsFolder, $new => $folder('New', ['app-d'])]]); +[$status] = req('POST', '/plugins/folder.view3/server/update.php', ['type' => 'vm', 'id' => $lab, 'content' => json_encode($folder('Lab 2', ['Debian 12']))]); +check($status === 200, "update vm: status $status"); +$leaves('update vm', ['vm' => [$lab => $folder('Lab 2', ['Debian 12'])]]); +[$status] = req('POST', '/plugins/folder.view3/server/delete.php', ['type' => 'docker', 'id' => $new]); +check($status === 200, "delete: status $status"); +$leaves('delete', ['docker' => [$media => $folder('Media 2', ['app-a']), $tools => $toolsFolder]]); +[$status] = req('POST', '/plugins/folder.view3/server/update_settings.php', ['key' => 'dashboard_docker_layout', 'value' => 'classic']); +check($status === 200, "update_settings: status $status"); +$leaves('update_settings', ['settings' => ['dashboard_docker_layout' => 'classic']]); +// Autostart out of the saved order: the sync puts it back in folder order with app-d's wait kept, and changes nothing else +file_put_contents($autostartFile, "app-d 10\napp-a\n"); +[$status, $body] = req('POST', '/plugins/folder.view3/server/sync_order.php', ['type' => 'docker']); +check($status === 200 && json_decode($body) !== null, "sync_order: status $status, $body"); +$leaves('sync_order', ['autostart' => "app-a\napp-d 10\n"]); + +// What the runs left in the logs +foreach (fv3_fake_log_checks() as [$ok, $what]) check($ok, $what); + +if ($failures) { + echo "FAILED [Unraid $version]: " . count($failures) . " of $checks checks\n- " . implode("\n- ", $failures) . "\n"; + exit(1); +} +echo "Unraid $version: Unraid's lists, FV3's reads and writes, and its pages in 4 themes passed ($checks checks)\n"; diff --git a/.github/unraid-compat/pages_test.sh b/.github/unraid-compat/pages_test.sh new file mode 100644 index 00000000..3306f716 --- /dev/null +++ b/.github/unraid-compat/pages_test.sh @@ -0,0 +1,6 @@ +#!/bin/bash +# FV3's endpoints and pages through a real Unraid webGUI. Disposable container only, as root, with no network; run from the +# repo root. Usage: pages_test.sh +set -u +bash .github/unraid-compat/fake_unraid.sh "$1" "$2" || exit 1 +php -d auto_prepend_file= -d display_errors=stderr .github/unraid-compat/pages_test.php "$2" diff --git a/.github/unraid-watch/README.md b/.github/unraid-watch/README.md index 7d20aef2..5b2eab3f 100644 --- a/.github/unraid-watch/README.md +++ b/.github/unraid-watch/README.md @@ -7,11 +7,14 @@ plugin stands. Both go to issue #52. - **check** (every CI run, offline). Reads each GraphQL document out of `scripts/*.js` and `*.page`, and each webgui PHP file the plugin references. Fails when a document is not a plain string, because nothing - downstream could see it. + downstream could see it. Also fails on a `var(--x)` in the plugin's stylesheets with no fallback when only + Unraid defines `--x`, so an Unraid release without it cannot drop the style; a variable the plugin's own + stylesheets declare, or its scripts or server code name (the CSS tool's presets), is the plugin's. - **contract** (pull requests that touch `src/`, this directory or the workflow file, every push to `main` or `beta`, and daily for both). Validates every operation against the `unraid/api` schema on `main` and at the - latest release, and confirms every watched path still exists upstream. Red means the plugin sends something - that is no longer valid, or a file it depends on is gone. The daily run checks each branch with that branch's + latest release, confirms every watched path still exists upstream, and checks every contract on each stable + webgui release tag from the plugin's declared minimum and on the development branch. Red means the plugin sends + something that is no longer valid, a file it depends on is gone, or a release it supports breaks a contract. The daily run checks each branch with that branch's copy of the tool; a manual run checks the branch it is started on. - **watch** (daily, on every push to `beta`, and by hand). It always reads the plugin on `beta`, whichever branch starts it, and does two things: @@ -39,8 +42,14 @@ Once it is on `main` the daily run starts by itself, and still reads `beta`. - **Coming with X.Y**: what the newest OS pre-release has that the newest stable does not. - **Operations by OS release**: how many of the plugin's operations are valid on the API each OS release bundles, and which fields are missing. +- **Contracts by OS release**: whether each contract holds on every webgui release tag from the plugin's minimum + and on the development branch. An OS release the webgui has no tag for (some patch releases) is not read. +- **Unraid CSS by OS release**: per theme, the layout properties Unraid's stylesheets set on an element the + plugin's own CSS styles, that differ between releases, and that the plugin does not set itself: what can + look different on an older or newer Unraid. A rule no longer set on the development branch is listed apart, + as it has often only moved to a stylesheet not read here. - **API not used yet**: Docker and VM operations the plugin never calls, and how much of each type it reads. -- **Still on legacy**: the webgui files the plugin still depends on. +- **Still on legacy**: the webgui files the plugin still depends on, stylesheets aside. OS versions and the API each one bundles are read from the release notes. They state it in prose, in several styles; a release whose notes do not say is listed as not stated rather than guessed. Files are compared by @@ -63,6 +72,12 @@ with `' + name + '` when `watch.json` has an `expand` rule naming the JS map tha operation from an unread field. - `releases`: directory holding the OS release notes. `since` is the oldest OS release to list, and `tags` is the repository whose tags carry those OS versions. +- `manifest`: the plugin's `.plg`. Its `min` attribute is the oldest OS release the contracts and stylesheets + are checked on; the webgui's stable tags from there up are the releases, and finding none is an error. +- `contracts`: what the plugin relies on in a webgui file, as a `path`, a regular expression it must `match`, + and `why`. Optional `since`: the oldest release held to it. Add one with each change that relies on Unraid. +- `styles`: the `themes`, and the stylesheet `files` that make up each one in load order (`{theme}` is + replaced). A file a release does not have is skipped, so files from older and newer layouts can both be listed. ## Running it by hand diff --git a/.github/unraid-watch/css.py b/.github/unraid-watch/css.py new file mode 100644 index 00000000..a0e08f48 --- /dev/null +++ b/.github/unraid-watch/css.py @@ -0,0 +1,185 @@ +"""Stylesheets, FolderView3's and Unraid's: rules as text, the elements FolderView3 styles, and what Unraid sets on them per release.""" +import re + +TOKEN = re.compile(r'[#.][A-Za-z_][\w-]*') +COMMENT = re.compile(r'/\*.*?\*/', re.S) +VAR = re.compile(r'var\(\s*(--[\w-]+)\s*([,)])') +DEFINED = re.compile(r'(?:^|[;{\s])(--[\w-]+)\s*:') +# At-rules whose block holds rules for elements; @keyframes, @font-face and @page hold none +GROUPS = ('@media', '@supports', '@container', '@layer', '@scope') +# What moves or wraps an element. Colours and fonts change with each theme by design, so they are left out +LAYOUT = frozenset({ + 'display', 'position', 'white-space', 'overflow', 'overflow-x', 'overflow-y', 'float', 'clear', 'box-sizing', 'flex', 'flex-direction', + 'flex-wrap', 'flex-grow', 'flex-shrink', 'flex-basis', 'align-items', 'justify-content', 'gap', 'grid-template-columns', 'width', + 'min-width', 'max-width', 'height', 'min-height', 'max-height', 'margin', 'margin-top', 'margin-bottom', 'margin-left', 'margin-right', + 'padding', 'padding-top', 'padding-bottom', 'padding-left', 'padding-right', 'line-height', 'font-size', 'vertical-align', 'text-align', + 'text-overflow', 'word-break', 'overflow-wrap', 'table-layout', 'border-collapse', 'border-spacing', 'top', 'left', 'right', 'bottom', + 'transform', 'z-index', 'visibility', 'content'}) + + +def uncommented(text): + """The text with each comment blanked out, line breaks kept so positions still give line numbers.""" + return COMMENT.sub(lambda m: '\n' * m.group(0).count('\n'), text) + + +def _plain(text): + """[(index, char)] for characters outside quoted strings, so a "}" or ";" in a content value is just text.""" + out, quote = [], None + for i, c in enumerate(text): + if quote: + quote = None if c == quote and text[i - 1] != '\\' else quote + elif c in '"\'': + quote = c + else: + out.append((i, c)) + return out + + +def _split(text, at): + """text cut at each `at` character outside parentheses and strings (a url() or :not() list keeps its ; and ,).""" + parts, depth, start = [], 0, 0 + for i, c in _plain(text): + depth += (c == '(') - (c == ')') + if c == at and depth <= 0: + parts.append(text[start:i]) + start = i + 1 + return parts + [text[start:]] + + +def _selector(text): + """One spelling per selector: spacing around combinators and quotes around attribute values do not make a rule new.""" + text = re.sub(r'\s*([>+~])\s*', r' \1 ', text) + text = re.sub(r'\[\s*([\w-]+)\s*([~|^$*]?=)\s*["\']?([^"\'\]]*?)["\']?\s*\]', r'[\1\2\3]', text) + return ' '.join(text.split()) + + +def _condition(text): + """One spelling per at-rule condition, `@media (max-width: 767px)`, however the stylesheet spaced it.""" + text = re.sub(r'\s*([():,])\s*', r'\1', text) + text = re.sub(r'(?<=[\w)])\(', ' (', text).replace(':', ': ').replace(',', ', ') + return ' '.join(text.split()) + + +def _parts(body): + """[(None, declarations) or (head, block)] of a body in source order: its own declarations and the rules nested in it.""" + items, depth, paren, start, item = [], 0, 0, 0, 0 + for i, c in _plain(body): + paren += (c == '(') - (c == ')') if depth == 0 else 0 + if c == '{': + if depth == 0: + items.append((None, body[start:item])) + head, opened = ' '.join(body[item:i].split()), i + 1 + depth += 1 + elif c == '}' and depth: + depth -= 1 + if depth == 0: + items.append((head, body[opened:i])) + start = item = i + 1 + elif c == ';' and depth == 0 and paren <= 0: + item = i + 1 + return items + [(None, body[start:])] + + +def _set(decls, name, value): + """A later declaration wins, except over an !important one that it is not itself.""" + if not (decls.get(name, '').endswith('!important') and not value.endswith('!important')): + decls[name] = value + + +def _declarations(text): + decls = {} + for part in _split(text, ';'): + name, colon, value = part.partition(':') + if colon and name.strip() and not name.strip().startswith('@'): + # `4px!important` and `4px !important` are the same value + _set(decls, name.strip().lower(), ' '.join(re.sub(r'\s*!\s*important\b', ' !important', value).split())) + return decls + + +def _walk(body, conditions, parents, out): + # In source order: a declaration after a nested rule comes after it, as the browser applies them + for head, block in _parts(body): + if head is None: + decls = _declarations(block) if parents else {} + out += [(conditions, parent, decls) for parent in parents if decls] + continue + if head.startswith('@'): + # `@media(max-width:600px)` needs no space after the keyword + if re.match(r'@[\w-]+', head).group(0).lower() in GROUPS: + _walk(block, ' '.join(filter(None, (conditions, _condition(head)))), parents, out) + continue + selectors = [_selector(s) for s in _split(head, ',') if s.strip()] + # Nesting: & is the parent rule; a nested selector without one is a descendant of it + resolved = [s.replace('&', p) if '&' in s else f'{p} {s}' for p in parents for s in selectors] if parents else selectors + _walk(block, conditions, resolved, out) + + +def rules(text): + """[(at-rule conditions, selector, {property: value})] in document order, nested rules resolved.""" + out = [] + _walk(uncommented(text), '', [], out) + return out + + +def subject(selector): + """The ids and classes of the element a selector styles; none for a ::before box, nothing an attribute value or :not() names.""" + # Innermost first, until none is left: 7.4 wraps its guards as :where(:not(.unapi *)) + bare, previous = re.sub(r'\[[^\]]*\]', '', selector), None + while bare != previous: + bare, previous = re.sub(r'\([^()]*\)', '', bare), bare + last = re.split(r'[\s>+~]+', bare.strip())[-1] + return [] if '::' in last else TOKEN.findall(last) + + +def styled(texts): + """{id or class: {(at-rule conditions, property)} FolderView3 sets on elements that carry it} from its own stylesheets.""" + out = {} + for text in texts: + for conditions, selector, decls in rules(text): + for token in subject(selector): + out.setdefault(token, set()).update((conditions, name) for name in decls) + return out + + +def merged(texts): + """{(at-rule conditions, selector, property): value} for stylesheets in load order: a later one wins, !important aside.""" + out = {} + for text in texts: + for conditions, selector, decls in rules(text): + for name, value in decls.items(): + _set(out, (conditions, selector, name), value) + return out + + +def _covers(ours, token, conditions, name): + # The plugin's own rule for this property covers Unraid's when it holds everywhere, or under the same condition + return ('', name) in ours[token] or (conditions, name) in ours[token] + + +def changes(by_ref, refs, ours): + """[(conditions, selector, property, [(ref, value)])]: layout Unraid changed on elements FolderView3 styles but does not set.""" + out = [] + for conditions, selector, name in sorted(set().union(*(by_ref[r] for r in refs))): + tokens = [t for t in subject(selector) if t in ours] + if name not in LAYOUT or not tokens or any(_covers(ours, t, conditions, name) for t in tokens): + continue + values = [(ref, by_ref[ref].get((conditions, selector, name))) for ref in refs] + if len({v for _, v in values}) > 1: + out.append((conditions, selector, name, values)) + return out + + +def fallbacks(stylesheets, code): + """Problems: var(--x) with no fallback where --x is Unraid's, not declared or keyed by FolderView3's own CSS or code.""" + texts = {f: uncommented(f.read_text(encoding='utf-8')) for f in stylesheets} + ours = {name for text in texts.values() for name in DEFINED.findall(text)} + named = '\n'.join(f.read_text(encoding='utf-8') for f in code) + + def keyed(name): + # The CSS tool's presets ('fv3-x': '#fff') and the generated theme CSS ('x' => '#fff') key the plugin's own variables; + # a name that is only read (getPropertyValue('--x')) is not defined by that + return re.search(r"['\"](?:--)?" + re.escape(name[2:]) + r"['\"]\s*(?::|=>)", named) + return [f'{f.name}:{text.count(chr(10), 0, m.start()) + 1}: var({m.group(1)}) has no fallback and FolderView3 does not ' + f'define {m.group(1)}, so the style needs every Unraid release to; give it a fallback' + for f, text in texts.items() for m in VAR.finditer(text) + if m.group(2) == ')' and m.group(1) not in ours and not keyed(m.group(1))] diff --git a/.github/unraid-watch/github.py b/.github/unraid-watch/github.py index 64911d7f..4e49da31 100644 --- a/.github/unraid-watch/github.py +++ b/.github/unraid-watch/github.py @@ -112,6 +112,22 @@ def read(): return self._call('GET', f'/repos/{repo}/contents/{urllib.parse.quote(path)}', {'ref': ref}, raw=True) return self._once(('file', repo, ref, path), read) + def blob(self, repo, sha): + """A file's text by its blob sha: one read per version of a file, however many releases share it.""" + return self._once(('blob', repo, sha), lambda: self._call('GET', f'/repos/{repo}/git/blobs/{sha}', raw=True)) + + def tags(self, repo): + """Every tag name of a repository.""" + def read(): + names = [] + for page in range(1, 51): + batch = self._call('GET', f'/repos/{repo}/tags', {'page': page, 'per_page': 100}) + names += [t['name'] for t in batch] + if len(batch) < 100: + return names + raise GitHubError(f'{repo}: more than 5000 tags') + return self._once(('tags', repo), read) + def exists(self, repo, ref, path): return bool(self._call('HEAD', f'/repos/{repo}/contents/{urllib.parse.quote(path.rstrip("/"))}', {'ref': ref}, missing=(404,))) diff --git a/.github/unraid-watch/releases.py b/.github/unraid-watch/releases.py index f8a09aa0..05ac29f2 100644 --- a/.github/unraid-watch/releases.py +++ b/.github/unraid-watch/releases.py @@ -16,6 +16,12 @@ def stable(version): return '-' not in version +def os_tag(name): + """A stable Unraid OS release tag such as 7.4.0; the webgui's oldest tags are dates such as 2014.11.10.""" + found = VERSION.fullmatch(name) + return bool(found) and stable(name) and int(found.group(1)) < 1000 + + def heading(text): """First markdown heading of a release-notes file; '' when it has none.""" for line in text.splitlines(): diff --git a/.github/unraid-watch/report.py b/.github/unraid-watch/report.py index 41366faa..f6af135a 100644 --- a/.github/unraid-watch/report.py +++ b/.github/unraid-watch/report.py @@ -91,6 +91,59 @@ def _changed(commits): return f'{len(commits)} commit{"" if len(commits) == 1 else "s"}, newest {code(f"{date} {subject}")}' +def _runs(values): + """`a` in `7.0.0`–`7.3.5`, not set in `7.4.0`–`master`: consecutive refs with the same value as one span.""" + runs = [] + for ref, value in values: + if runs and runs[-1][0] == value: + runs[-1][2] = ref + else: + runs.append([value, ref, ref]) + return ', '.join(f'{"not set" if value is None else code(value)} in {code(a)}' + ('' if a == b else f'–{code(b)}') + for value, a, b in runs) + + +def _contracts(view): + if 'unread' in view: + return ['', f'**Contracts and Unraid CSS by OS release**: could not be read ({code(view["unread"], 300)}).'] + *tags, branch = view['refs'] + span = f'{code(tags[0])} to {code(tags[-1])}' if len(tags) > 1 else code(tags[0]) + out = ['', '**Contracts by OS release**', '', + f'What FolderView3 relies on in the webgui, checked on its release tags {span} and on {code(branch)}.', ''] + for entry, found in view['contracts']: + held = [ref for ref, ok in found.items() if ok is not None] + broken = [f'{"missing" if ok == "missing" else "fails"} on {code(ref)}' for ref, ok in found.items() if ok not in (True, None)] + since = f' (from {code(entry["since"])})' if 'since' in entry else '' + out.append(f'- {code(entry["path"].rsplit("/", 1)[-1])} {entry["why"]}: ' + + (', '.join(broken) if broken else f'holds on all {len(held)}') + since) + return out + + +def _styles(view): + if 'unread' in view: + return [] + themes, changed, gone = list(view['styles']), {}, {} + for theme, rows in view['styles'].items(): + for context, selector, name, values in rows: + # Not set on the development branch any more: often a rule that moved to a stylesheet this does not read + (gone if values[-1][1] is None else changed).setdefault((context, selector, name, tuple(values)), []).append(theme) + + def row(key, who): + context, selector, name, values = key + where = '' if len(who) == len(themes) else f' ({", ".join(who)})' + return f'- {code((context + " " if context else "") + selector, 200)} {code(name)}: {_runs(values)}{where}' + out = ['', '**Unraid CSS by OS release**', '', + 'Layout properties Unraid sets on elements FolderView3 styles, that differ between releases, and that FolderView3 ' + 'does not set itself.', ''] + out += capped([row(k, w) for k, w in changed.items()], 40) or ['- none'] + if view['unmatched']: + out += ['', 'Listed in watch.json but on no release: ' + ', '.join(code(p, 200) for p in view['unmatched']) + '.'] + if gone: + out += ['', f'
No longer set in the stylesheets read here ({len(gone)}; may have moved elsewhere)', ''] + out += capped([row(k, w) for k, w in gone.items()], 40) + ['', '
'] + return out + + def render_status(view): newest, stable, latest = view['releases'] facts = [f'Newest OS release {code(newest[0])} (API {code(newest[1] or "not stated")})'] if newest else [] @@ -124,6 +177,8 @@ def render_status(view): + ', '.join(f'{code(name)} {read}/{total}' for name, read, total in view['types']) + '.'] out += ['', '', '', f'
Still on legacy ({len(view["legacy"])} webgui files)', ''] out += [f'- {code(path, 200)}: {why}' for path, why in view['legacy']] + ['', '
'] + # Last: the longest sections, so a body cut for length loses these before anything above + out += _contracts(view) + _styles(view) return fit('\n'.join(out)) + '\n\n' + STATUS diff --git a/.github/unraid-watch/scan.py b/.github/unraid-watch/scan.py index 15ac1f9b..0abd44a0 100644 --- a/.github/unraid-watch/scan.py +++ b/.github/unraid-watch/scan.py @@ -65,6 +65,21 @@ def operations(plugin, rules): return ops, problems +def stylesheets(plugin): + return _files(plugin, ('styles/*.css',)) + + +def code(plugin): + """The scripts and server code, which name the plugin's own CSS variables (the CSS tool, the generated theme CSS).""" + return _files(plugin, ('scripts/*.js', 'server/*.php')) + + +def minimum(manifest): + """The oldest Unraid release the plugin declares it runs on: the PLUGIN tag's min attribute; None when it has none.""" + found = re.search(r']*\bmin="(\d+\.\d+\.\d+)"', manifest.read_text(encoding='utf-8')) + return found and found.group(1) + + def endpoints(plugin, prefixes): """{upstream path: file:line} for every webgui PHP file the plugin references by URL or include.""" found = {} diff --git a/.github/unraid-watch/status.py b/.github/unraid-watch/status.py index d6414760..d40d6e90 100644 --- a/.github/unraid-watch/status.py +++ b/.github/unraid-watch/status.py @@ -2,6 +2,7 @@ import releases import schema import upstream +from github import GitHubError def stage(gh, cfg, ops, watched, spans): @@ -20,8 +21,8 @@ def stage(gh, cfg, ops, watched, spans): return out -def gather(gh, cfg, ops, watched, heads): - """Everything the status comment shows, as plain data.""" +def gather(gh, cfg, ops, watched, heads, local): + """Everything the status comment shows, as plain data; local is watch.local(): the plugin's floor and styled elements.""" api, docs = upstream.source(cfg, 'schema'), upstream.source(cfg, 'releases') core, tagged = api['repo'], docs['tags'] branch = next(s['branch'] for s in cfg['sources'] if s['repo'] == tagged) @@ -59,5 +60,16 @@ def bundled(release): current = schema.load(gh.file(core, heads[core], api['schema'])) idle, types = schema.coverage(current, api['surface'], schema.check(current, ops)[2], api['namespaces']) + held = upstream.source(cfg, 'contracts') + pinned = {held['branch']: heads[held['repo']]} + try: + refs = upstream.release_refs(gh, held, local['floor']) + styles, unmatched = upstream.stylesheets(gh, held, refs, local['styled'], pinned) + compat = {'refs': refs, 'contracts': upstream.contracts(gh, held, refs, pinned), 'styles': styles, 'unmatched': unmatched} + except GitHubError as e: + # These sections only report: a failed read leaves them out and says so, and the news still goes out + compat = {'refs': [], 'contracts': [], 'styles': {}, 'unread': str(e)} return {'releases': (newest, stable, latest), 'stages': stages, 'matrix': matrix, 'unstated': unstated, - 'idle': idle, 'types': types, 'legacy': sorted(watched[tagged].items())} + 'idle': idle, 'types': types, **compat, + # Stylesheets are not pages to move off: they are what the plugin's own CSS sits on + 'legacy': [(path, why) for path, why in sorted(watched[tagged].items()) if not path.endswith('.css')]} diff --git a/.github/unraid-watch/test_commands.py b/.github/unraid-watch/test_commands.py index afc99793..fab44c34 100644 --- a/.github/unraid-watch/test_commands.py +++ b/.github/unraid-watch/test_commands.py @@ -5,7 +5,7 @@ import watch from github import GitHubError -from world import API, CFG, DOCS, ME, NEW, RELEASE, SCRIPT, SDL, USERPREFS, WEB, WITHOUT_STOP, Case, World, sha, url +from world import API, CFG, DOCS, EVENTS, ME, NEW, RELEASE, SCRIPT, SDL, STYLES, USERPREFS, WEB, WITHOUT_STOP, Case, World, sha, url class ContractTest(Case): @@ -77,10 +77,59 @@ def test_scan_problem_fails_the_contract(self): self.assertEqual(code, 1) self.assertIn('::error::a.js:6:', out) + def test_contracts_are_checked_on_master_and_listed(self): + code, out = self.contract() + self.assertEqual(code, 0) + self.assertIn(f'- contract {EVENTS}: holds on 2 of 2 refs', out) + + def test_a_contract_a_supported_release_breaks_fails_and_names_the_release(self): + self.w.release_line() + self.w.tree(WEB, '9.0.3', {EVENTS: 'ev0'}) + self.w.blob(WEB, 'ev0', "$arrResponse = ['ok' => $DockerClient->startContainer($container)];\n") + code, out = self.contract() + self.assertEqual(code, 1) + self.assertIn(f"::error::unraid/webgui: {EVENTS} does not match \"'success' => \" on 9.0.3: answers in `success`", out) + self.assertIn(f'- contract {EVENTS}: holds on 1 of 2 refs', out) + + def test_a_contract_file_a_release_does_not_have_is_named_missing_not_mismatched(self): + self.w.release_line() + self.w.tree(WEB, '9.0.3', {}) + code, out = self.contract() + self.assertEqual(code, 1) + self.assertIn(f'::error::unraid/webgui: {EVENTS} is missing on 9.0.3: answers in `success`', out) + self.assertNotIn('does not match', out) + + def test_a_release_before_a_contracts_since_is_not_held_to_it(self): + self.w.release_line() + self.w.tree(WEB, '9.0.3', {}) + cfg = {**CFG, 'sources': [{**s, 'contracts': [{**s['contracts'][0], 'since': '9.1.0'}]} if 'contracts' in s else s + for s in CFG['sources']]} + code, out = self.captured(watch.cmd_contract, cfg, self.w.client(), self.w.root, None) + self.assertEqual(code, 0) + self.assertIn(f'- contract {EVENTS}: holds on 1 of 1 refs', out) + + def test_only_stable_releases_from_the_plugins_minimum_are_read(self): + self.w.release_line() + self.contract() + read = {c[1].split('/git/trees/')[1].split('?')[0] for c in self.w.fake.calls if f'/repos/{WEB}/git/trees/' in c[1]} + # master is read at the sha the run pinned it to, not by its moving name + self.assertEqual(read, {'9.0.3', NEW[WEB]}) + + def test_a_manifest_without_a_minimum_stops_the_contract_before_any_request(self): + world = World(self, manifest='\n') + with self.assertRaisesRegex(watch.Problem, 'no min="x.y.z"'): + self.contract(world=world) + self.assertEqual(world.fake.calls, []) + + def test_no_supported_release_tag_is_an_error_not_a_pass_on_master_alone(self): + self.w.tags(WEB, ['8.9.0', '9.1.0-beta.2', '2030.11.10']) + with self.assertRaisesRegex(GitHubError, 'no stable release tag at or after 9.0.0'): + self.contract() + class CheckTest(Case): - def check(self, script): - world = World(self, script) + def check(self, script, **world): + world = World(self, script, **world) code, out = self.captured(watch.cmd_check, CFG, world.root) self.assertEqual(world.fake.calls, []) return code, out @@ -107,6 +156,45 @@ def test_plugin_that_references_no_endpoint_fails(self): self.assertEqual(code, 1) self.assertIn('::error::unraid/webgui: the plugin references none of its endpoints', out) + def test_an_unraid_css_variable_without_a_fallback_fails(self): + code, out = self.check(SCRIPT, styles=STYLES + '.tip { margin: var(--gap); }\n') + self.assertEqual(code, 1) + self.assertIn('::error::a.css:3: var(--gap) has no fallback and FolderView3 does not define --gap', out) + + def test_a_manifest_without_a_minimum_fails(self): + code, out = self.check(SCRIPT, manifest='\n') + self.assertEqual(code, 1) + self.assertIn('::error::plugin.plg: no min="x.y.z" on its PLUGIN tag', out) + + def test_a_missing_manifest_says_it_is_missing(self): + world = World(self) + Path(world.root, 'plugin.plg').unlink() + code, out = self.captured(watch.cmd_check, CFG, world.root) + self.assertEqual(code, 1) + self.assertIn('::error::plugin.plg is missing', out) + + def test_a_plugin_with_no_stylesheets_fails_instead_of_reading_as_nothing_styled(self): + world = World(self) + Path(world.root, 'plugin/styles/a.css').unlink() + code, out = self.captured(watch.cmd_check, CFG, world.root) + self.assertEqual(code, 1) + self.assertIn('::error::plugin/styles has no stylesheets that style an element', out) + + def test_release_checks_with_nothing_to_check_fail_instead_of_passing(self): + web = next(s for s in CFG['sources'] if 'contracts' in s) + for broken in ({**web, 'contracts': []}, {**web, 'styles': {**web['styles'], 'themes': []}}, + {**web, 'contracts': [{'path': EVENTS, 'match': '(', 'why': 'w'}]}, {**web, 'contracts': [{'path': EVENTS}]}): + with self.subTest(broken=broken.get('contracts')): + cfg = {**CFG, 'sources': [broken if s is web else s for s in CFG['sources']]} + code, out = self.captured(watch.cmd_check, cfg, World(self).root) + self.assertEqual(code, 1) + self.assertIn('::error::watch.json: ', out) + + def test_an_empty_stylesheet_fails_the_same_way(self): + code, out = self.check(SCRIPT, styles='') + self.assertEqual(code, 1) + self.assertIn('::error::plugin/styles has no stylesheets that style an element', out) + class MainTest(Case): def main(self, *argv): diff --git a/.github/unraid-watch/test_css.py b/.github/unraid-watch/test_css.py new file mode 100644 index 00000000..b1455670 --- /dev/null +++ b/.github/unraid-watch/test_css.py @@ -0,0 +1,135 @@ +import tempfile +import unittest +from pathlib import Path + +import css + + +class RulesTest(unittest.TestCase): + def test_each_selector_gets_the_declarations_and_its_at_rule(self): + text = '/* a\ncomment */ @media (max-width: 9px) { .a, #b > .c { white-space: pre-line; margin-top:4px!important } }\n.d{display:none}' + self.assertEqual(css.rules(text), [ + ('@media (max-width: 9px)', '.a', {'white-space': 'pre-line', 'margin-top': '4px !important'}), + ('@media (max-width: 9px)', '#b > .c', {'white-space': 'pre-line', 'margin-top': '4px !important'}), + ('', '.d', {'display': 'none'})]) + + def test_nested_rules_resolve_against_their_parent_and_keep_its_own_declarations(self): + text = '.title { display: flex; &:first-of-type { margin-top: 0 } .x, &.y { top: 1px } @media (min-width: 1px) { clear: both } }' + self.assertEqual(css.rules(text), [ + ('', '.title', {'display': 'flex'}), + ('', '.title:first-of-type', {'margin-top': '0'}), + ('', '.title .x', {'top': '1px'}), + ('', '.title.y', {'top': '1px'}), + ('@media (min-width: 1px)', '.title', {'clear': 'both'})]) + + def test_a_declaration_after_a_nested_rule_wins_as_it_does_in_the_browser(self): + self.assertEqual(css.merged(['.tip { & { top: 1px; } top: 2px; }']), {('', '.tip', 'top'): '2px'}) + + def test_a_statement_at_rule_does_not_swallow_the_next_rule_and_keyframes_are_no_elements(self): + text = '@charset "UTF-8";\n@import url(a.css);\n.tip { display: block }\n@keyframes k { from { top: 0 } }' + self.assertEqual(css.rules(text), [('', '.tip', {'display': 'block'})]) + + def test_a_brace_inside_a_string_is_just_text(self): + self.assertEqual(css.rules('.q::after { content: "}"; } .row { top: 0 }'), + [('', '.q::after', {'content': '"}"'}), ('', '.row', {'top': '0'})]) + + def test_spacing_and_attribute_quotes_do_not_make_a_rule_new(self): + a = css.merged(['label+.content{top:0} [type="radio"]~.x{top:0} @media (max-width:9px){.y{top:0}}']) + b = css.merged(['label + .content { top: 0 } [type=radio] ~ .x { top: 0 } @media ( max-width: 9px ) { .y { top: 0 } }']) + self.assertEqual(a, b) + self.assertIn(('@media (max-width: 9px)', '.y', 'top'), a) + + def test_a_guard_nested_in_where_and_not_leaves_the_element_it_styles(self): + self.assertEqual(css.subject('a.button:where(:not(.ext *))'), ['.button']) + self.assertEqual(css.subject('.box:where(:not(:is(.a .b) .c))'), ['.box']) + + def test_a_generated_box_or_an_attribute_value_is_not_the_element(self): + self.assertEqual(css.subject('.tip::before'), []) + self.assertEqual(css.subject('a[href$=".png"]'), []) + + def test_a_group_rule_with_no_space_after_its_keyword_is_still_read(self): + self.assertEqual(css.rules('@media(max-width:600px){.tip{white-space:pre-line}} @supports(display:grid){.row{top:0}}'), + [('@media (max-width: 600px)', '.tip', {'white-space': 'pre-line'}), ('@supports (display: grid)', '.row', {'top': '0'})]) + + def test_a_comma_inside_not_stays_in_its_selector(self): + self.assertEqual(css.rules('.x:not(.p, .q), .z { display: none }'), + [('', '.x:not(.p, .q)', {'display': 'none'}), ('', '.z', {'display': 'none'})]) + + def test_a_semicolon_inside_a_url_does_not_end_the_declaration(self): + self.assertEqual(css.rules('.a { background: url(data:image/png;base64,xy); top: 0 }'), + [('', '.a', {'background': 'url(data:image/png;base64,xy)', 'top': '0'})]) + + def test_the_styled_element_is_the_last_compound_without_what_not_names(self): + self.assertEqual(css.subject('#docker_containers > tr.sortable:not(.folder)'), ['.sortable']) + self.assertEqual(css.subject('div.title:first-of-type'), ['.title']) + self.assertEqual(css.subject('label + .content'), ['.content']) + + +class ChangesTest(unittest.TestCase): + OURS = {'.tip': {('', 'color')}, '.row': {('', 'white-space')}} + + def by_ref(self, old, new): + return {'9.0.0': css.merged([old]), 'master': css.merged([new])} + + def test_a_layout_property_unraid_changed_on_an_element_the_plugin_styles_is_listed(self): + rows = css.changes(self.by_ref('.tip { display: block }', '.tip { white-space: pre-line }'), ['9.0.0', 'master'], self.OURS) + self.assertEqual(rows, [('', '.tip', 'display', [('9.0.0', 'block'), ('master', None)]), + ('', '.tip', 'white-space', [('9.0.0', None), ('master', 'pre-line')])]) + + def test_what_the_plugin_sets_itself_colours_and_elements_it_does_not_style_are_left_out(self): + # .row's colour: an element the plugin styles, a property it does not set, but a colour, not layout + old = '.row { white-space: nowrap; color: red } .tip { color: red } .other { display: block }' + new = '.row { white-space: normal; color: blue } .tip { color: blue } .other { display: flex }' + self.assertEqual(css.changes(self.by_ref(old, new), ['9.0.0', 'master'], self.OURS), []) + + def test_a_later_stylesheet_wins_as_the_page_loads_them(self): + self.assertEqual(css.merged(['.tip { top: 1px }', '.tip { top: 2px }']), {('', '.tip', 'top'): '2px'}) + + def test_an_important_declaration_is_not_overwritten_by_a_plain_one(self): + self.assertEqual(css.merged(['.tip { top: 1px !important }', '.tip { top: 2px }']), {('', '.tip', 'top'): '1px !important'}) + self.assertEqual(css.rules('.tip { top: 1px !important; top: 2px }'), [('', '.tip', {'top': '1px !important'})]) + self.assertEqual(css.merged(['.tip { top: 1px !important }', '.tip { top: 3px !important }']), {('', '.tip', 'top'): '3px !important'}) + + def test_the_plugin_setting_a_property_under_a_condition_covers_only_that_condition(self): + ours = css.styled(['@media (max-width: 600px) { .tip { white-space: normal } }']) + by_ref = {'9.0.0': css.merged(['.tip { white-space: nowrap }']), 'master': css.merged(['.tip { white-space: pre-line }'])} + self.assertEqual([r[2] for r in css.changes(by_ref, ['9.0.0', 'master'], ours)], ['white-space']) + same = {'9.0.0': css.merged(['@media (max-width: 600px) { .tip { white-space: nowrap } }']), + 'master': css.merged(['@media (max-width: 600px) { .tip { white-space: pre-line } }'])} + self.assertEqual(css.changes(same, ['9.0.0', 'master'], ours), []) + + +class FallbacksTest(unittest.TestCase): + def check(self, sheet, code=''): + tmp = tempfile.TemporaryDirectory() + self.addCleanup(tmp.cleanup) + a, js = Path(tmp.name, 'a.css'), Path(tmp.name, 'a.js') + a.write_text(sheet, encoding='utf-8') + js.write_text(code, encoding='utf-8') + return css.fallbacks([a], [js]) + + def test_an_unraid_variable_without_a_fallback_is_named_with_its_line(self): + (problem,) = self.check('/* two\nlines */\n.a { color: var(--text); }\n') + self.assertTrue(problem.startswith('a.css:3: var(--text) has no fallback and FolderView3 does not define --text'), problem) + + def test_a_fallback_or_a_variable_the_plugin_owns_passes(self): + self.assertEqual(self.check('.a { color: var(--text, #fff); }'), []) + self.assertEqual(self.check(':root { --gap: 5px; } .a { gap: var(--gap); }'), []) + self.assertEqual(self.check('.a { color: var(--fv3-name-color); }', "const vars = { 'fv3-name-color': '#fff' };"), []) + self.assertEqual(self.check('.a { color: var(--graph-cpu); }', "$vars = ['graph-cpu' => '#2b8da3'];"), []) + + def test_a_word_the_code_merely_uses_does_not_make_an_unraid_variable_the_plugins(self): + (problem,) = self.check('.a { color: var(--text); }', "fields.push({ type: 'text', label: 'Name' });") + self.assertIn('var(--text) has no fallback', problem) + + def test_code_that_only_reads_a_variable_does_not_define_it(self): + (problem,) = self.check('.a { color: var(--text); }', "getComputedStyle(document.body).getPropertyValue('--text');") + self.assertIn('var(--text) has no fallback', problem) + + def test_a_variable_nested_in_a_fallback_needs_its_own(self): + (problem,) = self.check('.a { color: var(--fv3-x, var(--text)); }', "'fv3-x'") + self.assertIn('var(--text) has no fallback', problem) + + +if __name__ == '__main__': + unittest.main() diff --git a/.github/unraid-watch/test_github.py b/.github/unraid-watch/test_github.py index dbe3043c..bdcf981c 100644 --- a/.github/unraid-watch/test_github.py +++ b/.github/unraid-watch/test_github.py @@ -15,6 +15,16 @@ def compare(self, page, status, total, numbers): self.fake.routes['GET', url(f'/repos/o/r/compare/{sha(1)}...{sha(2)}', page=page, per_page=100)] = { 'status': status, 'total_commits': total, 'commits': [commit(n) for n in numbers]} + def test_tags_follow_every_page(self): + self.fake.routes['GET', url('/repos/o/r/tags', page=1, per_page=100)] = [{'name': f't{n}'} for n in range(100)] + self.fake.routes['GET', url('/repos/o/r/tags', page=2, per_page=100)] = [{'name': 't100'}] + self.assertEqual(self.gh.tags('o/r'), [f't{n}' for n in range(101)]) + + def test_a_blob_is_read_once_however_many_releases_share_it(self): + self.fake.routes['GET', '/repos/o/r/git/blobs/abc'] = '.tip { top: 0 }' + self.assertEqual([self.gh.blob('o/r', 'abc') for _ in range(3)], ['.tip { top: 0 }'] * 3) + self.assertEqual(len(self.fake.sent('GET')), 1) + def test_between_follows_every_page(self): self.compare(1, 'ahead', 105, range(100, 200)) self.compare(2, 'ahead', 105, range(200, 205)) diff --git a/.github/unraid-watch/test_releases.py b/.github/unraid-watch/test_releases.py index d24901b5..8146c8b4 100644 --- a/.github/unraid-watch/test_releases.py +++ b/.github/unraid-watch/test_releases.py @@ -10,6 +10,9 @@ def test_versions_sort_beta_before_rc_before_final(self): ['9.0.9', '9.0.10', '9.1.0-beta.2', '9.1.0-beta.2.1', '9.1.0-beta.10', '9.1.0-rc.1', '9.1.0', '10.0.0-beta.1']) + def test_an_os_release_tag_is_a_stable_version_not_a_date(self): + self.assertEqual([t for t in ('9.4.0', '9.4.0-rc.1', '2030.11.10', 'v9.4.0', '9.4') if releases.os_tag(t)], ['9.4.0']) + def test_stable_means_no_prerelease_suffix(self): self.assertTrue(releases.stable('9.1.0')) self.assertFalse(releases.stable('9.1.0-rc.1')) diff --git a/.github/unraid-watch/test_report.py b/.github/unraid-watch/test_report.py index 4b01e673..a3c627de 100644 --- a/.github/unraid-watch/test_report.py +++ b/.github/unraid-watch/test_report.py @@ -181,5 +181,11 @@ def test_unexpected_comment_link_is_refused(self): report.announce(data, link) +class RunsTest(unittest.TestCase): + def test_releases_with_the_same_value_read_as_one_span(self): + values = [('9.0.0', None), ('9.1.0', None), ('9.2.0', '4px'), ('9.3.0', None), ('master', None)] + self.assertEqual(report._runs(values), 'not set in `9.0.0`–`9.1.0`, `4px` in `9.2.0`, not set in `9.3.0`–`master`') + + if __name__ == '__main__': unittest.main() diff --git a/.github/unraid-watch/test_status.py b/.github/unraid-watch/test_status.py index 8c97d6e3..d4ef3ff2 100644 --- a/.github/unraid-watch/test_status.py +++ b/.github/unraid-watch/test_status.py @@ -4,7 +4,7 @@ import status import upstream import watch -from world import (API, BETA_API, BETA_WEB, CFG, DOCS, LATEST_API, ME, NEW, RELEASE, SCRIPT, STABLE_WEB, USERPREFS, WEB, +from world import (API, BETA_API, BETA_WEB, CFG, DOCS, EVENTS, LATEST_API, ME, NEW, RELEASE, SCRIPT, STABLE_WEB, USERPREFS, WEB, Case, World, commit, sha, url) OPS = [('{ docker { statuses { name } } }', ['a.js:3']), @@ -42,7 +42,7 @@ def view(self): self.w.release_line() return status.gather(self.w.client(), CFG, OPS, {API: {'api/switch.ts': 'page switch'}, WEB: {'emhttp/page/': 'markup', USERPREFS: 'called from a.js:5'}, - DOCS: {}}, NEW) + DOCS: {}}, NEW, self.local()) def test_stages_cover_merged_released_and_the_prerelease(self): titles = [title for title, _ in self.view()['stages']] @@ -98,6 +98,25 @@ def test_status_reads_as_expected(self): '', '', '', + '**Contracts by OS release**', + '', + 'What FolderView3 relies on in the webgui, checked on its release tags `9.0.3` and on `master`.', + '', + '- `Events.php` answers in `success`: holds on all 2', + '', + '**Unraid CSS by OS release**', + '', + 'Layout properties Unraid sets on elements FolderView3 styles, that differ between releases, and that FolderView3 ' + 'does not set itself.', + '', + '- `.tip` `white-space`: not set in `9.0.3`, `pre-line` in `master`', + '', + '
No longer set in the stylesheets read here (1; may have moved elsewhere)', + '', + '- `.tip` `display`: `block` in `9.0.3`, not set in `master` (azure)', + '', + '
', + '', report.STATUS, ])) @@ -108,7 +127,7 @@ def test_each_schema_is_fetched_once_however_many_stages_use_it(self): url(f'/repos/{API}/contents/api/schema.graphql', ref=ref) for ref in (RELEASE, BETA_API, LATEST_API, NEW[API]))) def test_with_no_release_notes_the_stages_that_need_them_say_so(self): - body = report.render_status(status.gather(self.w.client(), CFG, OPS, {API: {}, WEB: {}, DOCS: {}}, NEW)) + body = report.render_status(status.gather(self.w.client(), CFG, OPS, {API: {}, WEB: {}, DOCS: {}}, NEW, self.local())) self.assertIn('Latest API release `v4.0.0`.', body) self.assertIn('**Coming: merged, in no release**\n\n- unraid/api `v4.0.0`...`main`: nothing\n' '- unraid/webgui `not stated`...`master`: not available\n', body) @@ -121,7 +140,7 @@ def test_with_no_release_notes_the_stages_that_need_them_say_so(self): def test_release_whose_notes_do_not_state_the_api_says_so(self): self.w.release_line() self.w.notes(NEW[DOCS], {'9.1.0.md': '# Version 9.1.0-beta.2 2030-01-02\n\nNothing about the API.\n'}) - body = report.render_status(status.gather(self.w.client(), CFG, OPS, {API: {}, WEB: {}, DOCS: {}}, NEW)) + body = report.render_status(status.gather(self.w.client(), CFG, OPS, {API: {}, WEB: {}, DOCS: {}}, NEW, self.local())) self.assertIn('Newest OS release `9.1.0-beta.2` (API `not stated`), latest API release `v4.2.0`.', body) self.assertIn('**Coming: released API, not in an OS build**\n\n- unraid/api `not stated`...`v4.2.0`: not available\n', body) self.assertIn('- API version not stated in the notes: `9.1.0-beta.2`', body) @@ -129,11 +148,42 @@ def test_release_whose_notes_do_not_state_the_api_says_so(self): def test_release_documented_before_its_tag_exists_is_shown_as_not_available(self): self.w.release_line() del self.w.fake.routes['GET', f'/repos/{WEB}/commits/9.1.0-beta.2'] - body = report.render_status(status.gather(self.w.client(), CFG, OPS, {API: {}, WEB: {}, DOCS: {}}, NEW)) + body = report.render_status(status.gather(self.w.client(), CFG, OPS, {API: {}, WEB: {}, DOCS: {}}, NEW, self.local())) self.assertIn('- unraid/webgui `9.1.0-beta.2`...`master`: not available\n', body) self.assertIn('- unraid/webgui `9.0.3`...`9.1.0-beta.2`: not available\n', body) self.assertIn('- unraid/api `v4.0.0`...`v4.1.0`:\n - removed', body) + def test_a_failed_read_leaves_the_release_checks_out_and_says_so(self): + self.w.fake.routes['GET', url(f'/repos/{WEB}/tags', page=1, per_page=100)] = (500, 'boom') + body = report.render_status(status.gather(self.w.client(), CFG, OPS, {API: {}, WEB: {}, DOCS: {}}, NEW, self.local())) + self.assertIn('**Operations by OS release**', body) + self.assertIn('**Contracts and Unraid CSS by OS release**: could not be read (`GET /repos/unraid/webgui/tags: HTTP 500', body) + self.assertNotIn('**Unraid CSS by OS release**', body) + + def test_a_listed_stylesheet_no_release_has_is_named(self): + cfg = {**CFG, 'sources': [{**s, 'styles': {**s['styles'], 'files': s['styles']['files'] + ['css/typo-{theme}.css']}} + if 'styles' in s else s for s in CFG['sources']]} + self.w.release_line() + body = report.render_status(status.gather(self.w.client(), cfg, OPS, {API: {}, WEB: {}, DOCS: {}}, NEW, self.local())) + self.assertIn('Listed in watch.json but on no release: `css/typo-azure.css`, `css/typo-black.css`.', body) + + def test_an_empty_stylesheet_counts_as_missing_not_as_no_change(self): + self.w.release_line() + self.w.blob(WEB, 'old0', ' \n') + body = report.render_status(status.gather(self.w.client(), CFG, OPS, {API: {}, WEB: {}, DOCS: {}}, NEW, self.local())) + self.assertIn('Listed in watch.json but on no release: `css/old-black.css`.', body) + + def test_one_themes_file_missing_on_every_release_is_named_though_another_theme_has_it(self): + self.w.release_line() + self.w.tree(WEB, '9.0.3', {EVENTS: 'ev1', 'css/old-azure.css': 'old1'}) + body = report.render_status(status.gather(self.w.client(), CFG, OPS, {API: {}, WEB: {}, DOCS: {}}, NEW, self.local())) + self.assertIn('Listed in watch.json but on no release: `css/old-black.css`.', body) + + def test_watched_stylesheets_are_not_listed_as_legacy_pages(self): + view = status.gather(self.w.client(), CFG, OPS, {API: {}, WEB: {'css/azure.css': 'theme', USERPREFS: 'prefs'}, DOCS: {}}, + NEW, self.local()) + self.assertEqual(view['legacy'], [(USERPREFS, 'prefs')]) + def test_file_that_differs_with_no_commit_to_show_just_says_differs(self): view = self.view() view['stages'] = [('Coming: merged, in no release', [(API, 'v4.2.0', 'main', [], [('api/switch.ts', 'w', [])])])] @@ -143,7 +193,7 @@ def test_when_the_newest_release_is_stable_there_is_no_prerelease_stage(self): self.w.release_line() self.w.notes(NEW[DOCS], {'9.0.3.md': '# Version 9.0.3 2030-01-01\n\n* The Unraid API version remains 4.0.0.\n'}) self.w.tree(WEB, STABLE_WEB, {'emhttp/page': 't1', USERPREFS: 'u0'}) - body = report.render_status(status.gather(self.w.client(), CFG, OPS, {API: {}, WEB: {}, DOCS: {}}, NEW)) + body = report.render_status(status.gather(self.w.client(), CFG, OPS, {API: {}, WEB: {}, DOCS: {}}, NEW, self.local())) self.assertIn('Newest OS release `9.0.3` (API `4.0.0`), latest API release `v4.2.0`.', body) self.assertNotIn('Coming with', body) self.assertIn('- unraid/webgui `9.0.3`...`master`: nothing', body) @@ -152,7 +202,7 @@ def test_when_the_newest_release_is_stable_there_is_no_prerelease_stage(self): def test_latest_release_that_an_os_build_already_ships_is_one_row(self): self.w.release_line() self.w.fake.routes['GET', f'/repos/{API}/releases/latest'] = {'tag_name': 'v4.1.0'} - body = report.render_status(status.gather(self.w.client(), CFG, OPS, {API: {}, WEB: {}, DOCS: {}}, NEW)) + body = report.render_status(status.gather(self.w.client(), CFG, OPS, {API: {}, WEB: {}, DOCS: {}}, NEW, self.local())) self.assertIn('`v4.1.0` (latest API release, Unraid 9.1.0-beta.2)', body) self.assertIn('- unraid/api `v4.1.0`...`v4.1.0`: nothing', body) @@ -161,7 +211,7 @@ def test_upstream_text_in_the_status_stays_inert(self): self.w.between(API, [50, 51], span=(LATEST_API, NEW[API])) self.w.fake.routes['GET', url(f'/repos/{API}/commits', page=1, path='api/switch.ts', per_page=100, sha=NEW[API])] = [ commit(51, 'x `y` @someone '), commit(50, 'older')] - body = report.render_status(status.gather(self.w.client(), CFG, OPS, {API: {'api/switch.ts': 'w'}, WEB: {}, DOCS: {}}, NEW)) + body = report.render_status(status.gather(self.w.client(), CFG, OPS, {API: {'api/switch.ts': 'w'}, WEB: {}, DOCS: {}}, NEW, self.local())) self.assertIn("`api/switch.ts`: 2 commits, newest `2026-10-01 x 'y' @someone `", body) self.assertTrue(body.endswith('\n\n' + report.STATUS)) @@ -213,6 +263,14 @@ def test_refreshed_even_when_news_was_posted(self): self.w.run() self.assertEqual((len(self.w.posted), len(self.w.status)), (1, 1)) + def test_news_still_goes_out_when_the_release_checks_cannot_be_read(self): + self.w.state() + self.w.hit() + self.w.fake.routes['GET', f'/repos/{WEB}/git/blobs/ev1'] = (500, 'boom') + code, _ = self.w.run() + self.assertEqual((code, len(self.w.posted), len(self.w.status)), (0, 1, 1)) + self.assertIn('could not be read', self.w.status[0][1]) + def test_status_command_prints_and_writes_nothing(self): code, out = self.captured(watch.cmd_status, CFG, self.w.client(), self.w.root) self.assertEqual(code, 0) diff --git a/.github/unraid-watch/upstream.py b/.github/unraid-watch/upstream.py index f482815f..bca34aa6 100644 --- a/.github/unraid-watch/upstream.py +++ b/.github/unraid-watch/upstream.py @@ -1,8 +1,10 @@ """What upstream looks like, read through the GitHub client: schema changes, release notes, file differences.""" import re +import css import releases import schema +from github import GitHubError NOTES = re.compile(r'[\w.-]+\.md\Z') @@ -67,6 +69,59 @@ def validity(gh, src, ops, refs): return out +def release_refs(gh, src, floor): + """Stable release tags at or after floor (the plugin's declared minimum), oldest first, then the development branch.""" + tags = [t for t in gh.tags(src['repo']) if releases.os_tag(t) and releases.order(t) >= releases.order(floor)] + # None would leave only the development branch checked, read as every release passing + if not tags: + raise GitHubError(f'{src["repo"]}: no stable release tag at or after {floor}') + return sorted(tags, key=releases.order) + [src['branch']] + + +def text_at(gh, repo, ref, path): + """A file's text at a ref; None when the file is not there.""" + blob = gh.tree(repo, ref).get(path) + return None if blob is None else gh.blob(repo, blob) + + +def applies(entry, ref): + """A contract with `since` holds only from that release on; the development branch is always held to it.""" + return 'since' not in entry or not releases.VERSION.fullmatch(ref) or releases.order(ref) >= releases.order(entry['since']) + + +def contracts(gh, src, refs, pinned): + """[(contract, {ref: True, False, 'missing' (no such file), or None where it does not apply})]; pinned maps a branch to its sha.""" + out = [] + for entry in src.get('contracts', []): + found = {} + for ref in refs: + text = text_at(gh, src['repo'], pinned.get(ref, ref), entry['path']) if applies(entry, ref) else None + found[ref] = None if not applies(entry, ref) else 'missing' if text is None else bool(re.search(entry['match'], text)) + out.append((entry, found)) + return out + + +def stylesheets(gh, src, refs, ours, pinned): + """({theme: css.changes}, [listed file no release has]) for Unraid's stylesheets; a file one release lacks is skipped.""" + styles = src['styles'] + out, seen = {}, set() + # Each theme's own file is its own input: one theme's themes/{theme}.css does not stand in for another's + listed = list(dict.fromkeys(p.format(theme=t) for t in styles['themes'] for p in styles['files'])) + for theme in styles['themes']: + by_ref = {} + for ref in refs: + texts = [] + for path in (p.format(theme=theme) for p in styles['files']): + text = text_at(gh, src['repo'], pinned.get(ref, ref), path) + # An empty file is no input either: a theme of empty files must not read as "no change" + if text and text.strip(): + texts.append(text) + seen.add(path) + by_ref[ref] = css.merged(texts) + out[theme] = css.changes(by_ref, refs, ours) + return out, [p for p in listed if p not in seen] + + def differences(gh, repo, paths, start, head): """[(path, why, commits)] for watched paths that differ between two commits; commits is None when start is no ancestor.""" a, b = gh.tree(repo, start), gh.tree(repo, head) diff --git a/.github/unraid-watch/watch.json b/.github/unraid-watch/watch.json index 6c15eb26..ab9b5fb4 100644 --- a/.github/unraid-watch/watch.json +++ b/.github/unraid-watch/watch.json @@ -1,6 +1,7 @@ { "issue": 52, "plugin": "src/folder.view3/usr/local/emhttp/plugins/folder.view3", + "manifest": "folder.view3.plg", "expand": [ {"match": "docker { ${gqlAction}", "map": "fv3DockerActionMap"}, {"match": "vm { ${gqlAction}", "map": "fv3VmActionMap"} @@ -48,7 +49,36 @@ "emhttp/plugins/dynamix/include/DefaultPageLayout/BodyInlineJS.php": "task tray openDocker runs through from 7.4", "emhttp/plugins/dynamix/include/TaskCommand.php": "task tray endpoint behind openDocker from 7.4", "emhttp/plugins/dynamix/styles/default-dynamix.css": "tooltipster rules the advanced preview popup is drawn inside (7.4 added white-space: pre-line)", - "emhttp/plugins/dynamix/styles/default-base.css": "sweet-alert, table and menu rules the plugin's dialogs and rows sit in" + "emhttp/plugins/dynamix/styles/default-base.css": "sweet-alert, table and menu rules the plugin's dialogs and rows sit in", + "emhttp/plugins/dynamix/styles/default-color-palette.css": "colour variables the plugin's CSS reads, each with its own fallback", + "emhttp/plugins/dynamix/styles/themes/azure.css": "azure theme rules under the folder rows and dialogs", + "emhttp/plugins/dynamix/styles/themes/black.css": "black theme rules under the folder rows and dialogs", + "emhttp/plugins/dynamix/styles/themes/gray.css": "gray theme rules under the folder rows and dialogs", + "emhttp/plugins/dynamix/styles/themes/white.css": "white theme rules under the folder rows and dialogs", + "emhttp/plugins/dynamix.docker.manager/sheets/DockerContainers.css": "Docker tab rows the folders sit among", + "emhttp/plugins/dynamix.vm.manager/sheets/VMMachines.css": "VMs tab rows the folders sit among", + "emhttp/plugins/dynamix.vm.manager/styles/dynamix.vm.manager.css": "VM list rules the VM folders inherit", + "emhttp/plugins/dynamix/sheets/DashStats.css": "Dashboard tiles the folder tiles are drawn in", + "emhttp/plugins/dynamix.docker.manager/include/Events.php": "answers a container action: true, or the reason Unraid refused it (shared.js fv3ActionAnswer)" + }, + "contracts": [ + {"path": "emhttp/plugins/dynamix.docker.manager/include/Events.php", "match": "'success' => \\$DockerClient->startContainer\\(", + "why": "answers a container start in `success`: true, or the reason it was refused (shared.js fv3ActionAnswer)"}, + {"path": "emhttp/plugins/dynamix.vm.manager/include/VMajax.php", "match": "\\['error' => ", + "why": "answers a refused VM action in `error` (shared.js fv3ActionAnswer)"}, + {"path": "emhttp/plugins/dynamix.docker.manager/include/UserPrefs.php", "match": "\\$save\\[\\] = \\$index\\[\\$i\\+\\+\\]\\.\"=\\\\\"\"\\.\\$name\\.\"\\\\\"\"", + "why": "saves the Docker order as N=\"name\" lines, the format the plugin reads and inserts into (lib.order.php fv3_parse_user_prefs)"} + ], + "styles": { + "themes": ["azure", "black", "gray", "white"], + "files": [ + "emhttp/plugins/dynamix/styles/default-{theme}.css", "emhttp/plugins/dynamix/styles/dynamix-{theme}.css", + "emhttp/plugins/dynamix/styles/default-color-palette.css", "emhttp/plugins/dynamix/styles/default-base.css", + "emhttp/plugins/dynamix/styles/themes/{theme}.css", "emhttp/plugins/dynamix/styles/default-dynamix.css", + "emhttp/plugins/dynamix/styles/jquery.sweetalert.css", "emhttp/plugins/dynamix/styles/default-popup.css", + "emhttp/plugins/dynamix.docker.manager/sheets/DockerContainers.css", "emhttp/plugins/dynamix.vm.manager/styles/dynamix.vm.manager.css", + "emhttp/plugins/dynamix.vm.manager/sheets/VMMachines.css", "emhttp/plugins/dynamix/sheets/DashStats.css" + ] } }, { diff --git a/.github/unraid-watch/watch.py b/.github/unraid-watch/watch.py index 4598e11c..728944cc 100644 --- a/.github/unraid-watch/watch.py +++ b/.github/unraid-watch/watch.py @@ -3,15 +3,17 @@ import argparse import json import os +import re import sys from pathlib import Path +import css import scan import schema import status from github import GitHub, GitHubError from report import announce, code, read_state, render, render_status, restamp, status_comment -from upstream import api_changes, release_notes, source, validity +from upstream import api_changes, contracts, release_notes, release_refs, source, validity HERE = Path(__file__).resolve().parent @@ -38,9 +40,48 @@ def derive(cfg, root): for path, where in found.items(): paths.setdefault(path, f'called from {where}') watched[src['repo']] = paths + problems += release_checks_config(cfg) return ops, watched, problems +def release_checks_config(cfg): + """Problems with the contracts and styles in watch.json: an empty list would read as every release passing.""" + held = next((s for s in cfg['sources'] if 'contracts' in s), {}) + styles = held.get('styles', {}) + problems = [] if held.get('contracts') and styles.get('themes') and styles.get('files') else \ + ['watch.json: the webgui source needs contracts and styles with themes and files, or the release checks read as passing'] + for entry in held.get('contracts', []): + if not all(isinstance(entry.get(key), str) and entry[key] for key in ('path', 'match', 'why')): + problems.append(f'watch.json: a contract needs a path, a match and a why: {entry}') + continue + try: + re.compile(entry['match']) + except re.error as e: + problems.append(f'watch.json: the contract on {entry["path"]} has a match that is not a regular expression ({e})') + return problems + + +def floor(cfg, root): + """The oldest Unraid release the plugin supports, from its manifest; a Problem when it states none.""" + manifest = Path(root, cfg['manifest']) + if not manifest.is_file(): + raise Problem(f'{cfg["manifest"]} is missing, so the oldest Unraid the plugin supports is unknown') + found = scan.minimum(manifest) + if not found: + raise Problem(f'{cfg["manifest"]}: no min="x.y.z" on its PLUGIN tag, so the oldest Unraid it supports is unknown') + return found + + +def local(cfg, root): + """What the checks per release need from the plugin: the oldest Unraid it supports, and what its stylesheets style.""" + sheets = scan.stylesheets(Path(root, cfg['plugin'])) + styled = css.styled(f.read_text(encoding='utf-8') for f in sheets) + # No stylesheets, or none that styles an element by id or class, would read as "Unraid changed nothing the plugin styles" + if not styled: + raise Problem(f'{cfg["plugin"]}/styles has no stylesheets that style an element by id or class, so what the plugin styles is unknown') + return {'floor': floor(cfg, root), 'styled': styled} + + def readable(cfg, root): """(operations, watched paths), or a Problem when the plugin source cannot be read reliably.""" ops, watched, problems = derive(cfg, root) @@ -71,6 +112,12 @@ def show(text): def cmd_check(cfg, root): ops, watched, problems = derive(cfg, root) + plugin = Path(root, cfg['plugin']) + problems += css.fallbacks(scan.stylesheets(plugin), scan.code(plugin)) + try: + local(cfg, root) + except Problem as e: + problems.append(str(e)) for query, where in ops: print(f'{where[0]:22s} {query}') for repo, paths in watched.items(): @@ -84,6 +131,8 @@ def cmd_check(cfg, root): def cmd_contract(cfg, gh, root, refs): ops, watched, errors = derive(cfg, root) + # Read before the first request: a manifest that states no minimum stops the run before anything is fetched + oldest = floor(cfg, root) api = source(cfg, 'schema') lines = [] for ref, bad, deprecated in validity(gh, api, ops, refs or [api['branch'], gh.latest_release(api['repo'])]): @@ -97,6 +146,17 @@ def cmd_contract(cfg, gh, root, refs): gone = [p for p in paths if not gh.exists(repo, head, p)] errors += [f'{repo}: {p} no longer exists on {src["branch"]}; fix the call or watch.json' for p in gone] lines.append(f'- {len(paths) - len(gone)} of {len(paths)} watched paths present in {repo}') + held = source(cfg, 'contracts') + pinned = {held['branch']: gh.sha(held['repo'], held['branch'])} + for entry, found in contracts(gh, held, release_refs(gh, held, oldest), pinned): + missing = [ref for ref, ok in found.items() if ok == 'missing'] + failed = [ref for ref, ok in found.items() if ok is False] + if missing: + errors.append(f'{held["repo"]}: {entry["path"]} is missing on {", ".join(missing)}: {entry["why"]}') + if failed: + errors.append(f'{held["repo"]}: {entry["path"]} does not match {entry["match"]!r} on {", ".join(failed)}: {entry["why"]}') + lines.append(f'- contract {entry["path"]}: holds on {sum(ok is True for ok in found.values())} of ' + f'{sum(ok is not None for ok in found.values())} refs') for error in errors: annotate('error', error) print('\n'.join(lines)) @@ -107,7 +167,7 @@ def cmd_contract(cfg, gh, root, refs): def cmd_status(cfg, gh, root): ops, watched = readable(cfg, root) heads = {src['repo']: gh.sha(src['repo'], src['branch']) for src in cfg['sources']} - show(render_status(status.gather(gh, cfg, ops, watched, heads))) + show(render_status(status.gather(gh, cfg, ops, watched, heads, local(cfg, root)))) return 0 @@ -151,7 +211,7 @@ def cmd_watch(cfg, gh, root, me, base, dry_run): for ref, bad, _ in validity(gh, api, ops, [api['branch'], gh.latest_release(api['repo'])])) + '.' body = render(report, heads) show(body) - view = render_status(status.gather(gh, cfg, ops, watched, heads)) + view = render_status(status.gather(gh, cfg, ops, watched, heads, local(cfg, root))) show(view) if dry_run: return 0 diff --git a/.github/unraid-watch/world.py b/.github/unraid-watch/world.py index 535c113c..35ba1b1e 100644 --- a/.github/unraid-watch/world.py +++ b/.github/unraid-watch/world.py @@ -19,6 +19,9 @@ BOT = {'login': 'github-actions[bot]'} API, WEB, DOCS = 'unraid/api', 'unraid/webgui', 'unraid/docs' USERPREFS = 'emhttp/plugins/dynamix.docker.manager/include/UserPrefs.php' +EVENTS = 'emhttp/plugins/dynamix.docker.manager/include/Events.php' +# The plugin colours .tip and sets .row's white-space itself +STYLES = '.tip { color: var(--c, red); }\n.row { white-space: nowrap; }\n' SDL = ''' type Query { docker: Docker! } type Mutation { docker: DockerMutations! } @@ -36,12 +39,15 @@ CFG = { 'issue': 52, 'plugin': 'plugin', + 'manifest': 'plugin.plg', 'expand': [{'match': 'docker { ${act}', 'map': 'dockerMap'}], 'sources': [ {'repo': API, 'branch': 'main', 'schema': 'api/schema.graphql', 'surface': '(?i)docker', 'namespaces': ['Docker', 'DockerMutations'], 'paths': {'api/switch.ts': 'page switch'}}, {'repo': WEB, 'branch': 'master', 'paths': {'emhttp/page/': 'markup'}, - 'endpoints': {'/plugins/dynamix.docker.manager/': 'emhttp/plugins/dynamix.docker.manager/'}}, + 'endpoints': {'/plugins/dynamix.docker.manager/': 'emhttp/plugins/dynamix.docker.manager/'}, + 'contracts': [{'path': EVENTS, 'match': "'success' => ", 'why': 'answers in `success`'}], + 'styles': {'themes': ['azure', 'black'], 'files': ['css/old-{theme}.css', 'css/{theme}.css']}}, {'repo': DOCS, 'branch': 'main', 'releases': 'notes/', 'since': '9.0.0', 'tags': WEB}, ], } @@ -94,13 +100,16 @@ def sent(self, method): class World: """A plugin, its issue, and three upstream repos that moved from OLD to NEW without touching anything watched.""" - def __init__(self, case, script=SCRIPT): + def __init__(self, case, script=SCRIPT, styles=STYLES, manifest='\n'): tmp = tempfile.TemporaryDirectory() case.addCleanup(tmp.cleanup) self.root = tmp.name scripts = Path(tmp.name, 'plugin/scripts') scripts.mkdir(parents=True) (scripts / 'a.js').write_text(script, encoding='utf-8') + Path(tmp.name, 'plugin/styles').mkdir() + Path(tmp.name, 'plugin/styles/a.css').write_text(styles, encoding='utf-8') + Path(tmp.name, 'plugin.plg').write_text(manifest, encoding='utf-8') self.fake, self.comments = Fake(), [] routes = self.fake.routes routes['GET', url(f'/repos/{ME}/issues/52/comments', page=1, per_page=100)] = self.comments @@ -120,6 +129,18 @@ def __init__(self, case, script=SCRIPT): self.tree(API, ref, {'api/schema.graphql': 's1', 'api/switch.ts': 'w1'}) self.notes(OLD[DOCS], {}) self.notes(NEW[DOCS], {}) + # One supported release that matches master: contracts hold and no stylesheet differs; master is read at its pinned sha + self.tags(WEB, ['9.0.3']) + for ref in ('9.0.3', NEW[WEB]): + self.tree(WEB, ref, {EVENTS: 'ev1', 'css/azure.css': 'st1', 'css/black.css': 'st1'}) + self.blob(WEB, 'ev1', "$arrResponse = ['success' => $DockerClient->startContainer($container)];\n") + self.blob(WEB, 'st1', '.tip { white-space: pre-line; color: red; }\n.row { white-space: normal; }\n') + + def tags(self, repo, names): + self.fake.routes['GET', url(f'/repos/{repo}/tags', page=1, per_page=100)] = [{'name': n} for n in names] + + def blob(self, repo, sha, text): + self.fake.routes['GET', f'/repos/{repo}/git/blobs/{sha}'] = text def between(self, repo, numbers, status='ahead', span=None): start, head = span or (OLD[repo], NEW[repo]) @@ -159,9 +180,15 @@ def release_line(self): self.tree(API, NEW[API], {'api/schema.graphql': 's2', 'api/switch.ts': 'w1'}) self.tree(WEB, STABLE_WEB, {'emhttp/page': 't0', USERPREFS: 'u0'}) self.tree(WEB, BETA_WEB, {'emhttp/page': 't1', USERPREFS: 'u0'}) - self.tree(WEB, NEW[WEB], {'emhttp/page': 't1', USERPREFS: 'u0'}) + self.tree(WEB, NEW[WEB], {'emhttp/page': 't1', USERPREFS: 'u0', EVENTS: 'ev1', 'css/azure.css': 'st1', 'css/black.css': 'st1'}) self.between(API, [50], span=(LATEST_API, NEW[API])) self.between(WEB, [60, 61], status='diverged', span=(STABLE_WEB, BETA_WEB)) + # Only 9.0.3 is a stable OS release at or after the plugin's floor (9.0.0); 9.0.3's azure theme sets .tip's display, + # master's does not but sets its white-space + self.tags(WEB, ['9.1.0-beta.2', '9.0.3', '8.9.0', '2030.11.10']) + self.tree(WEB, '9.0.3', {EVENTS: 'ev1', 'css/old-azure.css': 'old1', 'css/old-black.css': 'old0'}) + self.blob(WEB, 'old1', '.tip { display: block; color: blue; }\n') + self.blob(WEB, 'old0', '.other { top: 0; }\n') self.fake.routes['GET', url(f'/repos/{API}/commits', page=1, path='api/switch.ts', per_page=100, sha=NEW[API])] = [commit(50, 'feat: enable the new page', '2030-01-03T00:00:00Z')] @@ -228,6 +255,9 @@ def setUp(self): def saved(self, body): return report.read_state([bot(body)])[0] + def local(self): + return watch.local(CFG, self.w.root) + def outputs(self): """Point the job outputs at a file and return its path.""" path = Path(self.w.root, 'out') diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index c03e6f0a..6d5fbd4c 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -31,33 +31,24 @@ jobs: - name: Install tools run: sudo apt-get update && sudo apt-get install -y --no-install-recommends libxml2-utils - - name: PHP syntax check - run: | - status=0 - while IFS= read -r f; do - php -l "$f" || status=1 - done < <(find src -name '*.php') - exit $status - - - name: Update status test - run: php .github/scripts/update_status_test.php - - - name: FolderView Plus import tests - run: php .github/scripts/foreign_import_test.php - - - name: Autostart order tests - run: php .github/scripts/page_order_test.php + # php -l on every .php and .page file, then every .github/scripts/*_test.php; a deprecation fails too + - name: PHP syntax check and PHP tests + run: bash .github/scripts/php_tests.sh - - name: POST endpoint answer tests - run: php .github/scripts/post_answers_test.php + - name: Every !important has its reason + working-directory: .github/scripts + run: | + python3 -B -m unittest important_check_test + python3 -B important_check.py - - name: Folder file lock tests - run: php .github/scripts/folder_lock_test.php + - name: The Unraid matrix has a row for every minor up to the newest release + working-directory: .github/unraid-compat + run: | + python3 -B -m unittest matrix_check_test + python3 -B matrix_check.py - name: Variables tab tests - run: | - php .github/scripts/variables_tab_test.php - node .github/scripts/variables_tab_test.js + run: node .github/scripts/variables_tab_test.js - name: Autostart tab tests run: node .github/scripts/autostart_api_test.js @@ -98,6 +89,7 @@ jobs: node settings_order_test.js node import_order_test.js node name_order_test.js + node action_refusal_test.js node rename_update_test.js - name: Validate plugin manifest (XML) @@ -145,29 +137,161 @@ jobs: python -m unittest discover -s .github/unraid-watch -p 'test_*.py' python .github/unraid-watch/watch.py check - # A dispatched run's checks never reach the PR, so vendor-sync's run reports Lint + build there as a commit status. + php: + name: PHP ${{ matrix.php }} + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + # Unraid ships 8.3 up to 7.2.7 and in 7.3.0, 8.4 from 7.2.8, 7.3.1 and 7.4; 8.5 is ahead of every release + php: ['8.3', '8.4', '8.5'] + steps: + - name: Checkout + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + with: + persist-credentials: false + + - name: PHP syntax check and PHP tests + env: + PHP_VERSION: ${{ matrix.php }} + run: docker run --rm -v "$PWD:/repo" -w /repo "php:${PHP_VERSION}-cli" bash .github/scripts/php_tests.sh + + unraid: + name: Unraid ${{ matrix.unraid }}${{ matrix.row && format(' ({0})', matrix.row) || '' }} + runs-on: ubuntu-latest + strategy: + fail-fast: false + matrix: + # One release row per minor from the .plg's min, on the PHP its release notes name; Renovate moves each to its newest + # patch, and the 'newest tag' row to any newer tag. matrix_check.py fails when a release has no row of its own. + include: + - {unraid: '7.0.1', php: '8.3'} + - {unraid: '7.1.4', php: '8.3'} + - {unraid: '7.2.9', php: '8.4'} + - {unraid: '7.3.5', php: '8.4'} + - {unraid: '7.4.0', php: '8.4'} + # Runs only while it names a beta or release candidate; one on a newer PHP needs its php raised by hand + - {unraid: '7.4.0', php: '8.4', row: 'newest tag'} + env: + RUNS: ${{ !matrix.row || contains(matrix.unraid, '-') }} + steps: + - name: Nothing newer than the release rows + if: env.RUNS != 'true' + env: + UNRAID_VERSION: ${{ matrix.unraid }} + run: echo "::notice::Unraid $UNRAID_VERSION is a release, so its own release row tests it" + + - name: Checkout + if: env.RUNS == 'true' + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + with: + persist-credentials: false + + - name: Checkout the last stable release + if: env.RUNS == 'true' + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + with: + persist-credentials: false + ref: main + path: .stable + + - name: Checkout the Unraid webGUI at this version + if: env.RUNS == 'true' + uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7 + with: + persist-credentials: false + repository: unraid/webgui + ref: ${{ matrix.unraid }} + path: .webgui + sparse-checkout: emhttp + + - name: Install, upgrade, remove and failures through Unraid's own plugin installer + if: env.RUNS == 'true' + env: + PHP_VERSION: ${{ matrix.php }} + UNRAID_VERSION: ${{ matrix.unraid }} + run: | + docker build -q -t fv3-unraid --build-arg PHP="$PHP_VERSION" .github/unraid-compat + docker run --rm --network none -v "$PWD:/repo" -w /repo fv3-unraid bash .github/unraid-compat/install_test.sh .webgui "$UNRAID_VERSION" .stable + + # Runs after a failed install test too: the two report on different things + - name: FV3's endpoints and pages through this Unraid's webGUI, Docker and libvirt faked + if: ${{ (success() || failure()) && env.RUNS == 'true' }} + env: + UNRAID_VERSION: ${{ matrix.unraid }} + run: docker run --rm --network none -v "$PWD:/repo" -w /repo fv3-unraid bash .github/unraid-compat/pages_test.sh .webgui "$UNRAID_VERSION" + + # Runs after a failed step above too: this one checks what the pages draw. The runner's own npm installs the driver + - name: FV3's pages in Chromium and Firefox, every theme at desktop, tablet and phone widths + if: ${{ (success() || failure()) && env.RUNS == 'true' }} + timeout-minutes: 20 + env: + UNRAID_VERSION: ${{ matrix.unraid }} + run: | + (cd .github/unraid-compat && npm ci --ignore-scripts --no-audit --no-fund) + bash .github/unraid-compat/browser_test.sh fv3-unraid .webgui "$UNRAID_VERSION" "$RUNNER_TEMP/screenshots" + + - name: Keep the browsers' screenshots + if: ${{ always() && env.RUNS == 'true' }} + uses: actions/upload-artifact@cf430e030ddbb5b0abf93d22962f4752f3646cd9 # v7.0.2 + with: + name: screenshots-unraid-${{ matrix.unraid }} + path: ${{ runner.temp }}/screenshots + retention-days: 14 + + # One check per matrix for beta's branch protection: the version rows rename themselves as Renovate moves them + php-compat: + name: PHP compatibility + needs: php + if: always() + runs-on: ubuntu-latest + steps: + - name: Every PHP version passed + env: + RESULT: ${{ needs.php.result }} + run: '[ "$RESULT" = success ]' + + unraid-compat: + name: Unraid compatibility + needs: unraid + if: always() + runs-on: ubuntu-latest + steps: + - name: Every Unraid version passed + env: + RESULT: ${{ needs.unraid.result }} + run: '[ "$RESULT" = success ]' + + # A dispatched run's checks never reach the PR, so vendor-sync's run reports each gate there as a commit status. report: - needs: validate + needs: [validate, php-compat, unraid-compat] if: always() && github.event_name == 'workflow_dispatch' && inputs.base != '' runs-on: ubuntu-latest permissions: statuses: write steps: - - name: Post the result on the PR's commit + - name: Post the results on the PR's commit env: - RESULT: ${{ needs.validate.result }} + LINT: ${{ needs.validate.result }} + PHP: ${{ needs.php-compat.result }} + UNRAID: ${{ needs.unraid-compat.result }} GH_TOKEN: ${{ github.token }} REPO: ${{ github.repository }} SHA: ${{ github.sha }} RUN_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }} run: | set -euo pipefail - state=failure - [ "$RESULT" = success ] && state=success - gh api "repos/$REPO/statuses/$SHA" -f state="$state" -f context='Lint + build' -f target_url="$RUN_URL" -f description="CI dispatched for this PR: $RESULT" >/dev/null + post() { + local state=failure + [ "$2" = success ] && state=success + gh api "repos/$REPO/statuses/$SHA" -f state="$state" -f context="$1" -f target_url="$RUN_URL" -f description="CI dispatched for this PR: $2" >/dev/null + } + post 'Lint + build' "$LINT" + post 'PHP compatibility' "$PHP" + post 'Unraid compatibility' "$UNRAID" notify-failure: - needs: validate + needs: [validate, php, unraid] if: failure() && github.event_name != 'pull_request' && !inputs.base permissions: {} uses: chodeus/chodeus-ops/.github/workflows/notify-discord.yml@65484add1113c1bbc00827af7237ebe7a1837bbb # main diff --git a/.github/workflows/unraid-watch.yml b/.github/workflows/unraid-watch.yml index 090167d6..a69cea42 100644 --- a/.github/workflows/unraid-watch.yml +++ b/.github/workflows/unraid-watch.yml @@ -19,6 +19,8 @@ on: pull_request: paths: - 'src/**' + # its min attribute is the oldest Unraid release the contracts are checked on + - 'folder.view3.plg' - '.github/unraid-watch/**' - '.github/workflows/unraid-watch.yml' push: diff --git a/CHANGELOG.md b/CHANGELOG.md index 6bab7145..90cb8a04 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,33 @@ After updating, hard-refresh your browser (Ctrl/Cmd+Shift+R) and clear its cache if the page doesn't display correctly — before submitting a debug .json. +## Unreleased + +- Folder actions that Unraid refuses now show its reason instead of raw JSON +- A failed update no longer deletes the package the installed version reinstalls from at boot +- The error log no longer lists Unraid's own PHP warnings as FolderView3 errors +- Unraid's saved Docker order keeps folder members together in every start order mode, Off included +- A container two folders' regexes match is saved with the folder the Docker page shows it in +- Autostart mode Off now says FolderView3 never reorders the autostart file + +## 2026.11.16.2 (beta) + +- Unraid's saved Docker order keeps each folder's containers with it, so new members no longer come first +- A container two folders' regexes match is saved with the folder the Docker page shows it in +- Autostart mode Off now says FolderView3 never reorders the autostart file + +## 2026.11.16.1 (beta) + +- The error log no longer lists Unraid's own PHP warnings as FolderView3 errors + +## 2026.11.14.3 (beta) + +- A failed update no longer deletes the package the installed version reinstalls from at boot + +## 2026.11.14.2 (beta) + +- Folder actions that Unraid refuses now show its reason instead of raw JSON + ## 2026.11.14 - Folders no longer vanish when the Docker, VMs or Dashboard lists reload diff --git a/folder.view3.plg b/folder.view3.plg index 48e66279..b78de3ed 100644 --- a/folder.view3.plg +++ b/folder.view3.plg @@ -720,12 +720,6 @@ After updating, hard-refresh your browser (Ctrl/Cmd+Shift+R) and clear its cache - Initial Release - - - rm -f $(ls /boot/config/plugins/&name;/&name;*.txz 2>/dev/null | grep -v '&pkgname;') - - - https://github.com/&github;/releases/download/v&version;/&pkgname;.txz @@ -735,6 +729,8 @@ After updating, hard-refresh your browser (Ctrl/Cmd+Shift+R) and clear its cache + # Only once the new package is in: a failed download or upgradepkg keeps the archive the next boot installs from + rm -f $(ls /boot/config/plugins/&name;/&name;*.txz 2>/dev/null | grep -v '&pkgname;') for p in /var/log/packages/&name;-*; do [ -e "$p" ] || continue case "$(basename "$p")" in &pkgname;) continue;; esac diff --git a/renovate.json b/renovate.json index 42aeef79..084ed7a3 100644 --- a/renovate.json +++ b/renovate.json @@ -4,6 +4,26 @@ "forkProcessing": "enabled", "baseBranchPatterns": ["beta"], "customManagers": [ + { + "customType": "regex", + "description": "Each release row of the CI matrix's Unraid list, moved to the newest patch of its minor.", + "managerFilePatterns": ["/^\\.github/workflows/ci\\.yml$/"], + "matchStrings": ["- \\{unraid: '(?[^']+)', php: '[^']+'\\}"], + "datasourceTemplate": "github-tags", + "depNameTemplate": "unraid/webgui", + "depTypeTemplate": "unraid-release", + "versioningTemplate": "semver" + }, + { + "customType": "regex", + "description": "The matrix's 'newest tag' row: the newest Unraid tag of any kind, betas and release candidates included.", + "managerFilePatterns": ["/^\\.github/workflows/ci\\.yml$/"], + "matchStrings": ["- \\{unraid: '(?[^']+)', php: '[^']+', row: 'newest tag'\\}"], + "datasourceTemplate": "github-tags", + "depNameTemplate": "unraid/webgui", + "depTypeTemplate": "unraid-newest-tag", + "versioningTemplate": "semver" + }, { "customType": "regex", "description": "actionlint's release tag and tarball SHA-256 in validate-workflows.yml; Renovate takes the same asset's hash from the new release.", @@ -34,9 +54,41 @@ "datasourceTemplate": "git-refs", "packageNameTemplate": "https://github.com/{{{depName}}}", "depTypeTemplate": "bundled" + }, + { + "customType": "regex", + "description": "The Playwright image browser_test.sh runs the browsers in, by tag and digest.", + "managerFilePatterns": ["/^\\.github/unraid-compat/browser_test\\.sh$/"], + "matchStrings": [ + "browsers=(?mcr\\.microsoft\\.com/playwright):(?v[0-9.]+-noble)@(?sha256:[a-f0-9]{64})" + ], + "datasourceTemplate": "docker" } ], "packageRules": [ + { + "description": "Release rows take Unraid releases only (webgui also carries 2014.x date tags, which sort above every 7.x), within their minor.", + "matchDepTypes": ["unraid-release"], + "allowedVersions": "/^\\d{1,2}\\.\\d+\\.\\d+$/" + }, + { + "description": "A new minor gets a row of its own by hand, so a release row never leaves its minor.", + "matchDepTypes": ["unraid-release"], + "matchUpdateTypes": ["major", "minor"], + "enabled": false + }, + { + "description": "Unraid releases and their betas and release candidates, still without the date tags.", + "matchDepTypes": ["unraid-newest-tag"], + "allowedVersions": "/^\\d{1,2}\\.\\d+\\.\\d+(-[0-9A-Za-z.]+)?$/", + "ignoreUnstable": false + }, + { + "description": "One Unraid PR, merged by hand: a patch can change the PHP a release ships (7.2.8 did), so each row's php is checked against the release notes.", + "matchDepNames": ["unraid/webgui"], + "groupName": "Unraid versions", + "automerge": false + }, { "description": "Bundled libraries ship to users, so their updates are fix(deps).", "matchDepTypes": ["bundled"], @@ -59,6 +111,11 @@ "matchFileNames": [".github/scripts/package.json"], "matchPackageNames": ["jquery"], "allowedVersions": "<4" + }, + { + "description": "playwright-core drives only the browsers of its own release, so it moves with the Playwright image in one PR.", + "matchPackageNames": ["playwright-core", "mcr.microsoft.com/playwright"], + "groupName": "Playwright" } ] } diff --git a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/FolderView3.page b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/FolderView3.page index 20a6ec3f..fa3ecf0b 100644 --- a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/FolderView3.page +++ b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/FolderView3.page @@ -599,7 +599,7 @@ require_once('/usr/local/emhttp/plugins/folder.view3/langs/script.php'); @@ -608,7 +608,7 @@ require_once('/usr/local/emhttp/plugins/folder.view3/langs/script.php');
-

Folder-based keeps today's behaviour: the container start order follows your folder layout on the Docker page. Custom sequence uses the order below instead — it cuts across folders and overrides the Docker page order, and survives reboots. Off hands the start order fully back to Unraid; FolderView3 stops writing it entirely.

+

Folder-based keeps today's behaviour: the container start order follows your folder layout on the Docker page. Custom sequence uses the order below instead — it cuts across folders and overrides the Docker page order, and survives reboots. Off hands the start order back to Unraid: containers start in the autostart file's order, which Unraid re-sorts to the Docker page's order when you reorder containers or switch one's autostart there; FolderView3 stops reordering that file.

diff --git a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/de.json b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/de.json index dbad2037..ef254cc8 100644 --- a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/de.json +++ b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/de.json @@ -331,7 +331,7 @@ "autostart-mode": "Startreihenfolge-Modus:", "autostart-mode-folder": "Ordnerbasiert (Standard)", "autostart-mode-custom": "Eigene Reihenfolge", - "autostart-mode-off": "Aus (FolderView3 schreibt die Startreihenfolge nie)", + "autostart-mode-off": "Aus (FolderView3 ordnet die Autostart-Datei nie um)", "autostart-sequence": "Startreihenfolge", "autostart-not-started": "Kein Autostart", "autostart-col-container": "Container", @@ -355,7 +355,7 @@ "dashboard-animation-tooltip": "Aktiviert eine sanfte Gleitanimation beim Auf- und Zuklappen von Ordnern im Dashboard.", "default-preview-tooltip": "Standard-Vorschaustil für neue Ordner. Einzelne Ordner können dies in ihren Einstellungen überschreiben.", "default-update-column-tooltip": "Blendet die Aktualisierungsspalte \"Version\" für alle Ordnerzeilen aus und schafft so horizontalen Platz.", - "autostart-mode-tooltip": "Ordnerbasiert behält das bisherige Verhalten bei: Die Startreihenfolge der Container folgt deiner Ordneranordnung auf der Docker-Seite. Eigene Reihenfolge verwendet stattdessen die Reihenfolge unten — sie gilt ordnerübergreifend, überschreibt die Reihenfolge der Docker-Seite und bleibt nach einem Neustart erhalten. Aus überlässt die Startreihenfolge vollständig Unraid; FolderView3 schreibt sie dann überhaupt nicht mehr.", + "autostart-mode-tooltip": "Ordnerbasiert behält das bisherige Verhalten bei: Die Startreihenfolge der Container folgt deiner Ordneranordnung auf der Docker-Seite. Eigene Reihenfolge verwendet stattdessen die Reihenfolge unten — sie gilt ordnerübergreifend, überschreibt die Reihenfolge der Docker-Seite und bleibt nach einem Neustart erhalten. Aus überlässt die Startreihenfolge Unraid: Container starten in der Reihenfolge der Autostart-Datei, die Unraid nach der Reihenfolge der Docker-Seite neu sortiert, wenn du dort Container umordnest oder ihren Autostart umschaltest; FolderView3 ordnet diese Datei dann nicht mehr um.", "docker-only": "Nur Docker", "preview-text-width-placeholder": "z. B. 80px, 6rem (leer = keine Begrenzung)", "autostart-sequence-hint": "Ziehe Zeilen oder gib eine Positionsnummer ein, um die globale Startreihenfolge festzulegen (oben startet zuerst). Warten ist die Pause in Sekunden, nachdem dieser Container gestartet ist. Nur bearbeitbar, solange der Startreihenfolge-Modus auf Eigene Reihenfolge steht; Änderungen gelten nach Anwenden.", diff --git a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/en.json b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/en.json index 9556ea3a..52625d16 100644 --- a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/en.json +++ b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/en.json @@ -331,7 +331,7 @@ "autostart-mode": "Start order mode:", "autostart-mode-folder": "Folder-based (default)", "autostart-mode-custom": "Custom sequence", - "autostart-mode-off": "Off (FolderView3 never writes the start order)", + "autostart-mode-off": "Off (FolderView3 never reorders the autostart file)", "autostart-sequence": "Startup sequence", "autostart-not-started": "Not autostarted", "autostart-col-container": "Container", @@ -355,7 +355,7 @@ "dashboard-animation-tooltip": "Enables a smooth slide animation when expanding or collapsing folders on the Dashboard.", "default-preview-tooltip": "Default preview style for new folders. Individual folders can override this in their settings.", "default-update-column-tooltip": "Hides the \"Version\" update column for all folder rows, freeing up horizontal space.", - "autostart-mode-tooltip": "Folder-based keeps today's behaviour: the container start order follows your folder layout on the Docker page. Custom sequence uses the order below instead — it cuts across folders and overrides the Docker page order, and survives reboots. Off hands the start order fully back to Unraid; FolderView3 stops writing it entirely.", + "autostart-mode-tooltip": "Folder-based keeps today's behaviour: the container start order follows your folder layout on the Docker page. Custom sequence uses the order below instead — it cuts across folders and overrides the Docker page order, and survives reboots. Off hands the start order back to Unraid: containers start in the autostart file's order, which Unraid re-sorts to the Docker page's order when you reorder containers or switch one's autostart there; FolderView3 stops reordering that file.", "docker-only": "Docker only", "preview-text-width-placeholder": "e.g. 80px, 6rem (blank = no limit)", "autostart-sequence-hint": "Drag rows or type a position number to set the global start order (top starts first). Wait is the pause in seconds after that container starts. Editable only while Start order mode is set to Custom sequence; changes apply on Apply.", diff --git a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/es.json b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/es.json index 6953d964..f45a36b3 100644 --- a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/es.json +++ b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/es.json @@ -331,7 +331,7 @@ "autostart-mode": "Modo de orden de arranque:", "autostart-mode-folder": "Según carpetas (predeterminado)", "autostart-mode-custom": "Secuencia personalizada", - "autostart-mode-off": "Desactivado (FolderView3 nunca escribe el orden de arranque)", + "autostart-mode-off": "Desactivado (FolderView3 nunca reordena el archivo de autoarranque)", "autostart-sequence": "Secuencia de arranque", "autostart-not-started": "Sin autoarranque", "autostart-col-container": "Contenedor", @@ -355,7 +355,7 @@ "dashboard-animation-tooltip": "Activa una animación de deslizamiento suave al expandir o contraer carpetas en el panel de control.", "default-preview-tooltip": "Estilo de vista previa predeterminado para las carpetas nuevas. Cada carpeta puede cambiarlo en su configuración.", "default-update-column-tooltip": "Oculta la columna de actualización \"Versión\" en todas las filas de carpeta y libera espacio horizontal.", - "autostart-mode-tooltip": "Según carpetas mantiene el comportamiento actual: el orden de arranque de los contenedores sigue la disposición de tus carpetas en la página de Docker. Secuencia personalizada usa en su lugar el orden de abajo: abarca todas las carpetas, prevalece sobre el orden de la página de Docker y se conserva tras reiniciar. Desactivado devuelve el orden de arranque por completo a Unraid; FolderView3 deja de escribirlo.", + "autostart-mode-tooltip": "Según carpetas mantiene el comportamiento actual: el orden de arranque de los contenedores sigue la disposición de tus carpetas en la página de Docker. Secuencia personalizada usa en su lugar el orden de abajo: abarca todas las carpetas, prevalece sobre el orden de la página de Docker y se conserva tras reiniciar. Desactivado devuelve el orden de arranque a Unraid: los contenedores arrancan en el orden del archivo de autoarranque, que Unraid reordena según la página de Docker cuando reordenas allí los contenedores o cambias su autoarranque; FolderView3 deja de reordenar ese archivo.", "docker-only": "Solo Docker", "preview-text-width-placeholder": "p. ej. 80px, 6rem (vacío = sin límite)", "autostart-sequence-hint": "Arrastra las filas o escribe un número de posición para fijar el orden de arranque global (la de arriba arranca primero). Espera es la pausa en segundos tras arrancar ese contenedor. Solo se puede editar mientras el modo de orden de arranque sea Secuencia personalizada; los cambios se aplican con Aplicar.", diff --git a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/fr.json b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/fr.json index 1c2a74cd..1a2ca10e 100644 --- a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/fr.json +++ b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/fr.json @@ -331,7 +331,7 @@ "autostart-mode": "Mode d'ordre de démarrage :", "autostart-mode-folder": "Basé sur les dossiers (par défaut)", "autostart-mode-custom": "Séquence personnalisée", - "autostart-mode-off": "Désactivé (FolderView3 n'écrit jamais l'ordre de démarrage)", + "autostart-mode-off": "Désactivé (FolderView3 ne réordonne jamais le fichier de démarrage automatique)", "autostart-sequence": "Séquence de démarrage", "autostart-not-started": "Sans démarrage auto", "autostart-col-container": "Conteneur", @@ -355,7 +355,7 @@ "dashboard-animation-tooltip": "Active une animation de glissement fluide lors du déploiement ou de la réduction des dossiers sur le tableau de bord.", "default-preview-tooltip": "Style d'aperçu par défaut des nouveaux dossiers. Chaque dossier peut le remplacer dans ses paramètres.", "default-update-column-tooltip": "Masque la colonne de mise à jour \"Version\" pour toutes les lignes de dossier, ce qui libère de l'espace horizontal.", - "autostart-mode-tooltip": "Basé sur les dossiers conserve le comportement actuel : l'ordre de démarrage des conteneurs suit la disposition de vos dossiers sur la page Docker. Séquence personnalisée utilise plutôt l'ordre ci-dessous — il s'applique à travers les dossiers, remplace l'ordre de la page Docker et survit aux redémarrages. Désactivé rend entièrement l'ordre de démarrage à Unraid ; FolderView3 cesse complètement de l'écrire.", + "autostart-mode-tooltip": "Basé sur les dossiers conserve le comportement actuel : l'ordre de démarrage des conteneurs suit la disposition de vos dossiers sur la page Docker. Séquence personnalisée utilise plutôt l'ordre ci-dessous — il s'applique à travers les dossiers, remplace l'ordre de la page Docker et survit aux redémarrages. Désactivé rend l'ordre de démarrage à Unraid : les conteneurs démarrent dans l'ordre du fichier de démarrage automatique, qu'Unraid retrie selon l'ordre de la page Docker quand vous y réordonnez des conteneurs ou changez leur démarrage automatique ; FolderView3 cesse de réordonner ce fichier.", "docker-only": "Docker seulement", "preview-text-width-placeholder": "ex. 80px, 6rem (vide = aucune limite)", "autostart-sequence-hint": "Faites glisser les lignes ou saisissez un numéro de position pour définir l'ordre de démarrage global (le haut démarre en premier). Attente est la pause en secondes après le démarrage de ce conteneur. Modifiable uniquement lorsque le mode d'ordre de démarrage est Séquence personnalisée ; les changements s'appliquent avec Appliquer.", diff --git a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/it.json b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/it.json index 6c621946..d99cd3e6 100644 --- a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/it.json +++ b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/it.json @@ -331,7 +331,7 @@ "autostart-mode": "Modalità ordine di avvio:", "autostart-mode-folder": "In base alle cartelle (predefinito)", "autostart-mode-custom": "Sequenza personalizzata", - "autostart-mode-off": "Disattivato (FolderView3 non scrive mai l'ordine di avvio)", + "autostart-mode-off": "Disattivato (FolderView3 non riordina mai il file di avvio automatico)", "autostart-sequence": "Sequenza di avvio", "autostart-not-started": "Senza avvio automatico", "autostart-col-container": "Container", @@ -355,7 +355,7 @@ "dashboard-animation-tooltip": "Attiva un'animazione di scorrimento fluida quando si espandono o comprimono le cartelle nella dashboard.", "default-preview-tooltip": "Stile di anteprima predefinito per le nuove cartelle. Ogni cartella può sovrascriverlo nelle proprie impostazioni.", "default-update-column-tooltip": "Nasconde la colonna di aggiornamento \"Versione\" per tutte le righe delle cartelle, liberando spazio orizzontale.", - "autostart-mode-tooltip": "In base alle cartelle mantiene il comportamento attuale: l'ordine di avvio dei container segue la disposizione delle cartelle nella pagina Docker. Sequenza personalizzata usa invece l'ordine qui sotto — vale per tutte le cartelle, sovrascrive l'ordine della pagina Docker e resta valido dopo un riavvio. Disattivato restituisce completamente l'ordine di avvio a Unraid; FolderView3 smette del tutto di scriverlo.", + "autostart-mode-tooltip": "In base alle cartelle mantiene il comportamento attuale: l'ordine di avvio dei container segue la disposizione delle cartelle nella pagina Docker. Sequenza personalizzata usa invece l'ordine qui sotto — vale per tutte le cartelle, sovrascrive l'ordine della pagina Docker e resta valido dopo un riavvio. Disattivato restituisce l'ordine di avvio a Unraid: i container si avviano nell'ordine del file di avvio automatico, che Unraid riordina secondo la pagina Docker quando vi riordini i container o cambi il loro avvio automatico; FolderView3 smette di riordinare quel file.", "docker-only": "Solo Docker", "preview-text-width-placeholder": "es. 80px, 6rem (vuoto = nessun limite)", "autostart-sequence-hint": "Trascina le righe o digita un numero di posizione per impostare l'ordine di avvio globale (in alto parte per primo). Attesa è la pausa in secondi dopo l'avvio di quel container. Modificabile solo quando la modalità ordine di avvio è Sequenza personalizzata; le modifiche si applicano con Applica.", diff --git a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/pl.json b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/pl.json index c0dc44f9..dd932298 100644 --- a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/pl.json +++ b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/pl.json @@ -331,7 +331,7 @@ "autostart-mode": "Tryb kolejności startu:", "autostart-mode-folder": "Wg folderów (domyślnie)", "autostart-mode-custom": "Własna kolejność", - "autostart-mode-off": "Wyłączone (FolderView3 nigdy nie zapisuje kolejności startu)", + "autostart-mode-off": "Wyłączone (FolderView3 nigdy nie zmienia kolejności w pliku autostartu)", "autostart-sequence": "Kolejność startu", "autostart-not-started": "Bez autostartu", "autostart-col-container": "Kontener", @@ -355,7 +355,7 @@ "dashboard-animation-tooltip": "Włącza płynną animację przesuwania przy rozwijaniu i zwijaniu folderów na Dashboardzie.", "default-preview-tooltip": "Domyślny styl podglądu dla nowych folderów. Poszczególne foldery mogą to zmienić w swoich ustawieniach.", "default-update-column-tooltip": "Ukrywa kolumnę aktualizacji \"Wersja\" we wszystkich wierszach folderów, zwalniając miejsce w poziomie.", - "autostart-mode-tooltip": "Wg folderów zachowuje dotychczasowe działanie: kolejność uruchamiania kontenerów wynika z układu folderów na stronie Docker. Własna kolejność używa zamiast tego kolejności poniżej — obejmuje wszystkie foldery, zastępuje kolejność ze strony Docker i przetrwa ponowne uruchomienie. Wyłączone oddaje kolejność uruchamiania w całości Unraid; FolderView3 całkowicie przestaje ją zapisywać.", + "autostart-mode-tooltip": "Wg folderów zachowuje dotychczasowe działanie: kolejność uruchamiania kontenerów wynika z układu folderów na stronie Docker. Własna kolejność używa zamiast tego kolejności poniżej — obejmuje wszystkie foldery, zastępuje kolejność ze strony Docker i przetrwa ponowne uruchomienie. Wyłączone oddaje kolejność uruchamiania Unraid: kontenery startują w kolejności pliku autostartu, który Unraid sortuje według strony Docker, gdy zmienisz tam kolejność kontenerów lub przełączysz ich autostart; FolderView3 przestaje zmieniać kolejność w tym pliku.", "docker-only": "Tylko Docker", "preview-text-width-placeholder": "np. 80px, 6rem (puste = bez limitu)", "autostart-sequence-hint": "Przeciągnij wiersze lub wpisz numer pozycji, aby ustawić globalną kolejność uruchamiania (górny startuje pierwszy). Oczekiwanie to przerwa w sekundach po uruchomieniu danego kontenera. Edycja jest możliwa tylko, gdy tryb kolejności startu to Własna kolejność; zmiany obowiązują po kliknięciu Zastosuj.", diff --git a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/zh.json b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/zh.json index 6d40bb79..744e7467 100644 --- a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/zh.json +++ b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/langs/zh.json @@ -331,7 +331,7 @@ "autostart-mode": "启动顺序模式:", "autostart-mode-folder": "按文件夹(默认)", "autostart-mode-custom": "自定义顺序", - "autostart-mode-off": "关闭(FolderView3 不再写入启动顺序)", + "autostart-mode-off": "关闭(FolderView3 从不重新排列自动启动文件)", "autostart-sequence": "启动顺序", "autostart-not-started": "未自动启动", "autostart-col-container": "容器", @@ -355,7 +355,7 @@ "dashboard-animation-tooltip": "在仪表板上展开或折叠文件夹时启用平滑的滑动动画。", "default-preview-tooltip": "新文件夹的默认预览样式。各个文件夹可以在其设置中覆盖此项。", "default-update-column-tooltip": "隐藏所有文件夹行的“版本”更新栏,腾出水平空间。", - "autostart-mode-tooltip": "按文件夹保持现有行为:容器的启动顺序跟随 Docker 页面上的文件夹布局。自定义顺序改用下方的顺序——它跨越文件夹,覆盖 Docker 页面的顺序,并在重启后保留。关闭则将启动顺序完全交还给 Unraid;FolderView3 将完全停止写入它。", + "autostart-mode-tooltip": "按文件夹保持现有行为:容器的启动顺序跟随 Docker 页面上的文件夹布局。自定义顺序改用下方的顺序——它跨越文件夹,覆盖 Docker 页面的顺序,并在重启后保留。关闭则将启动顺序交还给 Unraid:容器按自动启动文件的顺序启动;当你在 Docker 页面调整容器顺序或切换其自动启动时,Unraid 会按该页面的顺序重新排列此文件;FolderView3 将停止重新排列该文件。", "docker-only": "仅 Docker", "preview-text-width-placeholder": "例如 80px、6rem(留空 = 不限制)", "autostart-sequence-hint": "拖动行或输入位置编号来设置全局启动顺序(最上面的最先启动)。等待是该容器启动后暂停的秒数。仅当启动顺序模式设为自定义顺序时才可编辑;点击应用后生效。", diff --git a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/scripts/docker.js b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/scripts/docker.js index c48fb954..7ad2597c 100644 --- a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/scripts/docker.js +++ b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/scripts/docker.js @@ -13,6 +13,7 @@ let folderobserverConfig = { let folderReq = []; const fv3Folders = fv3FolderData(); let fv3RemeasureWidths = false; +let fv3OrderSyncAsked = false; // Folder rendering — orchestrate (createFolders) and per-folder build (createFolder) const createFolders = async () => { @@ -219,6 +220,8 @@ const createFolders = async () => { globalFolders = foldersDone; fv3Debug('createFolders', 'Assigned foldersDone to globalFolders:', {...globalFolders}); + fv3SyncOrderOnce(unraidOrder, globalFolders); + requestAnimationFrame(() => fv3SyncPreviewHeights('docker_listview_mode')); fv3SyncOrganizer(globalFolders); @@ -1336,12 +1339,25 @@ $(document).ajaxComplete((event, jqXHR, settings) => { && data.indexOf('names=') !== -1; if (isAutostartToggle || isOrderSave) { fv3Debug('autostartSync', 'Rebuilding autostart after Unraid event', {url, sample: data.slice(0, 80)}); - // a refused sync leaves the start order stale: fv3ShowBanner shows the same message once until it is closed - $.post('/plugins/folder.view3/server/sync_order.php', {type: 'docker'}) - .fail((err) => fv3ShowBanner(fv3I18nOr('autostart-sync-failed', 'The Docker start order could not be updated: $1', fv3FailReason(err)))); + fv3SyncDockerOrder(); } }); +// Runs syncContainerOrder() (sync_order.php). A refused sync leaves the start order stale: fv3ShowBanner shows the same message +// once until it is closed +function fv3SyncDockerOrder() { + return $.post('/plugins/folder.view3/server/sync_order.php', {type: 'docker'}) + .fail((err) => fv3ShowBanner(fv3I18nOr('autostart-sync-failed', 'The Docker start order could not be updated: $1', fv3FailReason(err)))); +} + +// A container that joined a folder by regex or label stays missing from userprefs.cfg until a sync: one per page load, when a +// folder holds a container. With nothing saved there is nothing to regroup: the sync never creates userprefs.cfg +function fv3SyncOrderOnce(saved, folders) { + if (fv3OrderSyncAsked || !saved.length || !Object.values(folders).some((f) => Object.keys(f.containers ?? {}).length)) return; + fv3OrderSyncAsked = true; + fv3SyncDockerOrder(); +} + // checkAll shows "checking..." in every version cell until its loadlist's reply, and a measure before then caps the // labels at that text — so the rebuild from that reply drops the cache (not here, while the cells still read it). $(document).ajaxComplete((event, jqXHR, settings) => { diff --git a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/scripts/shared.js b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/scripts/shared.js index 812d4988..241972b6 100644 --- a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/scripts/shared.js +++ b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/scripts/shared.js @@ -1391,10 +1391,20 @@ window.fv3ContainerAction = async (type, id, action) => { return null; }; +// Unraid's answer to a container or VM action in the shape the folder actions read: a refusal's reason is in `success` (Events.php) +// or `error` (VMajax.php); an answer that is no object at all is a failure too +window.fv3ActionAnswer = (answer) => { + if (!answer || typeof answer !== 'object') return { success: false, text: answer ? String(answer) : fv3I18nOr('request-failed', 'Request failed') }; + if (answer.success === true) return answer; + var reason = typeof answer.success === 'string' && answer.success ? answer.success : answer.error; + return typeof reason === 'string' && reason ? { success: false, text: reason } : answer; +}; + window.fv3DockerAction = (action, containerId, fullId) => { var gqlAction = fv3DockerActionMap[action]; var phpFallback = () => $.post(window.eventURL || '/update.htm', { action: action, container: containerId }, null, 'json').promise() - .fail(() => fv3ShowBanner(fv3I18nOr('container-action-failed', 'The container action "$1" failed. Check your server connection.', action))); + .fail(() => fv3ShowBanner(fv3I18nOr('container-action-failed', 'The container action "$1" failed. Check your server connection.', action))) + .then(fv3ActionAnswer); if (fv3ApiAvailable && gqlAction && fullId) { return fv3GraphQL('mutation($id: PrefixedID!) { docker { ' + gqlAction + '(id: $id) { id } } }', { id: fullId }) .then(() => { fv3Debug('API', 'Docker', gqlAction, containerId, 'OK'); return { success: true }; }) @@ -1409,7 +1419,8 @@ window.fv3DockerAction = (action, containerId, fullId) => { window.fv3VmAction = (action, uuid) => { var gqlAction = fv3VmActionMap[action]; var phpFallback = () => $.post('/plugins/dynamix.vm.manager/include/VMajax.php', { action: action, uuid: uuid }, null, 'json').promise() - .fail(() => fv3ShowBanner(fv3I18nOr('vm-action-failed', 'The VM action "$1" failed. Check your server connection.', action.replace('domain-', '')))); + .fail(() => fv3ShowBanner(fv3I18nOr('vm-action-failed', 'The VM action "$1" failed. Check your server connection.', action.replace('domain-', '')))) + .then(fv3ActionAnswer); if (fv3ApiAvailable && gqlAction) { return fv3GraphQL('mutation($id: PrefixedID!) { vm { ' + gqlAction + '(id: $id) } }', { id: uuid }) .then(() => { fv3Debug('API', 'VM', gqlAction, uuid, 'OK'); return { success: true }; }) @@ -1901,6 +1912,7 @@ window.fv3InstallDockerTableWidthFix = () => { if (!storage || !storage.children.length) return; const children = Array.from(storage.children); const origStyles = children.map(c => c.getAttribute('style') || ''); + // !important: an inline !important outranks every stylesheet, so no theme or custom CSS shows a row while it is measured children.forEach(c => c.style.cssText += ';visibility:hidden !important;'); children.forEach(c => folder.parentNode.insertBefore(c, folder.nextSibling)); void tbl.offsetHeight; diff --git a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/server/lib.folders.php b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/server/lib.folders.php new file mode 100644 index 00000000..f94fc5fb --- /dev/null +++ b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/server/lib.folders.php @@ -0,0 +1,265 @@ + "$type.json is unreadable"]); + } + // Unparseable stays byte-identical so the client's cached-copy fallback still fires + $decoded = json_decode($raw, true); + if (json_last_error() !== JSON_ERROR_NONE) { return $raw; } + // Valid JSON that isn't a folder map fails like an unreadable file + if (!is_array($decoded)) { + fv3_error_log('readFolder', "$type.json does not contain a folder map"); + http_response_code(500); + return json_encode(['error' => "$type.json does not contain a folder map"]); + } + $clean = json_encode((object)fv3_without_dead_plus_icons(fv3_normalize_folders($decoded))); + return $clean !== false ? $clean : $raw; + } + + // Fills the fields every reader dereferences so a hand-edited folder can't break a render + // or the autostart sync; object fields stay JSON objects. Never writes the file. + function fv3_normalize_folders(array $folders): array { + $out = []; + foreach ($folders as $id => $f) { + if (!is_array($f)) { continue; } + $f['name'] = is_scalar($f['name'] ?? null) ? (string)$f['name'] : (string)$id; + $f['icon'] = is_string($f['icon'] ?? null) ? $f['icon'] : ''; + $f['regex'] = is_string($f['regex'] ?? null) ? $f['regex'] : ''; + $f['containers'] = is_array($f['containers'] ?? null) ? array_values($f['containers']) : []; + $f['hidden_preview'] = is_array($f['hidden_preview'] ?? null) ? array_values($f['hidden_preview']) : []; + $f['actions'] = is_array($f['actions'] ?? null) ? array_values(array_filter($f['actions'], 'is_array')) : []; + $f['settings'] = (object)(is_array($f['settings'] ?? null) ? $f['settings'] : []); + foreach (['containerImages', 'containerIds'] as $k) { + if (array_key_exists($k, $f)) { $f[$k] = (object)(is_array($f[$k]) ? $f[$k] : []); } + } + $out[$id] = $f; + } + return $out; + } + + // A container may be explicit in at most one folder (issue #62). $winnerId's list survives + // intact (the folder just saved wins); elsewhere ties resolve first-wins in key order. + function fv3_dedupe_explicit_members(array $folders, ?string $winnerId = null): array { + $claimed = []; + // PHP stores an all-digit id as an int key, so compare canonical strings below — on === + // the winner would fail to match its own id and be stripped of the members claimed here + $winnerKey = $winnerId === null ? null : (string)$winnerId; + $winnerMembers = $winnerId !== null ? ($folders[$winnerId]['containers'] ?? null) : null; + if (is_array($winnerMembers)) { + foreach ($winnerMembers as $ct) { + if (is_string($ct)) { $claimed[$ct] = true; } + } + } + foreach ($folders as $fid => $folder) { + if (($winnerKey !== null && (string)$fid === $winnerKey) || !is_array($folder['containers'] ?? null)) { continue; } + $kept = []; + foreach ($folder['containers'] as $ct) { + if (is_string($ct)) { + if (isset($claimed[$ct])) { continue; } + $claimed[$ct] = true; + } + $kept[] = $ct; + } + $folders[$fid]['containers'] = $kept; + } + return $folders; + } + + // Returns the folder's id (create.php hands a new one back for the import to place) + function updateFolder(string $type, string $content, ?string $id = null) : string { + $decoded = json_decode($content, true); + // A folder must be an object/array — reject scalars so a full-backup bundle can't pollute $type.json + if (json_last_error() !== JSON_ERROR_NONE || !is_array($decoded)) { + http_response_code(400); + exit; + } + // 'root' is reserved by Unraid's Docker organizer — a same-named folder can never mirror + if (isset($decoded['name']) && is_string($decoded['name']) && strtolower(trim($decoded['name'])) === 'root') { + http_response_code(400); + header('Content-Type: application/json'); + echo json_encode(['error' => "The folder name 'root' is reserved by Unraid's Docker organizer"]); + exit; + } + // Only create.php omits the id; importAll() re-keys its own bad ids, so a malformed one here is a bad + // request, never a new folder + if ($id === null) { + $id = generateId(); + } elseif (!fv3_is_folder_id($id)) { + http_response_code(400); + header('Content-Type: application/json'); + echo json_encode(['error' => 'Folder id may only contain letters, digits, _ and -']); + exit; + } + fv3_write_folders($type, 'updateFolder', function (array $fileData) use ($decoded, $id): array { + $fileData[$id] = $decoded; + return fv3_dedupe_explicit_members($fileData, $id); + }); + return $id; + } + + // The locked read-modify-write of docker.json / vm.json (imports and createFile() take the same lock themselves): $apply + // gets the folders as they are on disk, [] with no file, and returns them changed or null for no write. A failure is a 500 + function fv3_write_folders(string $type, string $caller, callable $apply, string $refusal = 'refusing to save so existing folders are not wiped'): void { + global $configDir; + $lock = fv3_config_lock("$type.json"); + $abort = function (string $logged, string $error) use ($caller, $lock): void { + if ($lock) fv3_config_unlock($lock); + fv3_error_log($caller, $logged); + http_response_code(500); + header('Content-Type: application/json'); + echo json_encode(['error' => $error]); + exit; + }; + // Corrupt config must fail closed: merging onto empty would wipe every other folder + $fileData = fv3_read_json_strict("$configDir/$type.json"); + if ($fileData === null) $abort("$type.json is unreadable, $refusal", "$type.json is unreadable — $refusal"); + $changed = $apply($fileData); + // Nothing is written without the lock; a request that changes nothing never fails for want of it + if ($changed !== null && !$lock) $abort("could not lock $type.json", "Could not lock $type.json — nothing was saved"); + if ($changed !== null && !fv3_atomic_write("$configDir/$type.json", json_encode($changed))) { + $abort("could not write $type.json", "Could not write $type.json — nothing was saved"); + } + if ($lock) fv3_config_unlock($lock); + } + + // True when $key is a folder setting and $value is one it can hold. Colours, the text width and the graph time come + // from the settings page under looser rules than the editor's (fv3_sanitize_setting), so they are judged by those + function fv3_default_value_ok(string $key, $value): bool { + $rule = fv3_foreign_setting_rules()[$key] ?? null; + if ($rule === null) return false; + // unset: the page sends '' for an empty field and null where the form held no number; a switch is always true or false + if ($rule[0] !== 'bool' && ($value === null || $value === '')) return true; + if ($rule[0] === 'color' || $rule[0] === 'width') return is_string($value) && strlen($value) <= 50 && !preg_match('/[<>"\'\\\\]/', $value); + if ($key === 'context_graph_time') return is_int($value) && $value >= 0; + [$clean, $ok] = fv3_foreign_setting_value($rule, $value); + return $ok && $clean === $value; + } + + // Apply defaults: $settings merged into the settings of every folder as it is on disk, so nothing a page loaded + // earlier is written back. A folder that locks its colours keeps them + function applyFolderDefaults(string $type, string $settings) : void { + $patch = json_decode($settings, true); + // One bad request must not stamp an unknown key or value on every folder + if (!is_array($patch) || array_filter($patch, fn($value, $key) => !fv3_default_value_ok((string)$key, $value), ARRAY_FILTER_USE_BOTH)) { + http_response_code(400); + header('Content-Type: application/json'); + echo json_encode(['error' => 'The defaults must be folder settings with values the settings page can send']); + exit; + } + if (!$patch) { return; } + $unlocked = array_diff_key($patch, array_flip(['preview_vertical_bars_color', 'preview_border_color', 'preview_row_separator_color'])); + fv3_write_folders($type, 'applyFolderDefaults', function (array $fileData) use ($patch, $unlocked): ?array { + $changed = false; + foreach ($fileData as $folderId => $folder) { + if (!is_array($folder)) continue; + $old = is_array($folder['settings'] ?? null) ? $folder['settings'] : []; + // what the pages call locked: any value JavaScript reads as true + $locked = !in_array($old['lock_colors'] ?? false, [false, null, 0, 0.0, ''], true); + $new = array_replace($old, $locked ? $unlocked : $patch); + if ($new === $old) continue; + $fileData[$folderId]['settings'] = $new; + $changed = true; + } + return $changed ? $fileData : null; + }); + } + + // What a Docker, VMs or Dashboard page found at load, per folder: `renames` (old name => new) and `identity` (name => the + // image or UUID to know it by), applied to the folder as it is on disk. Whole lists from a page's copy are not taken + function updateFolderIds(string $type, string $data) : void { + $updates = json_decode($data, true); + // A JSON list is not a report: its positions would be read as folder ids. Told by the text: a map keyed "0" decodes the same + if (json_last_error() !== JSON_ERROR_NONE || !is_array($updates) || ($updates && ltrim($data)[0] === '[')) { http_response_code(400); exit; } + if (!$updates) { return; } + $identity = $type === 'vm' ? 'containerIds' : 'containerImages'; + fv3_write_folders($type, 'updateFolderIds', function (array $fileData) use ($updates, $identity): ?array { + // Every name that is a member of some folder by now: a rename never takes one from where it was put + $claimed = []; + foreach ($fileData as $folder) { + foreach (is_array($folder) && is_array($folder['containers'] ?? null) ? $folder['containers'] : [] as $member) { + if (is_scalar($member)) $claimed[(string)$member] = true; + } + } + $changed = false; + foreach ($updates as $folderId => $patch) { + if (!fv3_is_folder_id($folderId)) continue; + if (!is_array($fileData[$folderId] ?? null) || !is_array($patch)) continue; + $folder = fv3_apply_member_changes($fileData[$folderId], $patch, $identity, $claimed); + if ($folder === $fileData[$folderId]) continue; + $fileData[$folderId] = $folder; + $changed = true; + } + // As before this took renames: one container still listed in two folders stays in the first (issue #62) + return $changed ? fv3_dedupe_explicit_members($fileData) : null; + }); + } + + // $folder with a page's renames and identity entries applied to its members as they are now. Mirrors what + // fv3ResolveRenamedContainers (customEvents.js) does to the page's own copy, its skip of a $claimed name included + function fv3_apply_member_changes(array $folder, array $patch, string $identity, array &$claimed): array { + $members = is_array($folder['containers'] ?? null) ? $folder['containers'] : []; + $find = function ($name) use (&$members) { + foreach ($members as $at => $member) { if (is_scalar($member) && (string)$member === (string)$name) return $at; } + return null; + }; + foreach (is_array($patch['renames'] ?? null) ? $patch['renames'] : [] as $old => $new) { + $at = is_string($new) && $new !== '' ? $find($old) : null; + // a new name that a save since the page loaded made a member somewhere is left there: the next load decides again + if ($at === null || isset($claimed[$new])) continue; + $claimed[$new] = true; + $members[$at] = $new; + $folder['containers'] = $members; + if (is_array($folder[$identity] ?? null) && array_key_exists($old, $folder[$identity])) { + $folder[$identity][$new] = $folder[$identity][$old]; + unset($folder[$identity][$old]); + } + $hidden = is_array($folder['hidden_preview'] ?? null) ? array_search((string)$old, $folder['hidden_preview'], true) : false; + if ($hidden !== false) $folder['hidden_preview'][$hidden] = $new; + } + foreach (is_array($patch['identity'] ?? null) ? $patch['identity'] : [] as $name => $value) { + if (!is_string($value) || $value === '' || $find($name) === null) continue; + if (is_array($folder[$identity] ?? null) && array_key_exists($name, $folder[$identity])) continue; + if (!is_array($folder[$identity] ?? null)) $folder[$identity] = []; + $folder[$identity][$name] = $value; + } + return $folder; + } + + function deleteFolder(string $type, string $id) : void { + fv3_write_folders($type, 'deleteFolder', function (array $fileData) use ($id): ?array { + // Already gone: delete stays idempotent, and there is nothing to rewrite + if (!array_key_exists($id, $fileData)) { return null; } + unset($fileData[$id]); + return $fileData; + }, 'refusing to delete so the folder config is not wiped'); + } + + function generateId(int $length = 20) : string { + return substr(str_replace(['+', '/', '='], '', base64_encode(random_bytes((int)ceil($length * 3 / 4)))), 0, $length); + } + + // Ids safe to write and to render into class/selector/onclick markup; int = all-digit JSON key + function fv3_is_folder_id($id) : bool { + return (is_string($id) || is_int($id)) && preg_match('/^[A-Za-z0-9_-]+$/D', (string)$id) === 1; + } + + function createFile(string $type): void { + global $configDir; + if (!is_dir($configDir)) { @mkdir($configDir, 0770, true); } + $path = "$configDir/$type.json"; + // Under the file's lock, and only if it is still missing: a save or an import since the caller's check is never replaced + $lock = fv3_config_lock("$type.json"); + if (!$lock) return; + clearstatcache(true, $path); + if (!file_exists($path)) { fv3_atomic_write($path, '{}'); } + fv3_config_unlock($lock); + } diff --git a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/server/lib.foreign_import.php b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/server/lib.foreign_import.php index 0b76d37e..f4755d1b 100644 --- a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/server/lib.foreign_import.php +++ b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/server/lib.foreign_import.php @@ -398,7 +398,7 @@ function fv3_convert_foreign_bundle(array $raw, ?string $onlyType, array $existi $regex = is_string($src['regex'] ?? null) && strlen($src['regex']) <= 1024 ? $src['regex'] : ''; // A regex present but refused on type or length is left out as much as one that will not compile if ($regex === '' && $suppliedRegex !== null && $suppliedRegex !== '') $r['dropped_regex']++; - // Same pattern build as syncContainerOrder in lib.php; a regex it can't compile would match nothing + // Same pattern build as fv3_compute_folder_membership in lib.order.php; a regex it can't compile would match nothing if ($regex !== '' && @preg_match('/' . str_replace('/', '\/', $regex) . '/', '') === false) { $regex = ''; $r['dropped_regex']++; } // The renderers hand the stored pattern to JS new RegExp(), so one PHP alone accepts // would place members the page then cannot show — see fv3_foreign_regex_js_safe() diff --git a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/server/lib.order.php b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/server/lib.order.php new file mode 100644 index 00000000..33d031ac --- /dev/null +++ b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/server/lib.order.php @@ -0,0 +1,649 @@ + !is_string($v))) return null; + uksort($parsed, fn($a, $b) => (int)$a <=> (int)$b); + return array_values($parsed); + } + + // userprefs.cfg in the format Unraid writes it, one N="name" line per entry + function fv3_write_user_prefs(string $file, array $entries): bool { + $lines = []; + foreach (array_values($entries) as $i => $name) { $lines[] = $i . '="' . $name . '"'; } + return fv3_atomic_write($file, implode("\n", $lines) . "\n"); + } + + function fv3_order_snapshot_file(string $type): string { + global $configDir; + return "$configDir/order-$type.json"; + } + + // The cleaned list, or null when an entry breaks the rules that keep healed userprefs.cfg lines sound (a string, at most 256 + // bytes, no quote or control character; at most 4096 entries). Empty ones (every stock names string ends ';') are dropped + function fv3_sanitize_order_entries(array $entries): ?array { + $clean = []; + foreach ($entries as $e) { + if (!is_string($e)) return null; + $e = trim($e); + if ($e === '') continue; + if (strlen($e) > 256 || strpos($e, '"') !== false || preg_match('/[\x00-\x1f\x7f]/', $e)) return null; + $clean[] = $e; + } + if (count($clean) > 4096) return null; + return $clean; + } + + function saveOrderSnapshot(string $type, array $entries): bool { + $clean = fv3_sanitize_order_entries($entries); + if ($clean === null || empty($clean)) return false; + return fv3_atomic_write(fv3_order_snapshot_file($type), json_encode(['fv3_order_version' => 1, 'entries' => $clean], JSON_PRETTY_PRINT)); + } + + // The entries of a wrapper saveOrderSnapshot could have written (version 1, non-empty entries that sanitize unchanged), else null: export + // and import share it, and anything else must be ignored, never read as empty (import would clear a valid snapshot) + function fv3_validate_order_snapshot(array $data): ?array { + if (($data['fv3_order_version'] ?? null) !== 1 + || !isset($data['entries']) || !is_array($data['entries'])) return null; + $clean = fv3_sanitize_order_entries($data['entries']); + if ($clean === null || empty($clean) || $clean !== array_values($data['entries'])) return null; + return $clean; + } + + // The tri-state importAll() relies on: no file -> [] (import clears the destination), unreadable or malformed -> null (import + // leaves it alone), valid -> the wrapped snapshot + function fv3_export_order_snapshot(string $type): ?array { + $path = fv3_order_snapshot_file($type); + if (!file_exists($path)) return []; + $raw = @file_get_contents($path); + $data = ($raw !== false) ? json_decode($raw, true) : null; + if (!is_array($data) || fv3_validate_order_snapshot($data) === null) return null; + return $data; + } + + // Puts live folders the saved order lacks (after a reinstall, or a lost entry) back where FV3's snapshot had them. Insert-only: + // existing entries keep their place and order + function fv3_heal_user_prefs(string $type, string $prefsFilePath): void { + global $configDir; + $raw = @file_get_contents($prefsFilePath); + if (!is_string($raw) || $raw === '') return; + $current = fv3_parse_user_prefs($raw); + if (!$current) return; + + $config = fv3_read_json("$configDir/$type.json"); + if (empty($config)) return; + // Prefs and the snapshot store folder rows as 'folder-' placeholders + // (the hidden appname the stock reorder serializes); config keys are raw ids. + $placeholders = []; + foreach (array_keys($config) as $fid) { $placeholders['folder-' . $fid] = true; } + if (empty(array_diff(array_keys($placeholders), $current))) return; + + $snap = fv3_read_json(fv3_order_snapshot_file($type)); + $saved = $snap['entries'] ?? null; + if (!is_array($saved)) return; + + $currentSet = array_flip($current); + // Each unpositioned live folder placeholder maps to the first later snapshot entry still present in prefs (null = append) + $insertBefore = []; + $pending = []; + foreach ($saved as $entry) { + if (!is_string($entry)) continue; + if (isset($placeholders[$entry]) && !isset($currentSet[$entry])) { $pending[] = $entry; continue; } + if (isset($currentSet[$entry])) { + foreach ($pending as $fid) { $insertBefore[$fid] = $entry; } + $pending = []; + } + } + foreach ($pending as $fid) { $insertBefore[$fid] = null; } + if (empty($insertBefore)) return; + + $out = []; + foreach ($current as $name) { + foreach ($insertBefore as $fid => $succ) { + if ($succ === $name) { $out[] = $fid; unset($insertBefore[$fid]); } + } + $out[] = $name; + } + foreach (array_keys($insertBefore) as $fid) { $out[] = $fid; } + + // Stock UserPrefs writes are lockless, so re-check the source right before + // replacing — a concurrent reorder mid-compute aborts the heal (next load retries). + if (@file_get_contents($prefsFilePath) !== $raw) return; + if (fv3_write_user_prefs($prefsFilePath, $out)) { + fv3_debug_log("fv3_heal_user_prefs($type): re-inserted " . (count($out) - count($current)) . " folder position(s)"); + } + } + + function fv3_autostart_file(): string { + global $fv3DockerManDir; + $dockerManPaths = @parse_ini_file("$fv3DockerManDir/dockerMan.cfg") ?: []; + return $dockerManPaths['autostart-file'] ?? "/var/lib/docker/unraid-autostart"; + } + + // null when the file cannot be read: a caller that rebuilds the file from these lines must stop, not write [] + function fv3_read_autostart_lines(string $file): ?array { + $lines = @file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); + return $lines === false ? null : $lines; + } + + function readAutostartConfig(): array { + global $configDir; + $cfg = fv3_read_json_strict("$configDir/autostart.json"); + // Corrupt config fails closed to 'off' (pure stock behaviour, no autostart writes) + // rather than silently flipping a custom/off user to folder mode. + if ($cfg === null) return ['mode' => 'off', 'sequence' => []]; + $mode = $cfg['mode'] ?? 'folder'; + if (!in_array($mode, ['folder', 'custom', 'off'], true)) $mode = 'folder'; + $sequence = []; + foreach ((array)($cfg['sequence'] ?? []) as $name) { + // docker name charset — a crafted config entry must not smuggle markup or newlines into the autostart file + if (is_string($name) && preg_match('/^[a-zA-Z0-9][a-zA-Z0-9_.-]*$/', $name)) $sequence[] = $name; + } + return ['mode' => $mode, 'sequence' => array_values(array_unique($sequence))]; + } + + function updateAutostartConfig(string $mode, array $sequence, array $waits): array { + global $configDir; + if (!in_array($mode, ['folder', 'custom', 'off'], true)) return ['error' => 'Invalid mode']; + $cleanSeq = []; + foreach ($sequence as $name) { + if (is_string($name) && preg_match('/^[a-zA-Z0-9][a-zA-Z0-9_.-]*$/', $name)) $cleanSeq[] = $name; + if (count($cleanSeq) >= 500) break; + } + $cleanSeq = array_values(array_unique($cleanSeq)); + // Read before the write: an unreadable autostart file aborts the save rather than reporting + // success with the wait edits silently dropped + $autoStartFile = fv3_autostart_file(); + $lines = null; + if (!empty($waits)) { + if (file_exists($autoStartFile)) { + // Zeros still have to reach the loop below: that is how a wait is cleared + $lines = fv3_read_autostart_lines($autoStartFile); + if ($lines === null) return ['error' => 'Could not read the autostart file — nothing was saved']; + } elseif (array_filter($waits, static fn($w) => (int)$w > 0)) { + // No file to apply them to; an all-zero map is the empty state and saves normally + return ['error' => 'Could not read the autostart file — nothing was saved']; + } + } + + $ok = fv3_atomic_write("$configDir/autostart.json", json_encode(['mode' => $mode, 'sequence' => $cleanSeq], JSON_PRETTY_PRINT)); + if (!$ok) return ['error' => 'Failed to write autostart config']; + + // Fold wait edits into the live file — a name only matches a line that already has autostart enabled + $waitFailed = null; + if ($lines !== null) { + $changed = false; + foreach ($lines as $i => $line) { + $name = explode(' ', $line, 2)[0]; + if (array_key_exists($name, $waits)) { + $wait = max(0, min(3600, (int)$waits[$name])); + $newLine = rtrim($name . ' ' . ($wait > 0 ? $wait : '')); + if ($newLine !== $line) { $lines[$i] = $newLine; $changed = true; } + } + } + if ($changed && @file_put_contents($autoStartFile, implode("\n", $lines) . "\n", LOCK_EX) === false) { + $waitFailed = 'the wait edits could not be written to the autostart file'; + } + } + + // Re-assert order under the saved mode — saving folder mode instantly restores folder-derived order. + // The mode and sequence are saved either way: wait edits that were not written and a sync that stopped come back as `warning` + $warnings = array_filter([$waitFailed, $mode !== 'off' ? syncContainerOrder('docker') : null]); + return ['success' => true, 'mode' => $mode, 'sequence' => $cleanSeq] + ($warnings ? ['warning' => implode('; ', $warnings)] : []); + } + + // The autostart lines in the saved custom sequence, waits kept; enabled containers it does not name keep their order at the end. + // Returns why the autostart file could not be put in that sequence, or null + function fv3_apply_custom_autostart(array $allContainerNames, bool $ctListComplete): ?string { + $autoStartFile = fv3_autostart_file(); + if (!file_exists($autoStartFile)) return null; + $sequence = readAutostartConfig()['sequence']; + $rawLines = fv3_read_autostart_lines($autoStartFile); + if ($rawLines === null) { fv3_debug_log("fv3_apply_custom_autostart: $autoStartFile unreadable, not rewriting"); return 'the autostart file could not be read'; } + $newAutoStart = fv3_reorder_autostart(fv3_prune_stale_autostart($rawLines, $allContainerNames, $ctListComplete), $sequence); + if ($newAutoStart === $rawLines) { return null; } + if (@file_put_contents($autoStartFile, implode("\n", $newAutoStart) . "\n", LOCK_EX) === false) { return 'the autostart file could not be written'; } + fv3_debug_log("fv3_apply_custom_autostart: wrote " . count($newAutoStart) . " entries (" . count($sequence) . " sequenced)"); + return null; + } + + // The one definition of "drop autostart entries whose container is gone". A degraded Docker + // read must never prune (#214/#231) — a transient blip would otherwise curtail the file. + function fv3_prune_stale_autostart(array $autoStartLines, array $allContainerNames, bool $ctListComplete): array { + if (!$ctListComplete) return $autoStartLines; + $kept = []; + foreach ($autoStartLines as $line) { + $name = explode(' ', $line, 2)[0]; + if (in_array($name, $allContainerNames, true)) { $kept[] = $line; continue; } + fv3_debug_log("autostart: dropped stale entry '$name' (container no longer exists)"); + } + return $kept; + } + + // The values of Unraid's saved Docker order (userprefs.cfg): [] when there is none, null when the file will not be read + function fv3_docker_prefs(): ?array { + global $fv3DockerManDir; + $file = "$fv3DockerManDir/userprefs.cfg"; + if (!is_file($file)) { return []; } + $raw = @file_get_contents($file); + return $raw === false ? null : fv3_parse_user_prefs($raw); + } + + // Container names in the order read_unraid_order.php hands the page (readUnraidOrder): saved names in userprefs.cfg + // order, unsaved ones after them as Docker lists them; A-Z when nothing is saved + function fv3_live_order(array $names, array $prefs): array { + if (!$prefs) { usort($names, 'strnatcasecmp'); return $names; } + $saved = array_values(array_unique(array_filter($prefs, function($p) use ($names) { return in_array($p, $names, true); }))); + return array_merge($saved, array_values(array_filter($names, function($n) use ($saved) { return !in_array($n, $saved, true); }))); + } + + // The order docker.js createFolders() draws folders in: the folders userprefs.cfg names, in its order, then the rest in JS key + // order; with nothing saved, A-Z by name + function fv3_folder_walk_order(array $folders, array $prefs): array { + $ids = fv3_js_key_order(array_keys($folders)); + if (!$prefs) { + $name = fn($id) => $folders[$id]['name'] !== '' ? $folders[$id]['name'] : "folder-$id"; + usort($ids, fn($a, $b) => fv3_name_cmp($name($a), $name($b))); + return $ids; + } + $saved = []; + foreach ($prefs as $entry) { + if (str_starts_with($entry, 'folder-') && array_key_exists(substr($entry, 7), $folders)) $saved[] = substr($entry, 7); + } + return array_values(array_unique(array_merge($saved, $ids))); + } + + // Membership, explicit > label > regex (#46/#55), for syncContainerOrder and read_membership.php alike (#61). $prefs orders regex + // and label members as the page lists them (fv3_live_order), and folders as it draws them: of two regex matches, the first drawn wins + function fv3_compute_folder_membership(array $folders, array $allContainerNames, array $ctLabels, array $prefs = []): array { + $live = fv3_live_order($allContainerNames, $prefs); + // One malformed entry is dropped or filled in, not allowed to abort every folder's sync + $folders = fv3_normalize_folders($folders); + $folderNameSet = []; + foreach ($folders as $folder) { + if (isset($folder['name'])) { $folderNameSet[$folder['name']] = true; } + } + $folderContainers = []; + $folderNames = []; + $assignedContainers = []; + $explicitMembers = []; + foreach ($folders as $folder) { + $explicitMembers = array_merge($explicitMembers, $folder['containers'] ?? []); + } + $explicitAssigned = $explicitMembers; + foreach ($ctLabels as $ctName => $ctLabel) { + if (isset($folderNameSet[$ctLabel])) { $explicitAssigned[] = (string)$ctName; } + } + foreach (fv3_folder_walk_order($folders, $prefs) as $folderId) { + $folder = $folders[$folderId]; + $members = $folder['containers'] ?? []; + // is_string + trim, not empty(): empty("0") is true, so a regex of "0" was dropped while docker.js applied it; the type + // check mirrors docker.js and keeps a non-string regex from fataling trim() + if (is_string($folder['regex'] ?? null) && trim($folder['regex']) !== '') { + $regex = '/' . str_replace('/', '\/', $folder['regex']) . '/'; + foreach ($live as $name) { + if (@preg_match($regex, $name) && !in_array($name, $members, true) && !in_array($name, $explicitAssigned, true)) { + $members[] = $name; + } + } + } + // Explicit containers[] entries anywhere win over a label claim (issue #55) + foreach ($live as $ctName) { + if (($ctLabels[$ctName] ?? null) === $folder['name'] && !in_array($ctName, $members, true) && !in_array($ctName, $explicitMembers, true)) { + $members[] = $ctName; + } + } + $members = array_values(array_filter($members, function($m) use ($allContainerNames, $assignedContainers) { + return in_array($m, $allContainerNames, true) && !in_array($m, $assignedContainers, true); + })); + $folderContainers["folder-$folderId"] = $members; + $folderNames["folder-$folderId"] = $folder['name'] ?? "folder-$folderId"; + $assignedContainers = array_merge($assignedContainers, $members); + } + return ['containers' => $folderContainers, 'names' => $folderNames, 'assigned' => $assignedContainers]; + } + + // The order JS walks an object's keys in: index-like keys ("7", not "07") first and ascending, then the rest as written + function fv3_js_key_order(array $keys): array { + $keys = array_map('strval', $keys); + $index = array_values(array_filter($keys, function($k) { return preg_match('/^(0|[1-9]\d{0,9})$/D', $k) === 1 && (int)$k < 4294967295; })); + sort($index, SORT_NUMERIC); + return array_merge($index, array_values(array_diff($keys, $index))); + } + + // A name as the Docker page sorts it with nothing saved: trimmed of what JavaScript's trim() removes + function fv3_js_trim(string $s): string { + return preg_replace('/^[\x{9}-\x{D}\x{20}\x{A0}\x{1680}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}\x{FEFF}]+|' + . '[\x{9}-\x{D}\x{20}\x{A0}\x{1680}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}\x{FEFF}]+$/u', '', $s) ?? trim($s); + } + + // The browser's name order (localeCompare(b, undefined, {numeric: true, sensitivity: 'base'})) for ASCII: space and punctuation in + // ICU's order, numbers by value, letters in any case. Other characters follow z by code point, so accents can differ + function fv3_name_cmp(string $a, string $b): int { + static $punct = null; + $punct ??= array_flip(str_split(" _-,;:!?.'\"()[]{}@*/\\&#%`^+<=>|~$")); + $tokens = static function (string $s) use ($punct): ?array { + if (preg_match_all('/[0-9]+|./su', $s, $m) === false) return null; + return array_map(static function (string $t) use ($punct): array { + if (ctype_digit($t)) return [100, ltrim($t, '0')]; + $c = strtolower($t); + if (isset($punct[$c])) return [$punct[$c], '']; + return strlen($c) === 1 && $c >= 'a' && $c <= 'z' ? [200 + ord($c), ''] : [1000, $t]; + }, $m[0]); + }; + $ta = $tokens($a); + $tb = $tokens($b); + if ($ta === null || $tb === null) return strnatcasecmp($a, $b); + foreach ($ta as $i => [$ga, $ka]) { + if (!isset($tb[$i])) return 1; + [$gb, $kb] = $tb[$i]; + if ($ga !== $gb) return $ga <=> $gb; + $c = $ga === 100 ? (strlen($ka) <=> strlen($kb) ?: strcmp($ka, $kb)) : strcmp($ka, $kb); + if ($c !== 0) return $c <=> 0; + } + return count($ta) < count($tb) ? -1 : 0; + } + + // The Docker page's order, top-down, as container names and folder placeholders, members before their folder. + // $prefs is userprefs.cfg's values; with none it is A-Z by name as createFolders() sorts it (fv3_name_cmp: accents can differ) + function fv3_page_order(array $membership, array $allContainerNames, array $prefs): array { + $folderContainers = $membership['containers']; + $assigned = $membership['assigned']; + if (!$prefs) { + $entries = []; + foreach ($folderContainers as $placeholder => $_members) { + // The page's last pass sorts the rows by the trimmed name it shows (docker.js createFolders) + $entries[] = ['key' => $placeholder, 'name' => fv3_js_trim($membership['names'][$placeholder])]; + } + foreach ($allContainerNames as $name) { + if (!in_array($name, $assigned, true)) { $entries[] = ['key' => $name, 'name' => $name]; } + } + usort($entries, function($a, $b) { return fv3_name_cmp($a['name'], $b['name']); }); + $prefs = array_column($entries, 'key'); + } + $placed = []; + $seen = []; + foreach ($prefs as $item) { + if (isset($folderContainers[$item])) { + foreach ($folderContainers[$item] as $ct) { + if (!in_array($ct, $seen, true)) { $placed[] = $ct; $seen[] = $ct; } + } + $placed[] = $item; + $seen[] = $item; + } elseif (in_array($item, $assigned, true)) { + continue; + } elseif (in_array($item, $allContainerNames, true) && !in_array($item, $seen, true)) { + $placed[] = $item; + $seen[] = $item; + } + } + // Not in userprefs.cfg yet: docker.js unshifts each such folder (the last one it meets ends on top); new containers sit below them + $new = []; + $ids = array_map(function($placeholder) { return substr($placeholder, 7); }, array_keys($folderContainers)); + foreach (array_reverse(fv3_js_key_order($ids)) as $id) { + $placeholder = "folder-$id"; + if (in_array($placeholder, $seen, true)) { continue; } + foreach ($folderContainers[$placeholder] as $ct) { $new[] = $ct; } + $new[] = $placeholder; + } + $loose = array_values(array_filter($allContainerNames, function($name) use ($seen, $assigned) { + return !in_array($name, $seen, true) && !in_array($name, $assigned, true); + })); + usort($loose, 'strnatcasecmp'); + return array_merge($new, $loose, $placed); + } + + // $prefs with each folder's members together in its order, on the side of its entry a neighbour shows (a drag: after, FV3's writers: + // before; after by default). A folder with no entry, and every other entry, stay as they are + function fv3_prefs_with_folder_members(array $prefs, array $membership): array { + $blocks = []; + foreach ($membership['containers'] as $placeholder => $members) { + $at = array_search($placeholder, $prefs, true); + if ($at === false || !$members) continue; + $after = isset($prefs[$at + 1]) && in_array($prefs[$at + 1], $members, true); + $before = !$after && $at > 0 && in_array($prefs[$at - 1], $members, true); + $blocks[$placeholder] = ['members' => $members, 'before' => $before]; + } + if (!$blocks) return $prefs; + $moved = array_merge(...array_column($blocks, 'members')); + $out = []; + foreach ($prefs as $entry) { + if (in_array($entry, $moved, true)) continue; + if (!isset($blocks[$entry])) { $out[] = $entry; continue; } + ['members' => $members, 'before' => $before] = $blocks[$entry]; + unset($blocks[$entry]); // a repeated entry for the same folder keeps its place, its members come once + array_push($out, ...($before ? [...$members, $entry] : [$entry, ...$members])); + } + return $out; + } + + // Keeps userprefs.cfg's containers with their folders, so what reads it (Unraid's Start all and autostart sort, other plugins) + // follows the page. Only a saved order is changed: creating one switches Unraid off its A-Z order (#112). Returns why it failed + function fv3_sync_user_prefs(array $prefs, array $membership): ?string { + global $fv3DockerManDir; + if (!$prefs) return null; + $updated = fv3_prefs_with_folder_members($prefs, $membership); + if ($updated === $prefs) return null; + $updated = fv3_sanitize_order_entries($updated); + if ($updated === null) return "Unraid's saved Docker order (userprefs.cfg) holds a name that cannot be written back"; + // Stock UserPrefs writes take no lock: an order saved since the read above is kept as it is + $now = fv3_docker_prefs(); + if ($now === null) return "Unraid's saved Docker order (userprefs.cfg) could not be read"; + if ($now !== $prefs) { fv3_debug_log("fv3_sync_user_prefs: userprefs.cfg changed meanwhile, left as it is"); return null; } + if (!fv3_write_user_prefs("$fv3DockerManDir/userprefs.cfg", $updated)) { + fv3_debug_log("fv3_sync_user_prefs: userprefs.cfg could not be written"); + return "Unraid's saved Docker order (userprefs.cfg) could not be updated"; + } + // FV3's own copy, which the heal restores from after a reinstall + if (!saveOrderSnapshot('docker', $updated)) fv3_error_log('fv3_sync_user_prefs', 'the order snapshot could not be written'); + fv3_debug_log("fv3_sync_user_prefs: wrote userprefs.cfg with " . count($updated) . " entries"); + return null; + } + + // Autostart lines ("name" or "name delay") in $order's sequence, each once; lines $order does not name follow, in file order + function fv3_reorder_autostart(array $lines, array $order): array { + $byName = []; + foreach ($lines as $line) { $byName[explode(' ', $line, 2)[0]] = $line; } + $ordered = []; + foreach ($order as $name) { + if (isset($byName[$name])) { $ordered[] = $byName[$name]; unset($byName[$name]); } + } + return array_merge($ordered, array_values($byName)); + } + + // Returns why userprefs.cfg or the autostart file could not be put in order (a file or a Docker read it needs failed), else null. + // An empty container list is only logged: a server with no containers, or with Docker off, has nothing to report + function syncContainerOrder(string $type): ?string { + fv3_debug_log("syncContainerOrder called for type: $type"); + if ($type !== 'docker') { return null; } + + $autostartMode = readAutostartConfig()['mode']; + $dockerClient = new DockerClient(); + $ctNames = fv3_read_container_names($dockerClient); + // 'custom' = the Autostart tab's saved sequence orders the autostart file; 'off' = FV3 never reorders it + $failed = array_filter([ + $autostartMode === 'custom' ? fv3_apply_custom_autostart($ctNames['names'], $ctNames['complete']) : null, + ...fv3_sync_page_order($dockerClient, $ctNames, $autostartMode === 'folder'), + ]); + return $failed ? implode('; ', $failed) : null; + } + + // Puts a saved userprefs.cfg's containers with their folders and, with $autostart, the autostart file in the order FV3 renders: + // top to bottom, folder members in their editor order. Returns why each part could not be done, [] when all went through + function fv3_sync_page_order(DockerClient $dockerClient, array $ctNames, bool $autostart): array { + global $configDir; + $stop = function (string $why): array { fv3_debug_log("syncContainerOrder: $why, aborting before write"); return [$why]; }; + $allContainerNames = $ctNames['names']; + // The page's order needs the WHOLE list: a container missing from a partial read would be put out of place in either file + if (!$ctNames['complete']) { + if ($ctNames['empty']) { fv3_debug_log("syncContainerOrder: no containers, nothing to sync"); return []; } + return $stop("Docker's container list could not be read"); + } + + // No docker.json, or no config folder at all (deleted by hand under an open tab), is "no folders": never a stop + $folders = fv3_read_json_strict("$configDir/docker.json"); + // Corrupt folder config must not be read as no folders + if ($folders === null) { return $stop('the folder config could not be read'); } + + $ctLabels = fv3_read_container_labels($dockerClient, $allContainerNames); + // Fail closed: without label claims, label-assigned containers would be written back out as unassigned + if ($ctLabels === null) { return $stop('the container labels could not be read'); } + $prefs = fv3_docker_prefs(); + // The page's order cannot be worked out then, so both files stay as they are + if ($prefs === null) { return $stop("Unraid's saved Docker order (userprefs.cfg) could not be read"); } + $membership = fv3_compute_folder_membership($folders, $allContainerNames, $ctLabels, $prefs); + // Each file is put in order on its own: one that fails does not hold the other back + return array_values(array_filter([ + fv3_sync_user_prefs($prefs, $membership), + $autostart ? fv3_sync_autostart_order(fv3_page_order($membership, $allContainerNames, $prefs), $allContainerNames) : null, + ])); + } + + // The autostart file's lines in $order's sequence, entries for removed containers dropped ($names must be Docker's whole list, + // #214). Returns why it could not be put in order, else null + function fv3_sync_autostart_order(array $order, array $names): ?string { + $autoStartFile = fv3_autostart_file(); + if (!file_exists($autoStartFile)) return null; + $rawLines = fv3_read_autostart_lines($autoStartFile); + if ($rawLines === null) return 'the autostart file could not be read'; + $newAutoStart = fv3_reorder_autostart(fv3_prune_stale_autostart($rawLines, $names, true), $order); + if ($newAutoStart === $rawLines) { fv3_debug_log("syncContainerOrder: autostart file already in order"); return null; } + if (@file_put_contents($autoStartFile, implode("\n", $newAutoStart) . "\n", LOCK_EX) === false) return 'the autostart file could not be written'; + fv3_debug_log("syncContainerOrder: wrote autostart file with " . count($newAutoStart) . " entries"); + return null; + } + + // Creates Unraid's saved Docker order on a server with none, after an import or a restore: $first's entries (placeholders, names) + // first. Its other writers: the heal and fv3_place_imported_docker_folders. Returns why it stopped, or null: also when one is saved + function fv3_create_docker_order(array $first): ?string { + global $configDir, $fv3DockerManDir; + if (!$first) return null; + $prefs = fv3_docker_prefs(); + if ($prefs === null) return "Unraid's saved Docker order (userprefs.cfg) could not be read"; + if ($prefs) return null; + $dockerClient = new DockerClient(); + $ctNames = fv3_read_container_names($dockerClient); + // Docker stopped and no containers both answer []. An order of folders alone would put every container above them once Docker lists them + if ($ctNames['empty']) return 'Docker is not running or lists no containers'; + if (!$ctNames['complete']) return "Docker's container list could not be read"; + $names = $ctNames['names']; + $folders = fv3_read_json_strict("$configDir/docker.json"); + if ($folders === null) return 'the folder config could not be read'; + $labels = fv3_read_container_labels($dockerClient, $names); + if ($labels === null) return 'the container labels could not be read'; + // A restore's order copy names containers too: regex and label members keep its order, the rest Docker's list order (A-Z with none) + $membership = fv3_compute_folder_membership($folders, $names, $labels, array_values(array_filter($first, fn($e) => is_string($e) && in_array($e, $names, true)))); + // Each folder after its members, as the page lists them; the rest as the page draws it with nothing saved (A-Z) + $out = []; + foreach ($first as $entry) { + if (!is_string($entry)) continue; + if (isset($membership['containers'][$entry])) { + array_push($out, ...$membership['containers'][$entry]); + $out[] = $entry; + } elseif (in_array($entry, $names, true) && !in_array($entry, $membership['assigned'], true)) { + $out[] = $entry; + } + } + $out = fv3_sanitize_order_entries(array_values(array_unique(array_merge($out, fv3_page_order($membership, $names, []))))); + if ($out === null) return 'a name cannot be written to userprefs.cfg'; + // Stock UserPrefs writes take no lock: an order Unraid saved meanwhile wins; one that cannot be read now is said + $now = fv3_docker_prefs(); + if ($now === null) return "Unraid's saved Docker order (userprefs.cfg) could not be read"; + if ($now) return null; + if (!is_dir($fv3DockerManDir) || !fv3_write_user_prefs("$fv3DockerManDir/userprefs.cfg", $out)) { + return "Unraid's saved Docker order (userprefs.cfg) could not be written"; + } + // FV3's own copy, which the heal restores from after a reinstall + if (!saveOrderSnapshot('docker', $out)) fv3_error_log('fv3_create_docker_order', 'the order snapshot could not be written'); + return null; + } + + // After an import: the new Docker folders ($first, placeholders in the file's order) go on top of Unraid's saved order, insert-only, + // so the page draws them and lets them claim containers top-first; with no saved order one is created. Returns why it stopped, or null + function fv3_place_imported_docker_folders(array $first): ?string { + global $configDir, $fv3DockerManDir; + $file = "$fv3DockerManDir/userprefs.cfg"; + $read = fn() => is_file($file) ? @file_get_contents($file) : ''; + for ($try = 0; $try < 3; $try++) { + $raw = $read(); + $current = $raw === false ? null : fv3_parse_user_prefs($raw); + if ($current === null) return "Unraid's saved Docker order (userprefs.cfg) could not be read"; + if (!$current) return fv3_create_docker_order($first); + $folders = fv3_read_json_strict("$configDir/docker.json"); + if ($folders === null) return 'the folder config could not be read'; + $new = array_values(array_unique(array_filter($first, fn($e) => is_string($e) && str_starts_with($e, 'folder-') + && array_key_exists(substr($e, 7), $folders) && !in_array($e, $current, true)))); + if (!$new) return null; + $out = array_merge($new, $current); + // Stock UserPrefs writes take no lock: an order Unraid saved meanwhile is read again + if ($read() !== $raw) continue; + if (!fv3_write_user_prefs($file, $out)) return "Unraid's saved Docker order (userprefs.cfg) could not be written"; + if (!saveOrderSnapshot('docker', $out)) fv3_error_log('fv3_place_imported_docker_folders', 'the order snapshot could not be written'); + return null; + } + return "Unraid's saved Docker order (userprefs.cfg) kept changing during the import"; + } + + function readUnraidOrder(string $type): array { + fv3_debug_log("readUnraidOrder called for type: $type"); + $user_prefs_path = "/boot/config/plugins"; + $order = []; + if ($type == "docker") { + $dockerClient = new DockerClient(); + // A userprefs.cfg that will not parse lists A-Z, as one that is absent does + $order = fv3_live_order(array_column($dockerClient->getDockerContainers() ?: [], 'Name'), fv3_docker_prefs() ?? []); + + } elseif ($type == "vm") { + if (!fv3_require_libvirt_helpers()) { fv3_debug_log("VM Order: libvirt_helpers.php not available."); return []; } + global $lv; + if (!isset($lv)) { $lv = new Libvirt(); if (!$lv->connect()) { fv3_debug_log("VM Order: Libvirt connection failed."); return []; } } + + $prefs_file = "$user_prefs_path/dynamix.vm.manager/userprefs.cfg"; + $vms = $lv->get_domains(); + + if (!empty($vms)) { + if (file_exists($prefs_file)) { + $prefs_ini = @parse_ini_file($prefs_file); + if ($prefs_ini) { + $prefs_array = array_values($prefs_ini); + $sort = []; + $count_vms = count($vms); + foreach ($vms as $vm_name) { + $search = array_search($vm_name, $prefs_array); + $sort[] = ($search === false) ? ($count_vms + count($sort) + 1) : $search; + } + if (!empty($sort)) { + @array_multisort($sort, SORT_NUMERIC, $vms); + } else { + natcasesort($vms); + } + } else { + natcasesort($vms); + } + } else { + natcasesort($vms); + } + $order = array_values($vms); + } + } + fv3_debug_log("readUnraidOrder for type: $type completed. Order: " . json_encode($order)); + return $order; + } diff --git a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/server/lib.php b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/server/lib.php index 62a55e9a..ad7cefc8 100644 --- a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/server/lib.php +++ b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/server/lib.php @@ -164,9 +164,11 @@ function fv3_read_error_log_tail(): ?string { if (!headers_sent()) { http_response_code(500); header('Content-Type: application/json'); } echo json_encode(['error' => 'Internal error']); }); - // Non-fatal PHP errors don't end the request, so only log them; deprecations are noise, not failures + // Non-fatal PHP errors don't end the request, so only log them; deprecations are noise, not failures. + // Only files under FV3's folder: a warning raised in Unraid's code (libvirt.php...) stays in Unraid's phplog alone set_error_handler(function(int $severity, string $message, string $file, int $line): bool { if (!(error_reporting() & $severity) || ($severity & (E_DEPRECATED | E_USER_DEPRECATED))) return false; + if (!str_starts_with($file, dirname(__DIR__) . '/')) return false; fv3_error_log('php-error', "$message at " . basename($file) . ":$line"); return false; }); @@ -319,29 +321,6 @@ function fv3_get_tailscale_fqdn_from_container(string $containerName): ?string { return null; } - function readFolder(string $type) : string { - global $configDir; - if(!file_exists("$configDir/$type.json")) { createFile($type); } - $raw = @file_get_contents("$configDir/$type.json"); - if ($raw === false) { - // Fail the request: a 200 '{}' would be cached client-side over the last good copy - fv3_error_log('readFolder', "$type.json is unreadable"); - http_response_code(500); - return json_encode(['error' => "$type.json is unreadable"]); - } - // Unparseable stays byte-identical so the client's cached-copy fallback still fires - $decoded = json_decode($raw, true); - if (json_last_error() !== JSON_ERROR_NONE) { return $raw; } - // Valid JSON that isn't a folder map fails like an unreadable file - if (!is_array($decoded)) { - fv3_error_log('readFolder', "$type.json does not contain a folder map"); - http_response_code(500); - return json_encode(['error' => "$type.json does not contain a folder map"]); - } - $clean = json_encode((object)fv3_without_dead_plus_icons(fv3_normalize_folders($decoded))); - return $clean !== false ? $clean : $raw; - } - // FolderView Plus serves its own icons (bundled, uploaded, third-party) under /plugins/folderview.plus/; uninstalling deletes them. // Matched after %XX decoding, \ read as /, lower-casing, and . and .. resolved function fv3_is_plus_icon(string $icon): bool { @@ -369,197 +348,6 @@ function fv3_without_dead_plus_icons(array $folders): array { return $folders; } - // Fills the fields every reader dereferences so a hand-edited folder can't break a render - // or the autostart sync; object fields stay JSON objects. Never writes the file. - function fv3_normalize_folders(array $folders): array { - $out = []; - foreach ($folders as $id => $f) { - if (!is_array($f)) { continue; } - $f['name'] = is_scalar($f['name'] ?? null) ? (string)$f['name'] : (string)$id; - $f['icon'] = is_string($f['icon'] ?? null) ? $f['icon'] : ''; - $f['regex'] = is_string($f['regex'] ?? null) ? $f['regex'] : ''; - $f['containers'] = is_array($f['containers'] ?? null) ? array_values($f['containers']) : []; - $f['hidden_preview'] = is_array($f['hidden_preview'] ?? null) ? array_values($f['hidden_preview']) : []; - $f['actions'] = is_array($f['actions'] ?? null) ? array_values(array_filter($f['actions'], 'is_array')) : []; - $f['settings'] = (object)(is_array($f['settings'] ?? null) ? $f['settings'] : []); - foreach (['containerImages', 'containerIds'] as $k) { - if (array_key_exists($k, $f)) { $f[$k] = (object)(is_array($f[$k]) ? $f[$k] : []); } - } - $out[$id] = $f; - } - return $out; - } - - function readUserPrefs(string $type) : string { - global $fv3DockerManDir; - $prefsFilePath = ''; - if($type == 'docker') { $prefsFilePath = "$fv3DockerManDir/userprefs.cfg"; } - elseif($type == 'vm') { $prefsFilePath = "/boot/config/plugins/dynamix.vm.manager/userprefs.cfg"; } - else { return '[]'; } - if(!file_exists($prefsFilePath)) { return '[]'; } - // Deliberate GET side-effect (self-heal on the page-load read, mirroring the - // organizer mirror + rename-backfill pattern): content derives only from - // server-side state, is idempotent, and inserts rather than removes — a - // cross-site-triggered GET can only cause the same heal the next page load would. - fv3_heal_user_prefs($type, $prefsFilePath); - $raw = @file_get_contents($prefsFilePath); - return json_encode(($raw === false ? null : fv3_parse_user_prefs($raw)) ?? []); - } - - // userprefs.cfg's values in Unraid's order (by the number before each =, as its Docker page sorts them); null when the text - // will not parse or holds a value that is not a string (a `n[]=` line) - function fv3_parse_user_prefs(string $raw): ?array { - $parsed = @parse_ini_string($raw); - if (!is_array($parsed) || array_filter($parsed, fn($v) => !is_string($v))) return null; - uksort($parsed, fn($a, $b) => (int)$a <=> (int)$b); - return array_values($parsed); - } - - // userprefs.cfg in the format Unraid writes it, one N="name" line per entry - function fv3_write_user_prefs(string $file, array $entries): bool { - $lines = []; - foreach (array_values($entries) as $i => $name) { $lines[] = $i . '="' . $name . '"'; } - return fv3_atomic_write($file, implode("\n", $lines) . "\n"); - } - - function fv3_order_snapshot_file(string $type): string { - global $configDir; - return "$configDir/order-$type.json"; - } - - // Returns the cleaned list, or null when any non-empty entry breaks the snapshot - // rules. Empty entries are delimiter artifacts (every stock names string ends ';') - // and are dropped, not treated as invalid. The rules exist because a violating - // entry could corrupt an ini line when healed back into userprefs.cfg. - function fv3_sanitize_order_entries(array $entries): ?array { - $clean = []; - foreach ($entries as $e) { - if (!is_string($e)) return null; - $e = trim($e); - if ($e === '') continue; - if (strlen($e) > 256 || strpos($e, '"') !== false || preg_match('/[\x00-\x1f\x7f]/', $e)) return null; - $clean[] = $e; - } - if (count($clean) > 4096) return null; - return $clean; - } - - function saveOrderSnapshot(string $type, array $entries): bool { - $clean = fv3_sanitize_order_entries($entries); - if ($clean === null || empty($clean)) return false; - return fv3_atomic_write(fv3_order_snapshot_file($type), json_encode(['fv3_order_version' => 1, 'entries' => $clean], JSON_PRETTY_PRINT)); - } - - // Snapshot state for export, keeping the tri-state importAll() relies on: - // absent file -> [] (present-empty: source authoritatively has no snapshot, import - // clears the destination); unreadable/malformed -> null (omit the bundle key, import - // leaves the destination untouched); valid -> the wrapped snapshot. - // A snapshot wrapper saveOrderSnapshot could actually have written: version 1 and a - // non-empty entries list that passes sanitization unchanged. Returns the entries or - // null. Shared by export (file contents) and import (bundle contents) so both sides - // apply identical validation — anything else is corrupt/tampered and must be ignored, - // never treated as empty (that would clear a valid destination snapshot on import). - function fv3_validate_order_snapshot(array $data): ?array { - if (($data['fv3_order_version'] ?? null) !== 1 - || !isset($data['entries']) || !is_array($data['entries'])) return null; - $clean = fv3_sanitize_order_entries($data['entries']); - if ($clean === null || empty($clean) || $clean !== array_values($data['entries'])) return null; - return $clean; - } - - function fv3_export_order_snapshot(string $type): ?array { - $path = fv3_order_snapshot_file($type); - if (!file_exists($path)) return []; - $raw = @file_get_contents($path); - $data = ($raw !== false) ? json_decode($raw, true) : null; - if (!is_array($data) || fv3_validate_order_snapshot($data) === null) return null; - return $data; - } - - // Re-insert folder rows into the stock sort prefs from FV3's own order snapshot. - // Fires when prefs has entries but at least one live folder is unpositioned — - // the post-reinstall state, or a folder whose entry went missing. Insert-only — - // existing prefs entries are never reordered or removed, so container order is - // preserved, and already-positioned folders are kept in place and used as anchors. - function fv3_heal_user_prefs(string $type, string $prefsFilePath): void { - global $configDir; - $raw = @file_get_contents($prefsFilePath); - if (!is_string($raw) || $raw === '') return; - $current = fv3_parse_user_prefs($raw); - if (!$current) return; - - $config = fv3_read_json("$configDir/$type.json"); - if (empty($config)) return; - // Prefs and the snapshot store folder rows as 'folder-' placeholders - // (the hidden appname the stock reorder serializes); config keys are raw ids. - $placeholders = []; - foreach (array_keys($config) as $fid) { $placeholders['folder-' . $fid] = true; } - if (empty(array_diff(array_keys($placeholders), $current))) return; - - $snap = fv3_read_json(fv3_order_snapshot_file($type)); - $saved = $snap['entries'] ?? null; - if (!is_array($saved)) return; - - $currentSet = array_flip($current); - // Each unpositioned live folder placeholder maps to the first later snapshot entry still present in prefs (null = append) - $insertBefore = []; - $pending = []; - foreach ($saved as $entry) { - if (!is_string($entry)) continue; - if (isset($placeholders[$entry]) && !isset($currentSet[$entry])) { $pending[] = $entry; continue; } - if (isset($currentSet[$entry])) { - foreach ($pending as $fid) { $insertBefore[$fid] = $entry; } - $pending = []; - } - } - foreach ($pending as $fid) { $insertBefore[$fid] = null; } - if (empty($insertBefore)) return; - - $out = []; - foreach ($current as $name) { - foreach ($insertBefore as $fid => $succ) { - if ($succ === $name) { $out[] = $fid; unset($insertBefore[$fid]); } - } - $out[] = $name; - } - foreach (array_keys($insertBefore) as $fid) { $out[] = $fid; } - - // Stock UserPrefs writes are lockless, so re-check the source right before - // replacing — a concurrent reorder mid-compute aborts the heal (next load retries). - if (@file_get_contents($prefsFilePath) !== $raw) return; - if (fv3_write_user_prefs($prefsFilePath, $out)) { - fv3_debug_log("fv3_heal_user_prefs($type): re-inserted " . (count($out) - count($current)) . " folder position(s)"); - } - } - - function fv3_autostart_file(): string { - global $fv3DockerManDir; - $dockerManPaths = @parse_ini_file("$fv3DockerManDir/dockerMan.cfg") ?: []; - return $dockerManPaths['autostart-file'] ?? "/var/lib/docker/unraid-autostart"; - } - - // null when the file cannot be read: a caller that rebuilds the file from these lines must stop, not write [] - function fv3_read_autostart_lines(string $file): ?array { - $lines = @file($file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); - return $lines === false ? null : $lines; - } - - function readAutostartConfig(): array { - global $configDir; - $cfg = fv3_read_json_strict("$configDir/autostart.json"); - // Corrupt config fails closed to 'off' (pure stock behaviour, no autostart writes) - // rather than silently flipping a custom/off user to folder mode. - if ($cfg === null) return ['mode' => 'off', 'sequence' => []]; - $mode = $cfg['mode'] ?? 'folder'; - if (!in_array($mode, ['folder', 'custom', 'off'], true)) $mode = 'folder'; - $sequence = []; - foreach ((array)($cfg['sequence'] ?? []) as $name) { - // docker name charset — a crafted config entry must not smuggle markup or newlines into the autostart file - if (is_string($name) && preg_match('/^[a-zA-Z0-9][a-zA-Z0-9_.-]*$/', $name)) $sequence[] = $name; - } - return ['mode' => $mode, 'sequence' => array_values(array_unique($sequence))]; - } - // Names of organizer folders FV3 created/manages — user-made native folders are never listed here function readOrganizerRegistry(): array { global $configDir; @@ -585,72 +373,6 @@ function updateOrganizerRegistry(array $names): bool { return fv3_atomic_write("$configDir/organizer-registry.json", json_encode(['folders' => $clean], JSON_PRETTY_PRINT)); } - function updateAutostartConfig(string $mode, array $sequence, array $waits): array { - global $configDir; - if (!in_array($mode, ['folder', 'custom', 'off'], true)) return ['error' => 'Invalid mode']; - $cleanSeq = []; - foreach ($sequence as $name) { - if (is_string($name) && preg_match('/^[a-zA-Z0-9][a-zA-Z0-9_.-]*$/', $name)) $cleanSeq[] = $name; - if (count($cleanSeq) >= 500) break; - } - $cleanSeq = array_values(array_unique($cleanSeq)); - // Read before the write: an unreadable autostart file aborts the save rather than reporting - // success with the wait edits silently dropped - $autoStartFile = fv3_autostart_file(); - $lines = null; - if (!empty($waits)) { - if (file_exists($autoStartFile)) { - // Zeros still have to reach the loop below: that is how a wait is cleared - $lines = fv3_read_autostart_lines($autoStartFile); - if ($lines === null) return ['error' => 'Could not read the autostart file — nothing was saved']; - } elseif (array_filter($waits, static fn($w) => (int)$w > 0)) { - // No file to apply them to; an all-zero map is the empty state and saves normally - return ['error' => 'Could not read the autostart file — nothing was saved']; - } - } - - $ok = fv3_atomic_write("$configDir/autostart.json", json_encode(['mode' => $mode, 'sequence' => $cleanSeq], JSON_PRETTY_PRINT)); - if (!$ok) return ['error' => 'Failed to write autostart config']; - - // Fold wait edits into the live file — a name only matches a line that already has autostart enabled - $waitFailed = null; - if ($lines !== null) { - $changed = false; - foreach ($lines as $i => $line) { - $name = explode(' ', $line, 2)[0]; - if (array_key_exists($name, $waits)) { - $wait = max(0, min(3600, (int)$waits[$name])); - $newLine = rtrim($name . ' ' . ($wait > 0 ? $wait : '')); - if ($newLine !== $line) { $lines[$i] = $newLine; $changed = true; } - } - } - if ($changed && @file_put_contents($autoStartFile, implode("\n", $lines) . "\n", LOCK_EX) === false) { - $waitFailed = 'the wait edits could not be written to the autostart file'; - } - } - - // Re-assert order under the saved mode — saving folder mode instantly restores folder-derived order. - // The mode and sequence are saved either way: wait edits that were not written and a sync that stopped come back as `warning` - $warnings = array_filter([$waitFailed, $mode !== 'off' ? syncContainerOrder('docker') : null]); - return ['success' => true, 'mode' => $mode, 'sequence' => $cleanSeq] + ($warnings ? ['warning' => implode('; ', $warnings)] : []); - } - - // Reorders the existing autostart lines to the saved custom sequence. Waits ride along - // verbatim; enabled-but-unsequenced containers keep their current relative order at the end. - // Returns why the autostart file could not be put in the saved sequence, or null - function fv3_apply_custom_autostart(array $allContainerNames, bool $ctListComplete): ?string { - $autoStartFile = fv3_autostart_file(); - if (!file_exists($autoStartFile)) return null; - $sequence = readAutostartConfig()['sequence']; - $rawLines = fv3_read_autostart_lines($autoStartFile); - if ($rawLines === null) { fv3_debug_log("fv3_apply_custom_autostart: $autoStartFile unreadable, not rewriting"); return 'the autostart file could not be read'; } - $newAutoStart = fv3_reorder_autostart(fv3_prune_stale_autostart($rawLines, $allContainerNames, $ctListComplete), $sequence); - if ($newAutoStart === $rawLines) { return null; } - if (@file_put_contents($autoStartFile, implode("\n", $newAutoStart) . "\n", LOCK_EX) === false) { return 'the autostart file could not be written'; } - fv3_debug_log("fv3_apply_custom_autostart: wrote " . count($newAutoStart) . " entries (" . count($sequence) . " sequenced)"); - return null; - } - // Container names from getDockerContainers(). 'complete' is false when the read is empty // or any entry is unnamed — a degraded list must not drive autostart pruning (#214). // DockerClient echoes its socket error when Docker is not running; buffer it out of the JSON response @@ -680,19 +402,6 @@ function fv3_read_container_names(DockerClient $dockerClient): array { return ['names' => $names, 'complete' => $complete, 'empty' => $empty]; } - // The one definition of "drop autostart entries whose container is gone". A degraded Docker - // read must never prune (#214/#231) — a transient blip would otherwise curtail the file. - function fv3_prune_stale_autostart(array $autoStartLines, array $allContainerNames, bool $ctListComplete): array { - if (!$ctListComplete) return $autoStartLines; - $kept = []; - foreach ($autoStartLines as $line) { - $name = explode(' ', $line, 2)[0]; - if (in_array($name, $allContainerNames, true)) { $kept[] = $line; continue; } - fv3_debug_log("autostart: dropped stale entry '$name' (container no longer exists)"); - } - return $kept; - } - // `folder.view3: ` label claims keyed by container name — getDockerContainers() // carries no Labels, so read the same raw endpoint readInfo() uses. Returns null on a // failed/partial/unnamed read so callers fail closed rather than treat label-assigned @@ -740,508 +449,6 @@ function fv3_read_image_env(string $imageId): ?array { return is_array($env) ? array_values(array_filter($env, 'is_string')) : null; } - // The values of Unraid's saved Docker order (userprefs.cfg): [] when there is none, null when the file will not be read - function fv3_docker_prefs(): ?array { - global $fv3DockerManDir; - $file = "$fv3DockerManDir/userprefs.cfg"; - if (!is_file($file)) { return []; } - $raw = @file_get_contents($file); - return $raw === false ? null : fv3_parse_user_prefs($raw); - } - - // Container names in the order read_unraid_order.php hands the page (readUnraidOrder): saved names in userprefs.cfg - // order, unsaved ones after them as Docker lists them; A-Z when nothing is saved - function fv3_live_order(array $names, array $prefs): array { - if (!$prefs) { usort($names, 'strnatcasecmp'); return $names; } - $saved = array_values(array_unique(array_filter($prefs, function($p) use ($names) { return in_array($p, $names, true); }))); - return array_merge($saved, array_values(array_filter($names, function($n) use ($saved) { return !in_array($n, $saved, true); }))); - } - - // Effective membership — explicit > label > regex (issues #46/#55). Single source of - // truth shared by syncContainerOrder and read_membership.php (issue #61). - // $prefs orders each folder's regex and label members as the Docker page lists them (fv3_live_order) - function fv3_compute_folder_membership(array $folders, array $allContainerNames, array $ctLabels, array $prefs = []): array { - $live = fv3_live_order($allContainerNames, $prefs); - // One malformed entry is dropped or filled in, not allowed to abort every folder's sync - $folders = fv3_normalize_folders($folders); - $folderNameSet = []; - foreach ($folders as $folder) { - if (isset($folder['name'])) { $folderNameSet[$folder['name']] = true; } - } - $folderContainers = []; - $folderNames = []; - $assignedContainers = []; - $explicitMembers = []; - foreach ($folders as $folder) { - $explicitMembers = array_merge($explicitMembers, $folder['containers'] ?? []); - } - $explicitAssigned = $explicitMembers; - foreach ($ctLabels as $ctName => $ctLabel) { - if (isset($folderNameSet[$ctLabel])) { $explicitAssigned[] = (string)$ctName; } - } - foreach ($folders as $folderId => $folder) { - $members = $folder['containers'] ?? []; - // is_string + trim, not empty(): empty("0") is true in PHP, so a regex of "0" was - // silently dropped while docker.js applied it. The type check mirrors docker.js and - // keeps a non-string regex from fataling trim() (TypeError on PHP 8). - if (is_string($folder['regex'] ?? null) && trim($folder['regex']) !== '') { - $regex = '/' . str_replace('/', '\/', $folder['regex']) . '/'; - foreach ($live as $name) { - if (@preg_match($regex, $name) && !in_array($name, $members, true) && !in_array($name, $explicitAssigned, true)) { - $members[] = $name; - } - } - } - // Explicit containers[] entries anywhere win over a label claim (issue #55) - foreach ($live as $ctName) { - if (($ctLabels[$ctName] ?? null) === $folder['name'] && !in_array($ctName, $members, true) && !in_array($ctName, $explicitMembers, true)) { - $members[] = $ctName; - } - } - $members = array_values(array_filter($members, function($m) use ($allContainerNames, $assignedContainers) { - return in_array($m, $allContainerNames, true) && !in_array($m, $assignedContainers, true); - })); - $folderContainers["folder-$folderId"] = $members; - $folderNames["folder-$folderId"] = $folder['name'] ?? "folder-$folderId"; - $assignedContainers = array_merge($assignedContainers, $members); - } - return ['containers' => $folderContainers, 'names' => $folderNames, 'assigned' => $assignedContainers]; - } - - // The order JS walks an object's keys in: index-like keys ("7", not "07") first and ascending, then the rest as written - function fv3_js_key_order(array $keys): array { - $keys = array_map('strval', $keys); - $index = array_values(array_filter($keys, function($k) { return preg_match('/^(0|[1-9]\d{0,9})$/D', $k) === 1 && (int)$k < 4294967295; })); - sort($index, SORT_NUMERIC); - return array_merge($index, array_values(array_diff($keys, $index))); - } - - // A name as the Docker page sorts it with nothing saved: trimmed of what JavaScript's trim() removes - function fv3_js_trim(string $s): string { - return preg_replace('/^[\x{9}-\x{D}\x{20}\x{A0}\x{1680}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}\x{FEFF}]+|' - . '[\x{9}-\x{D}\x{20}\x{A0}\x{1680}\x{2000}-\x{200A}\x{2028}\x{2029}\x{202F}\x{205F}\x{3000}\x{FEFF}]+$/u', '', $s) ?? trim($s); - } - - // The browser's name order (localeCompare(b, undefined, {numeric: true, sensitivity: 'base'})) for ASCII: space and punctuation in - // ICU's order, numbers by value, letters in any case. Other characters follow z by code point, so accents can differ - function fv3_name_cmp(string $a, string $b): int { - static $punct = null; - $punct ??= array_flip(str_split(" _-,;:!?.'\"()[]{}@*/\\&#%`^+<=>|~$")); - $tokens = static function (string $s) use ($punct): ?array { - if (preg_match_all('/[0-9]+|./su', $s, $m) === false) return null; - return array_map(static function (string $t) use ($punct): array { - if (ctype_digit($t)) return [100, ltrim($t, '0')]; - $c = strtolower($t); - if (isset($punct[$c])) return [$punct[$c], '']; - return strlen($c) === 1 && $c >= 'a' && $c <= 'z' ? [200 + ord($c), ''] : [1000, $t]; - }, $m[0]); - }; - $ta = $tokens($a); - $tb = $tokens($b); - if ($ta === null || $tb === null) return strnatcasecmp($a, $b); - foreach ($ta as $i => [$ga, $ka]) { - if (!isset($tb[$i])) return 1; - [$gb, $kb] = $tb[$i]; - if ($ga !== $gb) return $ga <=> $gb; - $c = $ga === 100 ? (strlen($ka) <=> strlen($kb) ?: strcmp($ka, $kb)) : strcmp($ka, $kb); - if ($c !== 0) return $c <=> 0; - } - return count($ta) < count($tb) ? -1 : 0; - } - - // The Docker page's order, top-down, as container names and folder placeholders, members before their folder. - // $prefs is userprefs.cfg's values; with none it is A-Z by name as createFolders() sorts it (fv3_name_cmp: accents can differ) - function fv3_page_order(array $membership, array $allContainerNames, array $prefs): array { - $folderContainers = $membership['containers']; - $assigned = $membership['assigned']; - if (!$prefs) { - $entries = []; - foreach ($folderContainers as $placeholder => $_members) { - // The page's last pass sorts the rows by the trimmed name it shows (docker.js createFolders) - $entries[] = ['key' => $placeholder, 'name' => fv3_js_trim($membership['names'][$placeholder])]; - } - foreach ($allContainerNames as $name) { - if (!in_array($name, $assigned, true)) { $entries[] = ['key' => $name, 'name' => $name]; } - } - usort($entries, function($a, $b) { return fv3_name_cmp($a['name'], $b['name']); }); - $prefs = array_column($entries, 'key'); - } - $placed = []; - $seen = []; - foreach ($prefs as $item) { - if (isset($folderContainers[$item])) { - foreach ($folderContainers[$item] as $ct) { - if (!in_array($ct, $seen, true)) { $placed[] = $ct; $seen[] = $ct; } - } - $placed[] = $item; - $seen[] = $item; - } elseif (in_array($item, $assigned, true)) { - continue; - } elseif (in_array($item, $allContainerNames, true) && !in_array($item, $seen, true)) { - $placed[] = $item; - $seen[] = $item; - } - } - // Not in userprefs.cfg yet: docker.js unshifts each such folder (the last one it meets ends on top); new containers sit below them - $new = []; - $ids = array_map(function($placeholder) { return substr($placeholder, 7); }, array_keys($folderContainers)); - foreach (array_reverse(fv3_js_key_order($ids)) as $id) { - $placeholder = "folder-$id"; - if (in_array($placeholder, $seen, true)) { continue; } - foreach ($folderContainers[$placeholder] as $ct) { $new[] = $ct; } - $new[] = $placeholder; - } - $loose = array_values(array_filter($allContainerNames, function($name) use ($seen, $assigned) { - return !in_array($name, $seen, true) && !in_array($name, $assigned, true); - })); - usort($loose, 'strnatcasecmp'); - return array_merge($new, $loose, $placed); - } - - // Autostart lines ("name" or "name delay") in $order's sequence, each once; lines $order does not name follow, in file order - function fv3_reorder_autostart(array $lines, array $order): array { - $byName = []; - foreach ($lines as $line) { $byName[explode(' ', $line, 2)[0]] = $line; } - $ordered = []; - foreach ($order as $name) { - if (isset($byName[$name])) { $ordered[] = $byName[$name]; unset($byName[$name]); } - } - return array_merge($ordered, array_values($byName)); - } - - // Returns why the autostart file could not be put in order (a file or a Docker read it needs failed), else null. - // Folder mode only logs an empty container list: a server with no containers, or with Docker off, has nothing to report - function syncContainerOrder(string $type): ?string { - // Rewrites the autostart file to match what FV3 renders on screen — top-to-bottom, - // folder members left-to-right in their editor-chosen order. Render order comes from - // userprefs.cfg when it exists (saved by a drag-reorder or an import), A-Z otherwise. - global $configDir; - fv3_debug_log("syncContainerOrder called for type: $type"); - $stop = function (string $why): string { fv3_debug_log("syncContainerOrder: $why, aborting before write"); return $why; }; - - if ($type !== 'docker') { return null; } - - $autostartMode = readAutostartConfig()['mode']; - // 'off' = FV3 never writes the autostart file — pure stock Unraid behaviour - if ($autostartMode === 'off') { fv3_debug_log("syncContainerOrder: autostart mode off, skipping"); return null; } - - $dockerClient = new DockerClient(); - $ctNames = fv3_read_container_names($dockerClient); - $allContainerNames = $ctNames['names']; - // Prune below only on a complete, fully-named container list — a degraded Docker read must not wipe autostart entries (#214) - $ctListComplete = $ctNames['complete']; - - // 'custom' = the Autostart tab's saved sequence overrides folder-derived order entirely - if ($autostartMode === 'custom') { - return fv3_apply_custom_autostart($allContainerNames, $ctListComplete); - } - - - // Folder-derived order needs the WHOLE list: containers missing from a partial read are - // skipped by the order walk and land appended at the end, silently reordering the file. - // Custom mode above is exempt — its order comes from the saved sequence, not this list. - if (!$ctListComplete) { - if ($ctNames['empty']) { fv3_debug_log("syncContainerOrder: no containers, nothing to sync"); return null; } - return $stop("Docker's container list could not be read"); - } - - // No docker.json, or no config folder at all (deleted by hand under an open tab), is "no folders": never a stop - $folders = fv3_read_json_strict("$configDir/docker.json"); - // Corrupt folder config must not rewrite the autostart file as if no folders exist - if ($folders === null) { return $stop('the folder config could not be read'); } - - $ctLabels = fv3_read_container_labels($dockerClient, $allContainerNames); - // Fail closed: without label claims the order below would write label-assigned - // containers back out as unassigned — the same hazard $ctListComplete guards above. - if ($ctLabels === null) { return $stop('the container labels could not be read'); } - $prefs = fv3_docker_prefs(); - // The page's order cannot be worked out then, so the file stays as it is - if ($prefs === null) { return $stop("Unraid's saved Docker order (userprefs.cfg) could not be read"); } - $newOrder = fv3_page_order(fv3_compute_folder_membership($folders, $allContainerNames, $ctLabels, $prefs), $allContainerNames, $prefs); - fv3_debug_log("syncContainerOrder: computed newOrder with " . count($newOrder) . " entries (" . ($prefs ? 'userprefs.cfg' : 'A-Z, no userprefs.cfg') . ")"); - - // Rewrite autostart file in $newOrder sequence; userprefs.cfg is not touched here - $autoStartFile = fv3_autostart_file(); - if (file_exists($autoStartFile)) { - $rawLines = fv3_read_autostart_lines($autoStartFile); - if ($rawLines === null) { return $stop('the autostart file could not be read'); } - $autoStartLines = fv3_prune_stale_autostart($rawLines, $allContainerNames, $ctListComplete); - $newAutoStart = fv3_reorder_autostart($autoStartLines, $newOrder); - if ($newAutoStart === $rawLines) { fv3_debug_log("syncContainerOrder: autostart file already in order"); return null; } - if (@file_put_contents($autoStartFile, implode("\n", $newAutoStart) . "\n", LOCK_EX) === false) { return $stop('the autostart file could not be written'); } - fv3_debug_log("syncContainerOrder: wrote autostart file with " . count($newAutoStart) . " entries"); - } - return null; - } - - // Creates Unraid's saved Docker order on a server with none, after an import or a restore: $first's entries (placeholders, names) - // first. Its other writers: the heal and fv3_place_imported_docker_folders. Returns why it stopped, or null: also when one is saved - function fv3_create_docker_order(array $first): ?string { - global $configDir, $fv3DockerManDir; - if (!$first) return null; - $prefs = fv3_docker_prefs(); - if ($prefs === null) return "Unraid's saved Docker order (userprefs.cfg) could not be read"; - if ($prefs) return null; - $dockerClient = new DockerClient(); - $ctNames = fv3_read_container_names($dockerClient); - // Docker stopped and no containers both answer []. An order of folders alone would put every container above them once Docker lists them - if ($ctNames['empty']) return 'Docker is not running or lists no containers'; - if (!$ctNames['complete']) return "Docker's container list could not be read"; - $names = $ctNames['names']; - $folders = fv3_read_json_strict("$configDir/docker.json"); - if ($folders === null) return 'the folder config could not be read'; - $labels = fv3_read_container_labels($dockerClient, $names); - if ($labels === null) return 'the container labels could not be read'; - // A restore's order copy names containers too: regex and label members keep its order, the rest Docker's list order (A-Z with none) - $membership = fv3_compute_folder_membership($folders, $names, $labels, array_values(array_filter($first, fn($e) => is_string($e) && in_array($e, $names, true)))); - // Each folder after its members, as the page lists them; the rest as the page draws it with nothing saved (A-Z) - $out = []; - foreach ($first as $entry) { - if (!is_string($entry)) continue; - if (isset($membership['containers'][$entry])) { - array_push($out, ...$membership['containers'][$entry]); - $out[] = $entry; - } elseif (in_array($entry, $names, true) && !in_array($entry, $membership['assigned'], true)) { - $out[] = $entry; - } - } - $out = fv3_sanitize_order_entries(array_values(array_unique(array_merge($out, fv3_page_order($membership, $names, []))))); - if ($out === null) return 'a name cannot be written to userprefs.cfg'; - // Stock UserPrefs writes take no lock: an order Unraid saved meanwhile wins; one that cannot be read now is said - $now = fv3_docker_prefs(); - if ($now === null) return "Unraid's saved Docker order (userprefs.cfg) could not be read"; - if ($now) return null; - if (!is_dir($fv3DockerManDir) || !fv3_write_user_prefs("$fv3DockerManDir/userprefs.cfg", $out)) { - return "Unraid's saved Docker order (userprefs.cfg) could not be written"; - } - // FV3's own copy, which the heal restores from after a reinstall - if (!saveOrderSnapshot('docker', $out)) fv3_error_log('fv3_create_docker_order', 'the order snapshot could not be written'); - return null; - } - - // After an import: the new Docker folders ($first, placeholders in the file's order) go on top of Unraid's saved order, insert-only, - // so the page draws them and lets them claim containers top-first; with no saved order one is created. Returns why it stopped, or null - function fv3_place_imported_docker_folders(array $first): ?string { - global $configDir, $fv3DockerManDir; - $file = "$fv3DockerManDir/userprefs.cfg"; - $read = fn() => is_file($file) ? @file_get_contents($file) : ''; - for ($try = 0; $try < 3; $try++) { - $raw = $read(); - $current = $raw === false ? null : fv3_parse_user_prefs($raw); - if ($current === null) return "Unraid's saved Docker order (userprefs.cfg) could not be read"; - if (!$current) return fv3_create_docker_order($first); - $folders = fv3_read_json_strict("$configDir/docker.json"); - if ($folders === null) return 'the folder config could not be read'; - $new = array_values(array_unique(array_filter($first, fn($e) => is_string($e) && str_starts_with($e, 'folder-') - && array_key_exists(substr($e, 7), $folders) && !in_array($e, $current, true)))); - if (!$new) return null; - $out = array_merge($new, $current); - // Stock UserPrefs writes take no lock: an order Unraid saved meanwhile is read again - if ($read() !== $raw) continue; - if (!fv3_write_user_prefs($file, $out)) return "Unraid's saved Docker order (userprefs.cfg) could not be written"; - if (!saveOrderSnapshot('docker', $out)) fv3_error_log('fv3_place_imported_docker_folders', 'the order snapshot could not be written'); - return null; - } - return "Unraid's saved Docker order (userprefs.cfg) kept changing during the import"; - } - - // A container may be explicit in at most one folder (issue #62). $winnerId's list survives - // intact (the folder just saved wins); elsewhere ties resolve first-wins in key order. - function fv3_dedupe_explicit_members(array $folders, ?string $winnerId = null): array { - $claimed = []; - // PHP stores an all-digit id as an int key, so compare canonical strings below — on === - // the winner would fail to match its own id and be stripped of the members claimed here - $winnerKey = $winnerId === null ? null : (string)$winnerId; - $winnerMembers = $winnerId !== null ? ($folders[$winnerId]['containers'] ?? null) : null; - if (is_array($winnerMembers)) { - foreach ($winnerMembers as $ct) { - if (is_string($ct)) { $claimed[$ct] = true; } - } - } - foreach ($folders as $fid => $folder) { - if (($winnerKey !== null && (string)$fid === $winnerKey) || !is_array($folder['containers'] ?? null)) { continue; } - $kept = []; - foreach ($folder['containers'] as $ct) { - if (is_string($ct)) { - if (isset($claimed[$ct])) { continue; } - $claimed[$ct] = true; - } - $kept[] = $ct; - } - $folders[$fid]['containers'] = $kept; - } - return $folders; - } - - // Returns the folder's id (create.php hands a new one back for the import to place) - function updateFolder(string $type, string $content, ?string $id = null) : string { - $decoded = json_decode($content, true); - // A folder must be an object/array — reject scalars so a full-backup bundle can't pollute $type.json - if (json_last_error() !== JSON_ERROR_NONE || !is_array($decoded)) { - http_response_code(400); - exit; - } - // 'root' is reserved by Unraid's Docker organizer — a same-named folder can never mirror - if (isset($decoded['name']) && is_string($decoded['name']) && strtolower(trim($decoded['name'])) === 'root') { - http_response_code(400); - header('Content-Type: application/json'); - echo json_encode(['error' => "The folder name 'root' is reserved by Unraid's Docker organizer"]); - exit; - } - // Only create.php omits the id; importAll() re-keys its own bad ids, so a malformed one here is a bad - // request, never a new folder - if ($id === null) { - $id = generateId(); - } elseif (!fv3_is_folder_id($id)) { - http_response_code(400); - header('Content-Type: application/json'); - echo json_encode(['error' => 'Folder id may only contain letters, digits, _ and -']); - exit; - } - fv3_write_folders($type, 'updateFolder', function (array $fileData) use ($decoded, $id): array { - $fileData[$id] = $decoded; - return fv3_dedupe_explicit_members($fileData, $id); - }); - return $id; - } - - // The locked read-modify-write of docker.json / vm.json (imports and createFile() take the same lock themselves): $apply - // gets the folders as they are on disk, [] with no file, and returns them changed or null for no write. A failure is a 500 - function fv3_write_folders(string $type, string $caller, callable $apply, string $refusal = 'refusing to save so existing folders are not wiped'): void { - global $configDir; - $lock = fv3_config_lock("$type.json"); - $abort = function (string $logged, string $error) use ($caller, $lock): void { - if ($lock) fv3_config_unlock($lock); - fv3_error_log($caller, $logged); - http_response_code(500); - header('Content-Type: application/json'); - echo json_encode(['error' => $error]); - exit; - }; - // Corrupt config must fail closed: merging onto empty would wipe every other folder - $fileData = fv3_read_json_strict("$configDir/$type.json"); - if ($fileData === null) $abort("$type.json is unreadable, $refusal", "$type.json is unreadable — $refusal"); - $changed = $apply($fileData); - // Nothing is written without the lock; a request that changes nothing never fails for want of it - if ($changed !== null && !$lock) $abort("could not lock $type.json", "Could not lock $type.json — nothing was saved"); - if ($changed !== null && !fv3_atomic_write("$configDir/$type.json", json_encode($changed))) { - $abort("could not write $type.json", "Could not write $type.json — nothing was saved"); - } - if ($lock) fv3_config_unlock($lock); - } - - // True when $key is a folder setting and $value is one it can hold. Colours, the text width and the graph time come - // from the settings page under looser rules than the editor's (fv3_sanitize_setting), so they are judged by those - function fv3_default_value_ok(string $key, $value): bool { - $rule = fv3_foreign_setting_rules()[$key] ?? null; - if ($rule === null) return false; - // unset: the page sends '' for an empty field and null where the form held no number; a switch is always true or false - if ($rule[0] !== 'bool' && ($value === null || $value === '')) return true; - if ($rule[0] === 'color' || $rule[0] === 'width') return is_string($value) && strlen($value) <= 50 && !preg_match('/[<>"\'\\\\]/', $value); - if ($key === 'context_graph_time') return is_int($value) && $value >= 0; - [$clean, $ok] = fv3_foreign_setting_value($rule, $value); - return $ok && $clean === $value; - } - - // Apply defaults: $settings merged into the settings of every folder as it is on disk, so nothing a page loaded - // earlier is written back. A folder that locks its colours keeps them - function applyFolderDefaults(string $type, string $settings) : void { - $patch = json_decode($settings, true); - // One bad request must not stamp an unknown key or value on every folder - if (!is_array($patch) || array_filter($patch, fn($value, $key) => !fv3_default_value_ok((string)$key, $value), ARRAY_FILTER_USE_BOTH)) { - http_response_code(400); - header('Content-Type: application/json'); - echo json_encode(['error' => 'The defaults must be folder settings with values the settings page can send']); - exit; - } - if (!$patch) { return; } - $unlocked = array_diff_key($patch, array_flip(['preview_vertical_bars_color', 'preview_border_color', 'preview_row_separator_color'])); - fv3_write_folders($type, 'applyFolderDefaults', function (array $fileData) use ($patch, $unlocked): ?array { - $changed = false; - foreach ($fileData as $folderId => $folder) { - if (!is_array($folder)) continue; - $old = is_array($folder['settings'] ?? null) ? $folder['settings'] : []; - // what the pages call locked: any value JavaScript reads as true - $locked = !in_array($old['lock_colors'] ?? false, [false, null, 0, 0.0, ''], true); - $new = array_replace($old, $locked ? $unlocked : $patch); - if ($new === $old) continue; - $fileData[$folderId]['settings'] = $new; - $changed = true; - } - return $changed ? $fileData : null; - }); - } - - // What a Docker, VMs or Dashboard page found at load, per folder: `renames` (old name => new) and `identity` (name => the - // image or UUID to know it by), applied to the folder as it is on disk. Whole lists from a page's copy are not taken - function updateFolderIds(string $type, string $data) : void { - $updates = json_decode($data, true); - // A JSON list is not a report: its positions would be read as folder ids. Told by the text: a map keyed "0" decodes the same - if (json_last_error() !== JSON_ERROR_NONE || !is_array($updates) || ($updates && ltrim($data)[0] === '[')) { http_response_code(400); exit; } - if (!$updates) { return; } - $identity = $type === 'vm' ? 'containerIds' : 'containerImages'; - fv3_write_folders($type, 'updateFolderIds', function (array $fileData) use ($updates, $identity): ?array { - // Every name that is a member of some folder by now: a rename never takes one from where it was put - $claimed = []; - foreach ($fileData as $folder) { - foreach (is_array($folder) && is_array($folder['containers'] ?? null) ? $folder['containers'] : [] as $member) { - if (is_scalar($member)) $claimed[(string)$member] = true; - } - } - $changed = false; - foreach ($updates as $folderId => $patch) { - if (!fv3_is_folder_id($folderId)) continue; - if (!is_array($fileData[$folderId] ?? null) || !is_array($patch)) continue; - $folder = fv3_apply_member_changes($fileData[$folderId], $patch, $identity, $claimed); - if ($folder === $fileData[$folderId]) continue; - $fileData[$folderId] = $folder; - $changed = true; - } - // As before this took renames: one container still listed in two folders stays in the first (issue #62) - return $changed ? fv3_dedupe_explicit_members($fileData) : null; - }); - } - - // $folder with a page's renames and identity entries applied to its members as they are now. Mirrors what - // fv3ResolveRenamedContainers (customEvents.js) does to the page's own copy, its skip of a $claimed name included - function fv3_apply_member_changes(array $folder, array $patch, string $identity, array &$claimed): array { - $members = is_array($folder['containers'] ?? null) ? $folder['containers'] : []; - $find = function ($name) use (&$members) { - foreach ($members as $at => $member) { if (is_scalar($member) && (string)$member === (string)$name) return $at; } - return null; - }; - foreach (is_array($patch['renames'] ?? null) ? $patch['renames'] : [] as $old => $new) { - $at = is_string($new) && $new !== '' ? $find($old) : null; - // a new name that a save since the page loaded made a member somewhere is left there: the next load decides again - if ($at === null || isset($claimed[$new])) continue; - $claimed[$new] = true; - $members[$at] = $new; - $folder['containers'] = $members; - if (is_array($folder[$identity] ?? null) && array_key_exists($old, $folder[$identity])) { - $folder[$identity][$new] = $folder[$identity][$old]; - unset($folder[$identity][$old]); - } - $hidden = is_array($folder['hidden_preview'] ?? null) ? array_search((string)$old, $folder['hidden_preview'], true) : false; - if ($hidden !== false) $folder['hidden_preview'][$hidden] = $new; - } - foreach (is_array($patch['identity'] ?? null) ? $patch['identity'] : [] as $name => $value) { - if (!is_string($value) || $value === '' || $find($name) === null) continue; - if (is_array($folder[$identity] ?? null) && array_key_exists($name, $folder[$identity])) continue; - if (!is_array($folder[$identity] ?? null)) $folder[$identity] = []; - $folder[$identity][$name] = $value; - } - return $folder; - } - - function deleteFolder(string $type, string $id) : void { - fv3_write_folders($type, 'deleteFolder', function (array $fileData) use ($id): ?array { - // Already gone: delete stays idempotent, and there is nothing to rewrite - if (!array_key_exists($id, $fileData)) { return null; } - unset($fileData[$id]); - return $fileData; - }, 'refusing to delete so the folder config is not wiped'); - } - function readSettings() : string { global $configDir; $raw = @file_get_contents("$configDir/settings.json"); @@ -1417,6 +624,9 @@ function updateSettingsBatch(array $settings) : void { // Theme listing, import, toggle and delete, and the styles/ confinement helpers they share require_once(__DIR__ . '/themes.php'); require_once(__DIR__ . '/lib.foreign_import.php'); + require_once(__DIR__ . '/lib.folders.php'); + // Docker and VM order: userprefs.cfg, the autostart file, folder membership and the page order + require_once(__DIR__ . '/lib.order.php'); function exportAll() : array { global $configDir; @@ -1821,27 +1031,6 @@ function fv3_render_css_files(array $config) : array { return $out; } - function generateId(int $length = 20) : string { - return substr(str_replace(['+', '/', '='], '', base64_encode(random_bytes((int)ceil($length * 3 / 4)))), 0, $length); - } - - // Ids safe to write and to render into class/selector/onclick markup; int = all-digit JSON key - function fv3_is_folder_id($id) : bool { - return (is_string($id) || is_int($id)) && preg_match('/^[A-Za-z0-9_-]+$/D', (string)$id) === 1; - } - - function createFile(string $type): void { - global $configDir; - if (!is_dir($configDir)) { @mkdir($configDir, 0770, true); } - $path = "$configDir/$type.json"; - // Under the file's lock, and only if it is still missing: a save or an import since the caller's check is never replaced - $lock = fv3_config_lock("$type.json"); - if (!$lock) return; - clearstatcache(true, $path); - if (!file_exists($path)) { fv3_atomic_write($path, '{}'); } - fv3_config_unlock($lock); - } - // Mirrors DockerContainers.php:82 — ':???' net-ref wins, then Unraid's per-container cache. // Only 'updated' comes from that cache; WebUi/Shell stay live here (the cache holds unresolved [IP]/[PORT:n]). function fv3_container_update_status(array $ct, string $name, array $cache, DockerUpdate $DockerUpdate): ?bool { @@ -2251,51 +1440,6 @@ function readInfo(string $type): array { return $info; } - function readUnraidOrder(string $type): array { - fv3_debug_log("readUnraidOrder called for type: $type"); - $user_prefs_path = "/boot/config/plugins"; - $order = []; - if ($type == "docker") { - $dockerClient = new DockerClient(); - // A userprefs.cfg that will not parse lists A-Z, as one that is absent does - $order = fv3_live_order(array_column($dockerClient->getDockerContainers() ?: [], 'Name'), fv3_docker_prefs() ?? []); - - } elseif ($type == "vm") { - if (!fv3_require_libvirt_helpers()) { fv3_debug_log("VM Order: libvirt_helpers.php not available."); return []; } - global $lv; - if (!isset($lv)) { $lv = new Libvirt(); if (!$lv->connect()) { fv3_debug_log("VM Order: Libvirt connection failed."); return []; } } - - $prefs_file = "$user_prefs_path/dynamix.vm.manager/userprefs.cfg"; - $vms = $lv->get_domains(); - - if (!empty($vms)) { - if (file_exists($prefs_file)) { - $prefs_ini = @parse_ini_file($prefs_file); - if ($prefs_ini) { - $prefs_array = array_values($prefs_ini); - $sort = []; - $count_vms = count($vms); - foreach ($vms as $vm_name) { - $search = array_search($vm_name, $prefs_array); - $sort[] = ($search === false) ? ($count_vms + count($sort) + 1) : $search; - } - if (!empty($sort)) { - @array_multisort($sort, SORT_NUMERIC, $vms); - } else { - natcasesort($vms); - } - } else { - natcasesort($vms); - } - } else { - natcasesort($vms); - } - $order = array_values($vms); - } - } - fv3_debug_log("readUnraidOrder for type: $type completed. Order: " . json_encode($order)); - return $order; - } function pathToMultiDimArray($dir) { $final = []; try { diff --git a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/server/read_membership.php b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/server/read_membership.php index ab7fa68d..1e3cc838 100644 --- a/src/folder.view3/usr/local/emhttp/plugins/folder.view3/server/read_membership.php +++ b/src/folder.view3/usr/local/emhttp/plugins/folder.view3/server/read_membership.php @@ -41,8 +41,8 @@ echo json_encode(['error' => 'container label read unavailable']); exit; } - // The map does not depend on the saved order (page_order_test.php), so userprefs.cfg is not read here - $m = fv3_compute_folder_membership($folders, $allContainerNames, $ctLabels); + // The saved order as the page reads it (readUserPrefs: unreadable = nothing saved) decides who gets a container two folders' regexes match + $m = fv3_compute_folder_membership($folders, $allContainerNames, $ctLabels, fv3_docker_prefs() ?? []); $out = []; foreach ($m['containers'] as $placeholder => $members) { foreach ($members as $ct) { $out[$ct] = $m['names'][$placeholder]; }