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

const root = new URL('../', import.meta.url);

test('partial OpenAL initialization can be shut down safely', async () => {
  const [patch, wasmBuild, nativeBuild, sourceOffer] = await Promise.all([
    readFile(new URL('port/native/patches/0015-guard-partial-openal-shutdown.patch', root), 'utf8'),
    readFile(new URL('tools/build-wasm.sh', root), 'utf8'),
    readFile(new URL('tools/build-native.sh', root), 'utf8'),
    readFile(new URL('tools/stage-source-offer.sh', root), 'utf8')
  ]);

  assert.match(patch, /if\( openalContext != NULL \)/);
  assert.match(patch, /if\( openalDevice != NULL \)/);
  assert.match(patch, /alcDestroyContext\( openalContext \)/);
  assert.match(patch, /alcCloseDevice\( openalDevice \)/);

  for (const build of [wasmBuild, nativeBuild, sourceOffer]) {
    assert.match(build, /0015-guard-partial-openal-shutdown\.patch/);
  }
});
