Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/test-fixtures/sources.js

Raw
'use strict';

// Where production sources and feature doubles live, for tests and harnesses.
// Production files sit at the shell root or in `features/<name>/`; fixture
// directories never count as production.
const fs = require('fs');
const path = require('path');

const project = path.resolve(__dirname, '..');
const skipped = ['fixtures', 'test-fixtures', 'vault-native', 'node_modules', '.system', '.pi', '.git'];

function productionFiles(pattern, root = project) {
    return fs.globSync(`**/${pattern}`, { cwd: root, exclude: skipped.map(name => `**/${name}/**`) }).sort();
}

// Relative path of the one production file with this basename.
function sourcePath(name, root = project) {
    const found = productionFiles(name, root);
    if (found.length !== 1) throw new Error(`expected one production ${name}, found ${found.length}: ${found.join(', ')}`);
    return found[0];
}

function readSource(name, root = project) {
    return fs.readFileSync(path.join(root, sourcePath(name, root)), 'utf8');
}

// `features/<feature>/fixtures/services/<Type>.qml` replaces the one production `<Type>.qml`;
// `except` names features whose production services stay.
function featureDoubles(root = project, feature = null, except = []) {
    return fs.globSync('features/*/fixtures/services/*', { cwd: root }).sort()
        .filter(file => (!feature || file.split('/')[1] === feature) && !except.includes(file.split('/')[1]))
        .map(source => ({ source, target: sourcePath(path.basename(source), root) }));
}

// Feature suites: `features/<name>/fixtures/suite.mjs`, run by `just ui-test <name>`.
function featureSuites(root = project) {
    return fs.globSync('features/*/fixtures/suite.mjs', { cwd: root }).map(file => file.split('/')[1]).sort();
}

// Quickshell resolves modules only inside the config folder, so a fixture runs as
// an entry at the shell root: every root-relative import (`"../.."`, `"../../features/x"`)
// becomes root-local. The shell root import `as N` must be present.
function rootEntry(text, depth) {
    const up = Array(depth).fill('..').join('/');
    if (!text.includes(`import "${up}" as N`)) throw new Error(`fixture must import the shell root as: import "${up}" as N`);
    return text.replace(new RegExp(`import "${up.replace(/\./g, '\\.')}(?=["/])`, 'g'), 'import ".');
}

// Registry entries in registration order with their literal top-level fields
// (`name`, `kind`, `width`, `height`); a field
// bound to an expression is absent. `panel` says whether the entry declares one.
// Tests read entries here, never mirror them.
function registryEntries(root = project) {
    const text = fs.readFileSync(path.join(root, 'Registry.qml'), 'utf8');
    const entries = [];
    for (const match of text.matchAll(/^\s*FeatureEntry \{/gm)) {
        const start = match.index;
        let depth = 0;
        let end = start;
        for (; end < text.length; end++) {
            if (text[end] === '{') depth++;
            else if (text[end] === '}' && --depth === 0) break;
        }
        const body = text.slice(text.indexOf('{', start) + 1, end);
        const indent = /\n(\s*)\S/.exec(body)[1];
        const field = new RegExp(`^${indent}(name|kind|width|height): (.+)$`, 'gm');
        const entry = { kind: 'popup', panel: new RegExp(`^${indent}panel: `, 'm').test(body) };
        for (const [, key, value] of body.matchAll(field)) {
            if (/^("[^"]*"|\d+|\[("[^"]*"(, )?)*\])$/.test(value)) entry[key] = JSON.parse(value);
        }
        entries.push(entry);
    }
    return entries;
}

module.exports = { featureDoubles, registryEntries, featureSuites, productionFiles, readSource, rootEntry, sourcePath };