import { sentryVitePlugin } from '@sentry/vite-plugin';
import react from '@vitejs/plugin-react';
import path from 'path';
import tsconfigPaths from 'vite-tsconfig-paths';
import { defineConfig } from 'vitest/config';

export default defineConfig({
  plugins: [
    tsconfigPaths(),
    react(),
    sentryVitePlugin({
      org: 'suno-ai',
      project: 'app',
    }),
  ],
  test: {
    /**
     * A note about test environments:
     *
     * Many of the existing `app-ui` tests require a browser-like environment
     * to run properly, especially for stuff that uses React.
     *
     * For this reason, we default to `jsdom` for test files ending with `.jsx`
     *
     * However, it's faster to just use the `node` environment when we can, so
     * that's the default. If that isn't sufficient for whatever reason, the
     * environment can be overridden by adding this at the top of the test file:
     *
     * // @vitest-environment jsdom
     */
    environment: 'node',
    env: {
      NEXT_PUBLIC_API_BASE_ECS: 'http://localhost:8000',
    },
    include: [
      'src/**/*.test.{ts,tsx}',
      'eslint-custom-rules/**/*.test.{ts,mjs}',
    ],
    globals: true, // avoids spurious `@emotion/react` warning
    coverage: {
      provider: 'v8',
      reporter: ['json-summary'],
      include: ['src/**/*.{ts,tsx}'],
      exclude: [
        'src/app/**/{layout,page}.tsx',
        'src/**/*.test.{ts,tsx}',
        'src/**/*.stories.{ts,tsx}',
        'src/lib/gen.ts',
        'src/proto_gen/**',
      ],
      thresholds: {
        lines: 9.36,
        statements: 9.36,
        functions: 21.2,
        branches: 50.65,
      },
      reportOnFailure: true,
    },
    projects: [
      {
        extends: true,
        test: {
          include: ['src/**/*.test.tsx'],
          environment: 'jsdom',
        },
      },
    ],
  },
  resolve: {
    alias: {
      '@': path.resolve(__dirname, './src'),
      '@suno/studiokit': path.resolve(__dirname, '../../studiokit/src'),
    },
  },
  build: {
    sourcemap: true,
  },
});
