wrolpi/app/src/semantic-baseline.test.js
Roland 2629891ebc
ui: Common.js and thirteen more files off Semantic
Phase 2, part 3.  Baseline 63 -> 50.

Common.js is the one that mattered: 51 files import it, so its Semantic usage
was propping up the rest of the migration.  Everything in it now comes from the
library -- Paginator, the two search inputs, DirectorySearch, TabLinks, the
toggles, the messages, APIButton, Breadcrumbs, SimpleAccordion, IframeViewer,
CardPoster, FileIcon, LoadStatistic, the theme menu, and the hotspot icons.

Also migrated: Placeholder, Apps, ArchivePreview, CbzViewer, AddToPlaylist,
TaggedDeleteConfirmModal, the collection edit/tag forms, and four inventory
components.

New in the library, all in the gallery and tested:

  SearchBox     Semantic's `<Search category>` with its grouped results shape
                kept, because four call sites already build it.  A real
                combobox rather than a div that listens for clicks.
  Pagination    Semantic's prop names; onPageChange gets the page number.
  TabBar        the strip only -- routing stays with the caller.
  IconStack     Semantic's IconGroup, named once for assistive technology.
  Table.HeaderCell sorted/onSort -- a real button carrying aria-sort.
  Button/IconButton translate Semantic's size names.

That last one is defensive: unmigrated callers still pass size='tiny', and
Mantine drops a size it does not recognise without a word, so the button
quietly renders at the wrong size.  Mapping here keeps the mixed state honest.

Three real bugs found and fixed on the way, none of them cosmetic:

  * `fileSuffixIconName` returned fourteen icon names the map did not have, so
    opening a folder with a PDF, a Word document or an archive in it logged an
    error and drew a fallback glyph.  All fourteen mapped, with a test that
    walks the mapper's own return values, and another asserting every mapped
    name resolves to a Tabler component that exists.
  * `Statistic` relied on Semantic's `Statistic.Group` for its spacing, so on
    the dashboard the load averages ran together into "1 MIN. LOAD5 MIN.
    LOAD15 MIN. LOAD".  It carries its own spacing now.
  * Semantic's inverted stylesheet colors every descendant of an inverted
    segment, so Toggle labels inherited #dddddd -- white pixels in night mode,
    on the Settings page, which is exactly where someone adjusts their WROLPi
    in the dark.  Restated from tokens.

The baseline ratchet gained a third assertion: a migrated file may still lean
on Semantic's CSS classes without importing the library, which the import check
cannot see and which would collapse silently when Phase 4 deletes the
stylesheet.  There is one such bridge today and it is now declared.
2026-08-08 18:04:00 -06:00

74 lines
3.2 KiB
JavaScript

import fs from 'fs';
import path from 'path';
import baseline from './semantic-baseline.json';
/*
* A ratchet for the Semantic UI removal.
*
* semantic-baseline.json lists every file that still imports Semantic UI (or the
* Semantic-based toast library). This test fails if a file not on that list
* starts importing them, and asks for the list to shrink when a file stops.
*
* It replaces an ESLint rule deliberately: `no-restricted-imports` would either
* print 75 warnings on every dev rebuild, or fail the build outright, and CI
* already unsets CI= for the build step so warnings there guard nothing.
*
* When the list reaches zero, delete this test, the JSON, and the dependencies.
*/
const SRC = path.join(__dirname);
const LIBRARIES = /from ['"](semantic-ui-react|semantic-ui-react\/|react-semantic-toasts-2)/;
/** Every source file under src/, excluding tests. */
const sourceFiles = (dir = SRC) => fs.readdirSync(dir, {withFileTypes: true}).flatMap(entry => {
const full = path.join(dir, entry.name);
if (entry.isDirectory()) return sourceFiles(full);
if (!/\.(js|jsx|ts|tsx)$/.test(entry.name)) return [];
if (/\.(test|cy)\.(js|jsx|ts|tsx)$/.test(entry.name)) return [];
return [path.relative(path.join(SRC, '..'), full)];
});
const importers = () => sourceFiles()
.filter(file => LIBRARIES.test(fs.readFileSync(path.join(SRC, '..', file), 'utf8')))
.sort();
describe('Semantic UI removal', () => {
it('has no new files importing Semantic UI', () => {
const added = importers().filter(file => !baseline.includes(file));
// New code must import from src/components/ui instead. See ui-migration-plan.md.
expect(added).toEqual([]);
});
it('keeps the baseline free of files that no longer import Semantic UI', () => {
const current = importers();
const stale = baseline.filter(file => !current.includes(file));
// Migrating a file should also delete its line here, so the count means something.
expect(stale).toEqual([]);
});
it('declares every migrated file that still leans on Semantic CSS classes', () => {
/*
* A file can stop importing semantic-ui-react and still depend on its stylesheet
* by writing `className="ui stackable grid"`. The import ratchet above cannot see
* that, so when Phase 4 deletes semantic.min.css the layout would silently
* collapse. Anything on this list is a deliberate bridge that must be revisited
* then; anything NOT on it is an accident.
*/
const allowed = ['src/components/collections/CollectionEditForm.js'];
const bridges = sourceFiles()
.filter(file => !baseline.includes(file))
.filter(file => /className=["']ui /.test(fs.readFileSync(path.join(SRC, '..', file), 'utf8')));
expect(bridges.sort()).toEqual(allowed.sort());
});
it('reports how much of the migration is left', () => {
// Not an assertion so much as a progress readout in the test output.
const remaining = importers().length;
expect(remaining).toBeLessThanOrEqual(baseline.length);
console.log(`Semantic UI: ${remaining} files remaining to migrate.`);
});
});