oficina / equipo Public

headless: browser test that a TensorX key needs the limit confirmation

Without "This key has a spending limit at TensorX" the browser does
not send the form, and sent anyway (the check stripped) the server
refuses it: nothing stored, no connect frame. With it the connection
is made, the key card opens and the key reaches the runner sealed in
the page, bound to TensorX's origin (the key card's browser test S1
left owed). dialFakeAt speaks a given protocol.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Paul Campbell pushed by paul@keymail.dev c0822242b264f3533f521922f1e96d303a8b567f parent c396d42
2 files changed, +107 −1
  • internal/headless/browser_metered_test.go +99 −0
  • internal/headless/browser_token_test.go +8 −1
diff --git a/internal/headless/browser_metered_test.go b/internal/headless/browser_metered_test.go
new file mode 100644
index 00000000..14627fd6
--- /dev/null
+++ b/internal/headless/browser_metered_test.go
@@ -0,0 +1,99 @@
+//go:build browser
+
+package headless
+
+import (
+ "strconv"
+ "testing"
+ "time"
+
+ "amadan.net/oficina/equipo/internal/browsertest"
+ "amadan.net/oficina/equipo/internal/thintest"
+ "amadan.net/oficina/equipo/internal/wire"
+ "github.com/chromedp/chromedp"
+)
+
+// canaryKey is an obviously fake TensorX key.
+const canaryKey = "tx-CANARY-not-a-real-key-CANARY-0123456789"
+
+// quiet says the runner received no frame of kind for a while.
+func (r *fakeRunner) quiet(t *testing.T, kind string, d time.Duration) {
+ t.Helper()
+ deadline := time.After(d)
+ for {
+ select {
+ case in, ok := <-r.frames:
+ if !ok {
+ return
+ }
+ if in.frame.Type == kind {
+ t.Fatalf("a %s frame reached the runner", kind)
+ }
+ case <-deadline:
+ return
+ }
+ }
+}
+
+// Connectors spec §2.1 (S1, the browser test S2 adds): a TensorX key is
+// saved only once the person confirms "This key has a spending limit at
+// TensorX". Without it, the browser does not send the form; sent anyway
+// (the check stripped), the server refuses it; either way nothing is
+// stored and nothing is queued for the runner. With it, the connection
+// is made, the key card opens, and the key goes to the runner sealed in
+// the page, bound to TensorX's origin.
+func TestBrowserConnectionsMeteredLimitRequired(t *testing.T) {
+ for _, width := range []int64{1280, 390} {
+ t.Run(strconv.FormatInt(width, 10), func(t *testing.T) {
+ inst := thintest.StartInstance(t)
+ runner := dialFakeAt(t, inst, wire.ProtocolMetered)
+ client := thintest.NewClient(inst.URL, inst.DeviceToken(t))
+ c := inst.Cookie()
+ p := openPage(t, inst.URL, c.Name, c.Value, width, 900, width > 600)
+ t.Cleanup(p.stop)
+ none := func(what string) {
+ t.Helper()
+ runner.quiet(t, wire.FrameConnect, 1500*time.Millisecond)
+ list, err := client.Connections(t.Context())
+ if err != nil || len(list.Items) != 0 {
+ t.Fatalf("%s: stored %+v %v", what, list.Items, err)
+ }
+ }
+ p.run(chromedp.Navigate(inst.URL+"/conversation/settings/connections/add"), browsertest.Wait(`!!document.querySelector('[data-add-metered="tensorx"]') && !!window.EquipoSeal`))
+ p.check(`(()=>{window.__equipoStayed=true;document.querySelector('[data-add-metered="tensorx"] summary').click();return document.querySelector('[data-add-metered="tensorx"]').open})()`, "TensorX did not open")
+ p.check(`(()=>{const box=document.querySelector('[data-limit-confirmed]');return box.required && !box.checked && box.closest('label').textContent.includes('This key has a spending limit at TensorX')})()`, "the confirmation is not there, required and unticked")
+ connShotOn(t, p, "2026-11-12", "metered-confirm", width)
+
+ // Unticked: the browser does not send it.
+ p.check(`(()=>{document.querySelector('[data-metered-continue]').click();return !document.querySelector('[data-limit-confirmed]').checkValidity() && location.pathname==='/conversation/settings/connections/add'})()`, "Continue went on without the confirmation")
+ none("unticked")
+ p.check(stayed, "the page reloaded or navigated")
+
+ // Sent anyway, the browser's check stripped: the server refuses.
+ p.check(`(()=>{const box=document.querySelector('[data-limit-confirmed]');box.required=false;document.querySelector('[data-metered-continue]').click();return true})()`, "submit")
+ p.run(waitLong(`!!document.querySelector('[data-metered-refused]') && document.querySelector('[data-metered-refused]').textContent.length>0 && !document.querySelector('[data-key-form]')`))
+ none("the check stripped")
+
+ // Ticked: the connection is made and the key card opens.
+ p.check(`(()=>{const d=document.querySelector('[data-add-metered="tensorx"]');d.open=true;const box=document.querySelector('[data-limit-confirmed]');box.checked=true;document.querySelector('[data-metered-continue]').click();return true})()`, "ticked")
+ cmd := runner.expect(t, wire.FrameConnect).(*wire.ConnectCommand)
+ if cmd.Kind != wire.KindAPIKey || cmd.Provider != wire.ProviderTensorX || cmd.Budget == nil {
+ t.Fatalf("connect %+v", cmd)
+ }
+ list, err := client.Connections(t.Context())
+ if err != nil || len(list.Items) != 1 || list.Items[0].LimitConfirmedAt == nil {
+ t.Fatalf("stored %+v %v", list.Items, err)
+ }
+ p.run(waitLong(`!!document.querySelector('[data-connecting][data-kind="api_key"] [data-key-form]')`))
+ p.check(recordPosts, "record posts")
+ p.check(`(()=>{const f=document.querySelector('[data-key-field]');f.value=`+jsonString(canaryKey)+`;f.form.requestSubmit();return f.value===''})()`, "the key field was not cleared at once")
+ p.run(waitLong(`window.__posts.length===1`))
+ p.check(`(()=>{const post=window.__posts[0];return post.url.endsWith('/secret') && !post.body.includes('CANARY') && !document.documentElement.outerHTML.includes('CANARY')})()`, "the request or the page holds the key")
+ secret := runner.expect(t, wire.FrameSecret).(*wire.SecretCommand)
+ opened := runner.open(t, secret.Sealed)
+ if opened.Secret != canaryKey || opened.Provider != wire.ProviderTensorX || opened.Endpoint != wire.APIKeyProviders[wire.ProviderTensorX].Endpoint || opened.ConnectionID != cmd.Connection || opened.Attempt != 1 {
+ t.Fatalf("the opened payload: provider %q endpoint %q", opened.Provider, opened.Endpoint)
+ }
+ })
+ }
+}
diff --git a/internal/headless/browser_token_test.go b/internal/headless/browser_token_test.go
index 2f90f12a..c51788d3 100644
--- a/internal/headless/browser_token_test.go
+++ b/internal/headless/browser_token_test.go
@@ -49,12 +49,19 @@ type fakeRunner struct {
}
func dialFake(t *testing.T, inst *thintest.Instance) *fakeRunner {
+ t.Helper()
+ return dialFakeAt(t, inst, 0)
+}
+
+// dialFakeAt is dialFake for a runner that speaks protocol (0: the
+// hello's default).
+func dialFakeAt(t *testing.T, inst *thintest.Instance, protocol int) *fakeRunner {
t.Helper()
public, private, err := box.GenerateKey(rand.Reader)
if err != nil {
t.Fatal(err)
}
- fr, err := thintest.DialRunnerHello(t.Context(), inst.URL, inst.EnrolRunner(t).Token, wire.Hello{Repos: []string{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "test"}, SealKey: base64.StdEncoding.EncodeToString(public[:])})
+ fr, err := thintest.DialRunnerHello(t.Context(), inst.URL, inst.EnrolRunner(t).Token, wire.Hello{Protocol: protocol, Repos: []string{}, Connections: []wire.HeldConnection{}, Versions: map[string]string{"runner": "test"}, SealKey: base64.StdEncoding.EncodeToString(public[:])})
if err != nil {
t.Fatal(err)
}