package api

import (
	"sort"
	"testing"

	"github.com/alexei/tinyforge/internal/gitops"
)

func TestValidGitOpsPath(t *testing.T) {
	cases := []struct {
		path string
		ok   bool
	}{
		{".tinyforge.yml", true},
		{"deploy/.tinyforge.yml", true},
		{"config/app.yaml", true},
		{"/etc/passwd", false},      // absolute
		{"\\windows\\path", false},  // absolute (backslash)
		{"../../etc/passwd", false}, // traversal
		{"deploy/../../x", false},   // traversal mid-path
		{"foo?ref=evil", false},     // query-param injection (LOW-1)
		{"foo#frag", false},         // fragment injection
		{"with space.yml", false},   // whitespace
		{"", false},                 // empty
	}
	for _, c := range cases {
		if got := validGitOpsPath(c.path); got != c.ok {
			t.Errorf("validGitOpsPath(%q) = %v, want %v", c.path, got, c.ok)
		}
	}
}

func TestPlanFields(t *testing.T) {
	spec := gitops.Spec{Version: 1, Deploy: gitops.DeploySpec{
		Port:           ptrInt(8080),
		DeployStrategy: ptrStr("blue-green"),
	}}
	got := planFields(gitops.BuildPlan(spec, gitops.SourceDockerfile))
	sort.Strings(got)
	want := []string{"deploy_strategy", "port"}
	if len(got) != len(want) || got[0] != want[0] || got[1] != want[1] {
		t.Fatalf("planFields = %v, want %v", got, want)
	}
}

func ptrInt(i int) *int       { return &i }
func ptrStr(s string) *string { return &s }
