testronaut 1.3.1 → 1.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11,6 +11,8 @@ import {
11
11
  getRetryLimit,
12
12
  resolveTurnLimits,
13
13
  enforceTurnBudget,
14
+ getDomListLimit,
15
+ getResourceGuardConfig,
14
16
  } from '../../core/config.js';
15
17
 
16
18
  describe('core/config', () => {
@@ -105,6 +107,100 @@ describe('core/config', () => {
105
107
  });
106
108
  });
107
109
 
110
+ describe('getDomListLimit', () => {
111
+ const OLD_ENV = { ...process.env };
112
+ beforeEach(() => {
113
+ process.env = { ...OLD_ENV };
114
+ delete process.env.TESTRONAUT_DOM_LIST_LIMIT;
115
+ });
116
+ afterEach(() => {
117
+ process.env = { ...OLD_ENV };
118
+ });
119
+
120
+ it('returns default when missing', () => {
121
+ const res = getDomListLimit({});
122
+ expect(res.value).toBe(3);
123
+ expect(res.mode).toBe('number');
124
+ expect(res.source).toBe('default');
125
+ expect(res.clamped).toBe(false);
126
+ });
127
+
128
+ it('uses config value (nested) and clamps to range', () => {
129
+ const res = getDomListLimit({ dom: { listItemLimit: 250 } });
130
+ expect(res.value).toBe(100);
131
+ expect(res.mode).toBe('number');
132
+ expect(res.source).toBe('config');
133
+ expect(res.clamped).toBe(true);
134
+
135
+ const nestedAlt = getDomListLimit({ dom: { listLimit: '9' } });
136
+ expect(nestedAlt.value).toBe(9);
137
+ expect(nestedAlt.source).toBe('config');
138
+ });
139
+
140
+ it('prefers env override and accepts keywords', () => {
141
+ process.env.TESTRONAUT_DOM_LIST_LIMIT = 'all';
142
+ const allRes = getDomListLimit({ domListLimit: 2 });
143
+ expect(allRes.value).toBe(Infinity);
144
+ expect(allRes.mode).toBe('all');
145
+ expect(allRes.source).toBe('env');
146
+
147
+ process.env.TESTRONAUT_DOM_LIST_LIMIT = 'none';
148
+ const noneRes = getDomListLimit({ domListLimit: 5 });
149
+ expect(noneRes.value).toBe(0);
150
+ expect(noneRes.mode).toBe('none');
151
+ expect(noneRes.source).toBe('env');
152
+ });
153
+
154
+ it('accepts numeric strings', () => {
155
+ const res = getDomListLimit({ domListLimit: '7' });
156
+ expect(res.value).toBe(7);
157
+ expect(res.mode).toBe('number');
158
+ });
159
+ });
160
+
161
+ describe('getResourceGuardConfig', () => {
162
+ const OLD_ENV = { ...process.env };
163
+ beforeEach(() => {
164
+ process.env = { ...OLD_ENV };
165
+ delete process.env.TESTRONAUT_RESOURCE_GUARD;
166
+ delete process.env.TESTRONAUT_RESOURCE_HREF_PATTERNS;
167
+ delete process.env.TESTRONAUT_RESOURCE_DATA_TYPES;
168
+ });
169
+ afterEach(() => {
170
+ process.env = { ...OLD_ENV };
171
+ });
172
+
173
+ it('returns defaults when nothing is set', () => {
174
+ const res = getResourceGuardConfig({});
175
+ expect(res.enabled).toBe(true);
176
+ expect(res.hrefIncludes.length).toBeGreaterThan(0);
177
+ expect(res.dataTypes).toContain('document');
178
+ });
179
+
180
+ it('respects env toggles and lists', () => {
181
+ process.env.TESTRONAUT_RESOURCE_GUARD = 'false';
182
+ process.env.TESTRONAUT_RESOURCE_HREF_PATTERNS = '/a,/b';
183
+ process.env.TESTRONAUT_RESOURCE_DATA_TYPES = 'doc,file';
184
+ const res = getResourceGuardConfig({ resourceGuard: { enabled: true } });
185
+ expect(res.enabled).toBe(false);
186
+ expect(res.hrefIncludes).toEqual(['/a', '/b']);
187
+ expect(res.dataTypes).toEqual(['doc', 'file']);
188
+ });
189
+
190
+ it('falls back to config arrays when env missing', () => {
191
+ const res = getResourceGuardConfig({
192
+ resourceGuard: {
193
+ enabled: 'yes',
194
+ hrefIncludes: ['/x', '/y'],
195
+ dataTypes: ['row'],
196
+ },
197
+ });
198
+ expect(res.enabled).toBe(true);
199
+ expect(res.hrefIncludes).toEqual(['/x', '/y']);
200
+ expect(res.dataTypes).toEqual(['row']);
201
+ });
202
+ });
203
+
108
204
  describe('enforceTurnBudget (lenient)', () => {
109
205
  it('clamps > hardMaxTurns', () => {
110
206
  const cfg = { maxTurns: 1000 };
@@ -0,0 +1,81 @@
1
+ import { describe, it, expect, afterEach } from 'vitest';
2
+ import fs from 'fs';
3
+ import path from 'path';
4
+ import os from 'os';
5
+ import { discoverMissionFiles } from '../../core/missionDiscovery.js';
6
+
7
+ function makeTempDir() {
8
+ return fs.mkdtempSync(path.join(os.tmpdir(), 'testronaut-missions-'));
9
+ }
10
+
11
+ function writeJson(p, obj) {
12
+ fs.writeFileSync(p, JSON.stringify(obj, null, 2));
13
+ }
14
+
15
+ const temps = [];
16
+
17
+ afterEach(() => {
18
+ while (temps.length) {
19
+ const dir = temps.pop();
20
+ fs.rmSync(dir, { recursive: true, force: true });
21
+ }
22
+ });
23
+
24
+ describe('discoverMissionFiles', () => {
25
+ it('falls back to legacy discovery when missions block is missing', async () => {
26
+ const tmp = makeTempDir();
27
+ temps.push(tmp);
28
+ const missionsDir = path.join(tmp, 'missions');
29
+ fs.mkdirSync(missionsDir, { recursive: true });
30
+ fs.writeFileSync(path.join(missionsDir, 'alpha.mission.js'), '// ok');
31
+ fs.writeFileSync(path.join(missionsDir, 'beta.txt'), '// ignore');
32
+ fs.mkdirSync(path.join(missionsDir, 'nested'), { recursive: true });
33
+ fs.writeFileSync(path.join(missionsDir, 'nested', 'gamma.mission.js'), '// nested');
34
+ fs.writeFileSync(path.join(missionsDir, 'delta.mission.ts'), '// ts not included by default');
35
+
36
+ const res = await discoverMissionFiles({ cwd: tmp });
37
+ expect(res.root).toBe(path.resolve(tmp, 'missions'));
38
+ expect(res.files).toEqual(['alpha.mission.js']);
39
+ });
40
+
41
+ it('respects missions.root with include patterns', async () => {
42
+ const tmp = makeTempDir();
43
+ temps.push(tmp);
44
+ const missionsDir = path.join(tmp, 'custom');
45
+ fs.mkdirSync(path.join(missionsDir, 'nested'), { recursive: true });
46
+ fs.writeFileSync(path.join(missionsDir, 'root.mission.js'), '// root');
47
+ fs.writeFileSync(path.join(missionsDir, 'nested', 'deep.mission.ts'), '// nested ts');
48
+
49
+ writeJson(path.join(tmp, 'testronaut-config.json'), {
50
+ missions: {
51
+ root: './custom',
52
+ include: ['*.mission.js', 'nested/**/*.mission.ts'],
53
+ },
54
+ });
55
+
56
+ const res = await discoverMissionFiles({ cwd: tmp });
57
+ expect(res.root).toBe(path.resolve(tmp, 'custom'));
58
+ expect(res.files).toEqual(['nested/deep.mission.ts', 'root.mission.js']);
59
+ });
60
+
61
+ it('applies missions.exclude after includes', async () => {
62
+ const tmp = makeTempDir();
63
+ temps.push(tmp);
64
+ const missionsDir = path.join(tmp, 'custom');
65
+ fs.mkdirSync(path.join(missionsDir, 'ignore'), { recursive: true });
66
+ fs.writeFileSync(path.join(missionsDir, 'keep.mission.js'), '// keep');
67
+ fs.writeFileSync(path.join(missionsDir, 'ignore', 'skip.mission.js'), '// skip');
68
+ fs.writeFileSync(path.join(missionsDir, 'keep.wip.mission.js'), '// skip by pattern');
69
+
70
+ writeJson(path.join(tmp, 'testronaut-config.json'), {
71
+ missions: {
72
+ root: './custom',
73
+ include: ['**/*.mission.js'],
74
+ exclude: ['ignore/**', '**/*.wip.mission.js'],
75
+ },
76
+ });
77
+
78
+ const res = await discoverMissionFiles({ cwd: tmp });
79
+ expect(res.files).toEqual(['keep.mission.js']);
80
+ });
81
+ });
@@ -22,6 +22,7 @@ const { shared } = vi.hoisted(() => {
22
22
  mode: 'save',
23
23
  })
24
24
  ),
25
+ list_local_files: vi.fn(async () => JSON.stringify({ files: ['a.pdf', 'b.pdf'] })),
25
26
  },
26
27
  },
27
28
  };
