package exploit

import (
	"context"
	"strings"
	"testing"

	"github.com/Armur-Ai/Pentest-Swarm-AI/internal/pipeline"
	"github.com/Armur-Ai/Pentest-Swarm-AI/internal/scope"
	"github.com/google/uuid"
)

func TestAssist_SkipReturnsBenignSkippedResult(t *testing.T) {
	called := 0
	confirm := func(step pipeline.AttackStep) (bool, error) {
		called++
		return false, nil
	}
	e := NewExecutor(
		&scope.ScopeDefinition{AllowedDomains: []string{"example.com"}},
		nil, false,
	).WithConfirm(confirm)

	step := pipeline.AttackStep{ID: uuid.New(), Name: "noop", Command: "echo hi"}
	res, err := e.Execute(context.Background(), step, uuid.New())
	if err != nil {
		t.Fatalf("skip should not be an error: %v", err)
	}
	if called != 1 {
		t.Errorf("confirm should be called exactly once, got %d", called)
	}
	if res == nil || res.Success {
		t.Errorf("skipped step must not be Success; got %+v", res)
	}
	if !strings.Contains(res.Output, "SKIPPED") {
		t.Errorf("output should mention SKIPPED, got %q", res.Output)
	}
}

func TestAssist_ErrorAborts(t *testing.T) {
	confirm := func(step pipeline.AttackStep) (bool, error) {
		return false, &assistTestErr{}
	}
	e := NewExecutor(
		&scope.ScopeDefinition{AllowedDomains: []string{"example.com"}},
		nil, false,
	).WithConfirm(confirm)

	step := pipeline.AttackStep{ID: uuid.New(), Name: "n", Command: "echo hi"}
	_, err := e.Execute(context.Background(), step, uuid.New())
	if err == nil {
		t.Fatal("confirm error must abort the step (returned nil error)")
	}
}

type assistTestErr struct{}

func (assistTestErr) Error() string { return "stdin closed" }
