Luigit
repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by admin

quickshell/nuguland/features/audio/fixtures/production.mjs

Raw
import assert from 'node:assert/strict';
import { spawnSync } from 'node:child_process';
import { existsSync, mkdirSync, readFileSync, writeFileSync } from 'node:fs';

export default async function audioProduction({ env, start, stop, waitFor, ipc, setFixture }) {
    assert.equal(process.env.NUGULAND_UI_SANDBOX, '1');
    const dir = '/home/test/.config/pipewire';
    for (const config of ['pipewire', 'client']) {
        mkdirSync(`${dir}/${config}.conf.d`, { recursive: true });
        writeFileSync(`${dir}/${config}.conf.d/nuguland-test.conf`, 'context.properties = { module.rt = false }\n');
    }
    // Server-owned adapters live entirely in this namespace, without hardware or a session manager.
    writeFileSync(`${dir}/pipewire.conf.d/nodes.conf`, `context.objects = [
      { factory = adapter args = { factory.name = support.null-audio-sink node.name = nuguland.sink.one node.description = "fixture one" media.class = Audio/Sink object.linger = true audio.position = [ FL FR ] } }
      { factory = adapter args = { factory.name = support.null-audio-sink node.name = nuguland.sink.two node.description = "fixture two" media.class = Audio/Sink object.linger = true audio.position = [ FL FR ] } }
      { factory = adapter args = { factory.name = support.null-audio-sink node.name = nuguland.input node.description = "fixture input" media.class = Audio/Source object.linger = true audio.position = [ FL FR ] } }
    ]\n`);
    const daemon = start('pipewire', [], 'audio-pipewire');
    await waitFor('private PipeWire socket', () => existsSync(`${env.XDG_RUNTIME_DIR}/pipewire-0`));
    const fixture = '/project/audio-native-probe.qml';
    writeFileSync(fixture, readFileSync('/project/features/audio/fixtures/native-probe.qml', 'utf8').replaceAll('import "../../../features/', 'import "./features/'));
    const child = start('qs', ['--no-color', '-p', fixture], 'audio-native-probe');
    setFixture(fixture, '/evidence/audio-native-probe.log', child);
    try {
        await waitFor('audio native probe loaded', () => readFileSync('/evidence/audio-native-probe.log', 'utf8').includes('Configuration Loaded'));
        const state = () => JSON.parse(ipc('snapshot'));
        const found = await waitFor('two sinks and input from private PipeWire', () => {
            const s = state();
            return s.pipewireReady && s.sinks.length === 2 && s.sources.length === 1 ? s : null;
        });
        const row = found.sinks.find(item => item.name === 'fixture two');
        const input = found.sources.find(item => item.name === 'fixture input');
        assert(row && input, JSON.stringify(found));
        // No metadata default exists in this graph: both sinks are non-default.
        assert.equal(row.isDefault, false);
        ipc('volume', row.id, '0.42');
        ipc('mute', row.id);
        ipc('volume', input.id, '0.63');
        await waitFor('non-default sink and input updates', () => {
            const s = state();
            const sink = s.sinks.find(item => item.id === row.id);
            const source = s.sources.find(item => item.id === input.id);
            return sink?.muted && Math.abs(sink.volume - 0.42) < 0.02 && Math.abs(source?.volume - 0.63) < 0.02;
        });
        const dump = spawnSync('pw-dump', [], { env, encoding: 'utf8', timeout: 5000 });
        assert.equal(dump.status, 0, dump.stderr);
        const nodes = JSON.parse(dump.stdout);
        for (const [id, volume, muted] of [[row.id, 0.42, true], [input.id, 0.63, false]]) {
            const node = nodes.find(value => String(value.id) === id);
            const props = node?.info?.params?.Props?.[0];
            assert(props, `missing native Props for ${id}`);
            assert.equal(props.mute, muted);
            // PipeWire stores linear amplitudes; Quickshell exposes their cube roots.
            assert(props.channelVolumes?.length > 0 && props.channelVolumes.every(value => Math.abs(Math.cbrt(value) - volume) < 0.02), JSON.stringify(props));
        }
        console.log('pass: private PipeWire non-default sink and input controls');
    } finally {
        await stop(child);
        await stop(daemon);
    }
}