/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.tinkerpop.tinkeradoc;

import org.asciidoctor.Asciidoctor;
import org.asciidoctor.Attributes;
import org.asciidoctor.Options;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;

/**
 * Tests for the GremlinPostprocessor HTML transforms.
 */
public class GremlinPostprocessorTest {

    @Test
    public void shouldNotModifyConumElements() {
        try (final Asciidoctor asciidoctor = Asciidoctor.Factory.create()) {
            final String input = "= Test\n\n" +
                    "[source,java]\n----\nint x = 1; // <1>\n----\n<1> Sets x\n";
            final Options options = Options.builder()
                    .attributes(Attributes.builder().attribute("tinkerpop-version", "3.7.7").build())
                    .build();
            final String result = asciidoctor.convert(input, options);
            assertThat(result, containsString("conum"));
        }
    }

    @Test
    public void shouldRemoveEmptyCommentSpans() {
        try (final Asciidoctor asciidoctor = Asciidoctor.Factory.create()) {
            // Simulate by testing the postprocessor directly with a mock-like Document
            final GremlinPostprocessor pp = new GremlinPostprocessor();
            // Create a minimal document via Asciidoctor for version resolution
            final Options options = Options.builder()
                    .attributes(Attributes.builder().attribute("tinkerpop-version", "3.7.7").build())
                    .build();
            asciidoctor.load("= T\n\nhi", options);

            // Test the pattern directly
            final String html = "code<span class=\"comment\">/* */</span>more";
            // We can't easily call process() with a Document without SPI,
            // so test the pattern logic
            assertThat(html.replaceAll("<span class=\"comment\">/\\*\\s*\\*/</span>", ""),
                    is("codemore"));
        }
    }

    @Test
    public void shouldReplaceVersionPlaceholder() {
        try (final Asciidoctor asciidoctor = Asciidoctor.Factory.create()) {
            final String input = "= Test\n:tinkerpop-version: 3.7.7\n\nDownload version x.y.z from the repo.";
            final Options options = Options.builder()
                    .attributes(Attributes.builder().attribute("tinkerpop-version", "3.7.7").build())
                    .build();
            final String result = asciidoctor.convert(input, options);
            assertThat(result, containsString("3.7.7"));
            assertThat(result, not(containsString("x.y.z")));
        }
    }

    @Test
    public void shouldNotModifyUnrelatedContent() {
        try (final Asciidoctor asciidoctor = Asciidoctor.Factory.create()) {
            final String input = "= Test\n\nHello world paragraph.";
            final Options options = Options.builder()
                    .attributes(Attributes.builder().attribute("tinkerpop-version", "3.7.7").build())
                    .build();
            final String result = asciidoctor.convert(input, options);
            assertThat(result, containsString("Hello world paragraph"));
        }
    }

    @Test
    public void shouldLeaveVersionWhenNoAttribute() {
        try (final Asciidoctor asciidoctor = Asciidoctor.Factory.create()) {
            final String input = "= Test\n\nversion x.y.z here";
            // No tinkerpop-version or revnumber set
            final String result = asciidoctor.convert(input, Options.builder().build());
            assertThat(result, containsString("x.y.z"));
        }
    }
}
