| 1 | package browser |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/sha256" |
| 6 | "encoding/json" |
| 7 | "fmt" |
| 8 | "strings" |
| 9 | "testing" |
| 10 | ) |
| 11 | |
| 12 | // Golden from 61cc393b2027: optional capabilities must not change the |
| 13 | // serialized permanent tool prefix, including descriptions and schemas. |
| 14 | func TestLegacyBrowserToolPrefixUnchanged(t *testing.T) { |
| 15 | var rows []struct { |
| 16 | Name, Description string |
| 17 | Schema json.RawMessage |
| 18 | } |
| 19 | for _, v := range Tools(nil) { |
| 20 | rows = append(rows, struct { |
| 21 | Name, Description string |
| 22 | Schema json.RawMessage |
| 23 | }{v.Name(), v.Description(), v.Schema()}) |
| 24 | } |
| 25 | data, err := json.Marshal(rows) |
| 26 | if err != nil { |
| 27 | t.Fatal(err) |
| 28 | } |
| 29 | if got := fmt.Sprintf("%x", sha256.Sum256(data)); got != "1625d99eb8ff973c6d6ebadc2eb12dd30098b6e434d2c4a9b3a80dd3da73bb1b" { |
| 30 | t.Fatalf("legacy browser tool prefix changed: %s", got) |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | type capabilityFake struct { |
| 35 | fakeExecutor |
| 36 | calls int |
| 37 | } |
| 38 | |
| 39 | func (f *capabilityFake) BrowserCapability(context.Context, string, json.RawMessage) (json.RawMessage, error) { |
| 40 | f.calls++ |
| 41 | return json.RawMessage(`{"count":2,"ambiguous":true}`), nil |
| 42 | } |
| 43 | func TestOptionalBrowserCapabilitiesPreserveOldExecutorAndSchemas(t *testing.T) { |
| 44 | old := &fakeExecutor{} |
| 45 | tools := CapabilityTools(old) |
| 46 | if len(Names()) != 14 || len(Tools(old)) != 14 { |
| 47 | t.Fatal("existing tool surface changed") |
| 48 | } |
| 49 | for _, tool := range tools { |
| 50 | _, err := tool.Execute(context.Background(), json.RawMessage(`{"tabId":"t"}`)) |
| 51 | if err == nil || !strings.Contains(err.Error(), "capability_unsupported") { |
| 52 | t.Fatalf("%s: %v", tool.Name(), err) |
| 53 | } |
| 54 | } |
| 55 | f := &capabilityFake{} |
| 56 | query := CapabilityTools(f)[0] |
| 57 | result, err := query.Execute(context.Background(), json.RawMessage(`{"tabId":"t","documentToken":"d","role":"button","name":"Save"}`)) |
| 58 | if err != nil || !strings.Contains(result, `"ambiguous":true`) { |
| 59 | t.Fatalf("%s %v", result, err) |
| 60 | } |
| 61 | _, err = query.Execute(context.Background(), json.RawMessage(`{"tabId":"t","script":"anything"}`)) |
| 62 | if err == nil || f.calls != 1 { |
| 63 | t.Fatal("unexpected fields reached the host") |
| 64 | } |
| 65 | viewport := CapabilityTools(f)[2] |
| 66 | _, err = viewport.Execute(context.Background(), json.RawMessage(`{"tabId":"t","action":"set","width":393,"height":852}`)) |
| 67 | if err == nil || f.calls != 1 { |
| 68 | t.Fatal("write reached the host without operation identity") |
| 69 | } |
| 70 | } |
| 71 |