import { resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { mergeConfigs, vscodeConfig } from "@vreko/vitest-config";
import { defineProject } from "vitest/config";

const __dirname = resolve(fileURLToPath(import.meta.url), "..");

/**
 * Vitest configuration for apps/vscode
 *
 * Uses the vscodeConfig preset from @vreko/vitest-config which:
 * - Sets environment to 'node'
 * - Externalizes 'vscode' module (mocked in setup)
 * - Inlines @sentry/* modules for ESM compatibility
 * - Includes standard test patterns and mock handling
 *
 * Test files live in test/ but import from src/
 *
 * NOTE: VS Code extension tests include many TDD tests and tests that require
 * complex VS Code API mocking. Many tests are "red phase" TDD tests.
 *
 * To run VS Code tests locally:
 *   VSCODE_TESTS_ENABLED=true pnpm --filter vreko-vscode test
 *
 * Tests are skipped by default in CI for stability.
 */

// Check if VS Code tests should run
const _vscodeTestsEnabled = process.env.VSCODE_TESTS_ENABLED === "true";

export default defineProject(
	mergeConfigs(vscodeConfig, {
		resolve: {
			alias: {
				// Mock the vscode module for testing
				vscode: resolve(__dirname, "./__mocks__/vscode.mjs"),
				// Mock the local-service-client for testing
				"@vreko/local-service-client": resolve(__dirname, "./__mocks__/@vreko/local-service-client.mjs"),
				// Mock node modules for testing
				"node:fs": resolve(__dirname, "./__mocks__/node:fs.mjs"),
				"node:child_process": resolve(__dirname, "./__mocks__/node:child_process.mjs"),
				// Map @vscode/ to src/ for cleaner test imports
				"@vscode": resolve(__dirname, "./src"),
				// Common submodule aliases
				"@vscode/config": resolve(__dirname, "./src/config"),
				"@vscode/services": resolve(__dirname, "./src/services"),
				"@vscode/storage": resolve(__dirname, "./src/storage"),
				"@vscode/types": resolve(__dirname, "./src/types"),
				"@vscode/utils": resolve(__dirname, "./src/utils"),
				"@vscode/handlers": resolve(__dirname, "./src/handlers"),
				"@vscode/ui": resolve(__dirname, "./src/ui"),
				"@vscode/snapshot": resolve(__dirname, "./src/snapshot"),
				"@vscode/views": resolve(__dirname, "./src/views"),
				"@vscode/providers": resolve(__dirname, "./src/providers"),
				"@vscode/domain": resolve(__dirname, "./src/domain"),
				"@vscode/commands": resolve(__dirname, "./src/commands"),
				"@vscode/decorations": resolve(__dirname, "./src/decorations"),
				"@vscode/network": resolve(__dirname, "./src/network"),
				"@vscode/security": resolve(__dirname, "./src/security"),
				"@vscode/performance": resolve(__dirname, "./src/performance"),
				"@vscode/checkpoint": resolve(__dirname, "./src/checkpoint"),
				"@vscode/rules": resolve(__dirname, "./src/rules"),
				"@vscode/policy": resolve(__dirname, "./src/policy"),
				"@vscode/integration": resolve(__dirname, "./src/integration"),
				"@vscode/notifications": resolve(__dirname, "./src/notifications"),
				"@vscode/signage": resolve(__dirname, "./src/signage"),
				"@vscode/suppressions": resolve(__dirname, "./src/suppressions"),
				"@vscode/auth": resolve(__dirname, "./src/auth"),
				"@vscode/protection": resolve(__dirname, "./src/protection"),
				"@vscode/telemetry": resolve(__dirname, "./src/telemetry"),
				"@vscode/pioneer": resolve(__dirname, "./src/pioneer"),
			},
		},
		test: {
			name: "@vreko/vscode",
			include: ["test/**/*.test.ts"],
			// @integration: require live daemon or specific platform features
			exclude: [
				// @integration: requires live daemon bridge with vitals connection (SB-801)
				// EXPIRES: 2026-09-30
				"test/integration/vitalsDashboardPanel-daemonBridge.integration.test.ts",
				// @perf: requires consistent hardware for performance baseline (SB-802)
				// EXPIRES: 2026-09-30
				"test/welcome/welcome-panel.perf.test.ts",
			],
			// Global setup file for all tests
			setupFiles: ["./test/unit/setup.ts"],
			server: {
				deps: {
					// Don't externalize vscode - we're aliasing to mock
					external: [],
					inline: [/@sentry/],
				},
			},
		},
	}),
);
