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 browser: typeof import('wxt/browser')['browser']
            const builtinDrivers: typeof import('wxt/storage')['builtinDrivers']
            const createContentScriptIframe: typeof import('wxt/client')['createContentScriptIframe']
            const createContentScriptUi: typeof import('wxt/client')['createContentScriptUi']
            const createStorage: typeof import('wxt/storage')['createStorage']
            const defineBackground: typeof import('wxt/client')['defineBackground']
            const defineConfig: typeof import('wxt')['defineConfig']
            const defineContentScript: typeof import('wxt/client')['defineContentScript']
            const defineDriver: typeof import('wxt/storage')['defineDriver']
            const defineUnlistedScript: typeof import('wxt/sandbox')['defineUnlistedScript']
            const fakeBrowser: typeof import('wxt/testing')['fakeBrowser']
            const joinKeys: typeof import('wxt/storage')['joinKeys']
            const normalizeBaseKey: typeof import('wxt/storage')['normalizeBaseKey']
            const normalizeKey: typeof import('wxt/storage')['normalizeKey']
            const prefixStorage: typeof import('wxt/storage')['prefixStorage']
            const restoreSnapshot: typeof import('wxt/storage')['restoreSnapshot']
            const snapshot: typeof import('wxt/storage')['snapshot']
            const storage: typeof import('wxt/storage')['storage']
            const webExtensionDriver: typeof import('wxt/storage')['webExtensionDriver']
          }
          "
        `);
    });

    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=\\"vite/client\\" />
        /// <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=\\"vite/client\\" />
        /// <reference types=\\"./types/paths.d.ts\\" />
        /// <reference types=\\"./types/i18n.d.ts\\" />
        /// <reference types=\\"./types/globals.d.ts\\" />
        "
      `,
      );
    });
  });
});
