Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/load-test.mjs

Raw
import assert from 'node:assert/strict';
import { copyFileSync, cpSync, globSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from 'node:fs';
import { tmpdir } from 'node:os';
import { dirname, join, relative, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import { assertOffscreenPass, runOffscreen } from './test-fixtures/offscreen.mjs';
import { featureDoubles, rootEntry } from './test-fixtures/sources.js';
import { assertPureService } from './ui-test.mjs';

const project = dirname(fileURLToPath(import.meta.url));
const skipped = new Set(['vault-native', '.system', '.pi', '.qmlls.ini', 'node_modules']);

// A feature's load target is `features/<name>/fixtures/load.qml`, importing the
// shell root as `import "../../.." as N`. Every feature's doubles replace their
// production services in a private copy, because the registry a target loads
// composes every feature's service; the target runs offscreen as a root entry.
// Load evidence only: no compositor, input, or pixels.
export function loadTargets(root = project) {
    return globSync('features/*/fixtures/load.qml', { cwd: root }).map(file => file.split('/')[1]).sort();
}

export function loadFeature(feature, options = {}) {
    return loadFeatures([feature], options)[0];
}

export function loadFeatures(features, { timeout = 10_000 } = {}) {
    const targets = loadTargets();
    for (const feature of features) {
        assert.match(feature, /^[a-z][a-z0-9-]*$/, `invalid feature name: ${feature}`);
        assert(targets.includes(feature), `no load target: features/${feature}/fixtures/load.qml`);
    }
    const directory = mkdtempSync(join(tmpdir(), 'nuguland-load-'));
    try {
        const copy = join(directory, 'project');
        cpSync(project, copy, { recursive: true, filter: source => !skipped.has(relative(project, source).split('/')[0]) });
        for (const { source, target } of featureDoubles(copy)) {
            assertPureService(readFileSync(join(copy, source), 'utf8'));
            copyFileSync(join(copy, source), join(copy, target));
        }
        return features.map(feature => {
            const started = Date.now();
            const runDirectory = mkdtempSync(join(directory, `${feature}-`));
            try {
                const entry = join(copy, `load-test-${feature}.qml`);
                writeFileSync(entry, rootEntry(readFileSync(join(copy, 'features', feature, 'fixtures', 'load.qml'), 'utf8'), 3));
                const run = runOffscreen(entry, { directory: runDirectory, cwd: copy, timeout });
                assertOffscreenPass(run, new RegExp(`^.*load ${feature}: PASS$`, 'm'));
                console.log(`pass load ${feature} (${((Date.now() - started) / 1000).toFixed(1)}s)`);
                return run;
            } finally {
                rmSync(runDirectory, { recursive: true, force: true });
            }
        });
    } finally {
        rmSync(directory, { recursive: true, force: true });
    }
}

if (process.argv[1] && resolve(process.argv[1]) === fileURLToPath(import.meta.url)) {
    const [requested = 'all', ...rest] = process.argv.slice(2);
    try {
        assert.equal(rest.length, 0, 'usage: just load-test [feature|all]');
        const features = requested === 'all' ? loadTargets() : [requested];
        assert(features.length > 0, 'no load targets under features/*/fixtures/load.qml');
        loadFeatures(features);
    } catch (error) {
        console.error(error.stack || String(error));
        process.exitCode = 1;
    }
}