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
|
#!/usr/bin/env python3
# Checks for wallhaven-dl's non-GUI logic. Run: python3 test_wallhaven_dl.py
#
# Copyright (C) 2026 Danilo M. <danix@danix.xyz>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2 as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import importlib.machinery
import importlib.util
import os
import sys
import tempfile
from pathlib import Path
sys.dont_write_bytecode = True
loader = importlib.machinery.SourceFileLoader("whdl", str(Path(__file__).with_name("wallhaven-dl")))
spec = importlib.util.spec_from_loader("whdl", loader)
whdl = importlib.util.module_from_spec(spec)
loader.exec_module(whdl)
# target(): which monitor an image is meant for
wide, tall, big = (2560, 1080), (1080, 1920), (3840, 2160)
assert whdl.target(1920, 1080, [wide, tall]) == 0 # landscape image -> landscape monitor
assert whdl.target(1080, 1920, [wide, tall]) == 1 # portrait image -> portrait monitor
assert whdl.target(1920, 1080, [wide, big, tall]) == 1 # largest of the landscape ones
assert whdl.target(1080, 1920, [wide, big]) == 1 # no portrait monitor -> largest overall
# api_delay(): rate limiter
assert whdl.api_delay([], 100.0) == 0
assert whdl.api_delay([50.0 + i for i in range(39)], 100.0) == 0 # under the limit
assert whdl.api_delay([50.0 + i for i in range(40)], 100.0) == 10.0 # oldest of last 40 at t=50
assert whdl.api_delay([0.0] * 5 + [50.0 + i for i in range(39)], 100.0) == 0 # old calls expired
# scan_library(): wallhaven ids anywhere under the roots, hidden folders and .part skipped
with tempfile.TemporaryDirectory() as tmp:
for f in ["wallhaven-aaa111.jpg", "sub/deeper/wallhaven-bbb222.png", "sub/holiday.jpg",
"sub/wallhaven-eee555_2560x1080.png",
".current/wallhaven-ccc333.jpg", "wallhaven-ddd444.part"]:
Path(tmp, f).parent.mkdir(parents=True, exist_ok=True)
Path(tmp, f).write_bytes(b"")
found = whdl.scan_library(Path(tmp), Path(tmp, "missing"))
assert found == {"aaa111": Path(tmp, "wallhaven-aaa111.jpg"),
"bbb222": Path(tmp, "sub/deeper/wallhaven-bbb222.png"),
"eee555": Path(tmp, "sub/wallhaven-eee555_2560x1080.png")}, found
# nearest_color() / palette_histogram(): local color extraction mapped onto wallhaven's palette
assert whdl.COLORS[whdl.nearest_color(0, 0, 0)] == "000000"
assert whdl.COLORS[whdl.nearest_color(255, 255, 250)] == "ffffff"
assert whdl.COLORS[whdl.nearest_color(10, 100, 210)] == "0066cc" # a slightly off blue
assert whdl.COLORS[whdl.nearest_color(250, 0, 5)] == "cc0000" # pure-ish red
hist = whdl.palette_histogram([(10, 100, 210)] * 60 + [(0, 0, 0)] * 30 + [(255, 255, 0)] * 10)
assert [whdl.COLORS[i] for i, _ in hist.most_common()] == ["0066cc", "000000", "ffff00"], hist
# search_pick(): prefers a chromatic color over greys when it covers enough of the image
grey, brown, blue, black = (whdl.COLORS.index(c) for c in ("999999", "996633", "0066cc", "000000"))
assert whdl.search_pick(whdl.Counter({grey: 47, brown: 35, blue: 18})) == brown
assert whdl.search_pick(whdl.Counter({grey: 60, black: 35, blue: 5})) == grey # blue under 10%
# read_apikey(): parses a fake `pass` put first on PATH
with tempfile.TemporaryDirectory() as tmp:
fake = Path(tmp, "pass")
fake.write_text('#!/bin/sh\ncase "$2" in\n'
" multi) printf 'sitepassword\\nlogin: someone\\nAPIKey: abc123\\n' ;;\n"
" plain) printf 'xyz789\\n' ;;\n"
" *) exit 1 ;;\nesac\n")
fake.chmod(0o755)
os.environ["PATH"] = f"{tmp}:{os.environ['PATH']}"
assert whdl.read_apikey("multi") == "abc123"
assert whdl.read_apikey("plain") == "xyz789"
assert whdl.read_apikey("missing") == ""
print("ok")
|