返回 DeepSeek-Reasonix
fake_test.go
根目录 / internal / browser / fake_test.go
1 package browser
2
3 import (
4 "context"
5 "encoding/json"
6 "testing"
7
8 "reasonix/internal/tool"
9 )
10
11 // fakeExecutor records every call and answers from its fixed fields; err,
12 // when set, is returned by every method.
13 type fakeExecutor struct {
14 err error
15 tabs []Tab
16 snapshot Snapshot
17 screenshot Screenshot
18 act ActResult
19 downloads []Download
20
21 calls []string
22 opens []OpenRequest
23 navs []NavigateRequest
24 snaps []SnapshotRequest
25 shots []ScreenshotRequest
26 acts []ActRequest
27 dls []DownloadsRequest
28 closed []string
29 closes []CloseRequest
30 }
31
32 func (f *fakeExecutor) Tabs(context.Context) ([]Tab, error) {
33 f.calls = append(f.calls, "tabs")
34 return f.tabs, f.err
35 }
36
37 func (f *fakeExecutor) Open(_ context.Context, req OpenRequest) (Tab, error) {
38 f.calls = append(f.calls, "open")
39 f.opens = append(f.opens, req)
40 if f.err != nil {
41 return Tab{}, f.err
42 }
43 return Tab{ID: "t-new", URL: req.URL, Temporary: req.Temporary}, nil
44 }
45
46 func (f *fakeExecutor) Navigate(_ context.Context, req NavigateRequest) (Tab, error) {
47 f.calls = append(f.calls, "navigate")
48 f.navs = append(f.navs, req)
49 if f.err != nil {
50 return Tab{}, f.err
51 }
52 return Tab{ID: req.TabID, URL: req.URL}, nil
53 }
54
55 func (f *fakeExecutor) Snapshot(_ context.Context, req SnapshotRequest) (Snapshot, error) {
56 f.calls = append(f.calls, "snapshot")
57 f.snaps = append(f.snaps, req)
58 return f.snapshot, f.err
59 }
60
61 func (f *fakeExecutor) Screenshot(_ context.Context, req ScreenshotRequest) (Screenshot, error) {
62 f.calls = append(f.calls, "screenshot")
63 f.shots = append(f.shots, req)
64 return f.screenshot, f.err
65 }
66
67 func (f *fakeExecutor) Act(_ context.Context, req ActRequest) (ActResult, error) {
68 f.calls = append(f.calls, "act")
69 f.acts = append(f.acts, req)
70 return f.act, f.err
71 }
72
73 func (f *fakeExecutor) Downloads(_ context.Context, req DownloadsRequest) ([]Download, error) {
74 f.calls = append(f.calls, "downloads")
75 f.dls = append(f.dls, req)
76 return f.downloads, f.err
77 }
78
79 func (f *fakeExecutor) Close(_ context.Context, req CloseRequest) error {
80 f.calls = append(f.calls, "close")
81 f.closed = append(f.closed, req.TabID)
82 f.closes = append(f.closes, req)
83 return f.err
84 }
85
86 // gatedExecutor adds Availability to the fake.
87 type gatedExecutor struct {
88 *fakeExecutor
89 available bool
90 }
91
92 func (g gatedExecutor) Available(context.Context) bool { return g.available }
93
94 func toolByName(t *testing.T, exec Executor, name string) tool.Tool {
95 t.Helper()
96 for _, tl := range Tools(exec) {
97 if tl.Name() == name {
98 return tl
99 }
100 }
101 t.Fatalf("no tool %q", name)
102 return nil
103 }
104
105 func run(t *testing.T, exec Executor, name, args string) (string, error) {
106 t.Helper()
107 return toolByName(t, exec, name).Execute(context.Background(), json.RawMessage(args))
108 }
109
109 lines GO