Repository navigation
Expand file tree
/
Copy pathtest_parser_browser.py
More file actions
285 lines (267 loc) · 15.3 KB
/
Copy pathtest_parser_browser.py
File metadata and controls
285 lines (267 loc) · 15.3 KB
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
#!/usr/bin/env python3
"""Real UI checks for failed-item retry and same-label stream identity."""
import os
import json
import socket
import subprocess
import sys
import time
import urllib.request
from pathlib import Path
from playwright.sync_api import sync_playwright
ROOT = Path(__file__).resolve().parents[1]
def check_failed_download_subtitle_status(browser, origin: str) -> None:
page = browser.new_page()
fixture = {'id': 'failed-media', 'kind': 'media', 'title': 'failed-media',
'status': 'failed', 'payload': {'link': 'https://example.test/video'},
'logs': [], 'error': '平台要求交互验证',
'result': {'outputPath': None, 'subtitleJob': {'status': 'unavailable'}}}
try:
page.route('**/api/tasks?kind=media', lambda route: route.fulfill(
content_type='application/json', body=json.dumps({'success': True, 'tasks': [fixture]})))
page.goto(f'{origin}/use#completed')
page.get_by_role('button', name='查看详情', exact=True).first.click()
dialog = page.get_by_role('dialog')
assert '视频文件已经完成' not in dialog.inner_text()
assert dialog.locator('.subtitle-job-section').count() == 0
finally:
page.close()
def check_portrait_resolution(browser, origin: str) -> None:
page = browser.new_page()
try:
page.goto(f'{origin}/use#parse')
for width, height, expected in (
(1080, 1920, '1080P'), (1920, 1080, '1080P'),
(720, 1280, '720P'), (1440, 2560, '2K'),
(2160, 3840, '4K'), (3840, 2160, '4K'),
):
assert page.evaluate('(s) => window.StreamDockQuality.friendlyResolution(s)',
{'width': width, 'height': height}) == expected
finally:
page.close()
def check_share_text_and_stale_edits(browser, origin: str) -> None:
"""Deterministic UI regression; API metadata is a fixture, not a live probe."""
page = browser.new_page()
fixture = {
'success': True, 'platform': 'douyin', 'title': '分享文案测试',
'contentType': 'video', 'videoStreams': [
{'streamId': 'sid:share-text', 'qualityLabel': '高清', 'height': 720, 'codec': 'h264'}
], 'audioStreams': [], 'assetSummary': {'subtitleCount': 0},
'recommendations': {'best_quality': {'stream': {
'streamId': 'sid:share-text', 'qualityLabel': '高清', 'height': 720, 'codec': 'h264'
}}}, 'probeSummary': {'qualityCount': 1},
}
page.route('**/api/media/probe', lambda route: route.fulfill(json=fixture))
page.goto(f'{origin}/use#parse')
page.locator('#link').fill('9.94 分享文案 中文😀 https://v.douyin.com/share-fixture/ 复制此链接,打开Dou音观看!')
page.locator('#submitButton').click()
page.wait_for_function("document.querySelector('#submitButton').textContent.includes('确认并开始下载')", timeout=7000)
assert not page.locator('#mediaProbePreview').is_hidden()
assert '分享文案测试' in page.locator('#mediaProbeTitle').inner_text()
page.goto(f'{origin}/use#parse')
page.evaluate("""fixture => {
window.fetch = () => new Promise(resolve => {
window.__releaseProbe = () => resolve(new Response(JSON.stringify(fixture),
{headers: {'Content-Type':'application/json'}}));
});
document.querySelector('#link').value = '文案 https://v.douyin.com/old/';
window.__completed = false;
window.StreamDockQuality.probeQualityOptions('https://v.douyin.com/old/', {silent:true})
.then(result => { window.__probeResult = result; window.__completed = true; });
}""", fixture)
page.evaluate("document.querySelector('#link').value = '新文案 https://v.douyin.com/new/'; window.__releaseProbe()")
page.wait_for_function('window.__completed')
assert page.evaluate('window.__probeResult === null')
page.close()
def check_probe_failure_diagnostics(browser, origin: str) -> None:
page = browser.new_page()
try:
page.route('**/api/media/probe', lambda route: route.fulfill(json={
'success': False, 'error': '媒体解析未取得可用资源', 'traceId': 'fixture-trace',
'errorInfo': {'code': 'parser_failed', 'stage': 'browser_capture',
'causes': ['share-page: structure missing', 'no-login: No media URL captured']},
}))
page.goto(f'{origin}/use#parse')
page.locator('#link').fill('https://v.douyin.com/diagnostic-fixture/')
page.locator('#submitButton').click()
page.wait_for_function("document.body.textContent.includes('诊断编号:fixture-trace')")
text = page.locator('body').inner_text()
assert '失败阶段:browser_capture' in text
assert 'no-login: No media URL captured' in text
assert '请确认浏览器登录态' not in text
assert not page.locator('#submitButton').is_disabled()
finally:
page.close()
def check_stalled_response_body(browser, origin: str) -> None:
"""Use a real HTTP body that stalls after headers, not a mocked promise."""
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
from threading import Event, Thread
release = Event()
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.send_header('Content-Length', '9999')
self.send_header('Access-Control-Allow-Origin', '*')
self.end_headers()
self.wfile.write(b'{')
self.wfile.flush()
release.wait(5)
def log_message(self, *_args):
pass
http_server = ThreadingHTTPServer(('127.0.0.1', 0), Handler)
thread = Thread(target=http_server.serve_forever, daemon=True)
thread.start()
page = browser.new_page()
try:
page.goto(f'{origin}/use#parse')
page.evaluate("""url => {
const originalFetch = window.fetch.bind(window);
const originalTimeout = window.setTimeout.bind(window);
window.setTimeout = (callback, delay, ...args) => originalTimeout(callback, delay === 120000 ? 450 : delay, ...args);
window.fetch = (urlArg, options = {}) => String(urlArg).endsWith('/api/media/probe')
? originalFetch(url, {signal: options.signal}) : originalFetch(urlArg, options);
}""", f'http://127.0.0.1:{http_server.server_port}/stalled-json')
page.locator('#link').fill('文案 https://v.douyin.com/stalled/')
page.locator('#submitButton').click()
page.wait_for_function("document.body.textContent.includes('清晰度识别超时')", timeout=2500)
assert not page.locator('#submitButton').is_disabled()
assert page.locator('#submitButton').inner_text() == '开始解析'
finally:
page.close()
release.set()
http_server.shutdown()
http_server.server_close()
thread.join(timeout=2)
def main() -> None:
with socket.socket() as sock:
sock.bind(('127.0.0.1', 0))
port = sock.getsockname()[1]
server = subprocess.Popen([sys.executable, '-m', 'uvicorn', 'app:app', '--host', '127.0.0.1', '--port', str(port)],
cwd=ROOT, env={**os.environ, 'STREAMDOCK_TASK_STORAGE_PATH': ''},
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
try:
for _ in range(100):
try:
urllib.request.urlopen(f'http://127.0.0.1:{port}/api/health/live', timeout=1).close()
break
except Exception:
time.sleep(.1)
else:
raise RuntimeError('test server did not start')
with sync_playwright() as playwright:
browser = playwright.chromium.launch(headless=True)
page = browser.new_page()
page.goto(f'http://127.0.0.1:{port}/use#parse')
page.evaluate('''() => {
const original = window.fetch.bind(window);
window.__sent = null; window.__failedOnce = false;
window.fetch = (url, options = {}) => {
if (String(url).endsWith('/api/media/probe')) {
const link = JSON.parse(options.body).link;
if (link === 'https://example.test/broken' && !window.__failedOnce) {
window.__failedOnce = true;
return Promise.resolve(new Response(JSON.stringify({success:false,error:'fixture failed'}),
{status:200,headers:{'Content-Type':'application/json'}}));
}
const streams = [
{streamId:'sid:avc',qualityLabel:'高清',codec:'avc',width:1280,height:720,bitrate:1000000},
{streamId:'sid:hevc',qualityLabel:'高清',codec:'hevc',width:1280,height:720,bitrate:900000}
];
return Promise.resolve(new Response(JSON.stringify({success:true,platform:'kuaishou',
title:'fixture',contentType:'video',videoStreams:streams,audioStreams:[],
recommendations:{best_quality:{stream:streams[0]}},probeSummary:{qualityCount:2},assetSummary:{subtitleCount:0}}),
{status:200,headers:{'Content-Type':'application/json'}}));
}
if (String(url).endsWith('/api/fetch/batch')) {
window.__sent = JSON.parse(options.body);
return Promise.resolve(new Response(JSON.stringify({success:true,tasks:[{id:'task-1'}]}),
{status:200,headers:{'Content-Type':'application/json'}}));
}
return original(url, options);
};
}''')
page.locator('#link').fill('https://example.test/good\nhttps://example.test/broken')
page.locator('#submitButton').click()
try:
page.wait_for_function("document.querySelector('#submitButton').textContent.includes('继续处理 1 条')", timeout=7000)
except Exception:
print('DEBUG', page.locator('#submitButton').inner_text(), page.locator('#mediaProbePreview').inner_text()[:700])
raise
page.locator('#mediaProbeRetryFailed').click()
page.locator('#submitButton').click()
try:
page.wait_for_function("document.querySelector('#submitButton').textContent.includes('确认并开始')", timeout=7000)
except Exception:
print('RETRY_DEBUG', page.locator('#submitButton').inner_text(), page.locator('#mediaProbePreview').inner_text()[:500])
raise
page.locator('#submitButton').click()
page.wait_for_function('window.__sent !== null')
assert page.evaluate('window.__sent.links') == ['https://example.test/good', 'https://example.test/broken']
page.wait_for_function("location.hash === '#downloading'")
page.goto(f'http://127.0.0.1:{port}/use#parse')
page.locator('#link').fill('https://example.test/good')
# Reinstall the probe fixture after navigation.
page.evaluate('''() => { const original = window.fetch.bind(window); window.__sent = null;
window.fetch = (url, options = {}) => {
if (String(url).endsWith('/api/media/probe')) {
const streams = [{streamId:'sid:avc',qualityLabel:'高清',codec:'avc',width:1280,height:720,bitrate:1000000},
{streamId:'sid:hevc',qualityLabel:'高清',codec:'hevc',width:1280,height:720,bitrate:900000}];
return Promise.resolve(new Response(JSON.stringify({success:true,platform:'kuaishou',title:'fixture',
contentType:'video',videoStreams:streams,audioStreams:[],recommendations:{best_quality:{stream:streams[0]}},
probeSummary:{qualityCount:2},assetSummary:{subtitleCount:0}}),
{status:200,headers:{'Content-Type':'application/json'}}));
}
if (String(url).endsWith('/api/fetch/batch')) { window.__sent = JSON.parse(options.body);
return Promise.resolve(new Response(JSON.stringify({success:true,tasks:[{id:'task-2'}]}),
{status:200,headers:{'Content-Type':'application/json'}})); }
return original(url, options);
};
}''')
page.locator('#submitButton').click()
page.wait_for_function("document.querySelector('#submitButton').textContent.includes('确认并开始下载')")
page.locator('#mediaProbeToggle').click()
page.locator('[data-select-stream="sid:hevc"]').click()
page.locator('#submitButton').click()
page.wait_for_function('window.__sent !== null')
assert page.evaluate('window.__sent.videoQuality') == 'sid:hevc'
page.goto(f'http://127.0.0.1:{port}/use#settings')
page.locator('[data-use-tab="settings"]').click()
page.evaluate('''() => { const original = window.fetch.bind(window); window.__diagCancelled = false;
window.fetch = (url, options = {}) => {
const value = String(url);
if (value === '/api/media/diagnostics' && options.method === 'POST')
return Promise.resolve(new Response(JSON.stringify({success:true,diagnostic:{id:'diag-fixture',status:'queued'}}),
{status:202,headers:{'Content-Type':'application/json'}}));
if (value === '/api/media/diagnostics/diag-fixture')
return Promise.resolve(new Response(JSON.stringify({success:true,diagnostic:{id:'diag-fixture',
status:window.__diagCancelled?'cancelled':'running',results:[]}}),
{status:200,headers:{'Content-Type':'application/json'}}));
if (value === '/api/tasks/diag-fixture' && options.method === 'DELETE') {
window.__diagCancelled = true;
return Promise.resolve(new Response(JSON.stringify({success:true}),
{status:200,headers:{'Content-Type':'application/json'}}));
}
return original(url, options);
};
}''')
page.locator('#mediaDiagnosticLinks').fill('https://weibo.com/tv/show/1034:12345')
page.locator('#mediaDiagnosticRun').click()
page.locator('#mediaDiagnosticCancel').click()
page.wait_for_function("document.querySelector('#mediaDiagnosticStatus').textContent.includes('cancelled')", timeout=5000)
assert page.evaluate('window.__diagCancelled')
check_share_text_and_stale_edits(browser, f'http://127.0.0.1:{port}')
check_stalled_response_body(browser, f'http://127.0.0.1:{port}')
check_probe_failure_diagnostics(browser, f'http://127.0.0.1:{port}')
check_portrait_resolution(browser, f'http://127.0.0.1:{port}')
check_failed_download_subtitle_status(browser, f'http://127.0.0.1:{port}')
browser.close()
print('REAL_BROWSER_PARSER=passed batch-retry=preserved stream-id=hevc diagnostic-cancel=passed share-text=accepted stale-edit=discarded real-stalled-body=timeout-visible')
finally:
server.terminate()
try:
server.wait(timeout=5)
except subprocess.TimeoutExpired:
server.kill()
if __name__ == '__main__':
main()