/**
 * Copyright (c) Microsoft Corporation.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
import { test, expect, playwrightCtConfigText } from './playwright-test-fixtures';
import fs from 'fs';
import path from 'path';
test.describe.configure({ mode: 'parallel' });
test('should work with the empty component list', async ({ runInlineTest }, testInfo) => {
  const result = await runInlineTest({
    'playwright.config.ts': playwrightCtConfigText,
    'playwright/index.html': ``,
    'playwright/index.js': ``,
    'a.test.ts': `
      import { test, expect } from '@playwright/experimental-ct-react';
      test('pass', async ({ mount }) => {});
    `,
  }, { workers: 1 });
  expect(result.exitCode).toBe(0);
  expect(result.passed).toBe(1);
  const output = result.output;
  expect(output).toContain('transforming...');
  expect(output.replace(/\\+/g, '/')).toContain('.cache/index.html');
  const metainfo = JSON.parse(fs.readFileSync(testInfo.outputPath('playwright/.cache/metainfo.json'), 'utf-8'));
  expect(metainfo.version).toEqual(require('playwright-core/package.json').version);
  expect(metainfo.viteVersion).toEqual(require('vite/package.json').version);
  expect(Object.entries(metainfo.deps)).toHaveLength(0);
  expect(Object.entries(metainfo.sources)).toHaveLength(9);
});
test('should extract component list', async ({ runInlineTest }, testInfo) => {
  const result = await runInlineTest({
    'playwright.config.ts': playwrightCtConfigText,
    'playwright/index.html': ``,
    'playwright/index.ts': ``,
    'src/button.tsx': `
      export const Button = () => Button ;
    `,
    'src/components.tsx': `
      export const Component1 = () => 
Component 1
;
      export const Component2 = () => Component 2
;
    `,
    'src/defaultExport.tsx': `
      export default () => Default export
;
    `,
    'src/clashingNames1.tsx': `
      export const ClashingName = () => Clashing name 1
;
    `,
    'src/clashingNames2.tsx': `
      export const ClashingName = () => Clashing name 2
;
    `,
    'src/one-import.spec.tsx': `
      import { test, expect } from '@playwright/experimental-ct-react';
      import { Button } from './button';
      test('pass', async ({ mount }) => {
        const component = await mount(Button ;
    `,
    'src/relative-import-different-folders/one/one.spec.tsx': `
      import { test, expect } from '@playwright/experimental-ct-react';
      import Button from '.';
      test('pass', async ({ mount }) => {
        const component = await mount(Button ;
    `,
    'src/relative-import-different-folders/two/two.spec.tsx': `
      import { test, expect } from '@playwright/experimental-ct-react';
      import Button from '.';
      test('pass', async ({ mount }) => {
        const component = await mount(Button ;
      `,
      'src/button.test.tsx': `
          import { test, expect } from '@playwright/experimental-ct-react';
        import { Button } from './button.tsx';
        test('pass', async ({ mount }) => {
          const component = await mount(Button 2 ;
      `,
    }, { workers: 1 });
    expect(result.exitCode).toBe(0);
    expect(result.passed).toBe(1);
    const output = result.output;
    expect(output, 'should rebuild bundle').toContain('modules transformed');
  });
});
test('should grow cache', async ({ runInlineTest }, testInfo) => {
  test.slow();
  await test.step('original test', async () => {
    const result = await runInlineTest({
      'playwright.config.ts': playwrightCtConfigText,
      'playwright/index.html': ``,
      'playwright/index.ts': ``,
      'src/button1.tsx': `
        export const Button1 = () => Button 1 ;
      `,
      'src/button2.tsx': `
        export const Button2 = () => Button 2 ;
      `,
      'src/button1.test.tsx': `
          import { test, expect } from '@playwright/experimental-ct-react';
        import { Button1 } from './button1.tsx';
        test('pass', async ({ mount }) => {
          const component = await mount(Button ;
    `,
    'src/button.spec.tsx': `
      import { test, expect } from '@playwright/experimental-ct-react';
      import { Button } from './button';
      test('pass', async ({ mount }) => {
        const component = await mount(Button ;
      `,
      'src/button.test.tsx': `
        import { test, expect } from '@playwright/experimental-ct-react';
        import { Button } from './button.tsx';
        test('pass', async ({ mount }) => {
          const component = await mount(Button ;
    `,
    'src/buttonHelper.ts': `
      import { Button } from './button.tsx';
      export { Button };
    `,
    'src/button.test.tsx': `
      import { test, expect } from '@playwright/experimental-ct-react';
      import { Button } from './buttonHelper';
      test('pass', async ({ mount }) => {
        const component = await mount(Button ;
    `,
    'src/buttonFixture.tsx': `
      import { Button } from './button';
      import { test as baseTest } from '@playwright/experimental-ct-react';
      export { expect } from '@playwright/experimental-ct-react';
      export const test = baseTest.extend({
        button: async ({ mount }, use) => {
          await use(await mount({types.join(' ')}
;
      };
    `,
    'src/component.spec.tsx': `
      import { test, expect } from '@playwright/experimental-ct-react';
      import { Button } from './button';
      test('renders props with builtin types', async ({ mount, page }) => {
        const component = await mount({typeof value}
;
      };
    `,
    'src/component.spec.tsx': `
      import { test, expect } from '@playwright/experimental-ct-react';
      import { Component } from './component';
      test('renders props with undefined type', async ({ mount, page }) => {
        const component = await mount(