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 = '0028-flush-dedicated-stdout.patch';

test('native replay readiness messages are flushed to the gateway immediately', async () => {
  const [build, patch, smoke] = await Promise.all([
    readFile(new URL('tools/build-native.sh', root), 'utf8'),
    readFile(new URL(`port/native/patches/${patchName}`, root), 'utf8'),
    readFile(new URL('port/native/smoke-server-demo-join.sh', root), 'utf8'),
  ]);

  assert.equal(build.split(patchName).length - 1, 1);
  assert.match(build, /grep -Fq \$'fflush\( stdout \);'/);
  assert.match(patch, /\+\tfflush\( stdout \);/);
  assert.match(patch, /\n \tfputs\( text, stdout \);\n\+\tfflush\( stdout \);/);
  assert.match(smoke, /setsid "\$\{server_command\[@\]\}" <&3 >"\$\{SERVER_RAW\}" 2>&1 &/);
  assert.doesNotMatch(smoke, /script -qefc "\$\{server_command_line\}"/);
});
