droboticflow/internal/api/acceptance_release_compat_test.go

184 lines
8.1 KiB
Go

package api
import (
"droboticflow/internal/domain"
"encoding/json"
"net/http"
"strings"
"testing"
"time"
)
// aggregateReleasedSourcesFixture builds the v5.8 aggregate acceptance scene
// from TestReviewDecisionAggregateRecoveryDoesNotReopenSourceTwice, with every
// source already partitioned for release — the legacy production shape where
// the pre-v5.8 pipeline released sources before the final acceptance existed.
func aggregateReleasedSourcesFixture(t *testing.T) combinedDraftFixture {
t.Helper()
f := acceptanceReviewDraftFixture(t)
batch, e := f.store.GetHumanBatch(f.ctx, f.item.BatchID)
if e != nil {
t.Fatal(e)
}
sourceBatch := *batch
sourceBatch.ID = "source-batch-released"
sourceBatch.Stages = domain.DefaultFanyuV58HumanStages()
sourceBatch.RequirePrePackageAcceptance = true
sourceBatch.ProcessingProfile = domain.ProcessingProfileRawHuman
if e := f.store.UpsertHumanBatch(f.ctx, sourceBatch); e != nil {
t.Fatal(e)
}
batch.SourceBatchID = sourceBatch.ID
batch.ProcessingProfile = domain.ProcessingProfileRawHuman
// Production naming: the acceptance batch derives from the source batch,
// so the approve-time forced flush finds the (now accepted) aggregate
// item instead of trying to materialize a new one.
batch.ID = fanyuV5PrePackageAcceptanceBatchID(sourceBatch.ID)
if e := f.store.UpsertHumanBatch(f.ctx, *batch); e != nil {
t.Fatal(e)
}
f.item.BatchID = batch.ID
f.item.ID = fanyuV5PrePackageAcceptanceItemID(sourceBatch.ID)
if e := f.store.UpsertHumanItem(f.ctx, f.item); e != nil {
t.Fatal(e)
}
for _, id := range []string{"source-a", "source-b"} {
source := f.item
source.ID = id
source.BatchID = sourceBatch.ID
source.CurrentStageID = domain.FanyuV58StageQualityReview
source.Status = domain.HumanItemAccepted
source.ReviewRound = 4
source.Version = 10
source.AssignedUserID = ""
source.LegacyTaskID = "task-" + id
source.AcceptedAt = nowISO()
source.ReleasePartitionID = "human-partition-already-dispatched"
if e := f.store.UpsertHumanItem(f.ctx, source); e != nil {
t.Fatal(e)
}
task := f.task
task.ID = source.LegacyTaskID
task.Status = domain.AnnotationTaskStatusAccepted
if e := f.store.UpsertAnnotationTask(f.ctx, task); e != nil {
t.Fatal(e)
}
}
return f
}
// Defect 5 (server root): production keeps aggregate acceptance items whose
// sources were partitioned for release by the pre-v5.8 pipeline. Approving is
// the release confirmation itself and must not be blocked forever by the
// already-released marker; every 验收通过 click currently 409s.
func TestAggregateAcceptanceApproveToleratesReleasedSources(t *testing.T) {
f := aggregateReleasedSourcesFixture(t)
version, headers := openReviewDraftSession(t, f)
payload := map[string]any{"version": f.item.Version, "decision": "approved", "operationId": "approve-released", "draftVersion": 0, "resultVersion": version}
endpoint := "/api/human-items/" + f.item.ID
result := reviewSessionRequest(f, http.MethodPost, endpoint+"/decisions", f.reviewer, payload, headers)
if result.Code != 200 {
t.Fatalf("approve with released sources %d %s", result.Code, result.Body.String())
}
after, e := f.store.GetHumanItem(f.ctx, f.item.ID)
if e != nil || after == nil {
t.Fatalf("read aggregate item: %v", e)
}
if after.Status != domain.HumanItemAccepted {
t.Fatalf("aggregate item not accepted: %s", after.Status)
}
}
// A rejection must still be fenced: released sources cannot be recalled into
// rework, and the pending reason must say it was already released rather than
// pretending the source never passed quality review.
func TestAggregateAcceptanceRejectStillFencedForReleasedSources(t *testing.T) {
f := aggregateReleasedSourcesFixture(t)
version, headers := openReviewDraftSession(t, f)
payload := map[string]any{"version": f.item.Version, "decision": "rejected", "reasonCodes": []string{"batch_problem"}, "comment": "需要重新核对", "operationId": "reject-released", "draftVersion": 0, "resultVersion": version}
endpoint := "/api/human-items/" + f.item.ID
result := reviewSessionRequest(f, http.MethodPost, endpoint+"/decisions", f.reviewer, payload, headers)
if result.Code != 409 || !strings.Contains(result.Body.String(), "acceptance_source_not_ready") {
t.Fatalf("reject must stay fenced %d %s", result.Code, result.Body.String())
}
if !strings.Contains(result.Body.String(), `"alreadyReleased":true`) {
t.Fatalf("pending reason must be honest about release: %s", result.Body.String())
}
}
// Defect 3 (server root): a write fence that outlived its operation (the
// deciding process crashed after the outcome settled, or the marker references
// an operation that no longer exists) must not lock the item forever. Takeover
// clears settled fences; genuinely in-flight decisions stay fenced.
func TestReviewSessionTakeoverClearsSettledWriteFences(t *testing.T) {
f := acceptanceReviewDraftFixture(t)
version, headers := openReviewDraftSession(t, f)
_ = version
key := reviewSessionKey(f.item)
finished := &domain.ReviewOperation{
AcceptanceClaim: f.item.AcceptanceClaim, ID: reviewOperationID(f.item, f.reviewer.ID, "settled-crash"),
OperationID: "settled-crash", OrganizationID: f.item.OrganizationID, ProjectID: f.item.ProjectID,
UserID: f.reviewer.ID, ItemID: f.item.ID, StageID: f.item.CurrentStageID, ReviewRound: f.item.ReviewRound,
Kind: "acceptance", Decision: "approved", Status: "completed", CreatedAt: nowISO(), CompletedAt: nowISO(),
}
if ok, e := f.store.CompareAndSwapReviewOperation(f.ctx, *finished, 0); e != nil || !ok {
t.Fatalf("seed finished op: %v %v", ok, e)
}
base := "/api/human-items/" + f.item.ID
for _, fence := range []struct {
name string
writeID string
code int
}{
{"terminal operation", finished.ID, 200},
{"missing operation", reviewOperationID(f.item, f.reviewer.ID, "never-persisted"), 200},
} {
session, e := f.store.GetReviewSession(f.ctx, key)
if e != nil || session == nil {
t.Fatalf("session %v %v", session, e)
}
next := *session
next.WriteID = fence.writeID
next.ExpiresAt = time.Now().UTC().Add(-time.Minute).Format(time.RFC3339Nano)
if ok, e := f.store.CompareAndSwapReviewSession(f.ctx, next, session.Revision); e != nil || !ok {
t.Fatalf("seed fence %s: %v %v", fence.name, ok, e)
}
takeover := f.request(http.MethodPost, base+"/review-session", f.reviewer, map[string]any{"action": "takeover", "pageId": "recovery-" + fence.name})
if takeover.Code != fence.code {
t.Fatalf("takeover after %s fence: %d %s", fence.name, takeover.Code, takeover.Body.String())
}
if fence.code == 200 {
var body struct {
Writable bool `json:"writable"`
}
_ = json.Unmarshal(takeover.Body.Bytes(), &body)
if !body.Writable {
t.Fatalf("takeover after %s fence must be writable: %s", fence.name, takeover.Body.String())
}
}
}
// A prepared formal decision is still in flight: takeover stays fenced and
// the dedicated reconcile route owns its recovery (existing contract).
inFlight := &domain.ReviewOperation{
AcceptanceClaim: f.item.AcceptanceClaim, ID: reviewOperationID(f.item, f.reviewer.ID, "in-flight"),
OperationID: "in-flight", OrganizationID: f.item.OrganizationID, ProjectID: f.item.ProjectID,
UserID: f.reviewer.ID, ItemID: f.item.ID, StageID: f.item.CurrentStageID, ReviewRound: f.item.ReviewRound,
Kind: "acceptance", Decision: "approved", Status: "prepared", CreatedAt: nowISO(),
}
if ok, e := f.store.CompareAndSwapReviewOperation(f.ctx, *inFlight, 0); e != nil || !ok {
t.Fatalf("seed prepared op: %v %v", ok, e)
}
session, _ := f.store.GetReviewSession(f.ctx, key)
next := *session
next.WriteID = inFlight.ID
next.ExpiresAt = time.Now().UTC().Add(-time.Minute).Format(time.RFC3339Nano)
if ok, e := f.store.CompareAndSwapReviewSession(f.ctx, next, session.Revision); e != nil || !ok {
t.Fatalf("seed in-flight fence: %v %v", ok, e)
}
_ = headers
fenced := f.request(http.MethodPost, base+"/review-session", f.reviewer, map[string]any{"action": "takeover", "pageId": "must-not-pass"})
if fenced.Code != 409 || !strings.Contains(fenced.Body.String(), "review_write_in_progress") {
t.Fatalf("in-flight decision must stay fenced %d %s", fenced.Code, fenced.Body.String())
}
}