import assert from 'node:assert/strict';
import fs from 'node:fs';
import path from 'node:path';
import test from 'node:test';
import { fileURLToPath } from 'node:url';

const root = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..');

test('the web build bypasses unsupported POSIX memory-lock syscalls', () => {
  const build = fs.readFileSync(path.join(root, 'tools/build-wasm.sh'), 'utf8');
  const sourceOffer = fs.readFileSync(path.join(root, 'tools/stage-source-offer.sh'), 'utf8');
  const patch = fs.readFileSync(
    path.join(root, 'port/native/patches/0027-disable-web-memory-lock-syscalls.patch'),
    'utf8'
  );

  assert.match(build, /0027-disable-web-memory-lock-syscalls\.patch/);
  assert.match(sourceOffer, /0027-disable-web-memory-lock-syscalls\.patch/);
  assert.equal((patch.match(/#if defined\( __EMSCRIPTEN__ \)/g) || []).length, 2);
  assert.match(patch, /return mlock\(/);
  assert.match(patch, /return munlock\(/);
});
