oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package chat
2
3import (
4 "context"
5 "errors"
6 "strings"
7 "testing"
8 "time"
9
10 "amadan.net/oficina/equipo/internal/wire"
11)
12
13func TestReportThreadOwnershipContextAndIsolation(t *testing.T) {
14 r := newMeteredRig(t)
15 ctx := context.Background()
16 now := r.s.now()
17 must := func(err error) {
18 t.Helper()
19 if err != nil {
20 t.Fatal(err)
21 }
22 }
23 must(r.s.db.G.Create(&projectRecord{ID: "p_report", Title: "Woodstar", TitleSet: true, Repo: "amadan.net/oficina/sample"}).Error)
24 must(r.s.db.G.Create(&taskRecord{ID: "tk_report", ProjectID: "p_report", Title: "Improve sign-in", Creator: hkOwner, Stage: wire.StageInReview, CreatedAt: now, UpdatedAt: now}).Error)
25 c := cardRecord{ID: "c_report", Person: hkOwner, TaskID: "tk_report", Kind: wire.CardTask, State: wire.StageInReview, Options: "[]", Revision: 1, CreatedAt: now, UpdatedAt: now}
26 must(r.s.db.G.Create(&c).Error)
27 must(r.s.db.G.Create(&turnRecord{ID: "t_busy", Person: hkOwner, State: wire.TurnRunning, Dispatched: true, CreatedAt: now}).Error)
28 if err := r.s.ReplyCard(ctx, hkMember, c.ID, "other", "Other person"); !errors.Is(err, ErrNotFound) {
29 t.Fatalf("other person: %v", err)
30 }
31 r.removed = true
32 if err := r.s.ReplyCard(ctx, hkOwner, c.ID, "removed", "Removed member"); !errors.Is(err, ErrForbidden) {
33 t.Fatalf("removed member: %v", err)
34 }
35 r.removed = false
36 send := func(key string) string {
37 var id string
38 must(r.s.tx(ctx, func(tx *txn) error {
39 sent, _, err := tx.send("", hkOwner, key, key, nil, nil)
40 id = sent.Turn.ID
41 return err
42 }))
43 return id
44 }
45 before := send("main-before")
46 r.s.now = func() time.Time { return now.Add(time.Second) }
47 must(r.s.ReplyCard(ctx, hkOwner, c.ID, "reply", "Can we simplify it?"))
48 must(r.s.ReplyCard(ctx, hkOwner, c.ID, "reply", "Can we simplify it?"))
49 if err := r.s.ReplyCard(ctx, hkOwner, c.ID, "reply", "Changed retry"); !errors.Is(err, ErrConflict) {
50 t.Fatalf("idempotency: %v", err)
51 }
52 r.s.now = func() time.Time { return now.Add(2 * time.Second) }
53 after := send("main-after")
54 var links []cardThreadRecord
55 must(r.s.db.G.Find(&links).Error)
56 if len(links) != 1 || links[0].TurnID == before || links[0].TurnID == after {
57 t.Fatalf("thread batched or duplicated: %+v main %s %s", links, before, after)
58 }
59 var m messageRecord
60 must(r.s.db.G.First(&m, "id = ?", links[0].MessageID).Error)
61 if m.Origin != originComposer {
62 t.Fatalf("typed card reply lost composer provenance: %+v", m)
63 }
64 must(r.s.db.G.Model(&taskRecord{}).Where("id = ?", c.TaskID).Update("title", "New sign-in title").Error)
65 must(r.s.tx(ctx, func(tx *txn) error {
66 text, err := tx.answerPrefix(m)
67 for _, want := range []string{"tk_report", "New sign-in title", "Woodstar", "in_review", "Can we simplify it?"} {
68 if !strings.Contains(text, want) {
69 t.Errorf("context missing %q: %s", want, text)
70 }
71 }
72 return err
73 }))
74 must(r.s.db.G.Create(&messageRecord{ID: "m_report_reply", Person: hkOwner, Role: wire.RoleCoordinator, TurnID: links[0].TurnID, Text: "We can simplify the first step.", Seq: 100, CreatedAt: now}).Error)
75 ps, err := r.s.CardPresentations(ctx, hkOwner, []string{c.ID})
76 must(err)
77 if len(ps[c.ID].Thread) != 2 || ps[c.ID].Thread[1].Own {
78 t.Fatalf("thread persistence: %+v", ps)
79 }
80 other, err := r.s.CardPresentations(ctx, hkMember, []string{c.ID})
81 must(err)
82 if len(other) != 0 {
83 t.Fatalf("other person read thread: %+v", other)
84 }
85 r.removed = true
86 if _, err := r.s.CardPresentations(ctx, hkOwner, []string{c.ID}); !errors.Is(err, ErrForbidden) {
87 t.Fatalf("removed read: %v", err)
88 }
89}
90
91func TestReportNextStepStoreAuthority(t *testing.T) {
92 for _, action := range []string{"round", "merge"} {
93 t.Run(action, func(t *testing.T) {
94 r := newMeteredRig(t)
95 if err := r.s.db.G.Create(&taskRecord{ID: "tk_actions", Creator: hkOwner, Stage: wire.StageInReview}).Error; err != nil {
96 t.Fatal(err)
97 }
98 run := r.s.RoundTask
99 if action == "merge" {
100 run = r.s.MergeTask
101 }
102 if _, err := run(context.Background(), hkMember, "tk_actions"); !errors.Is(err, ErrForbidden) {
103 t.Fatalf("other creator: %v", err)
104 }
105 r.removed = true
106 if _, err := run(context.Background(), hkOwner, "tk_actions"); !errors.Is(err, ErrForbidden) {
107 t.Fatalf("removed owner: %v", err)
108 }
109 r.removed = false
110 if _, err := run(context.Background(), hkOwner, "tk_actions"); err == nil {
111 t.Fatal("unavailable action accepted")
112 }
113 })
114 }
115}
116