@@ -63,6 +64,7 @@ vi.mock('../../tools/toolSchema.js', () => ({
63
64
  { type: 'function', function: { name: 'click_text', description: 'click by text', parameters: {} } },
64
65
  { type: 'function', function: { name: 'get_dom', description: 'get DOM html', parameters: {} } },
65
66
  { type: 'function', function: { name: 'screenshot', description: 'take a screenshot', parameters: {} } },
67
+ { type: 'function', function: { name: 'list_local_files', description: 'list files', parameters: {} } },
66
68
  ],
67
69
  }));
68
70
 
@@ -96,7 +98,7 @@ vi.mock('../../core/redaction.js', () => ({
96
98
  }));
97
99
 
98
100
  // Import SUT after mocks
99
- import { turnLoop } from '../../core/turnLoop.js';
101
+ import { turnLoop, __docProgressInternals } from '../../core/turnLoop.js';
100
102
 
101
103
  // Helper
102
104
  function baseMessages() {
@@ -226,4 +228,47 @@ describe('turnLoop', () => {
226
228
  expect(res.steps[1].result).toBe('✅ Passed');
227
229
  expect(res.steps[2].result).toMatch(/Success/);
228
230
  });
231
+
232
+ describe('__docProgressInternals helpers', () => {
233
+ const { parseDocListFromDom, extractDocIdFromUrl, ensureDocProgress } = __docProgressInternals;
234
+
235
+ it('parses injected doc list summary', () => {
236
+ const html = `<pre data-testronaut-doc-list>
237
+ List
238
+ - [123] File A /document/123
239
+ - [456] File B /document/456
240
+ </pre>`;
241
+ const { items, scriptDocs } = parseDocListFromDom(html);
242
+ expect(items).toHaveLength(2);
243
+ expect(items[0].id).toBe('123');
244
+ expect(items[1].title.length).toBeGreaterThan(0);
245
+ expect(scriptDocs).toBe(2);
246
+ });
247
+
248
+ it('extracts doc id from url and tracks progress', () => {
249
+ expect(extractDocIdFromUrl('https://x.com/document/999/foo')).toBe('999');
250
+ expect(extractDocIdFromUrl('nope')).toBe('');
251
+
252
+ const mem = {};
253
+ const cfg = { enabled: true, hrefIncludes: ['/doc'], dataTypes: ['doc'] };
254
+ const prog = ensureDocProgress(mem, cfg);
255
+ expect(prog.patterns).toBe(cfg);
256
+ prog.items = [{ id: '1', title: 'Doc 1' }];
257
+ prog.downloaded.add('1');
258
+ expect(prog.downloaded.has('1')).toBe(true);
259
+ });
260
+
261
+ it('updates progress from list_local_files results', () => {
262
+ const mem = {};
263
+ const cfg = { enabled: true, hrefIncludes: ['/doc'], dataTypes: ['doc'] };
264
+ const prog = ensureDocProgress(mem, cfg);
265
+ prog.items = [];
266
+ // simulate list_local_files JSON result
267
+ const parsed = JSON.parse(JSON.stringify({ files: ['x.pdf', 'y.pdf'] }));
268
+ prog.items = parsed.files.map(f => ({ id: f, title: f, href: f }));
269
+ prog.downloaded.add('x.pdf');
270
+ expect(prog.items).toHaveLength(2);
271
+ expect(prog.downloaded.has('x.pdf')).toBe(true);
272
+ });
273
+ });
229
274
  });
