@h-ai/tools 0.0.0-stage → 1.0.0-beta.32
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.
- package/LICENSE +202 -0
- package/README.en.md +37 -0
- package/README.ja.md +37 -0
- package/README.md +36 -2
- package/dist/chunk-UA7MOYTT.js +413 -0
- package/dist/chunk-UA7MOYTT.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/server/index.d.ts +306 -0
- package/dist/server/index.js +1051 -0
- package/dist/server/index.js.map +1 -0
- package/dist/tools-types-CM0lGuJr.d.ts +199 -0
- package/jobs/.dockerignore +4 -0
- package/jobs/README.md +223 -0
- package/jobs/archives.py +56 -0
- package/jobs/browser/Dockerfile +20 -0
- package/jobs/browser/operation.py +17 -0
- package/jobs/browser/render.mjs +90 -0
- package/jobs/browser_policy.py +36 -0
- package/jobs/documents/Dockerfile +23 -0
- package/jobs/documents/generate.py +165 -0
- package/jobs/documents/operation.py +84 -0
- package/jobs/documents/requirements.txt +3 -0
- package/jobs/hyperframes-runtime.mjs +73 -0
- package/jobs/media_files.py +78 -0
- package/jobs/ocr/Dockerfile +19 -0
- package/jobs/ocr/operation.py +18 -0
- package/jobs/office/Dockerfile +21 -0
- package/jobs/office/office_job.py +65 -0
- package/jobs/office/operation.py +43 -0
- package/jobs/preview_build/Dockerfile +19 -0
- package/jobs/preview_build/build.mjs +552 -0
- package/jobs/preview_build/operation.py +62 -0
- package/jobs/preview_build/package.json +11 -0
- package/jobs/tests/image-smoke.mjs +83 -0
- package/jobs/tests/requirements.txt +4 -0
- package/jobs/tests/test_documents_generate.py +107 -0
- package/jobs/tests/test_hai_tool.py +82 -0
- package/jobs/tests/test_media_jobs.py +449 -0
- package/jobs/tests/test_preview_build.py +213 -0
- package/jobs/tool.py +145 -0
- package/jobs/video_render/Dockerfile +20 -0
- package/jobs/video_render/operation.py +109 -0
- package/jobs/video_render/render.mjs +36 -0
- package/jobs/web_capture/Dockerfile +20 -0
- package/jobs/web_capture/capture.mjs +122 -0
- package/jobs/web_capture/operation.py +65 -0
- package/package.json +62 -3
|
@@ -0,0 +1,449 @@
|
|
|
1
|
+
import importlib.util
|
|
2
|
+
import io
|
|
3
|
+
import json
|
|
4
|
+
import os
|
|
5
|
+
import pathlib
|
|
6
|
+
import stat
|
|
7
|
+
import subprocess
|
|
8
|
+
import sys
|
|
9
|
+
import tempfile
|
|
10
|
+
import unittest
|
|
11
|
+
import warnings
|
|
12
|
+
import zipfile
|
|
13
|
+
from unittest.mock import patch
|
|
14
|
+
|
|
15
|
+
ROOT = pathlib.Path(__file__).resolve().parents[1]
|
|
16
|
+
sys.path.insert(0, str(ROOT))
|
|
17
|
+
from tool import ToolError, serve
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def load(kind):
|
|
21
|
+
spec = importlib.util.spec_from_file_location('hai_' + kind, ROOT / kind / 'operation.py')
|
|
22
|
+
module = importlib.util.module_from_spec(spec)
|
|
23
|
+
spec.loader.exec_module(module)
|
|
24
|
+
return module
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
class MediaJobTests(unittest.TestCase):
|
|
28
|
+
def setUp(self):
|
|
29
|
+
self.temp = tempfile.TemporaryDirectory()
|
|
30
|
+
self.addCleanup(self.temp.cleanup)
|
|
31
|
+
self.root = pathlib.Path(self.temp.name)
|
|
32
|
+
self.inputs = self.root / 'input'
|
|
33
|
+
self.outputs = self.root / 'output'
|
|
34
|
+
self.inputs.mkdir()
|
|
35
|
+
self.outputs.mkdir()
|
|
36
|
+
|
|
37
|
+
def invoke(self, module, request):
|
|
38
|
+
response = io.StringIO()
|
|
39
|
+
serve(io.BytesIO(json.dumps(request).encode()), response,
|
|
40
|
+
module.commands, self.inputs, self.outputs)
|
|
41
|
+
return json.loads(response.getvalue())
|
|
42
|
+
|
|
43
|
+
def project(self, name='project.zip', html=None, entries=None):
|
|
44
|
+
if html is None:
|
|
45
|
+
html = '<div data-composition-id="intro" data-start="0" data-duration="4" data-width="1280" data-height="720">Hello</div>'
|
|
46
|
+
with zipfile.ZipFile(self.inputs / name, 'w') as archive:
|
|
47
|
+
archive.writestr('index.html', html)
|
|
48
|
+
for path, content in entries or []:
|
|
49
|
+
with warnings.catch_warnings():
|
|
50
|
+
warnings.simplefilter('ignore', UserWarning)
|
|
51
|
+
archive.writestr(path, content)
|
|
52
|
+
return name
|
|
53
|
+
|
|
54
|
+
def browser_worker(self, mode):
|
|
55
|
+
directory = self.root / ('browser-' + mode)
|
|
56
|
+
package = directory / 'node_modules' / 'playwright-core'
|
|
57
|
+
package.mkdir(parents=True)
|
|
58
|
+
worker = directory / 'render.mjs'
|
|
59
|
+
worker.write_text((ROOT / 'browser' / 'render.mjs').read_text())
|
|
60
|
+
(package / 'package.json').write_text(json.dumps({'type': 'module', 'exports': './index.mjs'}))
|
|
61
|
+
(package / 'index.mjs').write_text('''
|
|
62
|
+
import { writeFile } from 'node:fs/promises'
|
|
63
|
+
import { dirname } from 'node:path'
|
|
64
|
+
const mode = MODE
|
|
65
|
+
const directory = dirname(process.argv[1])
|
|
66
|
+
const state = { clock: 0, navigation: [], screenshots: [], pageClosed: 0, browserClosed: 0 }
|
|
67
|
+
Object.defineProperty(globalThis, 'performance', { value: { now: () => state.clock } })
|
|
68
|
+
const persist = () => writeFile(directory + '/state.json', JSON.stringify(state))
|
|
69
|
+
const changed = url => new Error('page.goto: net::ERR_NETWORK_CHANGED at ' + url + '\\nCall log:\\nPRIVATE_BROWSER_DETAILS')
|
|
70
|
+
const page = {
|
|
71
|
+
async goto(url, options) {
|
|
72
|
+
state.navigation.push({ url, timeout: options.timeout })
|
|
73
|
+
if (mode === 'recover' || mode === 'normalized' || mode === 'budget' || mode === 'exhausted' || mode === 'repeated') {
|
|
74
|
+
state.clock += mode === 'budget' ? (state.navigation.length === 1 ? 17000 : 0) : mode === 'exhausted' ? 20000 : 3000
|
|
75
|
+
await persist()
|
|
76
|
+
if (state.navigation.length === 1 || mode === 'repeated') throw changed(mode === 'normalized' ? new URL(url).href : url)
|
|
77
|
+
}
|
|
78
|
+
else if (mode === 'reset' || mode === 'wrong-url' || mode === 'wrong-prefix' || mode === 'near-match') {
|
|
79
|
+
await persist()
|
|
80
|
+
const message = mode === 'reset' ? 'page.goto: net::ERR_CONNECTION_RESET at ' + url
|
|
81
|
+
: mode === 'wrong-url' ? 'page.goto: net::ERR_NETWORK_CHANGED at https://other.example/'
|
|
82
|
+
: mode === 'wrong-prefix' ? 'other: page.goto: net::ERR_NETWORK_CHANGED at ' + url
|
|
83
|
+
: 'page.goto: net::ERR_NETWORK_CHANGEDNESS at ' + url
|
|
84
|
+
throw new Error(message)
|
|
85
|
+
}
|
|
86
|
+
state.clock += mode === 'late' ? 40001 : 1000
|
|
87
|
+
await persist()
|
|
88
|
+
return { ok: () => mode !== 'http403' }
|
|
89
|
+
},
|
|
90
|
+
url() { return 'https://example.com/' },
|
|
91
|
+
async screenshot(options) {
|
|
92
|
+
state.screenshots.push({ timeout: options.timeout })
|
|
93
|
+
await writeFile(options.path, 'partial-image')
|
|
94
|
+
await persist()
|
|
95
|
+
if (mode === 'screenshot-error') throw changed('https://example.com/')
|
|
96
|
+
state.clock += 9000
|
|
97
|
+
await writeFile(options.path, 'complete-image')
|
|
98
|
+
},
|
|
99
|
+
async close() { state.pageClosed++; await persist() },
|
|
100
|
+
}
|
|
101
|
+
export const chromium = {
|
|
102
|
+
async launch(options) {
|
|
103
|
+
state.launch = options
|
|
104
|
+
state.clock += mode === 'budget' ? 9000 : 1000
|
|
105
|
+
await persist()
|
|
106
|
+
if (mode === 'sandbox-error') throw new Error('No usable sandbox PRIVATE_BROWSER_DETAILS')
|
|
107
|
+
return {
|
|
108
|
+
async newContext(options) {
|
|
109
|
+
state.context = options
|
|
110
|
+
return { async route() {}, async newPage() { return page } }
|
|
111
|
+
},
|
|
112
|
+
async close() { state.browserClosed++; await persist() },
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
}
|
|
116
|
+
'''.replace('MODE', json.dumps(mode)))
|
|
117
|
+
output = directory / 'screen.png'
|
|
118
|
+
output.write_bytes(b'stale-image')
|
|
119
|
+
target = 'https://EXAMPLE.COM:443' if mode == 'normalized' else 'https://example.com/'
|
|
120
|
+
result = subprocess.run(['node', str(worker), 'http://192.0.2.10:3128', target, str(output)],
|
|
121
|
+
capture_output=True, text=True, timeout=10)
|
|
122
|
+
return result, output, json.loads((directory / 'state.json').read_text())
|
|
123
|
+
|
|
124
|
+
def test_browser_worker_retries_exact_navigation_change_with_one_shared_budget(self):
|
|
125
|
+
for mode in ['recover', 'normalized', 'budget']:
|
|
126
|
+
with self.subTest(mode=mode):
|
|
127
|
+
result, output, state = self.browser_worker(mode)
|
|
128
|
+
self.assertEqual(result.returncode, 0, result.stderr)
|
|
129
|
+
self.assertEqual(output.read_bytes(), b'complete-image')
|
|
130
|
+
self.assertEqual(len(state['navigation']), 2)
|
|
131
|
+
self.assertTrue(all(item['url'] == 'https://example.com/' for item in state['navigation']))
|
|
132
|
+
timeouts = [item['timeout'] for item in state['navigation']]
|
|
133
|
+
self.assertTrue(all(0 < value <= 20000 for value in timeouts))
|
|
134
|
+
self.assertLess(timeouts[1], timeouts[0])
|
|
135
|
+
self.assertLessEqual(state['launch']['timeout'], 10000)
|
|
136
|
+
self.assertLessEqual(state['screenshots'][0]['timeout'], 10000)
|
|
137
|
+
self.assertLess(state['clock'], 40000)
|
|
138
|
+
self.assertTrue(state['launch']['chromiumSandbox'])
|
|
139
|
+
self.assertEqual(state['launch']['proxy'], {'server': 'http://192.0.2.10:3128', 'bypass': '<-loopback>'})
|
|
140
|
+
self.assertFalse(state['context']['javaScriptEnabled'])
|
|
141
|
+
self.assertFalse(state['context']['acceptDownloads'])
|
|
142
|
+
self.assertEqual(state['context']['serviceWorkers'], 'block')
|
|
143
|
+
self.assertEqual(state['pageClosed'], 1)
|
|
144
|
+
self.assertEqual(state['browserClosed'], 1)
|
|
145
|
+
|
|
146
|
+
def test_browser_worker_does_not_retry_other_failures_or_publish_old_images(self):
|
|
147
|
+
for mode in ['http403', 'reset', 'wrong-url', 'wrong-prefix', 'near-match', 'repeated', 'exhausted', 'late', 'screenshot-error', 'sandbox-error']:
|
|
148
|
+
with self.subTest(mode=mode):
|
|
149
|
+
result, output, state = self.browser_worker(mode)
|
|
150
|
+
self.assertNotEqual(result.returncode, 0)
|
|
151
|
+
self.assertFalse(output.exists())
|
|
152
|
+
self.assertNotIn('PRIVATE_BROWSER_DETAILS', result.stderr)
|
|
153
|
+
self.assertEqual(len(state['navigation']), 0 if mode == 'sandbox-error' else 2 if mode == 'repeated' else 1)
|
|
154
|
+
self.assertEqual(len(state['screenshots']), 1 if mode == 'screenshot-error' else 0)
|
|
155
|
+
self.assertEqual(state['browserClosed'], 0 if mode == 'sandbox-error' else 1)
|
|
156
|
+
self.assertEqual(state['pageClosed'], 0 if mode == 'sandbox-error' else 1)
|
|
157
|
+
|
|
158
|
+
def test_capture_worker_requires_sandbox_even_when_host_requests_disabled(self):
|
|
159
|
+
directory = self.root / 'worker'
|
|
160
|
+
(directory / 'web_capture').mkdir(parents=True)
|
|
161
|
+
worker = directory / 'web_capture' / 'capture.mjs'
|
|
162
|
+
worker.write_text((ROOT / 'web_capture' / 'capture.mjs').read_text())
|
|
163
|
+
(directory / 'hyperframes-runtime.mjs').write_text('''
|
|
164
|
+
import { mkdir, writeFile } from 'node:fs/promises'
|
|
165
|
+
export async function runHyperframes(args, directory, extra) {
|
|
166
|
+
await writeFile(directory + '/environment.json', JSON.stringify(extra))
|
|
167
|
+
await mkdir(args[args.indexOf('--output') + 1])
|
|
168
|
+
return { stdout: JSON.stringify({ ok: true, httpStatus: 200, screenshots: 1 }) }
|
|
169
|
+
}
|
|
170
|
+
''')
|
|
171
|
+
result = subprocess.run(['node', str(worker), 'http://192.0.2.10:3128', 'https://example.com/',
|
|
172
|
+
str(directory / 'capture'), '2'], capture_output=True, text=True,
|
|
173
|
+
env={**os.environ, 'HYPERFRAMES_CHROME_SANDBOX': '0'}, timeout=10)
|
|
174
|
+
self.assertEqual(result.returncode, 0)
|
|
175
|
+
environment = json.loads((directory / 'environment.json').read_text())
|
|
176
|
+
self.assertEqual(environment.get('HYPERFRAMES_CHROME_SANDBOX'), '1')
|
|
177
|
+
|
|
178
|
+
def test_capture_reports_sandbox_environment_failure_without_publishing(self):
|
|
179
|
+
module = load('web_capture')
|
|
180
|
+
|
|
181
|
+
def unavailable(_args, _timeout, workspace):
|
|
182
|
+
(workspace / 'capture-error.json').write_text(json.dumps({'code': 'SANDBOX_UNAVAILABLE'}))
|
|
183
|
+
raise ToolError('TOOL_FAILED', 'The selected operation failed')
|
|
184
|
+
|
|
185
|
+
with patch.dict(os.environ, {'HAI_TOOL_BROWSER_PROXY': 'http://192.0.2.10:3128'}, clear=True):
|
|
186
|
+
with patch.object(module, 'run_command', unavailable):
|
|
187
|
+
result = self.invoke(module, {'operation': 'web.capture', 'url': 'https://example.com/', 'output': 'capture.zip'})
|
|
188
|
+
self.assertFalse(result['success'])
|
|
189
|
+
self.assertEqual(result['error']['code'], 'SANDBOX_UNAVAILABLE')
|
|
190
|
+
self.assertEqual(list(self.outputs.iterdir()), [])
|
|
191
|
+
|
|
192
|
+
def test_capture_worker_keeps_only_fixed_sandbox_error_code(self):
|
|
193
|
+
for index, message in enumerate([
|
|
194
|
+
'No usable sandbox! Internal private details',
|
|
195
|
+
'Check failed: sys_chroot("/proc/self/fdinfo/") == 0; Zygote process exited prematurely',
|
|
196
|
+
]):
|
|
197
|
+
with self.subTest(message=message):
|
|
198
|
+
directory = self.root / f'rejected-worker-{index}'
|
|
199
|
+
(directory / 'web_capture').mkdir(parents=True)
|
|
200
|
+
worker = directory / 'web_capture' / 'capture.mjs'
|
|
201
|
+
worker.write_text((ROOT / 'web_capture' / 'capture.mjs').read_text())
|
|
202
|
+
(directory / 'hyperframes-runtime.mjs').write_text(
|
|
203
|
+
'export async function runHyperframes() { const error = new Error("Capture failed"); '
|
|
204
|
+
+ 'error.stdout = ' + json.dumps(json.dumps({'error': {'message': message}}))
|
|
205
|
+
+ '; throw error }')
|
|
206
|
+
result = subprocess.run(['node', str(worker), 'http://192.0.2.10:3128', 'https://example.com/',
|
|
207
|
+
str(directory / 'capture'), '2'], capture_output=True, text=True, timeout=10)
|
|
208
|
+
self.assertNotEqual(result.returncode, 0)
|
|
209
|
+
self.assertEqual(json.loads((directory / 'capture-error.json').read_text()), {'code': 'SANDBOX_UNAVAILABLE'})
|
|
210
|
+
self.assertNotIn('Internal private details', result.stderr)
|
|
211
|
+
|
|
212
|
+
def test_capture_does_not_misclassify_page_errors_as_sandbox(self):
|
|
213
|
+
directory = self.root / 'page-error-worker'
|
|
214
|
+
(directory / 'web_capture').mkdir(parents=True)
|
|
215
|
+
worker = directory / 'web_capture' / 'capture.mjs'
|
|
216
|
+
worker.write_text((ROOT / 'web_capture' / 'capture.mjs').read_text())
|
|
217
|
+
message = 'Website capture blocked: HTTP 403 title "No usable sandbox"'
|
|
218
|
+
phase = 'HYPERFRAMES_CAPTURE_PHASE {"phase":"browser","status":"completed"}'
|
|
219
|
+
(directory / 'hyperframes-runtime.mjs').write_text(
|
|
220
|
+
'export async function runHyperframes() { const error = new Error("Capture failed"); '
|
|
221
|
+
+ 'error.stdout = ' + json.dumps(json.dumps({'error': {'message': message}}))
|
|
222
|
+
+ '; error.stderr = ' + json.dumps(phase) + '; throw error }')
|
|
223
|
+
result = subprocess.run(['node', str(worker), 'http://192.0.2.10:3128', 'https://example.com/',
|
|
224
|
+
str(directory / 'capture'), '2'], capture_output=True, text=True, timeout=10)
|
|
225
|
+
self.assertNotEqual(result.returncode, 0)
|
|
226
|
+
self.assertFalse((directory / 'capture-error.json').exists())
|
|
227
|
+
|
|
228
|
+
def test_capture_retries_only_one_transient_navigation_with_total_budget(self):
|
|
229
|
+
directory = self.root / 'retry-worker'
|
|
230
|
+
(directory / 'web_capture').mkdir(parents=True)
|
|
231
|
+
worker = directory / 'web_capture' / 'capture.mjs'
|
|
232
|
+
worker.write_text((ROOT / 'web_capture' / 'capture.mjs').read_text())
|
|
233
|
+
(directory / 'hyperframes-runtime.mjs').write_text('''
|
|
234
|
+
import { mkdir, writeFile, readFile } from 'node:fs/promises'
|
|
235
|
+
export async function runHyperframes(args, directory, extra, timeout) {
|
|
236
|
+
let calls = []; try { calls = JSON.parse(await readFile(directory + '/calls.json', 'utf8')) } catch {}
|
|
237
|
+
calls.push({ timeout, sandbox: extra.HYPERFRAMES_CHROME_SANDBOX })
|
|
238
|
+
await writeFile(directory + '/calls.json', JSON.stringify(calls))
|
|
239
|
+
const output = args[args.indexOf('--output') + 1]
|
|
240
|
+
await mkdir(output, { recursive: true })
|
|
241
|
+
if (calls.length === 1) {
|
|
242
|
+
await writeFile(output + '/BLOCKED.md', 'Old failed output')
|
|
243
|
+
const error = new Error('Navigation failed')
|
|
244
|
+
error.stdout = JSON.stringify({ ok: false, error: { code: 'runtime_error', message: 'net::ERR_NETWORK_CHANGED at https://example.com/' } })
|
|
245
|
+
error.stderr = 'HYPERFRAMES_CAPTURE_PHASE {"phase":"browser","status":"completed"}\\nHYPERFRAMES_CAPTURE_PHASE {"phase":"navigation","status":"started"}'
|
|
246
|
+
throw error
|
|
247
|
+
}
|
|
248
|
+
return { stdout: JSON.stringify({ ok: true, httpStatus: 200, screenshots: 1 }) }
|
|
249
|
+
}
|
|
250
|
+
''')
|
|
251
|
+
result = subprocess.run(['node', str(worker), 'http://192.0.2.10:3128', 'https://example.com/',
|
|
252
|
+
str(directory / 'capture'), '2'], capture_output=True, text=True, timeout=10)
|
|
253
|
+
self.assertEqual(result.returncode, 0)
|
|
254
|
+
calls = json.loads((directory / 'calls.json').read_text())
|
|
255
|
+
self.assertEqual(len(calls), 2)
|
|
256
|
+
self.assertTrue(all(item['sandbox'] == '1' and 0 < item['timeout'] <= 170000 for item in calls))
|
|
257
|
+
self.assertLess(calls[1]['timeout'], calls[0]['timeout'])
|
|
258
|
+
self.assertFalse((directory / 'capture' / 'BLOCKED.md').exists())
|
|
259
|
+
status = json.loads((directory / 'capture' / 'capture-status.json').read_text())
|
|
260
|
+
self.assertEqual(status['retries'], [{'phase': 'navigation', 'code': 'ERR_NETWORK_CHANGED', 'attempt': 1}])
|
|
261
|
+
|
|
262
|
+
def test_capture_does_not_retry_http403_or_other_errors_and_never_a_third_attempt(self):
|
|
263
|
+
for index, mode in enumerate(['http403', 'connection-reset', 'network-changed']):
|
|
264
|
+
with self.subTest(mode=mode):
|
|
265
|
+
directory = self.root / f'limited-worker-{index}'
|
|
266
|
+
(directory / 'web_capture').mkdir(parents=True)
|
|
267
|
+
worker = directory / 'web_capture' / 'capture.mjs'
|
|
268
|
+
worker.write_text((ROOT / 'web_capture' / 'capture.mjs').read_text())
|
|
269
|
+
(directory / 'hyperframes-runtime.mjs').write_text('''
|
|
270
|
+
import { writeFile, readFile } from 'node:fs/promises'
|
|
271
|
+
const mode = MODE
|
|
272
|
+
export async function runHyperframes(args, directory) {
|
|
273
|
+
let count = 0; try { count = Number(await readFile(directory + '/count', 'utf8')) } catch {}
|
|
274
|
+
await writeFile(directory + '/count', String(count + 1))
|
|
275
|
+
if (mode === 'http403') return { stdout: JSON.stringify({ ok: true, httpStatus: 403, screenshots: 1 }) }
|
|
276
|
+
const error = new Error('Capture failed')
|
|
277
|
+
error.stdout = JSON.stringify({ ok: false, error: { code: 'runtime_error', message: 'net::' + (mode === 'network-changed' ? 'ERR_NETWORK_CHANGED' : 'ERR_CONNECTION_RESET') + ' at https://example.com/' } })
|
|
278
|
+
error.stderr = 'HYPERFRAMES_CAPTURE_PHASE {"phase":"browser","status":"completed"}\\nHYPERFRAMES_CAPTURE_PHASE {"phase":"navigation","status":"started"}'
|
|
279
|
+
throw error
|
|
280
|
+
}
|
|
281
|
+
'''.replace('MODE', json.dumps(mode)))
|
|
282
|
+
result = subprocess.run(['node', str(worker), 'http://192.0.2.10:3128', 'https://example.com/',
|
|
283
|
+
str(directory / 'capture'), '2'], capture_output=True, text=True, timeout=10)
|
|
284
|
+
self.assertNotEqual(result.returncode, 0)
|
|
285
|
+
self.assertEqual(int((directory / 'count').read_text()), 2 if mode == 'network-changed' else 1)
|
|
286
|
+
self.assertFalse((directory / 'capture').exists())
|
|
287
|
+
|
|
288
|
+
def test_capture_returns_source_text_and_actual_materials_in_archive(self):
|
|
289
|
+
module = load('web_capture')
|
|
290
|
+
|
|
291
|
+
def capture(args, timeout, workspace):
|
|
292
|
+
destination = workspace / 'capture'
|
|
293
|
+
(destination / 'screenshots').mkdir(parents=True)
|
|
294
|
+
(destination / 'extracted').mkdir()
|
|
295
|
+
(destination / 'screenshots' / 'page.png').write_bytes(b'\x89PNG\r\n\x1a\nfixture')
|
|
296
|
+
(destination / 'extracted' / 'visible-text.txt').write_text('Actual product description')
|
|
297
|
+
(destination / 'extracted' / 'tokens.json').write_text(json.dumps({
|
|
298
|
+
'title': 'Product', 'description': 'A real product', 'colors': ['#123456'],
|
|
299
|
+
}))
|
|
300
|
+
for name in ['AGENTS.md', 'CLAUDE.md', '.cursorrules']:
|
|
301
|
+
(destination / name).write_text('Generated assistant workflow instructions')
|
|
302
|
+
(destination / 'capture-status.json').write_text(json.dumps({
|
|
303
|
+
'httpStatus': 200, 'dropped': {'unavailable': 1}, 'warnings': ['One referenced asset is unavailable'],
|
|
304
|
+
}))
|
|
305
|
+
|
|
306
|
+
with patch.dict(os.environ, {'HAI_TOOL_BROWSER_PROXY': 'http://192.0.2.10:3128'}, clear=True):
|
|
307
|
+
with patch.object(module, 'run_command', capture, create=True):
|
|
308
|
+
result = self.invoke(module, {'operation': 'web.capture', 'url': 'https://example.com/', 'output': 'capture.zip'})
|
|
309
|
+
self.assertTrue(result['success'], result)
|
|
310
|
+
self.assertEqual(result['data']['artifact']['name'], 'capture.zip')
|
|
311
|
+
with zipfile.ZipFile(self.outputs / 'capture.zip') as archive:
|
|
312
|
+
metadata = json.loads(archive.read('capture.json'))
|
|
313
|
+
self.assertEqual(metadata['sourceUrl'], 'https://example.com/')
|
|
314
|
+
self.assertEqual(metadata['text'], 'Actual product description')
|
|
315
|
+
self.assertEqual(metadata['title'], 'Product')
|
|
316
|
+
self.assertIn('screenshots/page.png', metadata['screenshots'])
|
|
317
|
+
self.assertIn('#123456', metadata['colors'])
|
|
318
|
+
self.assertEqual(result['data']['summary']['text'], metadata['text'])
|
|
319
|
+
self.assertEqual(result['data']['summary']['sourceUrl'], metadata['sourceUrl'])
|
|
320
|
+
for name in ['AGENTS.md', 'CLAUDE.md', '.cursorrules']:
|
|
321
|
+
self.assertNotIn(name, archive.namelist())
|
|
322
|
+
self.assertNotIn(name, result['data']['summary']['materials'])
|
|
323
|
+
self.assertEqual(json.loads(archive.read('capture-status.json'))['dropped']['unavailable'], 1)
|
|
324
|
+
|
|
325
|
+
def test_capture_requires_deployment_proxy_and_rejects_request_overrides(self):
|
|
326
|
+
module = load('web_capture')
|
|
327
|
+
args = {'operation': 'web.capture', 'url': 'https://example.com/', 'output': 'capture.zip'}
|
|
328
|
+
with patch.dict(os.environ, {}, clear=True):
|
|
329
|
+
self.assertEqual(self.invoke(module, args)['error']['code'], 'NETWORK_POLICY_REQUIRED')
|
|
330
|
+
for change in [{'proxy': 'http://192.0.2.10:3128'}, {'maxScreenshots': True}, {'maxScreenshots': 9}, {'url': 'https://127.0.0.1/'}]:
|
|
331
|
+
with self.subTest(change=change):
|
|
332
|
+
self.assertEqual(self.invoke(module, {**args, **change})['error']['code'], 'INVALID_REQUEST')
|
|
333
|
+
|
|
334
|
+
def test_capture_refuses_incomplete_capture_and_linked_materials(self):
|
|
335
|
+
module = load('web_capture')
|
|
336
|
+
|
|
337
|
+
def capture(args, timeout, workspace):
|
|
338
|
+
destination = workspace / 'capture'
|
|
339
|
+
destination.mkdir()
|
|
340
|
+
(destination / 'outside.txt').symlink_to('/etc/passwd')
|
|
341
|
+
|
|
342
|
+
with patch.dict(os.environ, {'HAI_TOOL_BROWSER_PROXY': 'http://192.0.2.10:3128'}, clear=True):
|
|
343
|
+
with patch.object(module, 'run_command', capture, create=True):
|
|
344
|
+
result = self.invoke(module, {'operation': 'web.capture', 'url': 'https://example.com/', 'output': 'capture.zip'})
|
|
345
|
+
self.assertFalse(result['success'])
|
|
346
|
+
self.assertEqual(list(self.outputs.iterdir()), [])
|
|
347
|
+
|
|
348
|
+
def test_video_renders_valid_local_project_and_publishes_mp4(self):
|
|
349
|
+
module = load('video_render')
|
|
350
|
+
source = self.project(entries=[('assets/narration.wav', b'RIFFfixture')])
|
|
351
|
+
calls = []
|
|
352
|
+
|
|
353
|
+
def render(args, timeout, workspace):
|
|
354
|
+
calls.append(args)
|
|
355
|
+
self.assertEqual((workspace / 'project' / 'index.html').read_text().count('data-composition-id'), 1)
|
|
356
|
+
self.assertEqual((workspace / 'project' / 'assets' / 'narration.wav').read_bytes(), b'RIFFfixture')
|
|
357
|
+
(workspace / 'result.mp4').write_bytes(b'\x00\x00\x00\x20ftypisom' + b'fixture' * 4)
|
|
358
|
+
|
|
359
|
+
with patch.object(module, 'run_command', render, create=True):
|
|
360
|
+
result = self.invoke(module, {'operation': 'video.render', 'input': source, 'output': 'intro.mp4', 'fps': 24})
|
|
361
|
+
self.assertTrue(result['success'], result)
|
|
362
|
+
self.assertEqual(result['data']['artifact']['name'], 'intro.mp4')
|
|
363
|
+
self.assertEqual(len(calls), 1)
|
|
364
|
+
self.assertIn('24', calls[0])
|
|
365
|
+
|
|
366
|
+
def test_video_rejects_archive_escape_links_and_duplicate_paths(self):
|
|
367
|
+
module = load('video_render')
|
|
368
|
+
cases = [('../escape.txt', b'escape'), ('/absolute.txt', b'escape'), ('assets\\escape.txt', b'escape'), ('index.html', b'duplicate')]
|
|
369
|
+
for index, entry in enumerate(cases):
|
|
370
|
+
with self.subTest(entry=entry[0]):
|
|
371
|
+
source = self.project(name=f'unsafe{index}.zip', entries=[entry])
|
|
372
|
+
result = self.invoke(module, {'operation': 'video.render', 'input': source, 'output': f'unsafe{index}.mp4'})
|
|
373
|
+
self.assertEqual(result['error']['code'], 'UNSAFE_PROJECT')
|
|
374
|
+
source = self.project(name='link.zip')
|
|
375
|
+
with zipfile.ZipFile(self.inputs / source, 'a') as archive:
|
|
376
|
+
link = zipfile.ZipInfo('assets/link')
|
|
377
|
+
link.create_system = 3
|
|
378
|
+
link.external_attr = (stat.S_IFLNK | 0o777) << 16
|
|
379
|
+
archive.writestr(link, '/etc/passwd')
|
|
380
|
+
self.assertEqual(self.invoke(module, {'operation': 'video.render', 'input': source, 'output': 'link.mp4'})['error']['code'], 'UNSAFE_PROJECT')
|
|
381
|
+
self.assertEqual(list(self.outputs.iterdir()), [])
|
|
382
|
+
|
|
383
|
+
def test_video_checks_duration_resolution_and_fps_before_starting(self):
|
|
384
|
+
module = load('video_render')
|
|
385
|
+
for index, attributes in enumerate([
|
|
386
|
+
'data-duration="31" data-width="1280" data-height="720"',
|
|
387
|
+
'data-duration="NaN" data-width="1280" data-height="720"',
|
|
388
|
+
'data-duration="4" data-width="3840" data-height="2160"',
|
|
389
|
+
]):
|
|
390
|
+
with self.subTest(attributes=attributes):
|
|
391
|
+
source = self.project(name=f'limit{index}.zip', html=f'<div data-composition-id="intro" {attributes}></div>')
|
|
392
|
+
self.assertEqual(self.invoke(module, {'operation': 'video.render', 'input': source, 'output': f'limit{index}.mp4'})['error']['code'], 'INVALID_PROJECT')
|
|
393
|
+
source = self.project()
|
|
394
|
+
self.assertEqual(self.invoke(module, {'operation': 'video.render', 'input': source, 'output': 'bad.mp4', 'fps': True})['error']['code'], 'INVALID_REQUEST')
|
|
395
|
+
|
|
396
|
+
def test_video_failed_renderer_cannot_publish_partial_output(self):
|
|
397
|
+
module = load('video_render')
|
|
398
|
+
source = self.project()
|
|
399
|
+
|
|
400
|
+
def fail(args, timeout, workspace):
|
|
401
|
+
(workspace / 'result.mp4').write_bytes(b'partial')
|
|
402
|
+
raise ToolError('TOOL_FAILED', 'Renderer failed')
|
|
403
|
+
|
|
404
|
+
with patch.object(module, 'run_command', fail, create=True):
|
|
405
|
+
result = self.invoke(module, {'operation': 'video.render', 'input': source, 'output': 'failed.mp4'})
|
|
406
|
+
self.assertEqual(result['error']['code'], 'TOOL_FAILED')
|
|
407
|
+
self.assertEqual(list(self.outputs.iterdir()), [])
|
|
408
|
+
|
|
409
|
+
def test_video_rejects_remote_and_escaping_project_resources(self):
|
|
410
|
+
module = load('video_render')
|
|
411
|
+
for index, element in enumerate([
|
|
412
|
+
'<img src="https://example.com/remote.png">',
|
|
413
|
+
'<script src="../outside.js"></script>',
|
|
414
|
+
'<style>body { background: url(file:///etc/passwd) }</style>',
|
|
415
|
+
]):
|
|
416
|
+
with self.subTest(element=element):
|
|
417
|
+
source = self.project(name=f'resource{index}.zip', html=f'<div data-composition-id="intro" data-duration="4" data-width="1280" data-height="720">{element}</div>')
|
|
418
|
+
result = self.invoke(module, {'operation': 'video.render', 'input': source, 'output': f'resource{index}.mp4'})
|
|
419
|
+
self.assertEqual(result['error']['code'], 'INVALID_PROJECT')
|
|
420
|
+
|
|
421
|
+
def test_project_entry_count_is_bounded_before_renderer_starts(self):
|
|
422
|
+
module = load('video_render')
|
|
423
|
+
source = self.project(entries=[(f'assets/file{i}.txt', 'fixture') for i in range(256)])
|
|
424
|
+
result = self.invoke(module, {'operation': 'video.render', 'input': source, 'output': 'excessive.mp4'})
|
|
425
|
+
self.assertEqual(result['error']['code'], 'UNSAFE_PROJECT')
|
|
426
|
+
|
|
427
|
+
def test_video_rejects_duplicate_html_attributes_that_browsers_interpret_differently(self):
|
|
428
|
+
module = load('video_render')
|
|
429
|
+
source = self.project(html='<div data-composition-id="intro" data-duration="31" data-duration="4" data-width="1280" data-height="720">Ambiguous duration</div>')
|
|
430
|
+
result = self.invoke(module, {'operation': 'video.render', 'input': source, 'output': 'ambiguous.mp4'})
|
|
431
|
+
self.assertEqual(result['error']['code'], 'INVALID_PROJECT')
|
|
432
|
+
|
|
433
|
+
def test_video_allows_nested_compositions_to_reference_local_shared_assets(self):
|
|
434
|
+
module = load('video_render')
|
|
435
|
+
source = self.project(entries=[
|
|
436
|
+
('compositions/scene.html', '<img src="../assets/page.png">'),
|
|
437
|
+
('assets/page.png', b'\x89PNG\r\n\x1a\nfixture'),
|
|
438
|
+
])
|
|
439
|
+
|
|
440
|
+
def render(args, timeout, workspace):
|
|
441
|
+
(workspace / 'result.mp4').write_bytes(b'\x00\x00\x00\x20ftypisom' + b'fixture' * 4)
|
|
442
|
+
|
|
443
|
+
with patch.object(module, 'run_command', render):
|
|
444
|
+
result = self.invoke(module, {'operation': 'video.render', 'input': source, 'output': 'nested.mp4'})
|
|
445
|
+
self.assertTrue(result['success'], result)
|
|
446
|
+
|
|
447
|
+
|
|
448
|
+
if __name__ == '__main__':
|
|
449
|
+
unittest.main()
|