package com.cloudhandson.vpdbackoffice.service;

import static org.assertj.core.api.Assertions.assertThat;

import com.cloudhandson.vpdbackoffice.domain.protectedobject.ProtectedObject;
import org.junit.jupiter.api.Test;

class OrdsProbeServiceTest {

  @Test
  void buildsVectorEffectiveSqlWithVpdPredicateAndDistancePlaceholder() {
    ProtectedObject object = new ProtectedObject(
        5L, "ADMIN", "CB_VECTOR_SEARCH_DOCUMENTS", "cb-agent-vector/search", "Y");

    String sql = OrdsProbeService.vectorEffectiveSql(
        object, "INSTR(',' || UPPER(d.TECH_TAG) || ',', ',ORACLE_VPD,') > 0");

    assertThat(sql)
        .contains("FROM ADMIN.CB_VECTOR_SEARCH_DOCUMENTS d")
        .contains("VECTOR_DISTANCE(d.embedding, TO_VECTOR(:embedding), COSINE)")
        .contains("WHERE d.embedding IS NOT NULL AND (INSTR")
        .contains("ORDER BY score")
        .contains("ROWNUM <= LEAST(GREATEST(NVL(:row_limit, 10), 1), 100)")
        .doesNotContain("Bearer", "0.10", "0.20");
  }

  @Test
  void omitsNoopAllowAllPredicateFromVectorEffectiveSql() {
    ProtectedObject object = new ProtectedObject(
        5L, "ADMIN", "CB_VECTOR_SEARCH_DOCUMENTS", "cb-agent-vector/search", "Y");

    String sql = OrdsProbeService.vectorEffectiveSql(object, "  1 = 1  ");

    assertThat(sql)
        .contains("WHERE d.embedding IS NOT NULL ORDER BY score")
        .doesNotContain("AND (1 = 1)", "REGEXP_LIKE");
  }

  @Test
  void filtersRecentCursorCandidatesInJavaByTheProtectedObjectName() {
    ProtectedObject object = new ProtectedObject(
        1L, "ADMIN", "CB_V_SEARCH_DOCUMENTS", "cb-ords/cb-object-query/admin/documents", "Y");

    assertThat(OrdsProbeService.referencesProtectedObject(
        "SELECT *\nFROM   ADMIN.CB_V_SEARCH_DOCUMENTS o WHERE ROWNUM <= :1", object)).isTrue();
    assertThat(OrdsProbeService.referencesProtectedObject(
        "SELECT * FROM ADMIN.CB_VECTOR_SEARCH_DOCUMENTS", object)).isFalse();
  }
}
