open-code-review/cmd/opencodereview/session_display_more_test.go
kite 840f85f9bc
test: raise statement coverage to 90% and enforce it in CI (#747)
* test: raise statement coverage to 90% and enforce it in CI

Add unit tests across the cmd and internal packages to bring total
statement coverage above 90%, and gate future regressions.

- Cover CLI helpers, provider TUI handlers, resume/manifest paths, and
  error branches in config, llm, llmloop, scan, session, agent, viewer,
  mcp, pathutil, and telemetry.
- Raise the coverage threshold from 80% to 90% in the Makefile
  (COVERAGE_THRESHOLD) and in the CI "Check coverage threshold" step.
- Ignore generated coverage.out and coverage.html artifacts.

Total statement coverage is now 90.5%, measured consistently by both
`make coverage` and the CI `go test ./...` scope.

* test: widen statement coverage margin with environment-independent unit tests

Add table-driven unit tests for pure, environment-independent functions to
raise the statement-coverage safety margin above the 90% threshold:

- session.ResumeState.ValidateScanOptions (70% -> 100%)
- rules.SystemRule.UnmarshalJSON error branches (71% -> 82%)
- llmloop.stripMarkdownFences no-newline branch (82% -> 100%)
- diff.firstLine empty/blank-input branch
- diff.extractCodeBlock missing-newline and no-closing-fence branches
- main.truncate n<=1 and normalization branches
- agent.Agent nil-receiver accessor guards
2026-08-06 12:55:44 +08:00

99 lines
2.7 KiB
Go

// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 alibaba/open-code-review Contributors
package main
import (
"testing"
"github.com/alibaba/open-code-review/internal/session"
)
// TestDisplayMode covers the empty and non-empty branches.
func TestDisplayMode(t *testing.T) {
if got := displayMode(""); got != "-" {
t.Errorf("displayMode(\"\") = %q, want -", got)
}
if got := displayMode("range"); got != "range" {
t.Errorf("displayMode(range) = %q, want range", got)
}
}
// TestDescribeRange covers each review-mode branch plus the fallthrough.
func TestDescribeRange(t *testing.T) {
tests := []struct {
name string
summary session.Summary
want string
}{
{
name: "range with endpoints",
summary: session.Summary{ReviewMode: session.ReviewModeRange, DiffFrom: "a", DiffTo: "b"},
want: "a..b",
},
{
name: "range without endpoints",
summary: session.Summary{ReviewMode: session.ReviewModeRange},
want: "-",
},
{
name: "commit",
summary: session.Summary{ReviewMode: session.ReviewModeCommit, DiffCommit: "abc123"},
want: "abc123",
},
{
name: "commit without value",
summary: session.Summary{ReviewMode: session.ReviewModeCommit},
want: "-",
},
{
name: "other mode",
summary: session.Summary{},
want: "-",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := describeRange(tt.summary); got != tt.want {
t.Errorf("describeRange() = %q, want %q", got, tt.want)
}
})
}
}
// TestDescribeStart covers the zero-time and formatted branches.
func TestDescribeStart(t *testing.T) {
if got := describeStart(session.Summary{}); got != "-" {
t.Errorf("describeStart(zero) = %q, want -", got)
}
s := session.Summary{}
s.StartTime = s.StartTime.AddDate(2024, 0, 0) // any non-zero time
if got := describeStart(s); got == "-" {
t.Error("describeStart(non-zero) should not be -")
}
}
// TestDescribeFilesNoManifest covers the branch where RunManifest is nil.
func TestDescribeFilesNoManifest(t *testing.T) {
s := session.Summary{CompletedFiles: 3}
if got := describeFiles(s); got != "3" {
t.Errorf("describeFiles(no manifest) = %q, want 3", got)
}
s.ReusedFiles = 2
if got := describeFiles(s); got != "5 (reused 2)" {
t.Errorf("describeFiles(reused) = %q, want 5 (reused 2)", got)
}
}
// TestCompleteEnum verifies the closure returns the provided values with the
// no-file-completion directive.
func TestCompleteEnum(t *testing.T) {
fn := completeEnum("a", "b", "c")
values, directive := fn(nil, nil, "")
if len(values) != 3 || values[0] != "a" {
t.Errorf("completeEnum values = %v, want [a b c]", values)
}
if directive == 0 {
t.Error("expected a non-zero shell completion directive")
}
}