package ai import ( "context" "os" "path/filepath" "testing" ) func TestRunConfirmedSkipsUnsafeActions(t *testing.T) { dir := t.TempDir() sugs := []Suggestion{ {RepoPath: dir, Action: "rm_rf", Command: "echo unsafe"}, // not in allowlist {RepoPath: dir, Action: "push", Command: ""}, // no command } confirmed := 0 err := RunConfirmed(context.Background(), sugs, func(s Suggestion) (bool, error) { confirmed++ return true, nil }) if err != nil { t.Fatalf("RunConfirmed: %v", err) } if confirmed != 0 { t.Errorf("confirm called %d times, want 0 (all suggestions skipped)", confirmed) } } func TestRunConfirmedSkipsUnconfirmed(t *testing.T) { dir := t.TempDir() sugs := []Suggestion{ {RepoPath: dir, Action: "pull", Command: "true"}, } err := RunConfirmed(context.Background(), sugs, func(s Suggestion) (bool, error) { return false, nil }) if err != nil { t.Fatalf("RunConfirmed: %v", err) } } func TestRunConfirmedExecutesConfirmed(t *testing.T) { dir := t.TempDir() target := filepath.Join(dir, "out.txt") sugs := []Suggestion{ {RepoPath: dir, Action: "stash", Command: "printf ok > " + filepath.Base(target)}, } err := RunConfirmed(context.Background(), sugs, func(s Suggestion) (bool, error) { return true, nil }) if err != nil { t.Fatalf("RunConfirmed: %v", err) } data, err := os.ReadFile(target) if err != nil { t.Fatalf("command did not run: %v", err) } if string(data) != "ok" { t.Errorf("output = %q, want ok", data) } } func TestRunConfirmedReturnsCommandError(t *testing.T) { sugs := []Suggestion{ {RepoPath: t.TempDir(), Action: "pull", Command: "exit 3"}, } err := RunConfirmed(context.Background(), sugs, func(s Suggestion) (bool, error) { return true, nil }) if err == nil { t.Error("RunConfirmed succeeded for failing command, want error") } }