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('SDL3 binds its Emscripten window to the shell WebGL canvas', async () => {
  const [html, patch, wasmBuild, sourceOffer] = await Promise.all([
    readFile(new URL('public/index.html', root), 'utf8'),
    readFile(new URL('port/native/patches/0012-bind-sdl3-web-canvas.patch', root), 'utf8'),
    readFile(new URL('tools/build-wasm.sh', root), 'utf8'),
    readFile(new URL('tools/stage-source-offer.sh', root), 'utf8'),
  ]);

  assert.match(html, /<canvas id="gameCanvas"/);
  assert.match(patch, /#if defined\(__EMSCRIPTEN__\)/);
  assert.match(patch, /SDL_HINT_EMSCRIPTEN_CANVAS_SELECTOR, "#gameCanvas"/);
  assert.match(patch, /return false/);
  for (const sourceChain of [wasmBuild, sourceOffer]) {
    assert.match(sourceChain, /0012-bind-sdl3-web-canvas\.patch/);
  }
});
