open-code-review/internal/telemetry/traceid_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

33 lines
999 B
Go

// SPDX-License-Identifier: Apache-2.0
// Copyright 2026 alibaba/open-code-review Contributors
package telemetry
import (
"context"
"testing"
)
// TestTraceIDFromContext_Empty covers the invalid-span branch: a bare context
// carries no span, so an empty string is returned.
func TestTraceIDFromContext_Empty(t *testing.T) {
if got := TraceIDFromContext(context.Background()); got != "" {
t.Errorf("TraceIDFromContext(bare ctx) = %q, want empty", got)
}
}
// TestTraceIDFromContext_Valid covers the valid-span branch: a context carrying
// an active span reports its hex-encoded trace ID.
func TestTraceIDFromContext_Valid(t *testing.T) {
setupEnabledTelemetry(t)
ctx, span := StartSpan(context.Background(), "test.traceid")
defer span.End()
got := TraceIDFromContext(ctx)
if got == "" {
t.Fatal("TraceIDFromContext with active span returned empty")
}
if want := span.SpanContext().TraceID().String(); got != want {
t.Errorf("TraceIDFromContext = %q, want %q", got, want)
}
}