import assert from 'node:assert/strict';
import { readFile } from 'node:fs/promises';
import test from 'node:test';

const root = new URL('../', import.meta.url);
const patchName = '0025-disable-audio-in-dedicated-build.patch';

test('dedicated builds never initialize or retry an audio backend', async () => {
  const patch = await readFile(new URL(`port/native/patches/${patchName}`, root), 'utf8');
  assert.match(patch, /diff --git a\/src\/framework\/Common\.cpp/);
  assert.match(patch, /\+#ifdef ID_DEDICATED\n\+\t\/\/ Dedicated builds must disable hardware before the sound system starts\.\n\+\tcvarSystem->SetCVarBool\( "s_noSound", true \);\n\+#endif\n\+\n \t\/\/ start the sound system/);
  assert.match(patch, /-\tcvarSystem->SetCVarBool\( "s_noSound", true \);/);
  assert.match(patch, /diff --git a\/src\/sound\/snd_system\.cpp/);
  assert.match(patch, /\+#if defined\( ID_DEDICATED \)/);
  assert.match(patch, /Dedicated map-load pacifier updates can call Render/);
  assert.match(patch, /\+\tSound_UpdateControllerRumble\( 0\.0f \);\n\+\treturn;/);
  assert.match(patch, /\+#endif/);
});

test('every reproducible source path applies the dedicated audio patch', async () => {
  for (const relative of [
    'tools/build-native.sh',
    'tools/build-wasm.sh',
    'tools/stage-source-offer.sh',
  ]) {
    const source = await readFile(new URL(relative, root), 'utf8');
    assert.equal(source.split(patchName).length - 1, 1, `${relative} must apply ${patchName} once`);
  }
});
