返回 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 previews []FilePreviewRequest
24 navs []NavigateRequest
25 snaps []SnapshotRequest
26 shots []ScreenshotRequest
27 acts []ActRequest
28 dls []DownloadsRequest
29 closed []string
30 closes []CloseRequest
31 }
32
33 func (f *fakeExecutor) PreviewFile(_ context.Context, req FilePreviewRequest) (Tab, error) {
34 f.calls = append(f.calls, "preview")
35 f.previews = append(f.previews, req)
36 if f.err != nil {
37 return Tab{}, f.err
38 }
39 return Tab{ID: "t-preview", URL: "http://127.0.0.1/preview"}, nil
40 }
41
42 func (f *fakeExecutor) Tabs(context.Context) ([]Tab, error) {
43 f.calls = append(f.calls, "tabs")
44 return f.tabs, f.err
45 }
46
47 func (f *fakeExecutor) Open(_ context.Context, req OpenRequest) (Tab, error) {
48 f.calls = append(f.calls, "open")
49 f.opens = append(f.opens, req)
50 if f.err != nil {
51 return Tab{}, f.err
52 }
53 return Tab{ID: "t-new", URL: req.URL, Temporary: req.Temporary}, nil
54 }
55
56 func (f *fakeExecutor) Navigate(_ context.Context, req NavigateRequest) (Tab, error) {
57 f.calls = append(f.calls, "navigate")
58 f.navs = append(f.navs, req)
59 if f.err != nil {
60 return Tab{}, f.err
61 }
62 return Tab{ID: req.TabID, URL: req.URL}, nil
63 }
64
65 func (f *fakeExecutor) Snapshot(_ context.Context, req SnapshotRequest) (Snapshot, error) {
66 f.calls = append(f.calls, "snapshot")
67 f.snaps = append(f.snaps, req)
68 return f.snapshot, f.err
69 }
70
71 func (f *fakeExecutor) Screenshot(_ context.Context, req ScreenshotRequest) (Screenshot, error) {
72 f.calls = append(f.calls, "screenshot")
73 f.shots = append(f.shots, req)
74 return f.screenshot, f.err
75 }
76
77 func (f *fakeExecutor) Act(_ context.Context, req ActRequest) (ActResult, error) {
78 f.calls = append(f.calls, "act")
79 f.acts = append(f.acts, req)
80 return f.act, f.err
81 }
82
83 func (f *fakeExecutor) Downloads(_ context.Context, req DownloadsRequest) ([]Download, error) {
84 f.calls = append(f.calls, "downloads")
85 f.dls = append(f.dls, req)
86 return f.downloads, f.err
87 }
88
89 func (f *fakeExecutor) Close(_ context.Context, req CloseRequest) error {
90 f.calls = append(f.calls, "close")
91 f.closed = append(f.closed, req.TabID)
92 f.closes = append(f.closes, req)
93 return f.err
94 }
95
96 // gatedExecutor adds Availability to the fake.
97 type gatedExecutor struct {
98 *fakeExecutor
99 available bool
100 }
101
102 func (g gatedExecutor) Available(context.Context) bool { return g.available }
103
104 func toolByName(t *testing.T, exec Executor, name string) tool.Tool {
105 t.Helper()
106 for _, tl := range Tools(exec) {
107 if tl.Name() == name {
108 return tl
109 }
110 }
111 t.Fatalf("no tool %q", name)
112 return nil
113 }
114
115 func run(t *testing.T, exec Executor, name, args string) (string, error) {
116 t.Helper()
117 return toolByName(t, exec, name).Execute(context.Background(), json.RawMessage(args))
118 }
119
119 lines GO