| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "gorm.io/gorm" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/wire" |
| 11 | ) |
| 12 | |
| 13 | func TestAccessResumeOnCatalogConnection(t *testing.T) { |
| 14 | for _, target := range []string{"carlos/platform", "amadan.net/carlos/platform"} { |
| 15 | for _, reconnect := range []bool{false, true} { |
| 16 | t.Run(target+map[bool]string{false: "/heartbeat", true: "/reconnect"}[reconnect], func(t *testing.T) { |
| 17 | r, turn := accessRig(t, "Fix the build in carlos/platform") |
| 18 | accessRunnerAuthority(r) |
| 19 | r.s.authority.Copy = func(key string) string { |
| 20 | if key == "access.continue" { |
| 21 | return "Access to %s was approved. Continue: %s" |
| 22 | } |
| 23 | return key |
| 24 | } |
| 25 | c := proposeAccess(t, r, turn, target) |
| 26 | ctx := context.Background() |
| 27 | if _, _, err := r.s.Answer(ctx, hkOwner, c.ID, wire.AnswerCard{Choice: "confirm", Revision: c.Revision}); err != nil { |
| 28 | t.Fatal(err) |
| 29 | } |
| 30 | if err := r.s.db.G.Model(&turnRecord{}).Where("id = ?", turn.ID).Update("state", wire.TurnReplied).Error; err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | lease := Lease{Runner: RunnerPrincipal{RunnerID: "RUNNER1", Person: hkOwner}, Protocol: wire.ProtocolCurrent} |
| 34 | report := wire.AccessReport{CardID: c.ID, Person: hkOwner, RequestID: "ar_one", State: "approved", ApproveURL: "https://amadan.net/settings/agents/requests/ar_one"} |
| 35 | if err := r.s.Apply(ctx, lease, wire.Frame{ID: "approved", Type: wire.FrameAccess}, &report); err != nil { |
| 36 | t.Fatal(err) |
| 37 | } |
| 38 | var before cardRecord |
| 39 | r.s.db.G.First(&before, "id = ?", c.ID) |
| 40 | if before.State != wire.CardOpen { |
| 41 | t.Fatal("resumed without verified catalog") |
| 42 | } |
| 43 | h := wire.Hello{Protocol: wire.ProtocolCurrent, Repos: []string{"amadan.net/carlos/platform"}, Catalog: []wire.RepoInfo{{Repo: "amadan.net/carlos/platform"}}} |
| 44 | var resolvedRevision int64 |
| 45 | for i := 0; i < 2; i++ { |
| 46 | var err error |
| 47 | if reconnect { |
| 48 | lease, err = r.s.Connect(ctx, lease.Runner, "after-deploy", h, "") |
| 49 | } else { |
| 50 | err = r.s.Heartbeat(ctx, lease, h) |
| 51 | } |
| 52 | if err != nil { |
| 53 | t.Fatal(err) |
| 54 | } |
| 55 | var resolved cardRecord |
| 56 | r.s.db.G.First(&resolved, "id = ?", c.ID) |
| 57 | if i == 0 { |
| 58 | resolvedRevision = resolved.Revision |
| 59 | } else if resolved.Revision != resolvedRevision { |
| 60 | t.Fatal("catch-up resolved twice") |
| 61 | } |
| 62 | } |
| 63 | var messages []messageRecord |
| 64 | r.s.db.G.Where("idempotency_key = ?", "access-approved:"+c.ID).Find(&messages) |
| 65 | if len(messages) != 1 || !strings.Contains(messages[0].Text, "Fix the build") { |
| 66 | t.Fatalf("continuations: %+v", messages) |
| 67 | } |
| 68 | r.s.db.G.First(&c, "id = ?", c.ID) |
| 69 | if c.State != wire.CardResolved { |
| 70 | t.Fatal("approved card remains open") |
| 71 | } |
| 72 | ps, err := r.s.CardPresentations(ctx, hkOwner, []string{c.ID}) |
| 73 | if err != nil { |
| 74 | t.Fatal(err) |
| 75 | } |
| 76 | if len(ps[c.ID].Turns) != 0 || len(ps[c.ID].OwnMessages) != 1 { |
| 77 | t.Fatal("continuation hidden inside resolved card", ps[c.ID]) |
| 78 | } |
| 79 | var commands int64 |
| 80 | r.s.db.G.Model(&commandRecord{}).Where("kind = ?", wire.FrameMessage).Count(&commands) |
| 81 | if commands != 1 { |
| 82 | t.Fatal("resume dispatch count", commands) |
| 83 | } |
| 84 | }) |
| 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | func TestAccessApprovalOpenedPersists(t *testing.T) { |
| 90 | r, turn := accessRig(t, "Fix carlos/platform") |
| 91 | accessRunnerAuthority(r) |
| 92 | c := proposeAccess(t, r, turn, "carlos/platform") |
| 93 | ctx := context.Background() |
| 94 | if _, _, err := r.s.Answer(ctx, hkOwner, c.ID, wire.AnswerCard{Choice: "confirm", Revision: c.Revision}); err != nil { |
| 95 | t.Fatal(err) |
| 96 | } |
| 97 | lease := Lease{Runner: RunnerPrincipal{RunnerID: "RUNNER1", Person: hkOwner}, Protocol: wire.ProtocolCurrent} |
| 98 | report := wire.AccessReport{CardID: c.ID, Person: hkOwner, RequestID: "ar_one", State: "pending", ApproveURL: "https://amadan.net/settings/agents/requests/ar_one"} |
| 99 | if err := r.s.Apply(ctx, lease, wire.Frame{ID: "pending", Type: wire.FrameAccess}, &report); err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | r.s.db.G.First(&c, "id = ?", c.ID) |
| 103 | if _, _, err := r.s.Answer(ctx, hkOwner, c.ID, wire.AnswerCard{Choice: "open_access", Revision: c.Revision}); err != nil { |
| 104 | t.Fatal("opening approval", err) |
| 105 | } |
| 106 | // A repeated runner poll must preserve the person's saved action. |
| 107 | if err := r.s.Apply(ctx, lease, wire.Frame{ID: "pending-again", Type: wire.FrameAccess}, &report); err != nil { |
| 108 | t.Fatal(err) |
| 109 | } |
| 110 | var saved cardRecord |
| 111 | r.s.db.G.First(&saved, "id = ?", c.ID) |
| 112 | var a map[string]any |
| 113 | json.Unmarshal([]byte(saved.Access), &a) |
| 114 | if a["approval_opened"] != true { |
| 115 | t.Fatal("busy state not persisted", a) |
| 116 | } |
| 117 | r.removed = true |
| 118 | if _, _, err := r.s.Answer(ctx, hkOwner, c.ID, wire.AnswerCard{Choice: "open_access", Revision: saved.Revision}); err == nil { |
| 119 | t.Fatal("removed person changed card") |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | func accessRunnerAuthority(r *meteredRig) { |
| 124 | r.s.db.G.Exec("CREATE TABLE runners (id text, person text, role text, label text, revoked_at datetime, removing_at datetime, tainted_at datetime)") |
| 125 | r.s.authority.CurrentRunner = func(*gorm.DB, RunnerPrincipal) error { return nil } |
| 126 | r.s.authority.RunnerRole = func(*gorm.DB, string) (string, error) { return wire.RolePrimary, nil } |
| 127 | } |
| 128 | |
| 129 | func TestCardNamesAccessResumeJoinsWaitingBatch(t *testing.T) { |
| 130 | r, turn := accessRig(t, "Fix the build in carlos/platform") |
| 131 | accessRunnerAuthority(r) |
| 132 | if err := r.s.db.G.Model(&turnRecord{}).Where("id = ?", turn.ID).Update("dispatched", true).Error; err != nil { |
| 133 | t.Fatal(err) |
| 134 | } |
| 135 | c := proposeAccess(t, r, turn, "carlos/platform") |
| 136 | ctx := context.Background() |
| 137 | if _, _, err := r.s.Answer(ctx, hkOwner, c.ID, wire.AnswerCard{Choice: "confirm", Revision: c.Revision}); err != nil { |
| 138 | t.Fatal(err) |
| 139 | } |
| 140 | queued, _, err := r.s.Send(ctx, hkOwner, wire.SendMessage{Text: "Check the other build too", IdempotencyKey: "waiting"}) |
| 141 | if err != nil { |
| 142 | t.Fatal(err) |
| 143 | } |
| 144 | lease := Lease{Runner: RunnerPrincipal{RunnerID: "RUNNER1", Person: hkOwner}, Protocol: wire.ProtocolCurrent} |
| 145 | report := wire.AccessReport{CardID: c.ID, Person: hkOwner, RequestID: "ar_one", State: "approved", ApproveURL: "https://amadan.net/settings/agents/requests/ar_one"} |
| 146 | if err := r.s.Apply(ctx, lease, wire.Frame{ID: "approved", Type: wire.FrameAccess}, &report); err != nil { |
| 147 | t.Fatal(err) |
| 148 | } |
| 149 | h := wire.Hello{Protocol: wire.ProtocolCurrent, Repos: []string{"amadan.net/carlos/platform"}, Catalog: []wire.RepoInfo{{Repo: "amadan.net/carlos/platform"}}} |
| 150 | if err := r.s.Heartbeat(ctx, lease, h); err != nil { |
| 151 | t.Fatal(err) |
| 152 | } |
| 153 | var m messageRecord |
| 154 | if err := r.s.db.G.First(&m, "idempotency_key = ?", "access-approved:"+c.ID).Error; err != nil { |
| 155 | t.Fatal(err) |
| 156 | } |
| 157 | if m.TurnID != queued.Turn.ID { |
| 158 | t.Fatal("access continuation forced fresh turn", m.TurnID, queued.Turn.ID) |
| 159 | } |
| 160 | } |
| 161 | |