Files
archie/internal/infrastructure/postgres/review_queue_test.go
T
sam e0db40963e fix(postgres): queue a review that arrives during a remediation
A review reaction that landed while a remediate run owned the task failed the
pr_open guard, was acked and dropped, and the poll cursor advanced past it.
Runs against one task are serialised, so the unit is appended to
tasks.pending_reviews instead; the transition that returns the task to pr_open
promotes the oldest waiting review back to queued. A re-delivered unit is
absorbed by payload equality, never queued twice.
2026-10-05 14:58:14 +11:00

107 lines
3.3 KiB
Go

package postgres_test
import (
"testing"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/pgstore"
"github.com/samcharles93/archie-core/internal/taskstate"
)
// A review that arrives while a remediation owns the task is queued behind it
// and remediated when that run finishes, in arrival order. A re-delivered
// review is absorbed rather than queued to run a second time.
func TestSecondReviewDuringRemediationIsQueued(t *testing.T) {
ctx := t.Context()
db := pgstore.Open(t)
id := openTaskAtPROpen(t, db)
first, second := reviewUnit(t, 11), reviewUnit(t, 22)
if err := db.BeginRemediation(ctx, id, first); err != nil {
t.Fatalf("begin first remediation: %v", err)
}
if err := db.BeginRemediation(ctx, id, second); err != nil {
t.Fatalf("review arriving during remediation was refused, not queued: %v", err)
}
if err := db.BeginRemediation(ctx, id, first); err != nil {
t.Fatalf("re-delivered review was refused: %v", err)
}
assertActiveReview(t, db, id, first)
// The first round runs and finishes; the second, still waiting, is
// promoted behind it.
runRemediationRound(t, db, id)
assertActiveReview(t, db, id, second)
// The second round runs and finds nothing left waiting.
runRemediationRound(t, db, id)
task, err := db.TaskByID(ctx, id)
if err != nil {
t.Fatal(err)
}
if task.Status != taskstate.PROpen {
t.Fatalf("status after both rounds = %q, want %q (a review was queued twice)", task.Status, taskstate.PROpen)
}
}
// openTaskAtPROpen enqueues a task, claims it and opens its pull request.
func openTaskAtPROpen(t *testing.T, db *pgstore.TaskDB) int64 {
t.Helper()
ctx := t.Context()
created, err := db.EnqueueChatTask(ctx, "acme", "widget", "a task", "", "tdd", "", nil)
if err != nil {
t.Fatal(err)
}
if _, err := db.ClaimNext(ctx); err != nil {
t.Fatal(err)
}
if err := db.Transition(ctx, created.ID, taskstate.Running, taskstate.PROpen, "opened the pull request"); err != nil {
t.Fatal(err)
}
return created.ID
}
// runRemediationRound claims the queued task and finishes one round of it.
func runRemediationRound(t *testing.T, db *pgstore.TaskDB, id int64) {
t.Helper()
ctx := t.Context()
run, err := db.ClaimNext(ctx)
if err != nil {
t.Fatal(err)
}
if run == nil || run.ID != id {
t.Fatalf("claim: got %v, want task %d queued", run, id)
}
if err := db.Transition(ctx, id, taskstate.Running, taskstate.PROpen, "remediated a review round"); err != nil {
t.Fatal(err)
}
}
// assertActiveReview asserts the task is queued on payload, which is the
// review a remediation would run next.
func assertActiveReview(t *testing.T, db *pgstore.TaskDB, id int64, payload string) {
t.Helper()
task, err := db.TaskByID(t.Context(), id)
if err != nil {
t.Fatal(err)
}
if task.Status != taskstate.Queued || task.Workflow != "remediate" {
t.Fatalf("task is %q on workflow %q, want a queued remediation", task.Status, task.Workflow)
}
if task.ReviewPayload != payload {
t.Fatalf("active review = %s, want %s", task.ReviewPayload, payload)
}
}
func reviewUnit(t *testing.T, reviewID int64) string {
t.Helper()
payload, err := workflow.EncodeReviewUnit(workflow.ReviewUnit{
ReviewID: reviewID, State: "requested_changes", Body: "please change this",
})
if err != nil {
t.Fatal(err)
}
return payload
}