forked from longtao.wu/droboticflow
189 lines
7.8 KiB
Go
189 lines
7.8 KiB
Go
package api
|
|
|
|
import (
|
|
"context"
|
|
"droboticflow/internal/domain"
|
|
"droboticflow/internal/store"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestReviewTraceExistingEvidenceAndMissingHistory(t *testing.T) {
|
|
for _, complete := range []bool{false, true} {
|
|
t.Run(map[bool]string{true: "complete", false: "legacy-gap"}[complete], func(t *testing.T) {
|
|
f := decisionFixture(t)
|
|
// A separate item makes the history boundary explicit rather than relying
|
|
// on fixture setup's events.
|
|
f.item.ID = "trace-item"
|
|
f.item.CurrentRevision = 3
|
|
f.item.LatestAuthorUserID = f.annotator.ID
|
|
if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
add := func(id, kind, at string, payload map[string]any) {
|
|
t.Helper()
|
|
if err := f.store.AppendHumanEvent(f.ctx, domain.HumanEventRecord{ID: id, OrganizationID: f.item.OrganizationID, ItemID: f.item.ID, EventType: kind, ActorUserID: f.annotator.ID, CreatedAt: at, Payload: payload}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if complete {
|
|
add("created", "created", "2026-01-01T00:00:00Z", nil)
|
|
}
|
|
add("submit", "combined_submitted", "2026-01-02T00:00:00Z", map[string]any{"revision": 2, "private": "must-not-leak"})
|
|
add("reject", "combined_quality_rejected", "2026-01-03T00:00:00Z", map[string]any{"reviewedRevision": 2})
|
|
add("resubmit", "combined_submitted", "2026-01-04T00:00:00Z", map[string]any{"annotationRevision": 3})
|
|
before, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
res := reviewSessionRequest(f, http.MethodGet, "/api/human-items/"+f.item.ID+"/review-trace", f.reviewer, nil, nil)
|
|
if res.Code != 200 {
|
|
t.Fatalf("%d %s", res.Code, res.Body.String())
|
|
}
|
|
var trace reviewTrace
|
|
if err := json.Unmarshal(res.Body.Bytes(), &trace); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(trace.Events) != 3 || trace.Events[0].Revision != 3 || trace.Events[0].Kind != "重新提交" || trace.Author != f.annotator.Name {
|
|
t.Fatalf("bad trace %+v", trace)
|
|
}
|
|
if complete {
|
|
if trace.Rejections == nil || *trace.Rejections != 1 || trace.FirstSubmittedAt != "2026-01-02T00:00:00Z" {
|
|
t.Fatalf("bad summary %+v", trace)
|
|
}
|
|
} else if trace.Rejections != nil || trace.FirstSubmittedAt != "" {
|
|
t.Fatal("invented legacy history")
|
|
}
|
|
if strings.Contains(res.Body.String(), "must-not-leak") || strings.Contains(res.Body.String(), "phone") {
|
|
t.Fatal("private data leaked")
|
|
}
|
|
after, _ := f.store.GetHumanItem(f.ctx, f.item.ID)
|
|
if before.Version != after.Version || before.AssignedUserID != after.AssignedUserID {
|
|
t.Fatal("trace mutated item")
|
|
}
|
|
foreignItem := f.item
|
|
foreignItem.ID = "other-tenant-item"
|
|
foreignItem.OrganizationID = "other-org"
|
|
if err := f.store.UpsertHumanItem(f.ctx, foreignItem); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
denied := reviewSessionRequest(f, http.MethodGet, "/api/human-items/"+foreignItem.ID+"/review-trace", f.reviewer, nil, nil)
|
|
if denied.Code == 200 {
|
|
t.Fatal("cross-tenant trace exposed")
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
type unavailableTraceStore struct{ store.Store }
|
|
|
|
func (s unavailableTraceStore) ListHumanTraceSummaries(context.Context, string, []domain.HumanItemRecord) (map[string]domain.HumanTraceSummary, error) {
|
|
return nil, errors.New("synthetic metadata failure")
|
|
}
|
|
func TestReviewTraceSummaryFailureDoesNotHideTaskPool(t *testing.T) {
|
|
f := decisionFixture(t)
|
|
f.handler = NewHandler(unavailableTraceStore{f.store}, nil)
|
|
res := reviewSessionRequest(f, http.MethodGet, "/api/human-items?projectId="+f.item.ProjectID+"&pool=mine&trace=1", f.reviewer, nil, nil)
|
|
if res.Code != 200 || !strings.Contains(res.Body.String(), f.item.ID) {
|
|
t.Fatalf("metadata failure hid work: %d %s", res.Code, res.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestReviewTraceAcceptanceProjectsOnlySelectedSource(t *testing.T) {
|
|
f := sharedAcceptanceFixture(t)
|
|
batch, err := f.store.GetHumanBatch(f.ctx, f.item.BatchID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f.item.CreatedAt = "2026-01-08T00:00:00Z"
|
|
f.item.AssignedUserID = f.reviewer.ID
|
|
f.item.CurrentRevision = 0
|
|
f.item.LatestAuthorUserID = ""
|
|
entries := f.item.Subject.Payload["items"].([]any)
|
|
for i, raw := range entries {
|
|
entry := raw.(map[string]any)
|
|
entry["runId"] = f.item.RunID
|
|
source := domain.HumanItemRecord{
|
|
ID: stringValue(entry["sourceHumanItemId"]), OrganizationID: f.item.OrganizationID,
|
|
ProjectID: f.item.ProjectID, WorkflowID: f.item.WorkflowID, RunID: f.item.RunID,
|
|
BatchID: batch.SourceBatchID, LegacyTaskID: stringValue(entry["taskId"]),
|
|
CurrentRevision: 9, LatestAuthorUserID: f.annotator.ID, SupplierID: f.annotator.SupplierID,
|
|
}
|
|
if i == 1 {
|
|
entry["annotationRevision"] = 4
|
|
}
|
|
if err := f.store.UpsertHumanItem(f.ctx, source); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
foreign := entries[0].(map[string]any)
|
|
entries = append(entries, map[string]any{"sourceHumanItemId": "wrong-batch", "taskId": foreign["taskId"], "runId": f.item.RunID})
|
|
f.item.Subject.Payload["items"] = entries
|
|
if err := f.store.UpsertHumanItem(f.ctx, f.item); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := f.store.UpsertHumanItem(f.ctx, domain.HumanItemRecord{
|
|
ID: "wrong-batch", OrganizationID: f.item.OrganizationID, ProjectID: f.item.ProjectID,
|
|
WorkflowID: f.item.WorkflowID, RunID: f.item.RunID, BatchID: "other", LegacyTaskID: stringValue(foreign["taskId"]),
|
|
}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
add := func(itemID, kind, day string, revision int) {
|
|
t.Helper()
|
|
err := f.store.AppendHumanEvent(f.ctx, domain.HumanEventRecord{
|
|
ID: itemID + "-" + day, ItemID: itemID, OrganizationID: f.item.OrganizationID, EventType: kind,
|
|
CreatedAt: "2026-01-" + day + "T00:00:00Z", ActorUserID: f.annotator.ID,
|
|
Payload: map[string]any{"reviewedRevision": revision, "comment": "private comment"},
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
add(f.item.ID, "created", "08", 0)
|
|
add(f.item.ID, "rejected", "09", 0)
|
|
add(f.item.ID, "created", "12", 0)
|
|
add("source-a", "created", "01", 0)
|
|
add("source-a", "combined_submitted", "02", 2)
|
|
add("source-a", "combined_quality_rejected", "03", 2)
|
|
add("source-a", "combined_submitted", "04", 3)
|
|
add("source-a", "combined_quality_approved", "05", 3)
|
|
add("source-a", "aggregate_acceptance_rejected", "09", 0)
|
|
add("source-a", "combined_quality_approved", "11", 3)
|
|
add("source-a", "combined_quality_approved", "13", 9)
|
|
add("source-b", "combined_submitted", "06", 4)
|
|
add("wrong-batch", "combined_quality_approved", "05", 7)
|
|
read := func(query string) reviewTrace {
|
|
t.Helper()
|
|
res := f.request(http.MethodGet, "/api/human-items/"+f.item.ID+"/review-trace"+query, f.reviewer, nil)
|
|
var trace reviewTrace
|
|
if res.Code != http.StatusOK || json.Unmarshal(res.Body.Bytes(), &trace) != nil {
|
|
t.Fatalf("trace status=%d body=%s", res.Code, res.Body.String())
|
|
}
|
|
if trace.AuthorID != "" || trace.Author != "" || trace.Supplier != "" || strings.Contains(res.Body.String(), "private comment") {
|
|
t.Fatalf("anonymous acceptance disclosed source metadata: %+v", trace)
|
|
}
|
|
return trace
|
|
}
|
|
a := read("?sourceItemId=source-a")
|
|
if a.Revision != 3 || a.FirstSubmittedAt != "2026-01-02T00:00:00Z" ||
|
|
a.QualityRejections == nil || *a.QualityRejections != 1 || a.Rejections == nil || *a.Rejections != 1 ||
|
|
len(a.Events) != 6 || a.Events[0].Kind != "质检通过" || a.Events[1].Kind != "验收退回" {
|
|
t.Fatalf("wrong source history: %+v", a)
|
|
}
|
|
for _, event := range a.Events {
|
|
if strings.HasPrefix(event.ID, "source-") && (event.Actor != "" || event.Revision == 9) {
|
|
t.Fatalf("source actor or newer revision exposed: %+v", event)
|
|
}
|
|
}
|
|
b := read("?sourceItemId=source-b")
|
|
if b.Revision != 4 || b.QualityRejections != nil || b.FirstSubmittedAt != "" || len(b.Events) != 2 {
|
|
t.Fatalf("wrong legacy source history: %+v", b)
|
|
}
|
|
for _, query := range []string{"", "?sourceItemId=wrong-batch", "?sourceItemId=unlisted"} {
|
|
got := read(query)
|
|
if got.Revision != 0 || got.QualityRejections != nil || len(got.Events) != 1 {
|
|
t.Fatalf("unexpected source read for %q: %+v", query, got)
|
|
}
|
|
}
|
|
}
|