package click.kamil.springstatemachineexporter.analysis.service;

import click.kamil.springstatemachineexporter.ast.common.CodebaseContext;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;

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

class VariableTracerBranchFilterTest {

    @Test
    void shouldReturnBranchLocalInitializerWithoutCrossBranchTernary(@TempDir Path tempDir) throws Exception {
        String source = """
            package com.example;
            class StateMachineDispatcher {
                void fireDocument(String eventString) {
                    DocumentEvent event = DocumentEvent.valueOf(eventString.toUpperCase());
                    send(event);
                }
                void send(DocumentEvent event) {}
            }
            enum DocumentEvent { SUBMIT }
            """;
        Files.writeString(tempDir.resolve("App.java"), source);

        CodebaseContext context = new CodebaseContext();
        context.setResolveBindings(true);
        context.scan(tempDir);

        HeuristicCallGraphEngine engine = new HeuristicCallGraphEngine(context);
        String traced = engine.variableTracer.traceLocalVariable(
                "com.example.StateMachineDispatcher.fireDocument",
                "event",
                Map.of("machineType", "DOCUMENT"));

        assertThat(traced).contains("DocumentEvent.valueOf");
    }
}
