taler-android

Android apps for GNU Taler (wallet, PoS, cashier)
Log | Files | Refs | README | LICENSE

smoke.py (4555B)


      1 # This file is part of GNU Taler
      2 # (C) 2026 Taler Systems S.A.
      3 #
      4 # GNU Taler is free software; you can redistribute it and/or modify it under the
      5 # terms of the GNU General Public License as published by the Free Software
      6 # Foundation; either version 3, or (at your option) any later version.
      7 #
      8 # GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
      9 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
     10 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
     11 #
     12 # You should have received a copy of the GNU General Public License along with
     13 # GNU Taler; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
     14 """Does any of this work at all?
     15 
     16 Six checks, in the order in which a failure stops being the harness's fault
     17 and starts being the terminal's.  Run this after every deploy; a scenario
     18 failing on a harness that never passed its smoke test says nothing.
     19 
     20     python3 smoke.py                 all of it
     21     python3 smoke.py provision pay   just those
     22 """
     23 
     24 from __future__ import annotations
     25 
     26 import os
     27 import sys
     28 import time
     29 
     30 import counterparty
     31 import merchant
     32 from device import Device, serial_for
     33 from pos import Pos
     34 
     35 WORKER = int(os.environ.get("POS_WORKER", "0"))
     36 SERIAL = serial_for(WORKER)
     37 
     38 
     39 def check_device(dev: Device, app: Pos) -> None:
     40     screen = dev.dump()
     41     assert screen.nodes, "no view hierarchy at all -- is the emulator up?"
     42     w, h = dev.size()
     43     assert w > h, f"the window is {w}x{h}; the terminal is landscape-locked"
     44     print(f"  ok  device {dev.serial}, API {dev.sdk_int}, window {w}x{h}")
     45 
     46 
     47 def check_hooks(dev: Device) -> None:
     48     import coverage
     49     dev.paste_text("pos-simulator smoke")
     50     path = coverage.dump(dev, f"/tmp/pos-smoke-{dev.serial}.ec", name="smoke.ec")
     51     size = path.stat().st_size
     52     assert size > 1000, f"coverage dump is only {size} bytes"
     53     print(f"  ok  sim hooks: clipboard, and {size} bytes of coverage")
     54 
     55 
     56 def check_backend() -> merchant.Instance:
     57     inst = merchant.demo()
     58     pos = inst.merchant.pos_config(inst.token.value)
     59     products = len(pos.get("products", []))
     60     assert products, "the instance has no products; the inventory screen has nothing to show"
     61     print(f"  ok  backend {inst.label}: {inst.currency}, {products} products, "
     62           f"{len(inst.merchant.accounts(inst.token.value))} bank accounts")
     63     return inst
     64 
     65 
     66 def check_provision(dev: Device, app: Pos, inst: merchant.Instance) -> None:
     67     app.fresh_start()
     68     app.provision_by_uri(inst.setup_uri())
     69     rows = app.about_rows()
     70     assert rows.get("Username") == inst.merchant.instance, \
     71         f"the terminal says it is bound to {rows.get('Username')!r}"
     72     print(f"  ok  provisioned by setup URI: {rows}")
     73 
     74 
     75 def check_order(dev: Device, app: Pos, inst: merchant.Instance) -> str:
     76     app.goto("amount")
     77     app.enter_amount("1.00")
     78     app.charge()
     79     uri = app.displayed_pay_uri()
     80     assert uri, "the payment screen offers no pay URI over NFC"
     81     print(f"  ok  order billed, terminal offers {uri[:70]}...")
     82     return uri
     83 
     84 
     85 def check_pay(dev: Device, app: Pos, inst: merchant.Instance, uri: str) -> None:
     86     wallet, account = counterparty.customer(
     87         db_dir=os.environ.get("POS_WALLET_DIR", "/tmp/pos-sim-wallets"))
     88     if not wallet.has_at_least(f"{inst.currency}:2"):
     89         print(f"      funding the customer wallet ({wallet.available(inst.currency)})")
     90         wallet.withdraw(account, f"{inst.currency}:20")
     91     wallet.pay(uri)
     92     app.wait_for_success()
     93     print(f"  ok  the customer paid and the terminal said so")
     94 
     95 
     96 def main(argv: list[str]) -> int:
     97     want = set(argv) or {"device", "hooks", "backend", "provision", "order", "pay"}
     98     dev = Device(SERIAL)
     99     app = Pos(dev)
    100     inst = None
    101     uri = None
    102     try:
    103         if "device" in want:
    104             check_device(dev, app)
    105         if "hooks" in want:
    106             check_hooks(dev)
    107         if "backend" in want or {"provision", "order", "pay"} & want:
    108             inst = check_backend()
    109         if "provision" in want:
    110             check_provision(dev, app, inst)
    111         if "order" in want or "pay" in want:
    112             uri = check_order(dev, app, inst)
    113         if "pay" in want:
    114             check_pay(dev, app, inst, uri)
    115     except Exception as e:
    116         print(f"  FAIL {type(e).__name__}: {e}")
    117         try:
    118             print(f"       screen: {dev.dump().texts()}")
    119         except Exception:
    120             pass
    121         return 1
    122     print("smoke test passed")
    123     return 0
    124 
    125 
    126 if __name__ == "__main__":
    127     sys.exit(main(sys.argv[1:]))