| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "amadan.net/oficina/equipo/internal/copydesk" |
| 5 | "amadan.net/oficina/equipo/internal/wire" |
| 6 | "encoding/json" |
| 7 | "errors" |
| 8 | "gorm.io/gorm" |
| 9 | "testing" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | func TestCopySettingAuthorityAndRevocation(t *testing.T) { |
| 14 | r, fail := rosterRig(t) |
| 15 | r.task("tkcopy", "Copy", hkOwner) |
| 16 | r.s.authority.Audit = func(*gorm.DB, string, string, string) error { return nil } |
| 17 | var d copydesk.Delegation |
| 18 | r.must(r.tx(func(tx *txn) error { var err error; d, err = tx.copyDelegation("p1"); return err })) |
| 19 | if d.Owner != "" { |
| 20 | t.Fatal("enabled by default") |
| 21 | } |
| 22 | r.must(r.s.SetCopyDelegation(r.ctx, hkOwner, "p1", true, 0)) |
| 23 | r.must(r.tx(func(tx *txn) error { var err error; d, err = tx.copyDelegation("p1"); return err })) |
| 24 | if d.Owner != hkOwner || d.Revision != 1 { |
| 25 | t.Fatalf("grant %+v", d) |
| 26 | } |
| 27 | if !errors.Is(r.s.SetCopyDelegation(r.ctx, hkOwner, "p1", false, 0), ErrCardConflict) { |
| 28 | t.Fatal("stale setting overwrote grant") |
| 29 | } |
| 30 | r.exec("UPDATE members SET role='member' WHERE address=?", hkOwner) |
| 31 | if r.s.SetCopyDelegation(r.ctx, hkOwner, "p1", true, 1) == nil { |
| 32 | t.Fatal("member changed delegation") |
| 33 | } |
| 34 | r.must(r.tx(func(tx *txn) error { var err error; d, err = tx.copyDelegation("p1"); return err })) |
| 35 | if d.Owner != "" { |
| 36 | t.Fatal("removed owner still grants authority") |
| 37 | } |
| 38 | *fail = true |
| 39 | if r.tx(func(tx *txn) error { _, err := tx.copyDelegation("p1"); return err }) == nil { |
| 40 | t.Fatal("lookup failure allowed") |
| 41 | } |
| 42 | } |
| 43 | func TestCopyCandidateSupersedesEngineering(t *testing.T) { |
| 44 | p := progressOf([]stepRecord{{Specialist: wire.SpecialistEngineering, Pushed: true, CandidateSHA: "eng"}, {Specialist: wire.SpecialistChecks, CandidateSHA: "eng", State: wire.StepDone}, {Specialist: wire.SpecialistCopy, Pushed: true, CandidateSHA: "copy", State: wire.StepDone}}) |
| 45 | if p.candidate != "copy" || p.checks != nil || p.eng.CandidateSHA != "eng" { |
| 46 | t.Fatalf("wrong candidate %+v", p) |
| 47 | } |
| 48 | } |
| 49 | func TestCopyOwnerFeedbackImport(t *testing.T) { |
| 50 | r, _ := rosterRig(t) |
| 51 | r.task("tkcopy", "Copy", hkOwner) |
| 52 | r.s.authority.Audit = func(*gorm.DB, string, string, string) error { return nil } |
| 53 | r.must(r.s.SetCopyDelegation(r.ctx, hkOwner, "p1", true, 0)) |
| 54 | r.must(r.s.db.G.Create(©ReviewRecord{ID: "record", ProjectID: "p1", Owner: hkOwner, Key: "save", Text: "Old text", Hash: copydesk.Hash("Old text"), Candidate: "head", Delegation: 1}).Error) |
| 55 | f := CopyImport{Version: 1, SourceID: "review-1", Strings: []copydesk.Feedback{{Key: "save", Before: "Old text", After: "New text"}}} |
| 56 | if r.s.ImportCopyReview(r.ctx, hkMember, "p1", f) == nil { |
| 57 | t.Fatal("other owner taught Copy") |
| 58 | } |
| 59 | r.must(r.s.ImportCopyReview(r.ctx, hkOwner, "p1", f)) |
| 60 | r.must(r.s.ImportCopyReview(r.ctx, hkOwner, "p1", f)) |
| 61 | var n int64 |
| 62 | r.must(r.s.db.G.Model(©FeedbackRecord{}).Count(&n).Error) |
| 63 | if n != 1 { |
| 64 | t.Fatalf("feedback duplicates: %d", n) |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | func TestCopyAuthenticatedEditRejectsOtherOwner(t *testing.T) { |
| 69 | r, _ := rosterRig(t) |
| 70 | r.task("tkcopy", "Copy", hkOwner) |
| 71 | r.s.authority.Audit = func(*gorm.DB, string, string, string) error { return nil } |
| 72 | r.must(r.s.SetCopyDelegation(r.ctx, hkOwner, "p1", true, 0)) |
| 73 | r.must(r.s.db.G.Create(©ReviewRecord{ID: "review", ProjectID: "p1", TaskID: "tkcopy", Owner: hkOwner, Key: "save", Text: "Save", Hash: copydesk.Hash("Save"), Delegation: 1}).Error) |
| 74 | if r.s.EditCopyWording(r.ctx, hkMember, "review", "Save changes", "") == nil { |
| 75 | t.Fatal("another owner edited delegated copy") |
| 76 | } |
| 77 | var n int64 |
| 78 | r.must(r.s.db.G.Model(©FeedbackRecord{}).Count(&n).Error) |
| 79 | if n != 0 { |
| 80 | t.Fatal("rejected owner edit taught Copy") |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | func TestCopyProtocolFenceHoldsQueued(t *testing.T) { |
| 85 | r, _ := rosterRig(t) |
| 86 | r.task("tkcopy", "Copy", hkOwner) |
| 87 | r.must(r.s.db.G.Create(&stepRecord{ID: "copy", TaskID: "tkcopy", Idx: 1, Person: hkOwner, Specialist: wire.SpecialistCopy, RunnerID: "RUNNER1", State: wire.StepQueued, Attempt: 1}).Error) |
| 88 | r.must(r.tx(func(tx *txn) error { |
| 89 | return tx.fenceCopy(Lease{Protocol: wire.ProtocolCopy - 1, Runner: RunnerPrincipal{Person: hkOwner, RunnerID: "RUNNER1"}}) |
| 90 | })) |
| 91 | var step stepRecord |
| 92 | r.must(r.s.db.G.Where("id='copy'").First(&step).Error) |
| 93 | if step.State != wire.StepQueued || step.Placement != wire.PlacementWaiting || step.Terminated || step.Reason != "copy_update_required" { |
| 94 | t.Fatalf("old peer did not hold: %+v", step) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | func TestCopyAttemptsPreserveBudgetAcrossRetries(t *testing.T) { |
| 99 | r, _ := rosterRig(t) |
| 100 | r.must(r.s.db.G.Create(©AttemptRecord{ID: "s/1", TaskID: "tk", StepID: "s", Attempt: 1, Tokens: 10000, Known: true}).Error) |
| 101 | r.must(r.s.db.G.Create(©AttemptRecord{ID: "s/2", TaskID: "tk", StepID: "s", Attempt: 2, Tokens: 8000, Known: true}).Error) |
| 102 | r.must(r.tx(func(tx *txn) error { |
| 103 | left, known, err := tx.copyBudget("tk") |
| 104 | if err == nil && (!known || left != copydesk.TaskTokens-18000) { |
| 105 | t.Fatalf("retry replenished allowance: %d %v", left, known) |
| 106 | } |
| 107 | return err |
| 108 | })) |
| 109 | r.exec("UPDATE chat_copy_attempts SET known=0 WHERE id='s/2'") |
| 110 | r.must(r.tx(func(tx *txn) error { |
| 111 | _, known, err := tx.copyBudget("tk") |
| 112 | if err == nil && !known { |
| 113 | t.Fatal("unknown usage was not charged as a pass") |
| 114 | } |
| 115 | return err |
| 116 | })) |
| 117 | } |
| 118 | |
| 119 | func TestCopyMergeRequiresServerReviewRecord(t *testing.T) { |
| 120 | r, _ := rosterRig(t) |
| 121 | k := r.task("tkcopy", "Copy", hkOwner) |
| 122 | r.s.authority.Audit = func(*gorm.DB, string, string, string) error { return nil } |
| 123 | r.must(r.s.SetCopyDelegation(r.ctx, hkOwner, "p1", true, 0)) |
| 124 | result := wire.CopyResult{Owner: hkOwner, Delegation: 1, Input: "eng", UsageKnown: true, Extraction: copydesk.Extraction{Version: 1, Strings: []copydesk.Literal{{Key: "save", Text: "Save"}}}, Review: copydesk.Review{Version: 1, Strings: []copydesk.Wording{{Key: "save", Text: "Save", Action: "accept"}}}} |
| 125 | raw, _ := json.Marshal(result) |
| 126 | r.must(r.s.db.G.Create(&stepRecord{ID: "copy", TaskID: k.ID, Idx: 1, Specialist: wire.SpecialistCopy, CopyOwner: hkOwner, CopyRevision: 1, State: wire.StepDone, Pushed: true, CandidateSHA: "head", CopyResult: string(raw), Attempt: 1}).Error) |
| 127 | check := func(want bool) { |
| 128 | t.Helper() |
| 129 | r.must(r.tx(func(tx *txn) error { |
| 130 | ready, err := tx.copyReady(k, "head") |
| 131 | if err == nil && ready != want { |
| 132 | t.Fatalf("Copy merge readiness %v, want %v", ready, want) |
| 133 | } |
| 134 | return err |
| 135 | })) |
| 136 | } |
| 137 | check(false) |
| 138 | r.must(r.s.db.G.Create(©ReviewRecord{ID: "review", TaskID: k.ID, StepID: "copy", Attempt: 1, Owner: hkOwner, Delegation: 1, Candidate: "head", Key: "save", Hash: copydesk.Hash("Save")}).Error) |
| 139 | check(true) |
| 140 | r.exec("UPDATE chat_copy_reviews SET hash='forged'") |
| 141 | check(false) |
| 142 | } |
| 143 | |
| 144 | func TestCopyLearningDefersWithoutForgetting(t *testing.T) { |
| 145 | r, _ := rosterRig(t) |
| 146 | r.task("tkcopy", "Copy", hkOwner) |
| 147 | for _, row := range []copyFeedbackRecord{{ID: "old", ProjectID: "p1", Owner: hkOwner, Key: "old", Before: "Old", After: "Known", Learned: true}, {ID: "new", ProjectID: "p1", Owner: hkOwner, Key: "new", Before: "New", After: "Pending"}, {ID: "other", ProjectID: "p1", Owner: hkMember, Key: "other", Before: "Other", After: "Private", Learned: true}} { |
| 148 | r.must(r.s.db.G.Create(&row).Error) |
| 149 | } |
| 150 | r.must(r.s.db.G.Create(&hkRecord{ID: "spent", Team: "TEAM1", Person: hkOwner, State: wire.ActionDone, ReservedTurns: 4, ClaimedAt: &r.now, ChargedTurns: 4, ChargedComplete: true, At: r.now}).Error) |
| 151 | r.must(r.tx(func(tx *txn) error { |
| 152 | notes, err := tx.copyNotes("p1", hkOwner) |
| 153 | if err == nil && (len(notes) != 1 || notes[0].Key != "old") { |
| 154 | t.Fatalf("deferred learning lost old notes or used pending ones: %+v", notes) |
| 155 | } |
| 156 | return err |
| 157 | })) |
| 158 | r.now = r.now.Add(24 * time.Hour) |
| 159 | r.must(r.tx(func(tx *txn) error { |
| 160 | notes, err := tx.copyNotes("p1", hkOwner) |
| 161 | if err == nil && len(notes) != 2 { |
| 162 | t.Fatalf("learning did not resume: %+v", notes) |
| 163 | } |
| 164 | return err |
| 165 | })) |
| 166 | } |
| 167 | |
| 168 | func TestCopyUsageReportsCannotReplenishAllowance(t *testing.T) { |
| 169 | r, _ := rosterRig(t) |
| 170 | k := taskRecord{ID: "tk"} |
| 171 | step := stepRecord{ID: "copy", Attempt: 1, CopyOwner: hkOwner, CopyRevision: 1, CandidateSHA: "head"} |
| 172 | for _, n := range []int64{10000, 0, 8000} { |
| 173 | r.must(r.tx(func(tx *txn) error { |
| 174 | return tx.copyUsageReport(k, step, &wire.StepReport{Copy: &wire.CopyResult{Owner: hkOwner, Delegation: 1, Input: "head", Tokens: n, UsageKnown: true}}) |
| 175 | })) |
| 176 | } |
| 177 | r.must(r.tx(func(tx *txn) error { |
| 178 | left, known, err := tx.copyBudget(k.ID) |
| 179 | if err == nil && (!known || left != copydesk.TaskTokens-10000) { |
| 180 | t.Fatalf("later report replenished allowance: %d %v", left, known) |
| 181 | } |
| 182 | return err |
| 183 | })) |
| 184 | } |
| 185 | |