1
0
mirror of https://github.com/yt-dlp/yt-dlp.git synced 2025-12-19 06:28:52 +00:00

test wasm

This commit is contained in:
c-basalt
2025-01-02 00:17:10 -05:00
parent e8eb983583
commit e0697299b6
4 changed files with 303 additions and 18 deletions

View File

@@ -16,10 +16,14 @@ sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from test.helper import (
FakeYDL,
)
from yt_dlp.utils import (
variadic,
)
from yt_dlp.cookies import YoutubeDLCookieJar
from yt_dlp.jsinterp.common import ExternalJSI
from yt_dlp.jsinterp.common import ExternalJSI, _ALL_FEATURES
from yt_dlp.jsinterp._deno import DenoJSI, DenoJITlessJSI, DenoJSDomJSI
from yt_dlp.jsinterp._phantomjs import PhantomJSJSI
from yt_dlp.jsinterp._helper import prepare_wasm_jsmodule
@dataclasses.dataclass
@@ -49,9 +53,26 @@ class NetscapeFields:
return all(getattr(self, attr) == getattr(other, attr) for attr in ['name', 'value', 'domain', 'path', 'secure', 'expires'])
covered_features = set()
def requires_feature(features):
covered_features.update(variadic(features))
def outer(func):
def wrapper(self, *args, **kwargs):
if not self.jsi._SUPPORTED_FEATURES.issuperset(variadic(features)):
print(f'{self._JSI_CLASS.__name__} does not support {features!r}, skipping')
self.skipTest(f'{"&".join(variadic(features))} not supported')
return func(self, *args, **kwargs)
return wrapper
return outer
class Base:
class TestExternalJSI(unittest.TestCase):
_JSI_CLASS: type[ExternalJSI] = None
_TESTDATA_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'testdata', 'jsi_external')
maxDiff = 2000
def setUp(self):
@@ -77,28 +98,21 @@ class Base:
jsi = self._JSI_CLASS(self.ydl, self.url_param, 10, {}, user_agent='test/ua')
self.assertEqual(jsi.execute('console.log(navigator.userAgent);'), 'test/ua')
@requires_feature('location')
def test_location(self):
if 'location' not in self._JSI_CLASS._SUPPORTED_FEATURES:
print(f'{self._JSI_CLASS.__name__} does not support location, skipping')
self.skipTest('Location not supported')
self.url_param = 'https://example.com/123/456'
self.assertEqual(self.jsi.execute('console.log(JSON.stringify([location.href, location.hostname]));'),
'["https://example.com/123/456","example.com"]')
@requires_feature('dom')
def test_execute_dom_parse(self):
if 'dom' not in self.jsi._SUPPORTED_FEATURES:
print(f'{self._JSI_CLASS.__name__} does not support DOM, skipping')
self.skipTest('DOM not supported')
self.assertEqual(self.jsi.execute(
'console.log(document.getElementById("test-div").innerHTML);',
html='<html><body><div id="test-div">Hello, world!</div></body></html>'),
'Hello, world!')
@requires_feature('dom')
def test_execute_dom_script(self):
if 'dom' not in self.jsi._SUPPORTED_FEATURES:
print(f'{self._JSI_CLASS.__name__} does not support DOM, skipping')
self.skipTest('DOM not supported')
self.assertEqual(self.jsi.execute(
'console.log(document.getElementById("test-div").innerHTML);',
html='''<html><head><title>Hello, world!</title><body>
@@ -112,11 +126,8 @@ class Base:
</body></html>'''),
'Hello, world!')
@requires_feature(['dom', 'location'])
def test_dom_location(self):
if not self._JSI_CLASS._SUPPORTED_FEATURES.issuperset({'dom', 'location'}):
print(f'{self._JSI_CLASS.__name__} does not support both DOM and location, skipping')
self.skipTest('DOM or location not supported')
self.url_param = 'https://example.com/123/456'
self.assertEqual(self.jsi.execute(
'console.log(document.getElementById("test-div").innerHTML);',
@@ -125,10 +136,8 @@ class Base:
<body><div id="test-div">Hello, world!</div></body></html>'''),
'example.com')
@requires_feature('cookies')
def test_execute_cookiejar(self):
if 'cookies' not in self.jsi._SUPPORTED_FEATURES:
print(f'{self._JSI_CLASS.__name__} does not support cookies, skipping')
self.skipTest('Cookies not supported')
cookiejar = YoutubeDLCookieJar()
ref_cookiejar = YoutubeDLCookieJar()
@@ -176,6 +185,22 @@ class Base:
cookiejar=cookiejar),
'test1=new1; test2=new2; test3=test3; test5=test5')
@requires_feature('wasm')
def test_wasm(self):
with open(os.path.join(self._TESTDATA_DIR, 'hello_wasm.js')) as f:
js_mod = f.read()
with open(os.path.join(self._TESTDATA_DIR, 'hello_wasm_bg.wasm'), 'rb') as f:
wasm = f.read()
js_base = prepare_wasm_jsmodule(js_mod, wasm)
js_code = js_base + ''';
console.log(add(1, 2));
greet('world');
'''
self.assertEqual(self.jsi.execute(js_code), '3\nHello, world!')
class TestDeno(Base.TestExternalJSI):
_JSI_CLASS = DenoJSI
@@ -193,5 +218,8 @@ class TestPhantomJS(Base.TestExternalJSI):
_JSI_CLASS = PhantomJSJSI
expect_covered_features = set(_ALL_FEATURES) - {'js'}
assert covered_features.issuperset(expect_covered_features), f'Missing tests for features: {expect_covered_features - covered_features}'
if __name__ == '__main__':
unittest.main()