| 1 | //go:build browser |
| 2 | |
| 3 | package headless |
| 4 | |
| 5 | import ( |
| 6 | "strconv" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "amadan.net/oficina/equipo/internal/browsertest" |
| 11 | "amadan.net/oficina/equipo/internal/thintest" |
| 12 | "amadan.net/oficina/equipo/internal/wire" |
| 13 | "github.com/chromedp/chromedp" |
| 14 | ) |
| 15 | |
| 16 | // canaryKey is an obviously fake TensorX key. |
| 17 | const canaryKey = "tx-CANARY-not-a-real-key-CANARY-0123456789" |
| 18 | |
| 19 | // quiet says the runner received no frame of kind for a while. |
| 20 | func (r *fakeRunner) quiet(t *testing.T, kind string, d time.Duration) { |
| 21 | t.Helper() |
| 22 | deadline := time.After(d) |
| 23 | for { |
| 24 | select { |
| 25 | case in, ok := <-r.frames: |
| 26 | if !ok { |
| 27 | return |
| 28 | } |
| 29 | if in.frame.Type == kind { |
| 30 | t.Fatalf("a %s frame reached the runner", kind) |
| 31 | } |
| 32 | case <-deadline: |
| 33 | return |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | // Connectors spec §2.1 (S1, the browser test S2 adds): a TensorX key is |
| 39 | // saved only once the person confirms "This key has a spending limit at |
| 40 | // TensorX". Without it, the browser does not send the form; sent anyway |
| 41 | // (the check stripped), the server refuses it; either way nothing is |
| 42 | // stored and nothing is queued for the runner. With it, the connection |
| 43 | // is made, the key card opens, and the key goes to the runner sealed in |
| 44 | // the page, bound to TensorX's origin. |
| 45 | func TestBrowserConnectionsMeteredLimitRequired(t *testing.T) { |
| 46 | for _, width := range []int64{1280, 390} { |
| 47 | t.Run(strconv.FormatInt(width, 10), func(t *testing.T) { |
| 48 | inst := thintest.StartInstance(t) |
| 49 | runner := dialFakeAt(t, inst, wire.ProtocolMetered) |
| 50 | client := thintest.NewClient(inst.URL, inst.DeviceToken(t)) |
| 51 | c := inst.Cookie() |
| 52 | p := openPage(t, inst.URL, c.Name, c.Value, width, 900, width > 600) |
| 53 | t.Cleanup(p.stop) |
| 54 | none := func(what string) { |
| 55 | t.Helper() |
| 56 | runner.quiet(t, wire.FrameConnect, 1500*time.Millisecond) |
| 57 | list, err := client.Connections(t.Context()) |
| 58 | if err != nil || len(list.Items) != 0 { |
| 59 | t.Fatalf("%s: stored %+v %v", what, list.Items, err) |
| 60 | } |
| 61 | } |
| 62 | p.run(chromedp.Navigate(inst.URL+"/conversation/settings/connections/add"), browsertest.Wait(`!!document.querySelector('[data-add-metered="tensorx"]') && !!window.EquipoSeal`)) |
| 63 | 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") |
| 64 | 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") |
| 65 | connShotOn(t, p, "2026-11-12", "metered-confirm", width) |
| 66 | |
| 67 | // Unticked: the browser does not send it. |
| 68 | 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") |
| 69 | none("unticked") |
| 70 | p.check(stayed, "the page reloaded or navigated") |
| 71 | |
| 72 | // Sent anyway, the browser's check stripped: the server refuses. |
| 73 | p.check(`(()=>{const box=document.querySelector('[data-limit-confirmed]');box.required=false;document.querySelector('[data-metered-continue]').click();return true})()`, "submit") |
| 74 | p.run(waitLong(`!!document.querySelector('[data-metered-refused]') && document.querySelector('[data-metered-refused]').textContent.length>0 && !document.querySelector('[data-key-form]')`)) |
| 75 | none("the check stripped") |
| 76 | |
| 77 | // Ticked: the connection is made and the key card opens. |
| 78 | 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") |
| 79 | cmd := runner.expect(t, wire.FrameConnect).(*wire.ConnectCommand) |
| 80 | if cmd.Kind != wire.KindAPIKey || cmd.Provider != wire.ProviderTensorX || cmd.Budget == nil { |
| 81 | t.Fatalf("connect %+v", cmd) |
| 82 | } |
| 83 | list, err := client.Connections(t.Context()) |
| 84 | if err != nil || len(list.Items) != 1 || list.Items[0].LimitConfirmedAt == nil { |
| 85 | t.Fatalf("stored %+v %v", list.Items, err) |
| 86 | } |
| 87 | p.run(waitLong(`!!document.querySelector('[data-connecting][data-kind="api_key"] [data-key-form]')`)) |
| 88 | p.check(recordPosts, "record posts") |
| 89 | 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") |
| 90 | p.run(waitLong(`window.__posts.length===1`)) |
| 91 | 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") |
| 92 | secret := runner.expect(t, wire.FrameSecret).(*wire.SecretCommand) |
| 93 | opened := runner.open(t, secret.Sealed) |
| 94 | if opened.Secret != canaryKey || opened.Provider != wire.ProviderTensorX || opened.Endpoint != wire.APIKeyProviders[wire.ProviderTensorX].Endpoint || opened.ConnectionID != cmd.Connection || opened.Attempt != 1 { |
| 95 | t.Fatalf("the opened payload: provider %q endpoint %q", opened.Provider, opened.Endpoint) |
| 96 | } |
| 97 | }) |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // goCanaryKey is an obviously fake OpenCode Go key. |
| 102 | const goCanaryKey = "sk-go-CANARY-not-a-real-key-CANARY-0123456789" |
| 103 | |
| 104 | // Connectors spec §2.1's Go row and §4.2 (S5, as S2 did for TensorX): an |
| 105 | // OpenCode Go key is saved only once the person confirms "This key has a |
| 106 | // spending limit at OpenCode Go", and there is no pay-as-you-go |
| 107 | // confirmation to give (Go stays metered, §0 decision 7). Without the |
| 108 | // confirmation the browser does not send the form; sent anyway, the |
| 109 | // server refuses it; either way nothing is stored or queued. With it the |
| 110 | // connection is made on a protocol 17 runner, and the key goes to the |
| 111 | // runner sealed in the page, bound to opencode.ai. |
| 112 | func TestBrowserConnectionsOpenCodeGoLimitRequired(t *testing.T) { |
| 113 | for _, width := range []int64{1280, 390} { |
| 114 | t.Run(strconv.FormatInt(width, 10), func(t *testing.T) { |
| 115 | inst := thintest.StartInstance(t) |
| 116 | runner := dialFakeAt(t, inst, wire.ProtocolOpenCode) |
| 117 | client := thintest.NewClient(inst.URL, inst.DeviceToken(t)) |
| 118 | c := inst.Cookie() |
| 119 | p := openPage(t, inst.URL, c.Name, c.Value, width, 900, width > 600) |
| 120 | t.Cleanup(p.stop) |
| 121 | none := func(what string) { |
| 122 | t.Helper() |
| 123 | runner.quiet(t, wire.FrameConnect, 1500*time.Millisecond) |
| 124 | list, err := client.Connections(t.Context()) |
| 125 | if err != nil || len(list.Items) != 0 { |
| 126 | t.Fatalf("%s: stored %+v %v", what, list.Items, err) |
| 127 | } |
| 128 | } |
| 129 | const form = `document.querySelector('[data-add-metered="opencode-go"]')` |
| 130 | p.run(chromedp.Navigate(inst.URL+"/conversation/settings/connections/add"), browsertest.Wait(`!!`+form+` && !!window.EquipoSeal`)) |
| 131 | p.check(`(()=>{window.__equipoStayed=true;`+form+`.querySelector('summary').click();return `+form+`.open})()`, "OpenCode Go did not open") |
| 132 | p.check(`(()=>{const box=`+form+`.querySelector('[data-limit-confirmed]');return box.required && !box.checked && box.closest('label').textContent.includes('This key has a spending limit at OpenCode Go') && !`+form+`.querySelector('[name="payg_off_confirmed"]')})()`, "the confirmation is not there, required and unticked, or a pay-as-you-go one is") |
| 133 | connShotOn(t, p, "2026-11-12", "opencode-go-confirm", width) |
| 134 | |
| 135 | // Unticked: the browser does not send it. |
| 136 | p.check(`(()=>{`+form+`.querySelector('[data-metered-continue]').click();return !`+form+`.querySelector('[data-limit-confirmed]').checkValidity() && location.pathname==='/conversation/settings/connections/add'})()`, "Continue went on without the confirmation") |
| 137 | none("unticked") |
| 138 | p.check(stayed, "the page reloaded or navigated") |
| 139 | |
| 140 | // Sent anyway, the browser's check stripped: the server refuses. |
| 141 | p.check(`(()=>{const box=`+form+`.querySelector('[data-limit-confirmed]');box.required=false;`+form+`.querySelector('[data-metered-continue]').click();return true})()`, "submit") |
| 142 | p.run(waitLong(`!!document.querySelector('[data-add-metered="opencode-go"] [data-metered-refused]') && !document.querySelector('[data-key-form]')`)) |
| 143 | none("the check stripped") |
| 144 | |
| 145 | // Ticked: the connection is made and the key card opens. |
| 146 | p.check(`(()=>{const d=`+form+`;d.open=true;d.querySelector('[data-limit-confirmed]').checked=true;d.querySelector('[data-metered-continue]').click();return true})()`, "ticked") |
| 147 | cmd := runner.expect(t, wire.FrameConnect).(*wire.ConnectCommand) |
| 148 | if cmd.Kind != wire.KindAPIKey || cmd.Provider != wire.ProviderOpenCodeGo || cmd.Billing != wire.BillingMetered || cmd.Budget == nil { |
| 149 | t.Fatalf("connect %+v", cmd) |
| 150 | } |
| 151 | list, err := client.Connections(t.Context()) |
| 152 | if err != nil || len(list.Items) != 1 || list.Items[0].LimitConfirmedAt == nil || list.Items[0].PaygOffConfirmedAt != nil || list.Items[0].Harness != wire.HarnessOpenCode { |
| 153 | t.Fatalf("stored %+v %v", list.Items, err) |
| 154 | } |
| 155 | p.run(waitLong(`!!document.querySelector('[data-connecting][data-kind="api_key"] [data-key-form]')`)) |
| 156 | p.check(recordPosts, "record posts") |
| 157 | p.check(`(()=>{const f=document.querySelector('[data-key-field]');f.value=`+jsonString(goCanaryKey)+`;f.form.requestSubmit();return f.value===''})()`, "the key field was not cleared at once") |
| 158 | p.run(waitLong(`window.__posts.length===1`)) |
| 159 | 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") |
| 160 | secret := runner.expect(t, wire.FrameSecret).(*wire.SecretCommand) |
| 161 | opened := runner.open(t, secret.Sealed) |
| 162 | if opened.Secret != goCanaryKey || opened.Provider != wire.ProviderOpenCodeGo || opened.Endpoint != "https://opencode.ai:443" || opened.ConnectionID != cmd.Connection || opened.Attempt != 1 { |
| 163 | t.Fatalf("the opened payload: provider %q endpoint %q", opened.Provider, opened.Endpoint) |
| 164 | } |
| 165 | }) |
| 166 | } |
| 167 | } |
| 168 | |