package org.corpus_tools.annis.gui;

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

import java.util.Arrays;
import org.junit.jupiter.api.Test;

class HelperTest {

  private static final String RAWTEXT_QUERY =
      "(a#tok | a#annis:node_type=\"datasource\") & doc#annis:node_name=/corpus\\x2Fdoc/ & #a @* #doc";
  private static final String WHOLEDOCUMENT_QUERY =
      "n#annis:node_type & doc#annis:node_name=/corpus\\x2Fdoc/ & #n @* #doc";

  @Test
  void testRightToLeft() {
    assertFalse(Helper.containsRTLText("Anise"));
    assertTrue(Helper.containsRTLText("אניס"));
    assertTrue(Helper.containsRTLText("يانسون"));
    assertTrue(Helper.containsRTLText("test cשּ"));
    assertTrue(Helper.containsRTLText("test ﻕ "));
    assertTrue(Helper.containsRTLText("test ﭦ"));
    assertFalse(Helper.containsRTLText(null));
    assertFalse(Helper.containsRTLText(""));
  }

  @Test
  void testBuildAQLDocumentQuery() {
    // Test the full document fallback query
    assertEquals(WHOLEDOCUMENT_QUERY, Helper.buildDocumentQuery("corpus/doc", null, false));
    // Test the raw text query
    assertEquals(RAWTEXT_QUERY, Helper.buildDocumentQuery("corpus/doc", null, true));
    // Test the node annotation filter
    assertEquals(
        "(a#tok | a#pos | a#something | a#annis:node_type=\"datasource\") & doc#annis:node_name=/corpus\\x2Fdoc/ & #a @* #doc",
        Helper.buildDocumentQuery("corpus/doc", Arrays.asList("pos", "something"), true));
    // Check invalid node annotation names lead to falling back to the whole document query
    assertEquals(WHOLEDOCUMENT_QUERY,
        Helper.buildDocumentQuery("corpus/doc", Arrays.asList("pos", "myanno=something"), false));
    // When giving giving both a node annotation filter argument and using raw text, the node filter
    // query should be returned
    assertEquals(
        "(a#tok | a#ns:pos | a#annis:node_type=\"datasource\") & doc#annis:node_name=/corpus\\x2Fdoc/ & #a @* #doc",
        Helper.buildDocumentQuery("corpus/doc", Arrays.asList("ns:pos"), true));


  }

}

@@ -1578,24 +1578,391 @@
 %22), true));%0A
+    // Test the node annotation filter with qualified annotation names%0A    assertEquals(%0A        %22(a#tok %7C a#ns:pos %7C a#something %7C a#default:another %7C a#annis:node_type=%5C%22datasource%5C%22) & doc#annis:node_name=/corpus%5C%5Cx2Fdoc/ & #a @* #doc%22,%0A        Helper.buildDocumentQuery(%22corpus/doc%22,%0A            Arrays.asList(%22ns::pos%22, %22something%22, %22default::another%22), true));%0A
     // Check

Add test for replacing the namespace