import { TestGenerator } from './src/tools/autotest/test-generator.js';

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

console.log('\n── AutoTest Navigation Selector Fix ──');

const generator = new TestGenerator(null);
const fixed = generator._fixNavigation(
  [{
    id: 'WF_001',
    page: '/Dashboard',
    steps: [
      { action: 'navigate', selector: 'https://example.test/app' },
      { action: 'click', selector: 'text=sidebarMessages', description: 'Navigate to messages' },
      { action: 'assert_visible', selector: 'vlid:sendBtn' },
    ],
  }],
  {
    homePage: '/Dashboard',
    pages: [
      { path: '/Dashboard', route: '/', navPath: [] },
      { path: '/MessageManage', route: '/messages', navPath: ['text=sidebarMessages'] },
    ],
  },
  'https://example.test/app'
);

assert(fixed[0].steps[1].selector === 'vlid:sidebarMessages', 'nav click should use vlid: selector for id-like nav entries');

const fixedLabelNav = generator._fixNavigation(
  [{
    id: 'WF_002',
    page: '/CourseList',
    steps: [
      { action: 'navigate', selector: 'https://example.test/app' },
      { action: 'assert_visible', selector: 'vlid:addButton' },
    ],
  }],
  {
    homePage: '/Dashboard',
    pages: [
      { path: '/Dashboard', route: '/', navPath: [] },
      { path: '/CourseList', route: '/courses', navPath: ['text=课程管理'] },
    ],
  },
  'https://example.test/app',
  [
    { instanceId: 'navItem', text: '课程管理', position: 'sidebar' },
  ]
);

assert(
  fixedLabelNav[0].steps[1].selector === 'vlid:navItem[text="课程管理"]',
  'label-based navPath should upgrade to vlid + text filter when nav discovery provides a component id'
);

console.log('PASS test-autotest-nav-selector-fix.js');