@@ -10,19 +10,25 @@ vi.mock('../../core/config.js', () => ({
10
10
  loadConfig: vi.fn(),
11
11
  enforceTurnBudget: vi.fn(),
12
12
  getRetryLimit: vi.fn(),
13
+ getDomListLimit: vi.fn(),
14
+ getResourceGuardConfig: vi.fn(),
13
15
  }));
14
16
 
15
17
  import { runAgent } from '../../core/agent.js';
16
- import { loadConfig, enforceTurnBudget, getRetryLimit } from '../../core/config.js';
18
+ import { loadConfig, enforceTurnBudget, getRetryLimit, getDomListLimit, getResourceGuardConfig } from '../../core/config.js';
17
19
 
18
20
  // Adjust the import path if your file lives elsewhere
19
- import { runMissions } from '../../runner/testronaut.js';
21
+ import { runMissions, __test__ as testronautInternals } from '../../runner/testronaut.js';
20
22
 
21
23
  describe('cli/testronaut.runMissions (with enforceTurnBudget)', () => {
24
+ const OLD_ENV = { ...process.env };
22
25
  beforeEach(() => {
26
+ process.env = { ...OLD_ENV };
23
27
  vi.clearAllMocks();
24
28
  // Default retry limit unless overridden in a test
25
29
  getRetryLimit.mockReturnValue({ value: 2, source: 'default', clamped: false });
30
+ getDomListLimit.mockReturnValue({ value: 3, mode: 'number', source: 'default', clamped: false });
31
+ getResourceGuardConfig.mockReturnValue({ enabled: true, hrefIncludes: ['/document/'], dataTypes: ['document'] });
26
32
  });
27
33
 
28
34
  it('passes effectiveMax to runAgent and logs any notes', async () => {
@@ -50,7 +56,7 @@ describe('cli/testronaut.runMissions (with enforceTurnBudget)', () => {
50
56
  expect.stringContaining('Clamping to 200')
51
57
  );
52
58
  expect(runAgent).toHaveBeenCalledWith(
53
- expect.any(Array), 'Budgeted Run', 200, 2
59
+ expect.any(Array), 'Budgeted Run', 200, 2, { domListLimit: 3, debug: false, resourceGuard: { enabled: true, hrefIncludes: ['/document/'], dataTypes: ['document'] } }
54
60
  );
55
61
 
56
62
  warn.mockRestore();
@@ -77,7 +83,33 @@ describe('cli/testronaut.runMissions (with enforceTurnBudget)', () => {
77
83
  await runMissions({ mission: 'No warnings' }, 'Clean');
78
84
 
79
85
  expect(warn).not.toHaveBeenCalled();
80
- expect(runAgent).toHaveBeenCalledWith(expect.any(Array), 'Clean', 20, 3);
86
+ expect(runAgent).toHaveBeenCalledWith(expect.any(Array), 'Clean', 20, 3, { domListLimit: 3, debug: false, resourceGuard: { enabled: true, hrefIncludes: ['/document/'], dataTypes: ['document'] } });
87
+
88
+ warn.mockRestore();
89
+ log.mockRestore();
90
+ });
91
+
92
+ it('warns when the DOM list limit is clamped', async () => {
93
+ loadConfig.mockResolvedValue({});
94
+ enforceTurnBudget.mockReturnValue({
95
+ effectiveMax: 20,
96
+ limits: {},
97
+ notes: [],
98
+ strict: false,
99
+ });
100
+ getDomListLimit.mockReturnValue({ value: 100, mode: 'number', source: 'config', clamped: true });
101
+
102
+ const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
103
+ const log = vi.spyOn(console, 'log').mockImplementation(() => {});
104
+ runAgent.mockResolvedValue([
105
+ { steps: [{ result: 'SUCCESS: ok' }], status: 'passed' },
106
+ ]);
107
+
108
+ await runMissions({ mission: 'Clamp lists' }, 'Clamp Mission');
109
+
110
+ expect(warn).toHaveBeenCalledWith(
111
+ expect.stringContaining('DOM list limit clamped to 100')
112
+ );
81
113
 
82
114
  warn.mockRestore();
83
115
  log.mockRestore();
@@ -144,7 +176,7 @@ describe('cli/testronaut.runMissions (with enforceTurnBudget)', () => {
144
176
  expect(goals[1].submissionName).toMatch(/^My Mission/);
145
177
 
146
178
  // Effective max turns and retry limit passed through
147
- expect(runAgent).toHaveBeenCalledWith(expect.any(Array), 'My Mission', 15, 2);
179
+ expect(runAgent).toHaveBeenCalledWith(expect.any(Array), 'My Mission', 15, 2, { domListLimit: 3, debug: false, resourceGuard: { enabled: true, hrefIncludes: ['/document/'], dataTypes: ['document'] } });
148
180
 
149
181
  log.mockRestore();
150
182
  });
@@ -166,4 +198,37 @@ describe('cli/testronaut.runMissions (with enforceTurnBudget)', () => {
166
198
 
167
199
  log.mockRestore();
168
200
  });
201
+
202
+ it('passes debug flag through when TESTRONAUT_DEBUG is set', async () => {
203
+ process.env.TESTRONAUT_DEBUG = 'true';
204
+ loadConfig.mockResolvedValue({});
205
+ enforceTurnBudget.mockReturnValue({
206
+ effectiveMax: 20,
207
+ limits: {},
208
+ notes: [],
209
+ strict: false,
210
+ });
211
+ const log = vi.spyOn(console, 'log').mockImplementation(() => {});
212
+ runAgent.mockResolvedValue([
213
+ { steps: [{ result: 'SUCCESS: ok' }], status: 'passed' },
214
+ ]);
215
+
216
+ await runMissions({ mission: 'Debug' }, 'Debug Mission');
217
+
218
+ expect(runAgent).toHaveBeenCalledWith(expect.any(Array), 'Debug Mission', 20, 2, { domListLimit: 3, debug: true, resourceGuard: { enabled: true, hrefIncludes: ['/document/'], dataTypes: ['document'] } });
219
+
220
+ log.mockRestore();
221
+ });
222
+
223
+ describe('__test__ helpers', () => {
224
+ it('formats list limits and reads debug env', () => {
225
+ const { isDebugEnabled, formatListLimit } = testronautInternals;
226
+ process.env.TESTRONAUT_DEBUG = 'on';
227
+ expect(isDebugEnabled()).toBe(true);
228
+ process.env.TESTRONAUT_DEBUG = '0';
229
+ expect(isDebugEnabled()).toBe(false);
230
+ expect(formatListLimit(Infinity)).toBe('all');
231
+ expect(formatListLimit(5)).toBe(5);
232
+ });
233
+ });
169
234
  });
@@ -195,6 +195,41 @@ describe('domControl helpers', () => {
195
195
  // Total children: 2 cards + 1 other + 1 marker
196
196
  expect(rootChildren.length).toBe(4);
197
197
  });
198
+
199
+ it('keeps all repeated divs when maxAllowed is Infinity', () => {
200
+ const html = `
201
+ <div id="root">
202
+ <div class="card">1</div>
203
+ <div class="card">2</div>
204
+ <div class="card">3</div>
205
+ <div class="card">4</div>
206
+ </div>
207
+ `;
208
+ const $ = cheerio.load(html);
209
+
210
+ reduceRepeatedDivs($, Infinity);
211
+
212
+ const cardDivs = $('#root').children('div.card');
213
+ expect(cardDivs.length).toBe(4);
214
+ expect($('#root').children('div[data-collapsed="true"]').length).toBe(0);
215
+ });
216
+
217
+ it('removes all repeated divs when maxAllowed is 0', () => {
218
+ const html = `
219
+ <div id="root">
220
+ <div class="card">A</div>
221
+ <div class="card">B</div>
222
+ </div>
223
+ `;
224
+ const $ = cheerio.load(html);
225
+
226
+ reduceRepeatedDivs($, 0);
227
+
228
+ const cardDivs = $('#root').children('div.card');
229
+ expect(cardDivs.length).toBe(0);
230
+ const markers = $('#root').children('div[data-collapsed="true"]');
231
+ expect(markers.length).toBe(1);
232
+ });
198
233
  });
199
234
 
200
235
  describe('removeObfuscatedClassNames', () => {
@@ -0,0 +1,24 @@
1
+ import { describe, it, expect } from 'vitest';
2
+ import toolsSchema from '../../tools/toolSchema.js';
3
+
4
+ describe('tools/toolSchema', () => {
5
+ it('includes resource_progress tool with empty params', () => {
6
+ const entry = toolsSchema.find(t => t.function?.name === 'resource_progress');
7
+ expect(entry?.type).toBe('function');
8
+ expect(entry.function.parameters).toEqual({ type: 'object', properties: {} });
9
+ expect(entry.function.description).toMatch(/remaining/i);
10
+ });
11
+
12
+ it('includes list_local_files tool', () => {
13
+ const entry = toolsSchema.find(t => t.function?.name === 'list_local_files');
14
+ expect(entry?.type).toBe('function');
15
+ expect(entry.function.parameters?.properties).toHaveProperty('dir');
16
+ });
17
+
18
+ it('defines navigate and download_file with required fields', () => {
19
+ const nav = toolsSchema.find(t => t.function?.name === 'navigate');
20
+ const download = toolsSchema.find(t => t.function?.name === 'download_file');
21
+ expect(nav?.function?.parameters?.required).toContain('url');
22
+ expect(download?.function?.parameters?.properties).toHaveProperty('url');
23
+ });
24
+ });