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 "crypto/ed25519"
5 "crypto/rand"
6 "encoding/base64"
7 "net/http"
8 "net/http/httptest"
9 "net/netip"
10 "path/filepath"
11 "strings"
12 "sync/atomic"
13 "testing"
14 "time"
15
16 "amadan.net/oficina/equipo/internal/fakeclaude"
17 "amadan.net/oficina/equipo/internal/runner/connect"
18 "amadan.net/oficina/equipo/internal/runner/thin"
19 "amadan.net/oficina/equipo/internal/runnerproto"
20 "amadan.net/oficina/equipo/internal/sealbox"
21 "amadan.net/oficina/equipo/internal/thintest"
22 "amadan.net/oficina/equipo/internal/wire"
23)
24
25// A token whose CLI names no account, pasted end to end: the real check
26// (the contained launch) answers, then reads the account profile from
27// the runner with the same token, at a fake profile endpoint on
28// 127.0.0.1. The email it names is the connection's label, from the
29// healthy report on: the row shows it and asks for no email. The token
30// reaches neither the instance's log nor its page.
31func TestHeadlessTokenProfileNamesAccount(t *testing.T) {
32 env, inst, r := localEnv(t)
33 _, key, _ := ed25519.GenerateKey(rand.Reader)
34 if err := runnerproto.JournalCredentials(filepath.Join(r.cfg.StateDir, "machine-key.json"), base64.RawURLEncoding.EncodeToString(key)); err != nil {
35 t.Fatal(err)
36 }
37 token := fakeclaude.Token("")
38 var reads atomic.Int64
39 profile := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
40 reads.Add(1)
41 if req.Method != http.MethodGet || req.URL.Path != "/api/oauth/profile" || req.Header.Get("Authorization") != "Bearer "+token || req.Header.Get("User-Agent") != thin.ProfileAgent+"/test" {
42 w.WriteHeader(http.StatusUnauthorized)
43 return
44 }
45 _, _ = w.Write([]byte(`{"account":{"uuid":"acc-1","email":"linked@example.com"},"organization":{"uuid":"org-1"}}`))
46 }))
47 defer profile.Close()
48 r.opts.Claude.Profile = &thin.ProfileReader{Origin: profile.URL, HTTP: connect.SafeClient(nil, func(a netip.Addr) bool { return a.IsLoopback() }, nil)}
49 r.only(t)
50 r.start(t)
51 s := open(t, env)
52 s.keep()
53 c, err := env.Client.Connect(s.ctx, wire.Connect{Provider: wire.ProviderClaude, Kind: wire.KindToken})
54 if err != nil {
55 t.Fatal(err)
56 }
57 s.wait(t, "token card", time.Minute, func(cv *thintest.Conversation) bool {
58 v := cv.Connections[c.ID]
59 return v.Prompt != nil && v.Prompt.Kind == wire.PromptToken
60 })
61 snap, err := env.Client.Snapshot(s.ctx)
62 if err != nil {
63 t.Fatal(err)
64 }
65 sealed, err := sealbox.Seal(snap.Runner.SealKey, wire.SealedPayload{Secret: token, RunnerID: snap.Runner.ID, ConnectionID: c.ID, Provider: wire.ProviderClaude, Endpoint: wire.EndpointAnthropic, Attempt: 1, ExpiresAt: time.Now().Add(wire.SealedTTL).UTC().Truncate(time.Second)})
66 if err != nil {
67 t.Fatal(err)
68 }
69 if _, err = env.Client.Secret(s.ctx, c.ID, sealed); err != nil {
70 t.Fatal(err)
71 }
72 var got wire.Connection
73 s.wait(t, "token healthy", time.Minute, func(cv *thintest.Conversation) bool {
74 got = cv.Connections[c.ID]
75 return got.State == wire.ConnHealthy
76 })
77 if got.Label != "linked@example.com" || reads.Load() != 1 {
78 t.Fatalf("healthy %+v after %d profile reads", got, reads.Load())
79 }
80 row := inst.Get(t, "/conversation/connections")
81 if !strings.Contains(row, "linked@example.com") || strings.Contains(row, "Add the account email") || strings.Contains(row, "(added by you)") {
82 t.Fatal("the row does not show the provider's email plainly, or still asks for one")
83 }
84 if strings.Contains(inst.Logs(), token) || strings.Contains(row, token) {
85 t.Fatal("the token reached the instance")
86 }
87}
88
89// A token connection made before the profile read existed: the runner
90// reads its profile once as it starts, with no check and no turn, and
91// names itself (thin.ProfileAgent/<version>). The row is named and asks for
92// no email. A restart reads it no more: the answer stands.
93func TestHeadlessTokenProfileAtStart(t *testing.T) {
94 env, inst, r := localEnv(t)
95 // A bare token connection, as the runner records one the CLI named no
96 // account for.
97 token := r.addToken(t, "claude-7", "token:claude-7")
98 var reads atomic.Int64
99 profile := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, req *http.Request) {
100 reads.Add(1)
101 if req.Method != http.MethodGet || req.URL.Path != "/api/oauth/profile" || req.Header.Get("Authorization") != "Bearer "+token || req.Header.Get("User-Agent") != thin.ProfileAgent+"/test" {
102 w.WriteHeader(http.StatusUnauthorized)
103 return
104 }
105 _, _ = w.Write([]byte(`{"account":{"uuid":"acc-7","email":"existing@example.com"},"organization":{"uuid":"org-7"}}`))
106 }))
107 defer profile.Close()
108 r.opts.Claude.Profile = &thin.ProfileReader{Origin: profile.URL, HTTP: connect.SafeClient(nil, func(a netip.Addr) bool { return a.IsLoopback() }, nil)}
109 r.only(t, "claude-7")
110 r.start(t)
111 s := open(t, env)
112 s.keep()
113 s.wait(t, "the existing row named", time.Minute, func(cv *thintest.Conversation) bool {
114 for _, c := range cv.Connections {
115 if c.LocalID == "claude-7" && c.Label == "existing@example.com" {
116 return true
117 }
118 }
119 return false
120 })
121 if reads.Load() != 1 {
122 t.Fatalf("%d profile reads at start", reads.Load())
123 }
124 if page := inst.Get(t, "/conversation/connections"); !strings.Contains(page, "existing@example.com") || strings.Contains(page, "Add the account email") {
125 t.Fatal("the row is not named, or still asks for the email")
126 }
127 r.stop(t)
128 r.start(t)
129 time.Sleep(2 * time.Second)
130 if reads.Load() != 1 {
131 t.Fatalf("a restart read again: %d", reads.Load())
132 }
133}
134