* feat(workflow): post line-anchored review comments with suggested fixes Bead archie-core-q9au (docs/prds/inline-review.md). A review's findings reached the operator only as a flat list in the PR body, so the author had to map every finding back to its line by hand and apply mechanical fixes by hand. Now the line-anchored findings are also posted as inline review comments, carrying a one-click suggestion when the reviewer could state the fix. Domain (internal/domain/workflow): - ReviewFinding gains Suggestion; Validate rejects one on anything but a confirmed finding, so the agent's record_finding call is refused with something it can act on. Validate splits into validateAnchor / validateClassification / validateDisposition, which also brings it back under the cyclop limit the gate enforces. - StagePostReviewComments runs after StageOpenPR in the implement workflow. It posts only line-anchored findings (Line > 0); a whole-file finding has no line to attach to and stays in the body list, which remains the complete record. It is best-effort: the PR is open and the body already lists every finding, so a failed comment is logged, never a park -- the same reasoning as OpenPR's existing best-effort LinkBranch call. - The suggestion fence is gated on Verdict == confirmed where the fence is written, because it is a one-click apply: a wrong suggestion breaks the code and the author has to notice. Forge (internal/forge): ReviewCommentWriter + GitHub/Gitea implementations, type-asserted like PullRequestReader so a forge that cannot is skipped rather than faked (NoopForge is pinned as not satisfying it). GitHub loops one CreateComment per comment and joins the failures, so one refused anchor cannot drop the rest; Gitea carries them on a single COMMENT-state review. Two deliberate departures from the PRD's sketch, both recorded in it: - The interface is batch (CreateReviewComments), not one call per comment: Gitea's only inline shape is one submitted review, and the set then crosses NATS once instead of once per finding. - The head SHA is resolved inside each forge implementation rather than by the caller. The stage runs in archie-agent, which holds no forge credentials and has no PullRequestReader; the daemon-side forgerpc handler is the only place that does. Transport (internal/forgerpc): workflow.Forger gains the method, so the agent reaches the daemon over the new create_review_comments subject; the server type-asserts the writer and answers an incapable forge with an error the stage logs. This is the one proxied method whose absence on an image skew does not park a task. The reviewer prompt and record_finding schema ask for a suggestion on a confirmed, mechanically fixable defect only. Regenerating the wfextract symbol table -- the documented way a new exported workflow symbol reaches interpreted stage plugins -- also picked up symbols that were missing from the committed table (the Status* and Source* constants, ReviewCheck, Store, Task, and the _Store wrapper). Nil-guards were re-applied to every wrapper afterwards, as that file's header requires. Evidence: task check exit 0 (incl. 229 UI tests). Three mutations of the new stage logic each fail a test that names the behaviour -- dropping the confirmed-only gate, dropping the Line > 0 filter, and letting a posting failure escape -- so the tests discriminate rather than passing vacuously. * fix(build): drop the Dockerfile pin assertion The guard fetched golangci-lint's go.mod at image build time to fail the build when GOLANGCI_LINT_VERSION and GOFUMPT_VERSION disagreed. It only ever tripped on self-inflicted pin drift, but it added a network fetch and a new failure mode to the release build, which is not where that belongs. The pins stay correct (golangci-lint 2.13.2, whose vendored gofumpt v0.11.0 equals GOFUMPT_VERSION) and the comment above them still states the invariant, so the reasoning survives without the build-time machinery. The pin change itself reaches the agent image through the normal release path: CI rebuilds and pushes archie-agent on an archie/vX.Y.Z release, and a gateway-only release deliberately does not. * feat(workflow): add the remediate workflow for PR review remediation Implements step 5 of docs/prds/pr-review-remediation.md: a remediate workflow that resumes the task's existing worktree onto its PR branch (worktree.Manager.Resume, not Prepare's refresh), runs a builder agent against the review's actionable comments, commits and pushes to the same branch, and replies via the forge's ReplyToReview. Task.RetryCount doubles as the remediation round counter, capped by the repo's max_retries; the round-cap stage parks the task and posts one comment when it's hit. Task gains a ReviewPayload field (JSON-encoded review unit) with full store/proto/staterpc plumbing. workflow.Forger gains Comment and ReplyToReview, proxied to archie-agent via a new forgerpc RPC. Resume and its Dir lookup are added as an unexported optional Trees capability rather than widening the exported interface, which is already at golangci-lint's interfacebloat cap and is projected into wfextract's Yaegi symbol table for repository-authored stage plugins. The daemon-side reaction consumer that dispatches into this workflow (subscribing ARCHIE_REACTIONS, grouping comments by review, enforcing per-task serialization) is tracked separately in archie-core-8li9.4. * feat(messaging): extract standalone messaging service binary and process composition * feat(messaging): adapt channel frontends to consume Gateway ChatContract * feat(messaging): remove in-process channels and Router from daemon bootstrap Telegram, email and webhook now run only in the Messaging Service, which reaches this process through the Gateway's ChatContract. The daemon builds no channel and no gateway.Router, so the wiring that existed only to serve them goes with it: setupGateways, setupEmailGateway, setupWebhookGateway, setupTelegramGateway, buildTelegramRouter, makeTelegramReload and the release-announcement path helper. The seams those paths owned move to where they can still be honoured: - Channel ValidateConfig runs in archiemessaging.compose, which now returns an error, so a configured-but-invalid front-end fails the service rather than being dropped silently. - The inbound rate limiter moves to the Gateway process, which owns the only Router and therefore the only place a per-sender budget applies to every channel's turns. - The daemon's gateway readiness probe becomes a ContractProbe against the Gateway. It cannot observe a channel lifecycle it no longer runs, and a channel-count probe would have reported every daemon degraded. - telegramSetup is now chatSetup (in chat_setup.go), trimmed to the fields a chat turn runner actually reads. Telegram operator features that were wired only in the daemon (/update, release announcements, version reporting, SIGHUP reload) are not yet wired in the Messaging Service: archie-core-8cda.6.6. Channel lifecycle reporting: archie-core-8cda.6.8. The Gateway process builds no worktree manager, so review_pr is unadvertised on every channel: archie-core-8cda.6.7. * feat(messaging): re-wire the Telegram operator surface in the Messaging Service setupTelegramGateway satisfied eight telegram.Gateway seams from the daemon's own process. Each is resolved by where the fact it needs is owned, not by where it used to be built; the decision table is in docs/architecture/migration-decisions.md. Wired here: /update (built from this service's own [chat.telegram] check and install commands), the update-report relay (same sha256(bot_user)[:8] path the daemon wrote, so a pending report is found rather than written afresh), show_tool_calls, and /restart's config reload, which re-resolves the token and allowlist from this service's own file and refuses a reload that resolves to an empty token. /version is sourced from the Gateway: ChatSnapshot gains a Version field carrying the Gateway's rendered build block, read per call and bounded by the dependency timeout. cmd/archie-messaging deliberately takes no version ldflags, so it cannot report a partial upgrade as a matched one. RunningVersions and ReleaseAnnouncements stay nil. Both need a component's real running version, and the only versions this process can obtain are a different binary's stamps -- reporting them would manufacture exactly the false success the update check exists to catch, and the announcer without parseable versions is a no-op that reads as configured. archie-core-8cda.6.9 carries the contract that unblocks them. Dangerous stays nil: no composition has ever supplied a command authority. Enrich is dropped from the update service: componentInstallTypeEnricher read [nats], which this service must not decode, so the check command's own reported install type stands. * fix(workflow): guard inline review comments against head drift Two findings from the review of q9au. A comment's line numbers describe exactly one revision of the branch, but the forge resolved the pull request's head at posting time -- after the review had already read the worktree. A push in that window (a collaborator, a retry) meant the findings were anchored to whatever now occupied those lines, mislabelling instead of failing. The revision the review measured now travels with the comment set: OpenPR records it from the same worktree read it already performs (TaskContext.ReviewedHeadSHA), StagePostReviewComments threads it through Forger to forgerpc, and the forge refuses the whole set once the head it reads has moved off it -- which the best-effort stage logs, the PR body remaining the record. An unmeasured revision posts unverified rather than dropping every finding, so a worktree that cannot report one degrades instead of regressing. Suggestion was documented single-line only, because GitHub applies a suggestion fence up to its first line and a multi-line replacement would silently corrupt the file. Only the verdict was validated, so a multi-line suggestion reached the renderer and posted as that truncated fence. Validate now rejects an embedded newline, which makes record_finding refuse it with feedback the model can act on instead of the reviewer discovering the corruption later.
970 lines
27 KiB
Go
970 lines
27 KiB
Go
package workflow
|
|
|
|
import (
|
|
"encoding/json"
|
|
"reflect"
|
|
"testing"
|
|
)
|
|
|
|
func TestReviewFindingBlockingMatrix(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
verdict ReviewVerdict
|
|
level ReviewLevel
|
|
wantBlocking bool
|
|
}{
|
|
{
|
|
name: "confirmed error blocks",
|
|
verdict: ReviewVerdictConfirmed,
|
|
level: ReviewLevelError,
|
|
wantBlocking: true,
|
|
},
|
|
{
|
|
name: "confirmed warn does not block",
|
|
verdict: ReviewVerdictConfirmed,
|
|
level: ReviewLevelWarn,
|
|
wantBlocking: false,
|
|
},
|
|
{
|
|
name: "confirmed empty level does not block",
|
|
verdict: ReviewVerdictConfirmed,
|
|
level: "",
|
|
wantBlocking: false,
|
|
},
|
|
{
|
|
name: "confirmed unknown level does not block",
|
|
verdict: ReviewVerdictConfirmed,
|
|
level: "critical",
|
|
wantBlocking: false,
|
|
},
|
|
{
|
|
name: "plausible error MUST NOT block",
|
|
verdict: ReviewVerdictPlausible,
|
|
level: ReviewLevelError,
|
|
wantBlocking: false,
|
|
},
|
|
{
|
|
name: "plausible warn does not block",
|
|
verdict: ReviewVerdictPlausible,
|
|
level: ReviewLevelWarn,
|
|
wantBlocking: false,
|
|
},
|
|
{
|
|
name: "plausible empty level does not block",
|
|
verdict: ReviewVerdictPlausible,
|
|
level: "",
|
|
wantBlocking: false,
|
|
},
|
|
{
|
|
name: "empty verdict error does not block",
|
|
verdict: "",
|
|
level: ReviewLevelError,
|
|
wantBlocking: false,
|
|
},
|
|
{
|
|
name: "empty verdict warn does not block",
|
|
verdict: "",
|
|
level: ReviewLevelWarn,
|
|
wantBlocking: false,
|
|
},
|
|
{
|
|
name: "empty verdict and level does not block",
|
|
verdict: "",
|
|
level: "",
|
|
wantBlocking: false,
|
|
},
|
|
{
|
|
name: "unknown verdict with error does not block",
|
|
verdict: "likely",
|
|
level: ReviewLevelError,
|
|
wantBlocking: false,
|
|
},
|
|
{
|
|
name: "uppercase confirmed error does not block (case sensitive)",
|
|
verdict: "CONFIRMED",
|
|
level: "ERROR",
|
|
wantBlocking: false,
|
|
},
|
|
{
|
|
name: "title case confirmed error does not block (case sensitive)",
|
|
verdict: "Confirmed",
|
|
level: "Error",
|
|
wantBlocking: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
f := ReviewFinding{
|
|
File: "internal/domain/workflow/review_findings.go",
|
|
Line: 42,
|
|
Defect: "sample defect statement",
|
|
FailureScenario: "sample failure scenario",
|
|
Verdict: tt.verdict,
|
|
Level: tt.level,
|
|
Category: ReviewCategoryNilRisk,
|
|
}
|
|
|
|
if got := f.Blocking(); got != tt.wantBlocking {
|
|
t.Errorf("ReviewFinding.Blocking() = %v, want %v (verdict=%q, level=%q)",
|
|
got, tt.wantBlocking, tt.verdict, tt.level)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestReviewBlockingSlice(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
findings []ReviewFinding
|
|
wantBlocking bool
|
|
}{
|
|
{
|
|
name: "nil slice returns false",
|
|
findings: nil,
|
|
wantBlocking: false,
|
|
},
|
|
{
|
|
name: "empty slice returns false",
|
|
findings: []ReviewFinding{},
|
|
wantBlocking: false,
|
|
},
|
|
{
|
|
name: "single confirmed error returns true",
|
|
findings: []ReviewFinding{
|
|
{
|
|
File: "main.go",
|
|
Line: 10,
|
|
Defect: "nil pointer dereference",
|
|
FailureScenario: "running with nil config crashes",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: ReviewLevelError,
|
|
Category: ReviewCategoryNilRisk,
|
|
},
|
|
},
|
|
wantBlocking: true,
|
|
},
|
|
{
|
|
name: "warn-only confirmed findings do not block",
|
|
findings: []ReviewFinding{
|
|
{
|
|
File: "main.go",
|
|
Line: 10,
|
|
Defect: "unused helper function",
|
|
FailureScenario: "function is never called",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: ReviewLevelWarn,
|
|
Category: ReviewCategoryDeadCode,
|
|
},
|
|
{
|
|
File: "util.go",
|
|
Line: 20,
|
|
Defect: "magic number in loop",
|
|
FailureScenario: "hardcoded 100 instead of constant",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: ReviewLevelWarn,
|
|
Category: ReviewCategoryHardcodedValue,
|
|
},
|
|
},
|
|
wantBlocking: false,
|
|
},
|
|
{
|
|
name: "plausible error findings do not block",
|
|
findings: []ReviewFinding{
|
|
{
|
|
File: "server.go",
|
|
Line: 50,
|
|
Defect: "potential race condition",
|
|
FailureScenario: "concurrent writes might race if mutex is missing",
|
|
Verdict: ReviewVerdictPlausible,
|
|
Level: ReviewLevelError,
|
|
Category: ReviewCategoryRace,
|
|
},
|
|
},
|
|
wantBlocking: false,
|
|
},
|
|
{
|
|
name: "mixed findings with one confirmed error returns true",
|
|
findings: []ReviewFinding{
|
|
{
|
|
File: "doc.go",
|
|
Line: 1,
|
|
Defect: "typo in comment",
|
|
FailureScenario: "spelling mistake in doc comment",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: ReviewLevelWarn,
|
|
Category: ReviewCategoryOther,
|
|
},
|
|
{
|
|
File: "handler.go",
|
|
Line: 45,
|
|
Defect: "unchecked error return from Write",
|
|
FailureScenario: "client disconnect causes unhandled error",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: ReviewLevelError,
|
|
Category: ReviewCategoryUncheckedError,
|
|
},
|
|
{
|
|
File: "worker.go",
|
|
Line: 100,
|
|
Defect: "unbounded goroutine launch",
|
|
FailureScenario: "burst of 10k items exhausts memory",
|
|
Verdict: ReviewVerdictPlausible,
|
|
Level: ReviewLevelError,
|
|
Category: ReviewCategoryGoroutineLeak,
|
|
},
|
|
},
|
|
wantBlocking: true,
|
|
},
|
|
{
|
|
name: "confirmed error as first element returns true",
|
|
findings: []ReviewFinding{
|
|
{
|
|
File: "first.go",
|
|
Line: 1,
|
|
Defect: "blocking issue",
|
|
FailureScenario: "crashes immediately",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: ReviewLevelError,
|
|
Category: ReviewCategoryNilRisk,
|
|
},
|
|
{
|
|
File: "second.go",
|
|
Line: 2,
|
|
Defect: "advisory issue",
|
|
FailureScenario: "minor warning",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: ReviewLevelWarn,
|
|
Category: ReviewCategoryOther,
|
|
},
|
|
},
|
|
wantBlocking: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := ReviewBlocking(tt.findings); got != tt.wantBlocking {
|
|
t.Errorf("ReviewBlocking() = %v, want %v", got, tt.wantBlocking)
|
|
}
|
|
})
|
|
}
|
|
|
|
t.Run("large slice detects confirmed error buried among warns", func(t *testing.T) {
|
|
findings := make([]ReviewFinding, 1000)
|
|
for i := range findings {
|
|
findings[i] = ReviewFinding{
|
|
File: "file.go",
|
|
Line: i,
|
|
Defect: "warn finding",
|
|
FailureScenario: "advisory scenario",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: ReviewLevelWarn,
|
|
Category: ReviewCategoryOther,
|
|
}
|
|
}
|
|
findings[750] = ReviewFinding{
|
|
File: "file.go",
|
|
Line: 750,
|
|
Defect: "confirmed error",
|
|
FailureScenario: "fatal crash on nil context",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: ReviewLevelError,
|
|
Category: ReviewCategoryNilRisk,
|
|
}
|
|
|
|
if !ReviewBlocking(findings) {
|
|
t.Error("ReviewBlocking() = false, want true for confirmed error at index 750")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestReviewReportStatusDistinction(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
report ReviewReport
|
|
wantRan bool
|
|
wantBlocking bool
|
|
wantPassed bool
|
|
}{
|
|
{
|
|
name: "ran and found zero findings passes",
|
|
report: ReviewReport{
|
|
Status: ReviewStatusCompleted,
|
|
Findings: []ReviewFinding{},
|
|
Summary: "adversarial review completed with 0 findings",
|
|
},
|
|
wantRan: true,
|
|
wantBlocking: false,
|
|
wantPassed: true,
|
|
},
|
|
{
|
|
name: "ran and found nil findings slice passes",
|
|
report: ReviewReport{
|
|
Status: ReviewStatusCompleted,
|
|
Findings: nil,
|
|
Summary: "adversarial review completed cleanly",
|
|
},
|
|
wantRan: true,
|
|
wantBlocking: false,
|
|
wantPassed: true,
|
|
},
|
|
{
|
|
name: "ran and found only warn findings passes",
|
|
report: ReviewReport{
|
|
Status: ReviewStatusCompleted,
|
|
Findings: []ReviewFinding{
|
|
{
|
|
File: "a.go",
|
|
Line: 12,
|
|
Defect: "advisory note",
|
|
FailureScenario: "minor style difference",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: ReviewLevelWarn,
|
|
Category: ReviewCategoryOther,
|
|
},
|
|
},
|
|
Summary: "advisories found",
|
|
},
|
|
wantRan: true,
|
|
wantBlocking: false,
|
|
wantPassed: true,
|
|
},
|
|
{
|
|
name: "ran and found plausible error findings passes",
|
|
report: ReviewReport{
|
|
Status: ReviewStatusCompleted,
|
|
Findings: []ReviewFinding{
|
|
{
|
|
File: "b.go",
|
|
Line: 34,
|
|
Defect: "hypothetical deadlock",
|
|
FailureScenario: "if channel buffers fill up",
|
|
Verdict: ReviewVerdictPlausible,
|
|
Level: ReviewLevelError,
|
|
Category: ReviewCategoryGoroutineLeak,
|
|
},
|
|
},
|
|
Summary: "plausible error found",
|
|
},
|
|
wantRan: true,
|
|
wantBlocking: false,
|
|
wantPassed: true,
|
|
},
|
|
{
|
|
name: "ran and found confirmed error blocks and does not pass",
|
|
report: ReviewReport{
|
|
Status: ReviewStatusCompleted,
|
|
Findings: []ReviewFinding{
|
|
{
|
|
File: "c.go",
|
|
Line: 56,
|
|
Defect: "unhandled error in critical section",
|
|
FailureScenario: "database error causes silent data corruption",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: ReviewLevelError,
|
|
Category: ReviewCategoryUncheckedError,
|
|
},
|
|
},
|
|
Summary: "confirmed blocking error",
|
|
},
|
|
wantRan: true,
|
|
wantBlocking: true,
|
|
wantPassed: false,
|
|
},
|
|
{
|
|
name: "did not run MUST NOT be considered passed even with empty findings",
|
|
report: ReviewReport{
|
|
Status: ReviewStatusNotRun,
|
|
Findings: []ReviewFinding{},
|
|
SkipReason: "stage not reached due to prior failure",
|
|
},
|
|
wantRan: false,
|
|
wantBlocking: false,
|
|
wantPassed: false,
|
|
},
|
|
{
|
|
name: "did not run with nil findings does not pass",
|
|
report: ReviewReport{
|
|
Status: ReviewStatusNotRun,
|
|
Findings: nil,
|
|
SkipReason: "pipeline stopped early",
|
|
},
|
|
wantRan: false,
|
|
wantBlocking: false,
|
|
wantPassed: false,
|
|
},
|
|
{
|
|
name: "skipped review does not count as ran and does not pass",
|
|
report: ReviewReport{
|
|
Status: ReviewStatusSkipped,
|
|
Findings: []ReviewFinding{},
|
|
SkipReason: "documentation-only diff skipped by policy",
|
|
},
|
|
wantRan: false,
|
|
wantBlocking: false,
|
|
wantPassed: false,
|
|
},
|
|
{
|
|
name: "zero-value report does not count as ran and does not pass",
|
|
report: ReviewReport{},
|
|
wantRan: false,
|
|
wantBlocking: false,
|
|
wantPassed: false,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
if got := tt.report.Ran(); got != tt.wantRan {
|
|
t.Errorf("ReviewReport.Ran() = %v, want %v", got, tt.wantRan)
|
|
}
|
|
if got := tt.report.Blocking(); got != tt.wantBlocking {
|
|
t.Errorf("ReviewReport.Blocking() = %v, want %v", got, tt.wantBlocking)
|
|
}
|
|
if got := tt.report.Passed(); got != tt.wantPassed {
|
|
t.Errorf("ReviewReport.Passed() = %v, want %v", got, tt.wantPassed)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestReviewReportConstructors(t *testing.T) {
|
|
t.Run("NewCompletedReviewReport sets completed status", func(t *testing.T) {
|
|
findings := []ReviewFinding{
|
|
{
|
|
File: "pkg/foo.go",
|
|
Line: 15,
|
|
Defect: "hardcoded port",
|
|
FailureScenario: "port 8080 conflicts in container",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: ReviewLevelWarn,
|
|
Category: ReviewCategoryHardcodedValue,
|
|
},
|
|
}
|
|
report := NewCompletedReviewReport(findings, "completed successfully")
|
|
if report.Status != ReviewStatusCompleted {
|
|
t.Errorf("Status = %q, want %q", report.Status, ReviewStatusCompleted)
|
|
}
|
|
if !report.Ran() {
|
|
t.Error("Ran() = false, want true")
|
|
}
|
|
if len(report.Findings) != 1 {
|
|
t.Errorf("len(Findings) = %d, want 1", len(report.Findings))
|
|
}
|
|
if report.Summary != "completed successfully" {
|
|
t.Errorf("Summary = %q, want %q", report.Summary, "completed successfully")
|
|
}
|
|
})
|
|
|
|
t.Run("NewCompletedReviewReport with nil findings initializes empty slice", func(t *testing.T) {
|
|
report := NewCompletedReviewReport(nil, "clean run")
|
|
if report.Findings == nil {
|
|
t.Error("Findings should be non-nil empty slice")
|
|
}
|
|
if len(report.Findings) != 0 {
|
|
t.Errorf("len(Findings) = %d, want 0", len(report.Findings))
|
|
}
|
|
if !report.Passed() {
|
|
t.Error("Passed() = false, want true for clean completed review")
|
|
}
|
|
})
|
|
|
|
t.Run("NewNotRunReviewReport sets not_run status", func(t *testing.T) {
|
|
report := NewNotRunReviewReport("prerequisite stage failed")
|
|
if report.Status != ReviewStatusNotRun {
|
|
t.Errorf("Status = %q, want %q", report.Status, ReviewStatusNotRun)
|
|
}
|
|
if report.Ran() {
|
|
t.Error("Ran() = true, want false")
|
|
}
|
|
if report.Passed() {
|
|
t.Error("Passed() = true, want false")
|
|
}
|
|
if report.SkipReason != "prerequisite stage failed" {
|
|
t.Errorf("SkipReason = %q, want %q", report.SkipReason, "prerequisite stage failed")
|
|
}
|
|
})
|
|
|
|
t.Run("NewSkippedReviewReport sets skipped status", func(t *testing.T) {
|
|
report := NewSkippedReviewReport("markdown change only")
|
|
if report.Status != ReviewStatusSkipped {
|
|
t.Errorf("Status = %q, want %q", report.Status, ReviewStatusSkipped)
|
|
}
|
|
if report.Ran() {
|
|
t.Error("Ran() = true, want false")
|
|
}
|
|
if report.Passed() {
|
|
t.Error("Passed() = true, want false")
|
|
}
|
|
if report.SkipReason != "markdown change only" {
|
|
t.Errorf("SkipReason = %q, want %q", report.SkipReason, "markdown change only")
|
|
}
|
|
})
|
|
}
|
|
|
|
func TestReviewFindingJSONRoundTrip(t *testing.T) {
|
|
original := ReviewFinding{
|
|
File: "internal/domain/workflow/steps.go",
|
|
Line: 142,
|
|
Defect: "Goroutine spawned without error channel drain causes leak",
|
|
FailureScenario: "When context is cancelled before worker exits, the goroutine remains blocked on ch <- result forever",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Disposition: ReviewDispositionFixed,
|
|
Level: ReviewLevelError,
|
|
Category: ReviewCategoryGoroutineLeak,
|
|
}
|
|
|
|
data, err := json.Marshal(original)
|
|
if err != nil {
|
|
t.Fatalf("json.Marshal(ReviewFinding) failed: %v", err)
|
|
}
|
|
|
|
var decoded ReviewFinding
|
|
if err := json.Unmarshal(data, &decoded); err != nil {
|
|
t.Fatalf("json.Unmarshal(ReviewFinding) failed: %v", err)
|
|
}
|
|
|
|
if !reflect.DeepEqual(original, decoded) {
|
|
t.Errorf("JSON round-trip mismatch:\n got: %#v\n want: %#v", decoded, original)
|
|
}
|
|
|
|
// Explicitly assert that verdict and disposition survived the round trip
|
|
if decoded.Verdict != ReviewVerdictConfirmed {
|
|
t.Errorf("Verdict = %q, want %q", decoded.Verdict, ReviewVerdictConfirmed)
|
|
}
|
|
if decoded.Disposition != ReviewDispositionFixed {
|
|
t.Errorf("Disposition = %q, want %q", decoded.Disposition, ReviewDispositionFixed)
|
|
}
|
|
if decoded.Level != ReviewLevelError {
|
|
t.Errorf("Level = %q, want %q", decoded.Level, ReviewLevelError)
|
|
}
|
|
if decoded.Category != ReviewCategoryGoroutineLeak {
|
|
t.Errorf("Category = %q, want %q", decoded.Category, ReviewCategoryGoroutineLeak)
|
|
}
|
|
if !decoded.Blocking() {
|
|
t.Error("Decoded finding should remain blocking after round-trip")
|
|
}
|
|
}
|
|
|
|
func TestReviewReportJSONRoundTrip(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
report ReviewReport
|
|
}{
|
|
{
|
|
name: "completed with multiple findings",
|
|
report: ReviewReport{
|
|
Status: ReviewStatusCompleted,
|
|
Findings: []ReviewFinding{
|
|
{
|
|
File: "pkg/auth/token.go",
|
|
Line: 88,
|
|
Defect: "Missing signature verification before claims decoding",
|
|
FailureScenario: "Forged JWT with 'none' alg is accepted as valid",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Disposition: ReviewDispositionNoChangeNeeded,
|
|
Level: ReviewLevelError,
|
|
Category: ReviewCategoryInterfaceSatisfaction,
|
|
},
|
|
{
|
|
File: "pkg/auth/token.go",
|
|
Line: 120,
|
|
Defect: "Debug log prints raw token",
|
|
FailureScenario: "Verbose logging exposes bearer token in daemon logs",
|
|
Verdict: ReviewVerdictPlausible,
|
|
Disposition: ReviewDispositionSkipped,
|
|
Level: ReviewLevelWarn,
|
|
Category: ReviewCategoryOther,
|
|
},
|
|
},
|
|
Summary: "Found 1 confirmed defect and 1 advisory",
|
|
},
|
|
},
|
|
{
|
|
name: "completed with zero findings",
|
|
report: ReviewReport{
|
|
Status: ReviewStatusCompleted,
|
|
Findings: []ReviewFinding{},
|
|
Summary: "Clean review pass",
|
|
},
|
|
},
|
|
{
|
|
name: "zero findings with checked properties",
|
|
report: ReviewReport{
|
|
Status: ReviewStatusCompleted,
|
|
Findings: []ReviewFinding{},
|
|
Checked: []ReviewCheck{
|
|
{Property: "nil-safety of Foo callers", Evidence: "read all 4 callers; each nil-checks"},
|
|
{Property: "error propagation in Bar", Evidence: "traced Bar through to the caller's error return"},
|
|
},
|
|
Summary: "Checked and cleared",
|
|
},
|
|
},
|
|
{
|
|
name: "not run report",
|
|
report: ReviewReport{
|
|
Status: ReviewStatusNotRun,
|
|
SkipReason: "gate failed prior to review",
|
|
},
|
|
},
|
|
{
|
|
name: "skipped report",
|
|
report: ReviewReport{
|
|
Status: ReviewStatusSkipped,
|
|
SkipReason: "doc changes only",
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
data, err := json.Marshal(tt.report)
|
|
if err != nil {
|
|
t.Fatalf("json.Marshal(ReviewReport) failed: %v", err)
|
|
}
|
|
|
|
var decoded ReviewReport
|
|
if err := json.Unmarshal(data, &decoded); err != nil {
|
|
t.Fatalf("json.Unmarshal(ReviewReport) failed: %v", err)
|
|
}
|
|
|
|
// For empty slice, JSON unmarshals as nil if omitted or [] if present.
|
|
// Compare semantic state.
|
|
if decoded.Status != tt.report.Status {
|
|
t.Errorf("Status = %q, want %q", decoded.Status, tt.report.Status)
|
|
}
|
|
if decoded.Summary != tt.report.Summary {
|
|
t.Errorf("Summary = %q, want %q", decoded.Summary, tt.report.Summary)
|
|
}
|
|
if decoded.SkipReason != tt.report.SkipReason {
|
|
t.Errorf("SkipReason = %q, want %q", decoded.SkipReason, tt.report.SkipReason)
|
|
}
|
|
if decoded.Ran() != tt.report.Ran() {
|
|
t.Errorf("Ran() = %v, want %v", decoded.Ran(), tt.report.Ran())
|
|
}
|
|
if decoded.Blocking() != tt.report.Blocking() {
|
|
t.Errorf("Blocking() = %v, want %v", decoded.Blocking(), tt.report.Blocking())
|
|
}
|
|
if decoded.Passed() != tt.report.Passed() {
|
|
t.Errorf("Passed() = %v, want %v", decoded.Passed(), tt.report.Passed())
|
|
}
|
|
if len(decoded.Findings) != len(tt.report.Findings) {
|
|
t.Fatalf("len(Findings) = %d, want %d", len(decoded.Findings), len(tt.report.Findings))
|
|
}
|
|
for i := range decoded.Findings {
|
|
if !reflect.DeepEqual(decoded.Findings[i], tt.report.Findings[i]) {
|
|
t.Errorf("Findings[%d] mismatch:\n got: %#v\n want: %#v",
|
|
i, decoded.Findings[i], tt.report.Findings[i])
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestReviewFindingValidation(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
finding ReviewFinding
|
|
wantError bool
|
|
}{
|
|
{
|
|
name: "valid full finding with confirmed verdict and error level",
|
|
finding: ReviewFinding{
|
|
File: "internal/domain/workflow/steps.go",
|
|
Line: 42,
|
|
Defect: "Nil check missing before dereferencing context",
|
|
FailureScenario: "Calling step with nil context causes panic",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Disposition: ReviewDispositionFixed,
|
|
Level: ReviewLevelError,
|
|
Category: ReviewCategoryNilRisk,
|
|
},
|
|
wantError: false,
|
|
},
|
|
{
|
|
name: "valid finding without disposition (new unacted finding)",
|
|
finding: ReviewFinding{
|
|
File: "internal/domain/workflow/steps.go",
|
|
Line: 10,
|
|
Defect: "Unchecked error return",
|
|
FailureScenario: "Write failure ignored silently",
|
|
Verdict: ReviewVerdictPlausible,
|
|
Level: ReviewLevelWarn,
|
|
Category: ReviewCategoryUncheckedError,
|
|
},
|
|
wantError: false,
|
|
},
|
|
{
|
|
name: "valid finding with line 0",
|
|
finding: ReviewFinding{
|
|
File: "README.md",
|
|
Line: 0,
|
|
Defect: "Missing architecture section",
|
|
FailureScenario: "New developers cannot find component map",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: ReviewLevelWarn,
|
|
Category: ReviewCategoryOther,
|
|
},
|
|
wantError: false,
|
|
},
|
|
{
|
|
name: "all valid checklist categories accepted",
|
|
finding: ReviewFinding{
|
|
File: "file.go",
|
|
Line: 1,
|
|
Defect: "Valid defect",
|
|
FailureScenario: "Valid scenario",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: ReviewLevelError,
|
|
Category: ReviewCategoryDeadCode,
|
|
},
|
|
wantError: false,
|
|
},
|
|
{
|
|
name: "invalid empty file",
|
|
finding: ReviewFinding{
|
|
File: "",
|
|
Line: 10,
|
|
Defect: "Defect without file",
|
|
FailureScenario: "Scenario",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: ReviewLevelError,
|
|
Category: ReviewCategoryNilRisk,
|
|
},
|
|
wantError: true,
|
|
},
|
|
{
|
|
name: "invalid negative line",
|
|
finding: ReviewFinding{
|
|
File: "file.go",
|
|
Line: -5,
|
|
Defect: "Negative line finding",
|
|
FailureScenario: "Scenario",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: ReviewLevelError,
|
|
Category: ReviewCategoryNilRisk,
|
|
},
|
|
wantError: true,
|
|
},
|
|
{
|
|
name: "invalid empty defect",
|
|
finding: ReviewFinding{
|
|
File: "file.go",
|
|
Line: 10,
|
|
Defect: "",
|
|
FailureScenario: "Scenario",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: ReviewLevelError,
|
|
Category: ReviewCategoryNilRisk,
|
|
},
|
|
wantError: true,
|
|
},
|
|
{
|
|
name: "invalid empty failure scenario",
|
|
finding: ReviewFinding{
|
|
File: "file.go",
|
|
Line: 10,
|
|
Defect: "Defect without scenario",
|
|
FailureScenario: "",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: ReviewLevelError,
|
|
Category: ReviewCategoryNilRisk,
|
|
},
|
|
wantError: true,
|
|
},
|
|
{
|
|
name: "multi-line suggestion is rejected",
|
|
finding: ReviewFinding{
|
|
File: "file.go",
|
|
Line: 10,
|
|
Defect: "Defect with multi-line suggestion",
|
|
FailureScenario: "Scenario",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: ReviewLevelError,
|
|
Category: ReviewCategoryNilRisk,
|
|
Suggestion: "return nil\nmu.Lock()",
|
|
},
|
|
wantError: true,
|
|
},
|
|
{
|
|
name: "single-line suggestion on a confirmed finding is valid",
|
|
finding: ReviewFinding{
|
|
File: "file.go",
|
|
Line: 10,
|
|
Defect: "Defect with single-line suggestion",
|
|
FailureScenario: "Scenario",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: ReviewLevelError,
|
|
Category: ReviewCategoryNilRisk,
|
|
Suggestion: "mu.Lock()",
|
|
},
|
|
wantError: false,
|
|
},
|
|
{
|
|
name: "invalid verdict",
|
|
finding: ReviewFinding{
|
|
File: "file.go",
|
|
Line: 10,
|
|
Defect: "Defect with bogus verdict",
|
|
FailureScenario: "Scenario",
|
|
Verdict: "maybe",
|
|
Level: ReviewLevelError,
|
|
Category: ReviewCategoryNilRisk,
|
|
},
|
|
wantError: true,
|
|
},
|
|
{
|
|
name: "invalid level",
|
|
finding: ReviewFinding{
|
|
File: "file.go",
|
|
Line: 10,
|
|
Defect: "Defect with bogus level",
|
|
FailureScenario: "Scenario",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: "critical",
|
|
Category: ReviewCategoryNilRisk,
|
|
},
|
|
wantError: true,
|
|
},
|
|
{
|
|
name: "invalid category",
|
|
finding: ReviewFinding{
|
|
File: "file.go",
|
|
Line: 10,
|
|
Defect: "Defect with bogus category",
|
|
FailureScenario: "Scenario",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: ReviewLevelError,
|
|
Category: "unknown-category",
|
|
},
|
|
wantError: true,
|
|
},
|
|
{
|
|
name: "invalid disposition",
|
|
finding: ReviewFinding{
|
|
File: "file.go",
|
|
Line: 10,
|
|
Defect: "Defect with bogus disposition",
|
|
FailureScenario: "Scenario",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Disposition: "ignored",
|
|
Level: ReviewLevelError,
|
|
Category: ReviewCategoryNilRisk,
|
|
},
|
|
wantError: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := tt.finding.Validate()
|
|
if (err != nil) != tt.wantError {
|
|
t.Errorf("ReviewFinding.Validate() error = %v, wantError %v", err, tt.wantError)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestReviewReportValidation(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
report ReviewReport
|
|
wantError bool
|
|
}{
|
|
{
|
|
name: "valid completed report with valid findings",
|
|
report: ReviewReport{
|
|
Status: ReviewStatusCompleted,
|
|
Findings: []ReviewFinding{
|
|
{
|
|
File: "file.go",
|
|
Line: 10,
|
|
Defect: "Valid defect",
|
|
FailureScenario: "Valid scenario",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: ReviewLevelError,
|
|
Category: ReviewCategoryNilRisk,
|
|
},
|
|
},
|
|
Summary: "valid summary",
|
|
},
|
|
wantError: false,
|
|
},
|
|
{
|
|
name: "valid not_run report",
|
|
report: ReviewReport{
|
|
Status: ReviewStatusNotRun,
|
|
SkipReason: "gate failed",
|
|
},
|
|
wantError: false,
|
|
},
|
|
{
|
|
name: "valid skipped report",
|
|
report: ReviewReport{
|
|
Status: ReviewStatusSkipped,
|
|
SkipReason: "policy skip",
|
|
},
|
|
wantError: false,
|
|
},
|
|
{
|
|
name: "invalid report status",
|
|
report: ReviewReport{
|
|
Status: "in_progress",
|
|
},
|
|
wantError: true,
|
|
},
|
|
{
|
|
name: "valid completed report with checked properties",
|
|
report: ReviewReport{
|
|
Status: ReviewStatusCompleted,
|
|
Checked: []ReviewCheck{{Property: "nil-safety of Foo callers", Evidence: "read all 4 callers"}},
|
|
},
|
|
wantError: false,
|
|
},
|
|
{
|
|
name: "checked entry without evidence is rejected",
|
|
report: ReviewReport{
|
|
Status: ReviewStatusCompleted,
|
|
Checked: []ReviewCheck{{Property: "looks fine"}},
|
|
},
|
|
wantError: true,
|
|
},
|
|
{
|
|
name: "checked entry without property is rejected",
|
|
report: ReviewReport{
|
|
Status: ReviewStatusCompleted,
|
|
Checked: []ReviewCheck{{Evidence: "read the file"}},
|
|
},
|
|
wantError: true,
|
|
},
|
|
{
|
|
name: "invalid finding inside findings slice",
|
|
report: ReviewReport{
|
|
Status: ReviewStatusCompleted,
|
|
Findings: []ReviewFinding{
|
|
{
|
|
File: "",
|
|
Line: 10,
|
|
Defect: "Missing file",
|
|
FailureScenario: "Scenario",
|
|
Verdict: ReviewVerdictConfirmed,
|
|
Level: ReviewLevelError,
|
|
Category: ReviewCategoryNilRisk,
|
|
},
|
|
},
|
|
},
|
|
wantError: true,
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
err := tt.report.Validate()
|
|
if (err != nil) != tt.wantError {
|
|
t.Errorf("ReviewReport.Validate() error = %v, wantError %v", err, tt.wantError)
|
|
}
|
|
})
|
|
}
|
|
}
|