import { TestRunner } from './src/tools/autotest/test-runner.js';

function assert(condition, message) {
  if (!condition) throw new Error(message);
}

console.log('\n── AutoTest Runner Navigation Reload ──');

const runner = new TestRunner({ baseUrl: 'https://example.test/app', timeout: 5000 });
runner.firstNavigationDone = true;

const calls = {
  goto: 0,
  reload: 0,
  waitForLoadState: 0,
  waitForFunction: 0,
};

runner.page = {
  async goto(url) {
    calls.goto += 1;
    calls.lastGoto = url;
  },
  async reload() {
    calls.reload += 1;
  },
  async waitForLoadState() {
    calls.waitForLoadState += 1;
  },
  async waitForFunction() {
    calls.waitForFunction += 1;
  },
};

await runner._navigateTo('https://example.test/app');
await runner._navigateTo('https://example.test/app');

assert(calls.goto === 1, 'first navigation should use page.goto once');
assert(calls.reload === 1, 'second navigation to same URL should reload instead of skipping');
assert(calls.waitForLoadState === 2, 'each navigation should wait for networkidle');
assert(calls.waitForFunction === 2, 'each navigation should wait for interactive elements');

console.log('PASS test-autotest-runner-navigation.js');
