import { describe, it, expect } from 'vitest';
import { TestProject } from '../utils';

describe('Auto Imports', () => {
  describe('imports: { ... }', () => {
    it('should generate a declaration file, imports.d.ts, for auto-imports', async () => {
      const project = new TestProject();
      project.addFile('entrypoints/popup.html', `<html></html>`);

      await project.build();

      expect(await project.serializeFile('.wxt/types/imports.d.ts'))
        .toMatchInlineSnapshot(`
          ".wxt/types/imports.d.ts
          ----------------------------------------
          // Generated by wxt
          export {}
          declare global {
            const ContentScriptContext: typeof import('wxt/client')['ContentScriptContext']
            const InvalidMatchPattern: typeof import('wxt/sandbox')['InvalidMatchPattern']
            const MatchPattern: typeof import('wxt/sandbox')['MatchPattern']
            const browser: typeof import('wxt/browser')['browser']
            const createIframeUi: typeof import('wxt/client')['createIframeUi']
            const createIntegratedUi: typeof import('wxt/client')['createIntegratedUi']
            const createShadowRootUi: typeof import('wxt/client')['createShadowRootUi']
            const defineBackground: typeof import('wxt/sandbox')['defineBackground']
            const defineConfig: typeof import('wxt')['defineConfig']
            const defineContentScript: typeof import('wxt/sandbox')['defineContentScript']
            const defineUnlistedScript: typeof import('wxt/sandbox')['defineUnlistedScript']
            const fakeBrowser: typeof import('wxt/testing')['fakeBrowser']
            const storage: typeof import('wxt/storage')['storage']
          }
          "
        `);
    });

    it('should include auto-imports in the project', async () => {
      const project = new TestProject();
      project.addFile('entrypoints/popup.html', `<html></html>`);

      await project.build();

      expect(await project.serializeFile('.wxt/wxt.d.ts'))
        .toMatchInlineSnapshot(`
          ".wxt/wxt.d.ts
          ----------------------------------------
          // Generated by wxt
          /// <reference types="wxt/vite-builder-env" />
          /// <reference types="./types/imports.d.ts" />
          /// <reference types="./types/paths.d.ts" />
          /// <reference types="./types/i18n.d.ts" />
          /// <reference types="./types/globals.d.ts" />
          "
        `);
    });
  });

  describe('imports: false', () => {
    it('should not generate a imports.d.ts file', async () => {
      const project = new TestProject();
      project.setConfigFileConfig({
        imports: false,
      });
      project.addFile('entrypoints/popup.html', `<html></html>`);

      await project.build();

      expect(await project.fileExists('.wxt/types/imports.d.ts')).toBe(false);
    });

    it('should not include imports.d.ts in the type references', async () => {
      const project = new TestProject();
      project.setConfigFileConfig({
        imports: false,
      });
      project.addFile('entrypoints/popup.html', `<html></html>`);

      await project.build();

      expect(
        await project.serializeFile('.wxt/wxt.d.ts'),
      ).toMatchInlineSnapshot(
        `
        ".wxt/wxt.d.ts
        ----------------------------------------
        // Generated by wxt
        /// <reference types="wxt/vite-builder-env" />
        /// <reference types="./types/paths.d.ts" />
        /// <reference types="./types/i18n.d.ts" />
        /// <reference types="./types/globals.d.ts" />
        "
      `,
      );
    });
  });
});
