2021-12-30 22:23:32 -07:00
|
|
|
import datetime
|
2026-06-11 17:54:22 -06:00
|
|
|
import io
|
2021-12-30 22:23:32 -07:00
|
|
|
import json
|
|
|
|
|
import pathlib
|
2026-06-11 17:54:22 -06:00
|
|
|
import zipfile
|
2023-07-27 15:53:47 -06:00
|
|
|
from typing import List
|
2021-12-30 22:23:32 -07:00
|
|
|
from uuid import uuid4
|
|
|
|
|
|
|
|
|
|
import pytest
|
2022-12-23 16:04:09 -07:00
|
|
|
import pytz
|
2021-12-30 22:23:32 -07:00
|
|
|
|
2022-02-02 20:43:11 -07:00
|
|
|
from modules.archive.lib import archive_strftime
|
2025-10-29 22:16:33 -06:00
|
|
|
from modules.archive.models import Archive
|
|
|
|
|
from wrolpi.collections import Collection
|
2026-05-07 15:55:38 -06:00
|
|
|
# Re-export make_test_ctx so existing `from modules.archive.conftest import make_test_ctx`
|
|
|
|
|
# call sites keep working. The canonical implementation lives in wrolpi/conftest.py.
|
|
|
|
|
from wrolpi.conftest import make_test_ctx # noqa: F401
|
2021-12-30 22:23:32 -07:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def archive_directory(test_directory) -> pathlib.Path:
|
|
|
|
|
path = test_directory / 'archive'
|
|
|
|
|
path.mkdir()
|
|
|
|
|
return path
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
2025-10-12 17:19:54 -06:00
|
|
|
def archive_factory(test_session, archive_directory, make_files_structure, image_bytes_factory):
|
2021-12-30 22:23:32 -07:00
|
|
|
def time_generator():
|
2022-12-23 16:04:09 -07:00
|
|
|
timestamp = datetime.datetime(2000, 1, 1, 0, 0, 0).astimezone(pytz.UTC)
|
2021-12-30 22:23:32 -07:00
|
|
|
while True:
|
|
|
|
|
timestamp += datetime.timedelta(seconds=1)
|
|
|
|
|
yield timestamp
|
|
|
|
|
|
|
|
|
|
now = time_generator()
|
|
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
def _(domain: str = None, url: str = None, title: str = 'NA', contents: str = None,
|
2025-10-12 17:19:54 -06:00
|
|
|
singlefile_contents: str = None, tag_names: List[str] = None, screenshot: bool = True) -> Archive:
|
2021-12-31 22:35:23 -07:00
|
|
|
if domain:
|
|
|
|
|
assert '/' not in domain
|
|
|
|
|
|
2023-07-27 15:53:47 -06:00
|
|
|
tag_names = tag_names or list()
|
|
|
|
|
|
2021-12-31 20:23:49 -07:00
|
|
|
title = title or str(uuid4())
|
2021-12-30 22:23:32 -07:00
|
|
|
domain_dir = domain or title
|
|
|
|
|
domain_dir = archive_directory / domain_dir
|
|
|
|
|
domain_dir.mkdir(exist_ok=True)
|
|
|
|
|
|
2023-10-24 10:20:06 -06:00
|
|
|
download_datetime = next(now)
|
|
|
|
|
timestamp = archive_strftime(download_datetime)
|
2021-12-30 22:23:32 -07:00
|
|
|
|
2022-08-06 05:22:07 +00:00
|
|
|
json_contents = {}
|
|
|
|
|
if title != 'NA':
|
|
|
|
|
json_contents['title'] = title
|
|
|
|
|
if url:
|
|
|
|
|
json_contents['url'] = url
|
|
|
|
|
json_contents = json.dumps(json_contents)
|
2025-10-12 17:19:54 -06:00
|
|
|
singlefile_path, readability_path, readability_txt_path, readability_json_path = \
|
2022-08-06 05:22:07 +00:00
|
|
|
make_files_structure({
|
|
|
|
|
str(domain_dir / f'{timestamp}_{title}.html'): singlefile_contents or '<html></html>',
|
|
|
|
|
str(domain_dir / f'{timestamp}_{title}.readability.html'): '<html></html>',
|
|
|
|
|
str(domain_dir / f'{timestamp}_{title}.readability.txt'): contents,
|
|
|
|
|
str(domain_dir / f'{timestamp}_{title}.readability.json'): json_contents,
|
|
|
|
|
})
|
2021-12-30 22:23:32 -07:00
|
|
|
|
|
|
|
|
if domain:
|
2025-10-29 22:16:33 -06:00
|
|
|
# Find or create domain collection
|
|
|
|
|
collection = test_session.query(Collection).filter_by(
|
|
|
|
|
name=domain,
|
|
|
|
|
kind='domain'
|
|
|
|
|
).one_or_none()
|
|
|
|
|
if not collection:
|
|
|
|
|
collection = Collection(
|
|
|
|
|
name=domain,
|
|
|
|
|
kind='domain',
|
|
|
|
|
directory=None, # Domain collections are unrestricted
|
|
|
|
|
)
|
|
|
|
|
test_session.add(collection)
|
|
|
|
|
test_session.flush([collection])
|
2026-08-11 18:45:05 -06:00
|
|
|
# Archive.validate() -> apply_domain() clears collection_id when URL is missing.
|
|
|
|
|
# Give a URL derived from the domain so the link survives validation.
|
|
|
|
|
if not url:
|
|
|
|
|
url = f'https://{domain}/'
|
2025-10-29 22:16:33 -06:00
|
|
|
else:
|
|
|
|
|
collection = None
|
2021-12-30 22:23:32 -07:00
|
|
|
|
2025-10-12 17:19:54 -06:00
|
|
|
screenshot_path = None
|
|
|
|
|
if screenshot:
|
|
|
|
|
screenshot_path = domain_dir / f'{timestamp}_{title}.png'
|
|
|
|
|
screenshot_path.write_bytes(image_bytes_factory())
|
|
|
|
|
|
2023-03-24 08:58:18 -06:00
|
|
|
# Only add files that were created.
|
|
|
|
|
files = (readability_path, readability_json_path, readability_txt_path, screenshot_path, singlefile_path)
|
|
|
|
|
files = list(filter(None, files))
|
|
|
|
|
|
|
|
|
|
archive = Archive.from_paths(test_session, *files)
|
|
|
|
|
archive.url = url
|
|
|
|
|
archive.title = title
|
2023-10-24 10:20:06 -06:00
|
|
|
archive.file_group.download_datetime = archive.file_group.published_datetime = next(now)
|
2023-10-20 18:53:26 -06:00
|
|
|
archive.file_group.modification_datetime = next(now)
|
2025-10-29 22:16:33 -06:00
|
|
|
archive.collection = collection
|
2023-03-24 08:58:18 -06:00
|
|
|
archive.validate()
|
2021-12-30 22:23:32 -07:00
|
|
|
|
2023-07-27 15:53:47 -06:00
|
|
|
for tag_name in tag_names:
|
2025-12-10 21:16:10 -07:00
|
|
|
archive.add_tag(test_session, tag_name)
|
2023-07-27 15:53:47 -06:00
|
|
|
|
2021-12-30 22:23:32 -07:00
|
|
|
return archive
|
|
|
|
|
|
|
|
|
|
return _
|
2026-06-11 17:54:22 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def compressed_singlefile_factory():
|
|
|
|
|
"""Mimic the file created by `single-file --compress-content`: an HTML prelude containing the
|
|
|
|
|
SingleFile comment header, followed by a ZIP containing the uncompressed page as index.html."""
|
|
|
|
|
|
|
|
|
|
def _(url: str = 'https://example.com', title: str = 'compressed title') -> bytes:
|
|
|
|
|
prelude = '<!DOCTYPE html> <html data-sfz><!--\n' \
|
|
|
|
|
' Page saved with SingleFile \n' \
|
|
|
|
|
f' url: {url} \n' \
|
|
|
|
|
' saved date: Thu Jun 11 2026 22:49:39 GMT+0000 (Coordinated Universal Time)\n' \
|
|
|
|
|
'--><meta charset=windows-1252><title></title>'
|
|
|
|
|
index_html = '<html><!--\n Page saved with SingleFile \n' \
|
|
|
|
|
f' url: {url} \n' \
|
Single file deno (#478)
* Singlefile runs under Deno 2.8.3; browser leftovers are reaped.
single-file-cli is designed for Deno. Under node 18 (Debian 12/RPi OS) it
cannot reach the browser CDP on IPv6-enabled hosts ("localhost" resolves to
::1 first with no IPv4 fallback) and it never exits after a successful save,
blocking the download worker forever.
The native path and the Archive docker container now run single-file with
"deno run". Deno is pinned at v2.8.3 everywhere because yt-dlp 2026.06.09
requires at least 2.3.0 (our node is Debian's v18, below yt-dlp's v22
minimum). Deno >= 2.3 kills the browser single-file spawns (piped stderr
that single-file never reads), so the browser is run through
singlefile_browser_wrapper.sh which discards its stderr.
Commands are run in their own process group which is killed when they
finish, reaping the browser single-file leaves behind. The Archive
downloader now has a ten minute timeout. Install scripts install Deno
(previously only upgrade.sh and the image builds did).
* Singlefile writes to a file instead of dumping to stdout.
Deno >= 2.3 truncates large --dump-content output at exit; compressed
(SingleFileZ) singlefiles of large pages lost their ZIP tail, failed the
compression check in write_archive_files, and were destroyed by the HTML
prettifier (blank white archives). single-file now writes its result to a
temporary file which WROLPi reads back.
As insurance, a singlefile marked compressed (<html data-sfz) whose ZIP is
unreadable is now stored verbatim instead of being prettified.
* Compressed Archive screenshots and readability images are no longer broken.
The index.html inside a compressed (SingleFileZ) singlefile references its
resources by relative ZIP paths. Screenshots are now taken from the fully
extracted ZIP so those references resolve. Readability images are inlined
as data URIs read from the ZIP (readability-extractor absolutizes relative
srcs against the page URL, so both forms are matched).
* Compressed Archives are created by converting the uncompressed capture.
The page is always captured uncompressed; readability, screenshot, and
title come from that canonical page (its images are inline data URIs, so
no ZIP extraction or image inlining is needed). When compress_singlefile
is set, a second single-file run converts the captured file into a
compressed (SingleFileZ) self-extracting file. The conversion is a local
file:// operation - the page is not fetched again, and single-file
preserves the original URL header when re-saving its own pages. If the
conversion fails, the uncompressed singlefile is stored instead.
* Consolidating duplicate compressed-singlefile tests.
The write_archive_files tests (verbatim write, title fallback, broken-ZIP
guard, readability inlining) are combined into one test; the helper probes
and the setting-plumbing tests are each merged into one. The do_singlefile
compression-failure scenario and the timeout constant are folded into the
main do_singlefile test, which shared their setup.
* Addressing review comments: removeprefix, PermissionError when killing.
* Bumping version to 0.24.1-beta.
* Install scripts assert the deno.zip hash to avoid malicious or invalid files.
The hashes are the official sha256sum assets of the Deno v2.8.3 release,
per architecture.
* Archive Dockerfile asserts the deno.zip hash to avoid malicious or invalid files.
---------
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 17:07:10 -06:00
|
|
|
f'-->\n<head><title>{title}</title></head>' \
|
|
|
|
|
'<body>test compressed singlefile<img src="images/0.png" srcset="images/0.png 1x"></body>' \
|
|
|
|
|
'</html>'
|
2026-06-11 17:54:22 -06:00
|
|
|
buf = io.BytesIO()
|
|
|
|
|
buf.write(prelude.encode())
|
|
|
|
|
# Deflate like the real single-file CLI; the page must not be readable as plain text.
|
|
|
|
|
with zipfile.ZipFile(buf, 'a', compression=zipfile.ZIP_DEFLATED) as zip_:
|
|
|
|
|
zip_.writestr('index.html', index_html)
|
Single file deno (#478)
* Singlefile runs under Deno 2.8.3; browser leftovers are reaped.
single-file-cli is designed for Deno. Under node 18 (Debian 12/RPi OS) it
cannot reach the browser CDP on IPv6-enabled hosts ("localhost" resolves to
::1 first with no IPv4 fallback) and it never exits after a successful save,
blocking the download worker forever.
The native path and the Archive docker container now run single-file with
"deno run". Deno is pinned at v2.8.3 everywhere because yt-dlp 2026.06.09
requires at least 2.3.0 (our node is Debian's v18, below yt-dlp's v22
minimum). Deno >= 2.3 kills the browser single-file spawns (piped stderr
that single-file never reads), so the browser is run through
singlefile_browser_wrapper.sh which discards its stderr.
Commands are run in their own process group which is killed when they
finish, reaping the browser single-file leaves behind. The Archive
downloader now has a ten minute timeout. Install scripts install Deno
(previously only upgrade.sh and the image builds did).
* Singlefile writes to a file instead of dumping to stdout.
Deno >= 2.3 truncates large --dump-content output at exit; compressed
(SingleFileZ) singlefiles of large pages lost their ZIP tail, failed the
compression check in write_archive_files, and were destroyed by the HTML
prettifier (blank white archives). single-file now writes its result to a
temporary file which WROLPi reads back.
As insurance, a singlefile marked compressed (<html data-sfz) whose ZIP is
unreadable is now stored verbatim instead of being prettified.
* Compressed Archive screenshots and readability images are no longer broken.
The index.html inside a compressed (SingleFileZ) singlefile references its
resources by relative ZIP paths. Screenshots are now taken from the fully
extracted ZIP so those references resolve. Readability images are inlined
as data URIs read from the ZIP (readability-extractor absolutizes relative
srcs against the page URL, so both forms are matched).
* Compressed Archives are created by converting the uncompressed capture.
The page is always captured uncompressed; readability, screenshot, and
title come from that canonical page (its images are inline data URIs, so
no ZIP extraction or image inlining is needed). When compress_singlefile
is set, a second single-file run converts the captured file into a
compressed (SingleFileZ) self-extracting file. The conversion is a local
file:// operation - the page is not fetched again, and single-file
preserves the original URL header when re-saving its own pages. If the
conversion fails, the uncompressed singlefile is stored instead.
* Consolidating duplicate compressed-singlefile tests.
The write_archive_files tests (verbatim write, title fallback, broken-ZIP
guard, readability inlining) are combined into one test; the helper probes
and the setting-plumbing tests are each merged into one. The do_singlefile
compression-failure scenario and the timeout constant are folded into the
main do_singlefile test, which shared their setup.
* Addressing review comments: removeprefix, PermissionError when killing.
* Bumping version to 0.24.1-beta.
* Install scripts assert the deno.zip hash to avoid malicious or invalid files.
The hashes are the official sha256sum assets of the Deno v2.8.3 release,
per architecture.
* Archive Dockerfile asserts the deno.zip hash to avoid malicious or invalid files.
---------
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
2026-06-12 17:07:10 -06:00
|
|
|
zip_.writestr('images/0.png', b'\x89PNG fake image bytes')
|
2026-06-11 17:54:22 -06:00
|
|
|
zip_.writestr('manifest.json', '{}')
|
|
|
|
|
return buf.getvalue()
|
|
|
|
|
|
|
|
|
|
return _
|