import {
  comfyPageFixture as test,
  comfyExpect as expect
} from '@e2e/fixtures/ComfyPage'

import enMessages from '@/locales/en/main.json' with { type: 'json' }

/**
 * PM-1497 (F13): the general-onboarding wait is cloud-only. `decideFirstRun`
 * returns `'complete'` on the first line for any non-cloud build
 * (`firstRunEntry.ts`: `if (!isCloud) return 'complete'`), so a local build
 * never shows Getting Started and never engages the first-run latch — the agent
 * onboarding behaves exactly as before this PR.
 *
 * `isCloud` is compile-time, so an absence assertion is only meaningful against
 * a genuine local bundle. This guards it: it proves the LOCAL run-button chunk
 * loaded at runtime and the CLOUD one never did, then asserts Getting Started is
 * absent. Run this against a local `dist` served on its own port; against a
 * cloud bundle the guard fails first (the cloud chunk loads), so the absence can
 * never pass for the wrong reason.
 *
 * Desktop takes the same `!isCloud -> 'complete'` path (`decideFirstRun` never
 * reads `isDesktop`), so this local run is the mechanical proof for the desktop
 * onboarding decision too; an Electron drive would need the desktop harness and
 * proves nothing extra about this branch.
 */
test.describe(
  'First-run onboarding is absent on a local build',
  { tag: ['@ui'] },
  () => {
    test('never shows Getting Started, and the local bundle is what loaded', async ({
      comfyPage
    }) => {
      const page = comfyPage.page

      const loadedChunks: string[] = []
      page.on('response', (response) => {
        const url = response.url()
        if (url.includes('RunButtonWrapper')) loadedChunks.push(url)
      })

      await test.step('The app boots on the local bundle', async () => {
        // The initial boot's chunk fetches predate this listener (navigation
        // happens in the fixture), so reload with the listener attached and
        // observe which run-button wrapper the bundle actually requests.
        // `isCloud` (compile-time) picks which wrapper `ComfyRunButton/index.ts`
        // imports, so exactly one of these chunks is ever fetched.
        await comfyPage.workflow.reloadAndWaitForApp()
        await expect(comfyPage.canvas).toBeVisible()
        await expect
          .poll(
            () =>
              loadedChunks.some((u) => u.includes('LocalRunButtonWrapper')),
            {
              message:
                'the local run-button chunk must load, proving this is a local bundle',
              timeout: 15_000
            }
          )
          .toBe(true)
        expect(
          loadedChunks.some((u) => u.includes('CloudRunButtonWrapper')),
          'the cloud run-button chunk must never load on a local bundle; if it did, an "onboarding absent" assertion would be against the wrong build'
        ).toBe(false)
      })

      await test.step('Getting Started never appears on the local build', async () => {
        await expect(
          page.getByRole('dialog', { name: enMessages.gettingStarted.title })
        ).toHaveCount(0)
        await expect(page.getByTestId('coach-spotlight')).toHaveCount(0)
      })
    })
  }
)
