Add a web server for printing from the site, deployed on stppi
CI / test (3.12) (push) Failing after 43s
CI / test (3.10) (push) Failing after 6m6s
CI / test (3.13) (push) Failing after 36s

`./receipt.py serve` (receipt_web.py, the new [web] extra) serves a page
for printing a note or an uploaded image. The site's nginx gates
/receipt/ on the admin session and proxies to it over Tailscale, adding
X-Receipt-Token; nothing but /healthz answers without it, and POSTs also
need X-Requested-With: receipt. Jobs run one at a time behind a lock
around run_action(), and uploads print from memory.

run_action(), _dispatch() and setup_network_quiet() move from the TUI to
receipt.py so the server doesn't need textual. --no-setup (needs_setup())
skips the sudo connect script everywhere it would run; serve always sets
it. cmd_print takes in-memory `data`.

deploy/ has the systemd user units. receipt-update.timer runs update.sh
every 5 minutes: it fast-forwards the clone from Gitea, runs the tests
and restarts receipt-web, rolling back if they fail.

Also included from earlier uncommitted work: the CI matrix moved to the
right place, connect_receipt.sh no longer re-parses CLI values in
bash -c, RECEIPT_PRINTER_IP/PORT are validated, pyusb is declared, the
ruff S rules are on, and the img/ cache and junk/ are no longer tracked.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-23 21:22:31 +01:00
co-authored by Claude Opus 5.5
parent e333d6729f
commit f55b20fb82
65 changed files with 1142 additions and 124712 deletions
-86
View File
@@ -1,33 +1,12 @@
import argparse
import pytest
from PIL import Image
import receipt
receipt_tui = pytest.importorskip("receipt_tui")
class FakePrinter:
def __init__(self):
self.calls = []
def textln(self, text=""):
self.calls.append(("textln", text))
def image(self, img, **kwargs):
self.calls.append(("image", img))
def qr(self, content, **kwargs):
self.calls.append(("qr", content))
def cut(self):
self.calls.append(("cut", None))
def close(self):
self.calls.append(("close", None))
def make_base_args(**overrides):
defaults = {
"usb": True, # avoids the network setup script in tests
@@ -39,70 +18,5 @@ def make_base_args(**overrides):
return argparse.Namespace(**defaults)
@pytest.fixture
def fake_printer(monkeypatch):
p = FakePrinter()
monkeypatch.setattr(receipt, "get_printer", lambda args: p)
return p
def test_run_action_note(fake_printer):
rc, output = receipt_tui.run_action(
"note", make_base_args(), text="hello", timestamp=False
)
assert rc == 0
assert ("textln", "hello") in fake_printer.calls
assert fake_printer.calls[-2] == ("cut", None)
assert fake_printer.calls[-1] == ("close", None) # handle released after action
def test_run_action_qr(fake_printer):
rc, _ = receipt_tui.run_action(
"qr", make_base_args(), content="https://example.com", message=None
)
assert rc == 0
assert ("qr", "https://example.com") in fake_printer.calls
def test_run_action_reprint_uses_cache(fake_printer, tmp_path, monkeypatch):
img_dir = tmp_path / "img"
img_dir.mkdir()
Image.new("RGB", (10, 10)).save(img_dir / "12345.png")
monkeypatch.setattr(receipt, "IMG_DIR", img_dir)
rc, _ = receipt_tui.run_action("reprint", make_base_args(), name="12345")
assert rc == 0
assert ("textln", "12345") in fake_printer.calls
def test_run_action_captures_output_and_errors(monkeypatch):
def boom(args):
raise OSError("no printer")
monkeypatch.setattr(receipt, "get_printer", boom)
rc, output = receipt_tui.run_action(
"note", make_base_args(), text="hi", timestamp=False
)
assert rc == 1
assert "no printer" in output
def test_run_action_list_needs_no_printer(monkeypatch, tmp_path):
monkeypatch.setattr(receipt, "IMG_DIR", tmp_path / "img")
monkeypatch.setattr(
receipt, "get_printer", lambda args: pytest.fail("should not connect")
)
rc, output = receipt_tui.run_action("list", make_base_args())
assert rc == 0
assert "empty" in output
def test_parser_accepts_tui():
assert receipt.build_parser().parse_args(["tui"]).command == "tui"