File size: 4,762 Bytes
739fc44
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/**
 * @license
 * Copyright 2026 Google LLC
 * SPDX-License-Identifier: Apache-2.0
 */

import { describe, it, expect, beforeEach, afterEach } from 'vitest';
import { TestRig, assertModelHasOutput } from './test-helper.js';
import { dirname, join } from 'node:path';
import { fileURLToPath } from 'node:url';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

describe('browser-agent-localhost', () => {
  let rig: TestRig;

  const browserSettings = {
    agents: {
      overrides: {
        browser_agent: {
          enabled: true,
        },
      },
      browser: {
        headless: true,
        sessionMode: 'isolated' as const,
      },
    },
  };

  beforeEach(() => {
    rig = new TestRig();
  });

  afterEach(async () => {
    await rig.cleanup();
  });

  it('should navigate to localhost fixture and read page content', async () => {
    rig.setup('localhost-navigate', {
      fakeResponsesPath: join(
        __dirname,
        'browser-agent-localhost.navigate.responses',
      ),
      settings: browserSettings,
    });

    const result = await rig.run({
      args: 'Navigate to http://127.0.0.1:18923/index.html and tell me the page title and list all links.',
    });

    assertModelHasOutput(result);

    const toolLogs = rig.readToolLogs();
    const browserAgentCall = toolLogs.find(
      (t) =>
        t.toolRequest.name === 'invoke_agent' &&
        JSON.parse(t.toolRequest.args).agent_name === 'browser_agent',
    );
    expect(
      browserAgentCall,
      'Expected browser_agent to be called',
    ).toBeDefined();
  });

  it('should fill out and submit a form on localhost', async () => {
    rig.setup('localhost-form', {
      fakeResponsesPath: join(
        __dirname,
        'browser-agent-localhost.form.responses',
      ),
      settings: browserSettings,
    });

    const result = await rig.run({
      args: "Navigate to http://127.0.0.1:18923/form.html, fill in name='Test User', email='test@example.com', message='Hello World', and submit the form.",
    });

    assertModelHasOutput(result);

    const toolLogs = rig.readToolLogs();
    const browserAgentCall = toolLogs.find(
      (t) =>
        t.toolRequest.name === 'invoke_agent' &&
        JSON.parse(t.toolRequest.args).agent_name === 'browser_agent',
    );
    expect(
      browserAgentCall,
      'Expected browser_agent to be called',
    ).toBeDefined();
  });

  it('should navigate through a multi-step flow', async () => {
    rig.setup('localhost-multistep', {
      fakeResponsesPath: join(
        __dirname,
        'browser-agent-localhost.multistep.responses',
      ),
      settings: browserSettings,
    });

    const result = await rig.run({
      args: "Go to http://127.0.0.1:18923/multi-step/step1.html, fill in 'testuser' as username, click Next, then click Finish on step 2. Report the result.",
    });

    assertModelHasOutput(result);

    const toolLogs = rig.readToolLogs();
    const browserAgentCall = toolLogs.find(
      (t) =>
        t.toolRequest.name === 'invoke_agent' &&
        JSON.parse(t.toolRequest.args).agent_name === 'browser_agent',
    );
    expect(
      browserAgentCall,
      'Expected browser_agent to be called',
    ).toBeDefined();
  });

  it('should handle dynamically loaded content', async () => {
    rig.setup('localhost-dynamic', {
      fakeResponsesPath: join(
        __dirname,
        'browser-agent-localhost.dynamic.responses',
      ),
      settings: browserSettings,
    });

    const result = await rig.run({
      args: 'Navigate to http://127.0.0.1:18923/dynamic.html, wait for content to load, and tell me what items appear.',
    });

    assertModelHasOutput(result);

    const toolLogs = rig.readToolLogs();
    const browserAgentCall = toolLogs.find(
      (t) =>
        t.toolRequest.name === 'invoke_agent' &&
        JSON.parse(t.toolRequest.args).agent_name === 'browser_agent',
    );
    expect(
      browserAgentCall,
      'Expected browser_agent to be called',
    ).toBeDefined();
  });

  it('should take a screenshot of localhost page', async () => {
    rig.setup('localhost-screenshot', {
      fakeResponsesPath: join(
        __dirname,
        'browser-agent-localhost.screenshot.responses',
      ),
      settings: browserSettings,
    });

    const result = await rig.run({
      args: 'Navigate to http://127.0.0.1:18923/index.html and take a screenshot.',
    });

    assertModelHasOutput(result);

    const toolLogs = rig.readToolLogs();
    const browserCalls = toolLogs.filter(
      (t) =>
        t.toolRequest.name === 'invoke_agent' &&
        JSON.parse(t.toolRequest.args).agent_name === 'browser_agent',
    );
    expect(browserCalls.length).toBeGreaterThan(0);
  });
});