| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "io" |
| 6 | "log/slog" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/equipo/mergegate" |
| 14 | "amadan.net/oficina/equipo/internal/wire" |
| 15 | "amadan.net/rastrillo/rastrillo/db" |
| 16 | ) |
| 17 | |
| 18 | // TestOutcomeMalformedRecords: an Assistant card is never made from |
| 19 | // stored data it cannot read. A team card's options, the Assistant |
| 20 | // card's own options or the merge decision's reasons that do not decode |
| 21 | // fail the change, rather than becoming missing choices or a go-ahead |
| 22 | // question without its reason. |
| 23 | func TestOutcomeMalformedRecords(t *testing.T) { |
| 24 | src, err := hkTemplatePath() |
| 25 | if err != nil { |
| 26 | t.Fatal(err) |
| 27 | } |
| 28 | raw, err := os.ReadFile(src) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | path := filepath.Join(t.TempDir(), "outcome.db") |
| 33 | if err := os.WriteFile(path, raw, 0o600); err != nil { |
| 34 | t.Fatal(err) |
| 35 | } |
| 36 | d, err := db.Open(path, slog.New(slog.NewTextHandler(io.Discard, nil))) |
| 37 | if err != nil { |
| 38 | t.Fatal(err) |
| 39 | } |
| 40 | t.Cleanup(func() { _ = d.Close() }) |
| 41 | s := NewStore(d, Authority{Copy: func(key string) string { return key }}, nil) |
| 42 | const person = "owner@example.com" |
| 43 | sha := strings.Repeat("2", 40) |
| 44 | now := time.Now().UTC() |
| 45 | if err := d.G.Create(&taskRecord{ID: "tk1", Creator: person, Stage: wire.StageReadyToMerge, MergeAsked: sha, Revision: 1, CreatedAt: now, UpdatedAt: now}).Error; err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | reasons := func(raw string) { |
| 49 | t.Helper() |
| 50 | if err := d.G.Exec("DELETE FROM chat_merge_audit").Error; err != nil { |
| 51 | t.Fatal(err) |
| 52 | } |
| 53 | if err := d.G.Create(&mergeAuditRecord{ID: "ma1", TaskID: "tk1", Kind: auditDecision, At: atDispatch, CandidateSHA: sha, Class: mergegate.ClassAsk, Reasons: raw, Inputs: "{}", CreatedAt: now}).Error; err != nil { |
| 54 | t.Fatal(err) |
| 55 | } |
| 56 | } |
| 57 | good := `[{"name":"approved_exact_sha","ok":true},{"name":"environment_reason","ok":false}]` |
| 58 | cases := map[string]struct { |
| 59 | src, mirror cardRecord |
| 60 | reasons string |
| 61 | }{ |
| 62 | "question options": {src: cardRecord{ID: "c1", Person: person, Kind: wire.CardQuestion, State: wire.CardOpen, Question: "Which page?", Options: `{"Web"`}, reasons: good}, |
| 63 | "merge options": {src: cardRecord{ID: "c2", Person: person, Kind: wire.CardMerge, State: wire.CardOpen, TaskID: "tk1", Options: `["merge"`}, reasons: good}, |
| 64 | "merge reasons": {src: cardRecord{ID: "c3", Person: person, Kind: wire.CardMerge, State: wire.CardOpen, TaskID: "tk1", Options: `["merge","wait"]`}, reasons: `[{"name":`}, |
| 65 | "its own options": {src: cardRecord{ID: "c4", Person: person, Kind: wire.CardMerge, State: wire.CardAnswered, Choice: wire.ChoiceMerge, TaskID: "tk1", Options: `["merge","wait"]`}, mirror: cardRecord{Options: `[{"value":"merge"`}, reasons: good}, |
| 66 | } |
| 67 | for name, c := range cases { |
| 68 | reasons(c.reasons) |
| 69 | err := s.tx(context.Background(), func(t *txn) error { |
| 70 | m := c.mirror |
| 71 | return t.fillMirror(&m, c.src) |
| 72 | }) |
| 73 | if err == nil { |
| 74 | t.Errorf("%s: a malformed record made a card", name) |
| 75 | } |
| 76 | } |
| 77 | // Well formed, the same records make the card. |
| 78 | reasons(good) |
| 79 | var m cardRecord |
| 80 | if err := s.tx(context.Background(), func(t *txn) error { |
| 81 | return t.fillMirror(&m, cardRecord{ID: "c5", Person: person, Kind: wire.CardMerge, State: wire.CardOpen, TaskID: "tk1", Options: `["merge","wait"]`}) |
| 82 | }); err != nil || !strings.HasPrefix(m.Question, "assistant.outcome.merge") || !strings.Contains(m.Options, `"value":"wait"`) { |
| 83 | t.Fatalf("well formed %+v %v", m, err) |
| 84 | } |
| 85 | } |
| 86 | |