| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "gorm.io/gorm" |
| 5 | "strings" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestCopyRecoveryRequiresOwnerAndIsOneOff(t *testing.T) { |
| 10 | r, _ := rosterRig(t) |
| 11 | r.task("tkcopy", "Copy", hkOwner) |
| 12 | r.s.authority.Audit = func(*gorm.DB, string, string, string) error { return nil } |
| 13 | if r.s.RecoverCopy(r.ctx, hkOwner, "tkcopy", strings.Repeat("a", 40), strings.Repeat("b", 40)) == nil { |
| 14 | t.Fatal("undelegated recovery") |
| 15 | } |
| 16 | r.must(r.s.SetCopyDelegation(r.ctx, hkOwner, "p1", true, 0)) |
| 17 | if r.s.RecoverCopy(r.ctx, hkMember, "tkcopy", strings.Repeat("a", 40), strings.Repeat("b", 40)) == nil { |
| 18 | t.Fatal("another owner recovered delegation") |
| 19 | } |
| 20 | r.exec("UPDATE chat_tasks SET stage='in_review',base_sha=?,repair_round=2 WHERE id='tkcopy'", strings.Repeat("b", 40)) |
| 21 | r.exec("INSERT INTO chat_steps(id,task_id,idx,specialist,person,state,pushed,candidate_sha,round) VALUES ('eng','tkcopy',1,'engineering',?,'done',1,?,2)", hkOwner, strings.Repeat("a", 40)) |
| 22 | r.exec("INSERT INTO chat_steps(id,task_id,idx,specialist,person,state,checks,candidate_sha) VALUES ('checks','tkcopy',2,'checks',?,'done','failed',?)", hkOwner, strings.Repeat("a", 40)) |
| 23 | r.must(r.s.RecoverCopy(r.ctx, hkOwner, "tkcopy", strings.Repeat("a", 40), strings.Repeat("b", 40))) |
| 24 | r.must(r.s.RecoverCopy(r.ctx, hkOwner, "tkcopy", strings.Repeat("a", 40), strings.Repeat("b", 40))) |
| 25 | var n int64 |
| 26 | r.must(r.s.db.G.Model(&stepRecord{}).Where("specialist='copy'").Count(&n).Error) |
| 27 | if n != 1 { |
| 28 | t.Fatal("recovery repeated", n) |
| 29 | } |
| 30 | r.must(r.s.db.G.Model(&hkRecord{}).Count(&n).Error) |
| 31 | if n != 0 { |
| 32 | t.Fatal("owner recovery consumed daily allowance") |
| 33 | } |
| 34 | r.exec("UPDATE chat_steps SET state='failed',terminated=1 WHERE specialist='copy'") |
| 35 | r.must(r.s.RecoverCopy(r.ctx, hkOwner, "tkcopy", strings.Repeat("a", 40), strings.Repeat("b", 40))) |
| 36 | r.must(r.s.db.G.Model(&stepRecord{}).Where("specialist='copy'").Count(&n).Error) |
| 37 | if n != 2 { |
| 38 | t.Fatal("failed recovery could not retry", n) |
| 39 | } |
| 40 | |
| 41 | } |
| 42 | |