const test = require("node:test");
const assert = require("node:assert/strict");
const fs = require("node:fs");
const path = require("node:path");

const source = fs.readFileSync(path.join(
  __dirname,
  "..",
  "..",
  "src",
  "MBN_STOCK_WEBVIEW.LegacyParityApp",
  "MainWindow.Playout.cs"), "utf8");

test("first PREPARE and TAKE IN freeze fresh PageN plans for every paged row", () => {
  assert.ok((source.match(/CreateFreshPagedPlayoutRequestAsync\(/g) || []).length >= 3);
  assert.match(source,
    /Where\(static entry => entry\.PageNavigation\?\.PageSize\.HasValue == true\)/u);
  assert.match(source, /PreflightPagePlansAsync\(/u);
  assert.match(source,
    /ReflectSuccessfulPlayout\([\s\S]*plan\.EntryId,[\s\S]*pageIndexZeroBased: 0,[\s\S]*plan\.PageCount,[\s\S]*synchronizeOperatorSelection: false\)/u);
});

test("fresh PageN wiring rejects empty or mismatched plans before vendor dispatch", () => {
  assert.match(source, /plans\.Count != pagedEntries\.Length/u);
  assert.match(source, /plan\.PageSize != \(int\)expectedPageSize/u);
  assert.match(source, /plan\.PageCount is < 1 or > ScenePaging\.MaximumPageCount/u);
  assert.match(source, /workflow\.State\.CurrentEntryId is not null/u);
});
