| 1 | package thin |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "fmt" |
| 7 | "net/http" |
| 8 | "sync/atomic" |
| 9 | "testing" |
| 10 | "time" |
| 11 | |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | ) |
| 14 | |
| 15 | func TestAccessGrantCatalogBeforeIndex(t *testing.T) { |
| 16 | for _, role := range []string{"agent", "reader"} { |
| 17 | t.Run(role, func(t *testing.T) { |
| 18 | ctx := context.Background() |
| 19 | var approved atomic.Bool |
| 20 | f := accessForge(t, func(w http.ResponseWriter, r *http.Request) { |
| 21 | switch { |
| 22 | case r.URL.Path == "/api/v1/repos": |
| 23 | if approved.Load() && role == "agent" { |
| 24 | fmt.Fprint(w, `{"repos":[{"ns":"carlos","name":"platform"}]}`) |
| 25 | } else { |
| 26 | fmt.Fprint(w, `{"repos":[]}`) |
| 27 | } |
| 28 | case r.URL.Path == "/api/v1/repos/carlos/platform": |
| 29 | if approved.Load() { |
| 30 | fmt.Fprint(w, `{"ns":"carlos","name":"platform","tier":"public"}`) |
| 31 | } else { |
| 32 | w.WriteHeader(404) |
| 33 | } |
| 34 | default: |
| 35 | v := accessResponse("ar_one", "approved") |
| 36 | v.Role = role |
| 37 | writeAccessInfo(w, v) |
| 38 | } |
| 39 | }) |
| 40 | state, err := OpenState(ctx, t.TempDir()) |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | defer state.Close() |
| 45 | f.accessTargets = state.accessReadTargets |
| 46 | cmd := accessCommand() |
| 47 | cmd.Role = role |
| 48 | if err = state.AcceptAccess(ctx, "cmd", cmd); err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | old := wire.AccessReport{CardID: cmd.CardID, Person: cmd.Person, RequestID: "ar_one", State: "pending", ApproveURL: "https://amadan.net/settings/agents/requests/ar_one"} |
| 52 | raw, _ := json.Marshal(old) |
| 53 | state.db.Exec("UPDATE access_requests SET state='pending',report=?", string(raw)) |
| 54 | index := &Index{Forge: f, catalog: []wire.RepoInfo{}} // No Clones: fetching cannot complete before the hello. |
| 55 | inst := newFakeInstance(t) |
| 56 | link, _, _, _ := startLinkWith(t, inst.url, &stubCoordinator{}, func() wire.Hello { |
| 57 | return wire.Hello{Repos: f.Repos(), Catalog: index.AccessCatalog(ctx, state), CreateIn: []string{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "test", "claude": "2.1.312"}} |
| 58 | }) |
| 59 | c := <-inst.conns |
| 60 | read(t, c, wire.FrameHello) |
| 61 | welcome, _ := json.Marshal(wire.Frame{ID: "welcome", Type: wire.FrameWelcome, Data: json.RawMessage(fmt.Sprintf(`{"generation":3,"runner_id":"RUNNER1","protocol":%d,"role":"primary"}`, wire.ProtocolCurrent))}) |
| 62 | write(t, c, welcome) |
| 63 | read(t, c, wire.FrameSync) |
| 64 | link.HelloSoon() |
| 65 | read(t, c, wire.FrameHello) |
| 66 | approved.Store(true) |
| 67 | // Exercise the production approval refresh, notification and live hello. |
| 68 | e := &AccessEngine{State: state, Forge: f, Refresh: func() { index.Kick(); link.HelloSoon() }} |
| 69 | start := time.Now() |
| 70 | if err = e.Step(ctx, cmd.CardID); err != nil { |
| 71 | t.Fatal(err) |
| 72 | } |
| 73 | frame, _ := read(t, c, wire.FrameHello) |
| 74 | var hello wire.Hello |
| 75 | if err = json.Unmarshal(frame.Data, &hello); err != nil { |
| 76 | t.Fatal(err) |
| 77 | } |
| 78 | if len(hello.Catalog) != 1 || hello.Catalog[0].Repo != "amadan.net/carlos/platform" { |
| 79 | t.Fatalf("grant held behind index: %+v", hello.Catalog) |
| 80 | } |
| 81 | if time.Since(start) > 2*time.Second { |
| 82 | t.Fatal("grant hello was not prompt") |
| 83 | } |
| 84 | if role == "reader" && len(hello.Repos) != 0 { |
| 85 | t.Fatal("read grant became writable") |
| 86 | } |
| 87 | approved.Store(false) |
| 88 | f.Recheck() |
| 89 | if got := index.AccessCatalog(ctx, state); len(got) != 0 { |
| 90 | t.Fatal("old approval supplied authority", got) |
| 91 | } |
| 92 | }) |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | func TestAccessResumeFoldsExplore(t *testing.T) { |
| 97 | ctx := context.Background() |
| 98 | state, err := OpenState(ctx, t.TempDir()) |
| 99 | if err != nil { |
| 100 | t.Fatal(err) |
| 101 | } |
| 102 | defer state.Close() |
| 103 | c := accessCommand() |
| 104 | if err = state.AcceptAccess(ctx, "grant", c); err != nil { |
| 105 | t.Fatal(err) |
| 106 | } |
| 107 | state.db.Exec("UPDATE access_requests SET state='approved'") |
| 108 | root := t.TempDir() |
| 109 | desk, err := OpenDesk(root, c.Person) |
| 110 | if err != nil { |
| 111 | t.Fatal(err) |
| 112 | } |
| 113 | if err = desk.SaveEntries([]wire.NotebookEntry{{ID: "n1", Source: "own", Text: "A settled desk."}}); err != nil { |
| 114 | t.Fatal(err) |
| 115 | } |
| 116 | if err = desk.SaveState(deskState{MovedIn: true, Namespaces: []string{"oficina"}}); err != nil { |
| 117 | t.Fatal(err) |
| 118 | } |
| 119 | index := &Index{catalog: []wire.RepoInfo{{Repo: "amadan.net/oficina/equipo"}, {Repo: "amadan.net/carlos/platform"}}, settled: true} |
| 120 | s := &settler{State: state, Link: &Link{}, Index: index, SessionRoot: root, Persons: func() []string { return []string{c.Person} }} |
| 121 | for i := 0; i < 2; i++ { |
| 122 | s.Check(ctx) |
| 123 | } |
| 124 | frames, _, err := state.Unacked(ctx, 0) |
| 125 | if err != nil { |
| 126 | t.Fatal(err) |
| 127 | } |
| 128 | for _, f := range frames { |
| 129 | if f.Type == wire.FrameMoveIn { |
| 130 | t.Fatal("grant also launched an explore turn") |
| 131 | } |
| 132 | } |
| 133 | again := &settler{State: state, Link: &Link{}, Index: index, SessionRoot: root, Persons: s.Persons} |
| 134 | again.Check(ctx) |
| 135 | frames, _, err = state.Unacked(ctx, 0) |
| 136 | if err != nil { |
| 137 | t.Fatal(err) |
| 138 | } |
| 139 | for _, f := range frames { |
| 140 | if f.Type == wire.FrameMoveIn { |
| 141 | t.Fatal("restart lost grant suppression") |
| 142 | } |
| 143 | } |
| 144 | if got := desk.State().Namespaces; len(got) != 1 { |
| 145 | t.Fatal("background refresh rewrote coordinator desk", got) |
| 146 | } |
| 147 | index.catalog = append(index.catalog, wire.RepoInfo{Repo: "amadan.net/other/new"}) |
| 148 | s.Check(ctx) |
| 149 | frames, _, err = state.Unacked(ctx, 0) |
| 150 | if err != nil { |
| 151 | t.Fatal(err) |
| 152 | } |
| 153 | n := 0 |
| 154 | for _, f := range frames { |
| 155 | if f.Type == wire.FrameMoveIn { |
| 156 | n++ |
| 157 | } |
| 158 | } |
| 159 | if n != 1 { |
| 160 | t.Fatal("unrelated access should still explore", n) |
| 161 | } |
| 162 | } |
| 163 | |