package review import ( "testing" "gitea-codex-bot/internal/domain" ) func TestParseConfigAndResolveMode(t *testing.T) { cfg, err := ParseRepoConfig("enabled: true\nreview:\n default_mode: security\n include_tests: false\n focus: [correctness, security]\nignore: [generated/]\n") if err != nil { t.Fatal(err) } if !cfg.Configured || cfg.DefaultMode != "security" || len(cfg.Focus) != 2 { t.Fatalf("unexpected config: %#v", cfg) } cmd := domain.ParsedCommand{Name: "review", Raw: "@codex review", Mode: "summary"} ResolveMode(&cmd, cfg) if cmd.Mode != "security" { t.Fatalf("mode was not resolved: %q", cmd.Mode) } } func TestResultValidationAndFormatting(t *testing.T) { suggestion := "Use a checked conversion." result := domain.ReviewResult{Verdict: "has_issues", Confidence: .9, Summary: "Found one issue", Findings: []domain.Finding{{Severity: "high", File: "internal/x.go", LineStart: 4, LineEnd: 5, Title: "Unsafe conversion", Body: "The conversion can overflow.", Suggestion: &suggestion}}} if err := ValidateResult(result); err != nil { t.Fatal(err) } body := FormatResultComment("abcdef123", result, false) if len(body) == 0 || body[:len("")] != "" { t.Fatalf("missing SHA marker: %s", body) } if !contains(body, "not configured") || !contains(body, "Unsafe conversion") { t.Fatalf("missing formatted details: %s", body) } } func TestBuildPromptUsesGenericIntentForBareReview(t *testing.T) { prompt := BuildPrompt(domain.ParsedCommand{Name: "review", Raw: "@codex review", Mode: "summary"}, domain.DefaultRepoReviewConfig(), domain.PullRequestContext{BaseSHA: "base", HeadSHA: "head"}) if contains(prompt, "review: review\n") || !contains(prompt, "review: review this pull request and report introduced issues.") { t.Fatalf("unexpected bare-review prompt: %s", prompt) } } func TestMarkdownCommentIncludesStructuredSummaryAndSuggestion(t *testing.T) { suggestion := "Use a checked conversion." result := domain.ReviewResult{Verdict: "has_issues", Confidence: .8, Summary: "Summary", MarkdownComment: "Primary markdown", Findings: []domain.Finding{{Severity: "medium", File: "x.go", LineStart: 2, LineEnd: 2, Title: "Issue", Body: "Details", Suggestion: &suggestion}}} body := FormatResultComment("head", result, true) for _, part := range []string{"Structured Findings", "Verdict: `has_issues`", "Summary", "Use a checked conversion."} { if !contains(body, part) { t.Fatalf("formatted comment missing %q: %s", part, body) } } } func contains(s, needle string) bool { for i := 0; i+len(needle) <= len(s); i++ { if s[i:i+len(needle)] == needle { return true } } return false }