open-code-review/internal/session/list_error_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

56 lines
1.6 KiB
Go

// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 alibaba/open-code-review Contributors
package session
import (
"os"
"path/filepath"
"testing"
)
// TestListSessions_DirIsFile covers the ReadDir error branch (a non-NotExist
// error): when the computed sessions dir path is occupied by a regular file,
// os.ReadDir fails with ENOTDIR and ListSessions must surface it.
func TestListSessions_DirIsFile(t *testing.T) {
t.Setenv("HOME", t.TempDir())
repoDir := t.TempDir()
dir, err := SessionsDir(repoDir)
if err != nil {
t.Fatalf("SessionsDir: %v", err)
}
if err := os.MkdirAll(filepath.Dir(dir), 0o755); err != nil {
t.Fatalf("mkdir parent: %v", err)
}
// Occupy the sessions-dir path with a file so ReadDir cannot treat it as a dir.
if err := os.WriteFile(dir, []byte("x"), 0o644); err != nil {
t.Fatalf("write file at dir path: %v", err)
}
if _, err := ListSessions(repoDir); err == nil {
t.Fatal("ListSessions should error when the sessions path is a file")
}
}
// TestRecordToItem covers the non-item type (returns false) and the
// empty-FilePath fallback to NewPath for a recognized item record.
func TestRecordToItem(t *testing.T) {
if _, ok := recordToItem(summaryRecord{Type: "session_start"}); ok {
t.Error("session_start should not convert to an item")
}
item, ok := recordToItem(summaryRecord{
Type: "review_item_done",
NewPath: "renamed.go",
})
if !ok {
t.Fatal("review_item_done should convert to an item")
}
if item.FilePath != "renamed.go" {
t.Errorf("FilePath = %q, want NewPath fallback %q", item.FilePath, "renamed.go")
}
if item.Type != "done" {
t.Errorf("Type = %q, want %q", item.Type, "done")
}
}