返回 DeepSeek-Reasonix
browser_backend_test.go
根目录 / internal / boot / browser_backend_test.go
1 package boot
2
3 import (
4 "slices"
5 "testing"
6
7 "reasonix/internal/browser"
8 "reasonix/internal/config"
9 )
10
11 // A dead loopback port: the backend is lazy, so a build that never calls a
12 // browser tool must never notice that nothing is listening there.
13 const configuredBrowserSection = `
14 [browser]
15 enabled = true
16 endpoint = "http://127.0.0.1:1"
17 `
18
19 // TestConfiguredBrowserBackendStaysOffTheProviderSurface is the cache guard
20 // for the configured backend: turning [browser] on registers the tools for
21 // use_capability and leaves the provider request's tool array and the system
22 // prompt byte-identical to a session with no browser at all.
23 func TestConfiguredBrowserBackendStaysOffTheProviderSurface(t *testing.T) {
24 isolateConfigHome(t)
25 dir := robustTempDir(t)
26 t.Chdir(dir)
27 registerBootTokenProfileTestProvider()
28
29 writeFile(t, dir, "reasonix.toml", browserBootConfig)
30 approveWorkspace(t, dir)
31 without := captureBrowserSurface(t, nil)
32 writeFile(t, dir, "reasonix.toml", browserBootConfig+configuredBrowserSection)
33 approveWorkspace(t, dir)
34 with := captureBrowserSurface(t, nil)
35
36 for _, name := range browser.Names() {
37 if !slices.Contains(with.registry, name) {
38 t.Errorf("registry with [browser] enabled lacks %s", name)
39 }
40 if slices.Contains(without.registry, name) {
41 t.Errorf("registry without [browser] registered %s", name)
42 }
43 if slices.Contains(with.visible, name) || slices.Contains(with.tools, name) {
44 t.Errorf("%s leaked into the provider-visible surface", name)
45 }
46 }
47 if !slices.Equal(with.tools, without.tools) {
48 t.Fatalf("provider tool array changed with [browser] enabled:\nwith %v\nwithout %v", with.tools, without.tools)
49 }
50 if with.prompt != without.prompt {
51 t.Fatalf("system prompt changed with [browser] enabled: %s", firstDivergence(without.prompt, with.prompt))
52 }
53 }
54
55 func TestBrowserBackendPrefersTheHostBrowser(t *testing.T) {
56 host := bootBrowserExecutor{}
57 // A host that already brought a browser keeps it, and owes no shutdown:
58 // the desktop shell and the SSH broker outlive one controller.
59 roots := []string{t.TempDir()}
60 exec, shutdown := browserBackend(host, config.BrowserConfig{Enabled: true}, roots)
61 if exec == nil || shutdown != nil {
62 t.Fatalf("host browser = %v with shutdown %v, want the host executor and no shutdown", exec, shutdown != nil)
63 }
64 if exec, shutdown := browserBackend(nil, config.BrowserConfig{}, roots); exec != nil || shutdown != nil {
65 t.Fatalf("disabled backend = %v, want no browser", exec)
66 }
67 exec, shutdown = browserBackend(nil, config.BrowserConfig{Enabled: true}, roots)
68 if exec == nil || shutdown == nil {
69 t.Fatal("an enabled backend must return an executor and the shutdown that reaps it")
70 }
71 shutdown()
72 }
73
73 lines GO