| 1 | package chat |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "io" |
| 6 | "log/slog" |
| 7 | "os" |
| 8 | "path/filepath" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | "amadan.net/rastrillo/rastrillo/db" |
| 14 | ) |
| 15 | |
| 16 | // TestSupersedePlacesOnlyTheirOwn (Astra on 8c5c1fbf): a person's request |
| 17 | // supersedes only their own place cards for the same project. Another |
| 18 | // person's card, or one for another project, handed to supersedePlaces |
| 19 | // stays open at its revision; standingCard never offers them as stale. |
| 20 | func TestSupersedePlacesOnlyTheirOwn(t *testing.T) { |
| 21 | src, err := hkTemplatePath() |
| 22 | if err != nil { |
| 23 | t.Fatal(err) |
| 24 | } |
| 25 | raw, err := os.ReadFile(src) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | path := filepath.Join(t.TempDir(), "supersede.db") |
| 30 | if err := os.WriteFile(path, raw, 0o600); err != nil { |
| 31 | t.Fatal(err) |
| 32 | } |
| 33 | d, err := db.Open(path, slog.New(slog.NewTextHandler(io.Discard, nil))) |
| 34 | if err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | t.Cleanup(func() { _ = d.Close() }) |
| 38 | s := NewStore(d, Authority{Copy: func(key string) string { return key }}, nil) |
| 39 | const bob, alice = "bob@example.com", "alice@example.com" |
| 40 | old := time.Now().UTC().Add(-time.Hour) |
| 41 | card := func(id, person, project string) cardRecord { |
| 42 | return cardRecord{ID: id, Person: person, Kind: wire.CardPlace, State: wire.CardOpen, ProjectID: project, Options: "[]", Revision: 3, CreatedAt: old, UpdatedAt: old} |
| 43 | } |
| 44 | for _, id := range []string{"p1", "p2"} { |
| 45 | if err := d.G.Create(&projectRecord{ID: id, Repo: "amadan.net/oficina/" + id, State: wire.ProjectActive, CreatedAt: old, UpdatedAt: old}).Error; err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
| 48 | } |
| 49 | cards := []cardRecord{card("c_alice", alice, "p1"), card("c_bobold", bob, "p1"), card("c_bobother", bob, "p2")} |
| 50 | for _, c := range cards { |
| 51 | if err := d.G.Create(&c).Error; err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | } |
| 55 | since := time.Now().UTC() |
| 56 | stands, stale := standingCard(cards[:2], bob, wire.LaneCoordinator, &since) |
| 57 | if stands != nil || len(stale) != 1 || stale[0].ID != "c_bobold" { |
| 58 | t.Fatalf("stale %+v", stale) |
| 59 | } |
| 60 | by := card("c_bobnew", bob, "p1") |
| 61 | if err := s.tx(context.Background(), func(t *txn) error { return t.supersedePlaces("coordinator:"+bob, cards, by) }); err != nil { |
| 62 | t.Fatal(err) |
| 63 | } |
| 64 | want := map[string]string{"c_alice": "open/3", "c_bobold": "resolved/4", "c_bobother": "open/3"} |
| 65 | for id, w := range want { |
| 66 | var c cardRecord |
| 67 | if err := d.G.Where("id = ?", id).First(&c).Error; err != nil { |
| 68 | t.Fatal(err) |
| 69 | } |
| 70 | if got := c.State + "/" + itoa64(c.Revision); got != w { |
| 71 | t.Fatalf("%s: %s, want %s", id, got, w) |
| 72 | } |
| 73 | } |
| 74 | } |
| 75 | |