import { describe, it } from 'vitest'
import Fixtures, { ProjectFixtureDir } from '@tooling/system-tests'
import * as FixturesScaffold from '@tooling/system-tests/lib/dep-installer'
import execa from 'execa'
import path from 'path'
import * as fs from 'fs-extra'

const scaffoldAngularProject = async (project: string) => {
  const projectPath = Fixtures.projectPath(project)

  Fixtures.removeProject(project)
  await Fixtures.scaffoldProject(project)
  await FixturesScaffold.scaffoldProjectNodeModules({ project })
  await fs.remove(path.join(projectPath, 'cypress.config.ts'))
  await fs.remove(path.join(projectPath, 'cypress'))

  return projectPath
}

const runCommandInProject = (command: string, projectPath: string) => {
  const [ex, ...args] = command.split(' ')

  return execa(ex, args, { cwd: projectPath, stdio: 'inherit' })
}

const cypressSchematicPackagePath = path.join(__dirname, '..')

const ANGULAR_PROJECTS: ProjectFixtureDir[] = ['angular-20', 'angular-21']

describe('ng add @cypress/schematic / only e2e', { timeout: 1000 * 60 * 5 }, function () {
  for (const project of ANGULAR_PROJECTS) {
    it('should install e2e files by default', async () => {
      const projectPath = await scaffoldAngularProject(project)

      await runCommandInProject(`yarn add @cypress/schematic@file:${cypressSchematicPackagePath}`, projectPath)
      if (project === 'angular-21') {
        // for angular 21, we have component testing files inside the source directory, so we need the component flag to set up the support file to declare the mount function
        await runCommandInProject('yarn ng add @cypress/schematic --e2e --component --add-ct-specs false', projectPath)
      } else {
        await runCommandInProject('yarn ng add @cypress/schematic --e2e --component false --add-ct-specs false', projectPath)
      }

      await runCommandInProject('yarn ng e2e --watch false', projectPath)
    })
  }
})
