oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package headless
2
3import (
4 "context"
5 "database/sql"
6 "encoding/json"
7 "strings"
8 "sync/atomic"
9 "testing"
10 "time"
11
12 "amadan.net/oficina/equipo/internal/runner/thin"
13 "amadan.net/oficina/equipo/internal/runner/workspace"
14 "amadan.net/oficina/equipo/internal/thintest"
15 "amadan.net/oficina/equipo/internal/wire"
16)
17
18// The real runner polls approval, refreshes reach and sends a live hello while
19// git for the new repository is blocked. The real server resumes exactly once.
20func TestAccessResumeBeforeGit(t *testing.T) {
21 env, inst, r := localEnv(t)
22 ctx := context.Background()
23 sent, err := env.Client.Send(ctx, "Fix the build in carlos/platform", "original-access")
24 if err != nil {
25 t.Fatal(err)
26 }
27 now := time.Now().UTC()
28 access := wire.AccessRequest{Target: "carlos/platform", Role: "agent", Reason: "Fix the build.", State: "pending", RequestID: "ar_live", ApproveURL: "https://amadan.net/settings/agents/requests/ar_live"}
29 raw, _ := json.Marshal(access)
30 if err := inst.DB.G.Exec("UPDATE chat_turns SET state='replied' WHERE id=?", sent.Turn.ID).Error; err != nil {
31 t.Fatal(err)
32 }
33 if err := inst.DB.G.Exec("DELETE FROM runner_commands WHERE kind=?", wire.FrameMessage).Error; err != nil {
34 t.Fatal(err)
35 }
36 if err := inst.DB.G.Exec("INSERT INTO chat_cards(id,person,kind,state,turn_id,name,access,options,revision,created_at,updated_at) VALUES(?,?,?,?,?,?,?,?,?,?,?)", "c_live", thintest.Owner, wire.CardAccess, wire.CardOpen, sent.Turn.ID, "carlos/platform", string(raw), "[]", 1, now, now).Error; err != nil {
37 t.Fatal(err)
38 }
39 state, err := thin.OpenState(ctx, r.cfg.StateDir)
40 if err != nil {
41 t.Fatal(err)
42 }
43 cmd := wire.AccessCommand{CardID: "c_live", Person: thintest.Owner, Target: access.Target, Role: access.Role, Reason: access.Reason, Confirmed: true}
44 if err = state.AcceptAccess(ctx, "confirmed", cmd); err != nil {
45 t.Fatal(err)
46 }
47 state.Close()
48 db, err := sql.Open("sqlite", r.cfg.StateDir+"/thin.db")
49 if err != nil {
50 t.Fatal(err)
51 }
52 report := wire.AccessReport{CardID: cmd.CardID, Person: cmd.Person, RequestID: access.RequestID, State: "pending", ApproveURL: access.ApproveURL}
53 raw, _ = json.Marshal(report)
54 if _, err = db.Exec("UPDATE access_requests SET state='pending',report=?", string(raw)); err != nil {
55 db.Close()
56 t.Fatal(err)
57 }
58 db.Close()
59 f := &accessResumeForge{localForge: r.opts.Forge.(*localForge), report: report}
60 r.opts.Forge = f
61 r.start(t)
62 start := time.Now()
63 f.approved.Store(true)
64 for {
65 var state string
66 if err := inst.DB.G.Raw("SELECT state FROM chat_cards WHERE id='c_live'").Scan(&state).Error; err != nil {
67 t.Fatal(err)
68 }
69 if state == wire.CardResolved {
70 break
71 }
72 if time.Since(start) > 5*time.Second {
73 t.Fatal("approved request still waits for git")
74 }
75 time.Sleep(20 * time.Millisecond)
76 }
77 var messages []struct{ Text string }
78 if err := inst.DB.G.Raw("SELECT text FROM chat_messages WHERE idempotency_key='access-approved:c_live'").Scan(&messages).Error; err != nil {
79 t.Fatal(err)
80 }
81 if len(messages) != 1 || !strings.Contains(messages[0].Text, "Fix the build in carlos/platform") {
82 t.Fatal("original request not continued exactly once")
83 }
84 r.stop(t)
85 r.start(t)
86 var n int64
87 inst.DB.G.Table("chat_messages").Where("idempotency_key = ?", "access-approved:c_live").Count(&n)
88 if n != 1 {
89 t.Fatal("reconnect duplicated continuation", n)
90 }
91}
92
93type accessResumeForge struct {
94 *localForge
95 approved atomic.Bool
96 report wire.AccessReport
97}
98
99func (f *accessResumeForge) RequestAccess(context.Context, wire.AccessCommand) (wire.AccessReport, error) {
100 return f.report, nil
101}
102func (f *accessResumeForge) PollAccess(ctx context.Context, _ string, _ wire.AccessCommand) (wire.AccessReport, error) {
103 for !f.approved.Load() {
104 select {
105 case <-ctx.Done():
106 return wire.AccessReport{}, ctx.Err()
107 case <-time.After(10 * time.Millisecond):
108 }
109 }
110 v := f.report
111 v.State = "approved"
112 return v, nil
113}
114func (f *accessResumeForge) Reachable(ctx context.Context) []string {
115 out := f.localForge.Reachable(ctx)
116 if f.approved.Load() {
117 out = append(out, "amadan.net/carlos/platform")
118 }
119 return out
120}
121func (f *accessResumeForge) Remote(ctx context.Context, repo string, use thin.Use) (workspace.Remote, error) {
122 if repo == "amadan.net/carlos/platform" {
123 <-ctx.Done()
124 return workspace.Remote{}, ctx.Err()
125 }
126 return f.localForge.Remote(ctx, repo, use)
127}
128