package com.xyw.console.config;

import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;

/** File purpose (plain language): verifies local development frontends that use an approved port can call the backend API. */
class WebConfigTest {
  /** Plain purpose: ensure the separate 5175 integration frontend is allowed to send login requests to the local backend. Related files: WebConfig.java, frontend/vite.config.js. Flow: browser origin -> CORS allowlist -> /api/auth/login -> authentication controller. */
  @Test void allowsIntegrationFrontendOnPort5175() {
    InspectableCorsRegistry registry = new InspectableCorsRegistry();
    new WebConfig().addCorsMappings(registry);
    CorsConfiguration configuration = registry.configurations().get("/api/**");

    assertTrue(configuration.getAllowedOrigins().contains("http://127.0.0.1:5175"));
  }

  /** Plain purpose: expose Spring's protected test-only CORS mapping view without changing production configuration. Related files: WebConfig.java. Flow: WebConfig registration -> registry map -> assertion. */
  private static final class InspectableCorsRegistry extends CorsRegistry {
    /** Plain purpose: return the mappings registered by WebConfig for a direct allowlist assertion. Related files: WebConfigTest.java. Flow: addCorsMappings -> configurations -> expected local origin. */
    java.util.Map<String, CorsConfiguration> configurations() { return getCorsConfigurations(); }
  }
}
