|
| 1 | +package sqlite |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +// A check can change status on the same commit (in_progress -> failed) via |
| 11 | +// UpsertPRCheck's ON CONFLICT DO UPDATE. CDC must emit on that transition, not |
| 12 | +// only on the first insert — otherwise live clients never see the status change. |
| 13 | +func TestPRChecksCDC_EmitsOnInsertAndStatusUpdate(t *testing.T) { |
| 14 | + s := newTestStore(t) |
| 15 | + ctx := context.Background() |
| 16 | + seedProject(t, s, "mer") |
| 17 | + rec, err := s.CreateSession(ctx, sampleRecord("mer")) |
| 18 | + if err != nil { |
| 19 | + t.Fatal(err) |
| 20 | + } |
| 21 | + url := "https://example/pr/1" |
| 22 | + if err := s.UpsertPR(ctx, PRRow{URL: url, SessionID: string(rec.ID), Number: 1}); err != nil { |
| 23 | + t.Fatal(err) |
| 24 | + } |
| 25 | + |
| 26 | + now := time.Now() |
| 27 | + mustCheck := func(status string) { |
| 28 | + if err := s.RecordCheck(ctx, PRCheckRow{PRURL: url, Name: "build", CommitHash: "c1", Status: status, CreatedAt: now}); err != nil { |
| 29 | + t.Fatal(err) |
| 30 | + } |
| 31 | + } |
| 32 | + mustCheck("in_progress") // insert -> event |
| 33 | + mustCheck("failed") // status change on same commit (update) -> event |
| 34 | + mustCheck("failed") // no-op re-poll (status unchanged) -> NO event |
| 35 | + |
| 36 | + rows, err := s.ReadChangeLogAfter(ctx, 0, 100) |
| 37 | + if err != nil { |
| 38 | + t.Fatal(err) |
| 39 | + } |
| 40 | + var checkEvents []ChangeLogRow |
| 41 | + for _, r := range rows { |
| 42 | + if r.EventType == "pr_check_recorded" { |
| 43 | + checkEvents = append(checkEvents, r) |
| 44 | + } |
| 45 | + } |
| 46 | + if len(checkEvents) != 2 { |
| 47 | + t.Fatalf("want 2 check CDC events (insert + status change, no-op suppressed), got %d", len(checkEvents)) |
| 48 | + } |
| 49 | + if !strings.Contains(checkEvents[1].Payload, `"status":"failed"`) { |
| 50 | + t.Fatalf("the update event should carry the new status, got %q", checkEvents[1].Payload) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +// WritePRObservation persists scalar facts, checks, and comments in one tx; all |
| 55 | +// three should be queryable afterward. |
| 56 | +func TestWritePRObservation_PersistsScalarsChecksAndComments(t *testing.T) { |
| 57 | + s := newTestStore(t) |
| 58 | + ctx := context.Background() |
| 59 | + seedProject(t, s, "mer") |
| 60 | + rec, err := s.CreateSession(ctx, sampleRecord("mer")) |
| 61 | + if err != nil { |
| 62 | + t.Fatal(err) |
| 63 | + } |
| 64 | + url := "https://example/pr/7" |
| 65 | + now := time.Now() |
| 66 | + |
| 67 | + err = s.WritePRObservation(ctx, |
| 68 | + PRRow{URL: url, SessionID: string(rec.ID), Number: 7, CIState: "failing", UpdatedAt: now}, |
| 69 | + []PRCheckRow{{PRURL: url, Name: "build", CommitHash: "c1", Status: "failed", CreatedAt: now}}, |
| 70 | + []PRCommentRow{{PRURL: url, CommentID: "1", Author: "reviewer", Body: "use a const", CreatedAt: now}}, |
| 71 | + ) |
| 72 | + if err != nil { |
| 73 | + t.Fatal(err) |
| 74 | + } |
| 75 | + |
| 76 | + pr, ok, err := s.GetPR(ctx, url) |
| 77 | + if err != nil || !ok || pr.CIState != "failing" { |
| 78 | + t.Fatalf("scalar facts not persisted: ok=%v ci=%q err=%v", ok, pr.CIState, err) |
| 79 | + } |
| 80 | + if checks, _ := s.ListChecks(ctx, url); len(checks) != 1 || checks[0].Status != "failed" { |
| 81 | + t.Fatalf("check not persisted: %+v", checks) |
| 82 | + } |
| 83 | + if comments, _ := s.ListPRComments(ctx, url); len(comments) != 1 || comments[0].Body != "use a const" { |
| 84 | + t.Fatalf("comment not persisted: %+v", comments) |
| 85 | + } |
| 86 | +} |
0 commit comments