534 lines
25 KiB
Python
534 lines
25 KiB
Python
"""The R1's streak number agrees with the desktop's: model.py's streak base, the Java port
|
|
(android/src/main/java/.../Model.java, Streaks.java, Digits.java, run on the JDK through
|
|
android/src/test/java/.../R1Check.java with a test-only org.json), and the base travelling through
|
|
a local routine-server (this one, and the old one from git HEAD~ when there is one).
|
|
|
|
Fake data only. Run: python3 -m unittest discover tests
|
|
"""
|
|
import atexit
|
|
import copy
|
|
import datetime as dt
|
|
import glob
|
|
import json
|
|
import os
|
|
import random
|
|
import shutil
|
|
import socket
|
|
import subprocess
|
|
import sys
|
|
import tempfile
|
|
import time
|
|
import unittest
|
|
import urllib.request
|
|
|
|
ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
sys.path.insert(0, ROOT)
|
|
from routine import model # noqa: E402
|
|
|
|
JAVA_HOME = os.environ.get("JAVA_HOME", "/usr/lib/jvm/java-21-openjdk")
|
|
JAVAC, JAVA = os.path.join(JAVA_HOME, "bin", "javac"), os.path.join(JAVA_HOME, "bin", "java")
|
|
SRC = os.path.join(ROOT, "android", "src")
|
|
TODAY = dt.date(2026, 11, 14)
|
|
|
|
SCHEDULE = {"rev": 3, "dayStartsAt": 4, "blocks": [
|
|
{"id": "am", "label": "AM", "start": 7, "end": 9, "color": "#ffffff", "items": [
|
|
{"id": "teeth", "label": "teeth"},
|
|
{"id": "run", "label": "run", "days": [1, 2, 3, 4, 5]}, # weekdays only
|
|
{"id": "tea", "label": "tea", "calm": True, "items": [{"label": "idea", "hint": True}]}]},
|
|
{"id": "pm", "label": "PM", "start": 18, "end": 22, "color": "#000000", "days": [0, 6], "items": [
|
|
{"id": "read", "label": "read"}]}, # weekends only
|
|
]}
|
|
ITEMS = ["am/teeth", "am/run", "am/tea", "pm/read"]
|
|
|
|
|
|
def fake_days(rng, n, p_done):
|
|
"""n days up to TODAY from SCHEDULE, each task done with probability p_done[item] (fake data)."""
|
|
days = []
|
|
for k in range(n):
|
|
d = TODAY - dt.timedelta(days=n - 1 - k)
|
|
day = model.new_day(SCHEDULE, d)
|
|
for b in day["blocks"]:
|
|
for it in b["items"]:
|
|
if not it.get("hint"):
|
|
r = rng.random()
|
|
if r < p_done[it["id"]]:
|
|
day["states"][it["id"]] = {"s": model.DONE, "t": 1}
|
|
elif r < p_done[it["id"]] + 0.03:
|
|
day["states"][it["id"]] = {"s": model.SKIPPED, "t": 1}
|
|
days.append(day)
|
|
return days
|
|
|
|
|
|
def ticked(days, iid, date):
|
|
"""days with iid ticked on date (as the app does just before asking for the streak)."""
|
|
out = list(days)
|
|
for k, d in enumerate(out):
|
|
if d["date"] == date:
|
|
d = out[k] = dict(d, states=dict(d["states"]))
|
|
d["states"][iid] = {"s": model.DONE, "t": 2}
|
|
return out
|
|
|
|
|
|
def on(day, iid):
|
|
return any(it["id"] == iid for b in day["blocks"] for it in b["items"])
|
|
|
|
|
|
def have_java():
|
|
return os.path.exists(JAVAC) and os.path.exists(JAVA)
|
|
|
|
|
|
class Java:
|
|
"""Compiles the R1's pure classes and R1Check once; run(cases) -> its answers."""
|
|
dir = None
|
|
|
|
@classmethod
|
|
def build(cls):
|
|
if cls.dir:
|
|
return
|
|
cls.dir = tempfile.mkdtemp(prefix="r1check-")
|
|
atexit.register(shutil.rmtree, cls.dir, True)
|
|
main = os.path.join(SRC, "main", "java", "me", "teafry", "routine")
|
|
srcs = [os.path.join(main, f) for f in ("Model.java", "Streaks.java", "Digits.java")]
|
|
srcs += glob.glob(os.path.join(SRC, "test", "java", "**", "*.java"), recursive=True)
|
|
subprocess.run([JAVAC, "--release", "11", "-Xlint:-options", "-d", os.path.join(cls.dir, "cls"), *srcs],
|
|
check=True, capture_output=True)
|
|
|
|
@classmethod
|
|
def run(cls, cases):
|
|
cls.build()
|
|
path = os.path.join(cls.dir, "in.json")
|
|
with open(path, "w") as f:
|
|
json.dump(cases, f)
|
|
r = subprocess.run([JAVA, "-cp", os.path.join(cls.dir, "cls"), "me.teafry.routine.R1Check", path],
|
|
check=True, capture_output=True, text=True)
|
|
return json.loads(r.stdout)
|
|
|
|
|
|
class Base(unittest.TestCase):
|
|
"""model.streak_base + streak_ending(base=) == streak_ending over the whole history."""
|
|
|
|
def test_base_matches_whole_history(self):
|
|
rng = random.Random(7)
|
|
checked = 0
|
|
for trial in range(12):
|
|
p = {i: rng.choice([0.75, 0.9, 0.97]) for i in ITEMS}
|
|
days = fake_days(rng, rng.randint(40, 140), p)
|
|
for cheat in (0, 1, 3, 7):
|
|
for lag in (1, 5, 30):
|
|
asof = (TODAY - dt.timedelta(days=lag)).isoformat()
|
|
base = model.streak_base(days, asof, cheat)
|
|
for back in range(0, lag):
|
|
date = (TODAY - dt.timedelta(days=back)).isoformat()
|
|
for iid in ITEMS:
|
|
dd = ticked(days, iid, date)
|
|
whole = model.streak_ending(dd, iid, date, cheat)
|
|
part = model.streak_ending([d for d in dd if d["date"] > asof], iid, date, cheat, base)
|
|
self.assertEqual(whole, part, (trial, cheat, lag, back, iid))
|
|
checked += whole > 0
|
|
self.assertGreater(checked, 1000)
|
|
|
|
def test_pending_cheat_day_at_asof(self):
|
|
# done x7 (a cheat day earned), a miss on asof (the cheat day used), then a miss: no rescue
|
|
days = []
|
|
pat = "DDDDDDDM"
|
|
for k, c in enumerate(pat + "MD"):
|
|
d = TODAY - dt.timedelta(days=len(pat) + 1 - k)
|
|
day = {"date": d.isoformat(), "rev": 1, "note": {"text": "", "t": 0},
|
|
"blocks": [{"id": "a", "label": "A", "start": 7, "end": 8, "color": "#fff",
|
|
"items": [{"id": "a/x", "label": "x", "depth": 0}]}],
|
|
"states": {"a/x": {"s": "done", "t": 1}} if c == "D" else {}}
|
|
days.append(day)
|
|
asof = days[len(pat) - 1]["date"]
|
|
base = model.streak_base(days, asof, 7)
|
|
self.assertEqual(base["items"]["a/x"], [7, 0, 0, 1])
|
|
date = days[-1]["date"]
|
|
self.assertEqual(model.streak_ending(days, "a/x", date, 7), 1)
|
|
self.assertEqual(model.streak_ending(days[len(pat):], "a/x", date, 7, base), 1)
|
|
# ... and with a done day after the cheat day instead, the run goes on
|
|
days[-2]["states"] = {"a/x": {"s": "done", "t": 1}}
|
|
self.assertEqual(model.streak_ending(days, "a/x", date, 7), 9)
|
|
self.assertEqual(model.streak_ending(days[len(pat):], "a/x", date, 7, base), 9)
|
|
|
|
|
|
@unittest.skipUnless(have_java(), "no JDK at " + JAVA_HOME)
|
|
class R1Port(unittest.TestCase):
|
|
"""The Java port gives the same answers as model.py / anim_paint.py."""
|
|
|
|
def test_levels(self):
|
|
ns = list(range(0, 1500))
|
|
got = Java.run({"levels": ns})["levels"]
|
|
self.assertEqual(got, [-1 if model.streak_level(n) is None else model.streak_level(n) for n in ns])
|
|
|
|
def test_model_streak_ending(self):
|
|
rng = random.Random(3)
|
|
cases, want = [], []
|
|
for trial in range(6):
|
|
days = fake_days(rng, 90, {i: rng.choice([0.8, 0.95]) for i in ITEMS})
|
|
for cheat in (0, 2, 7):
|
|
asof = (TODAY - dt.timedelta(days=20)).isoformat()
|
|
base = model.streak_base(days, asof, cheat)
|
|
for back in (0, 1, 6, 19, 25, 60):
|
|
date = (TODAY - dt.timedelta(days=back)).isoformat()
|
|
for iid in ITEMS:
|
|
dd = ticked(days, iid, date)
|
|
for b in (None, base):
|
|
cases.append({"days": dd, "item": iid, "date": date, "cheat": cheat, "base": b})
|
|
want.append(model.streak_ending(dd, iid, date, cheat, b))
|
|
self.assertEqual(Java.run({"model": cases})["model"], want)
|
|
|
|
def test_r1_streak_with_base(self):
|
|
"""What the R1 holds (the server's last 60 days, gaps for days nobody opened) + the desktop's base
|
|
gives exactly the desktop's number, ticking today or any day after asof."""
|
|
rng = random.Random(11)
|
|
cases, want = [], []
|
|
for trial in range(10):
|
|
days = fake_days(rng, 400, {i: rng.choice([0.85, 0.95, 0.99]) for i in ITEMS})
|
|
cheat = rng.choice([0, 3, 7])
|
|
asof = (TODAY - dt.timedelta(days=model.STREAK_BASE_LAG)).isoformat()
|
|
base = model.streak_base(days, asof, cheat)
|
|
held = [d for d in days if d["date"] >= (TODAY - dt.timedelta(days=60)).isoformat()]
|
|
held = [d for d in held if d["states"] or rng.random() < 0.85] # untouched days may be missing
|
|
for back in range(0, model.STREAK_BASE_LAG):
|
|
date = (TODAY - dt.timedelta(days=back)).isoformat()
|
|
for iid in ITEMS:
|
|
if not on(model.new_day(SCHEDULE, dt.date.fromisoformat(date)), iid):
|
|
continue
|
|
whole = model.streak_ending(ticked(days, iid, date), iid, date, cheat)
|
|
r1 = ticked(held, iid, date)
|
|
if not any(d["date"] == date for d in r1):
|
|
continue
|
|
cases.append({"days": r1, "schedule": SCHEDULE, "item": iid, "date": date,
|
|
"cheat": 99, "base": base}) # the base's cheat_after counts
|
|
want.append(whole)
|
|
got = Java.run({"ending": cases})["ending"]
|
|
self.assertEqual(got, want)
|
|
self.assertTrue(any(n > 60 for n in want), "some streaks longer than what the R1 holds")
|
|
|
|
def test_r1_streak_without_base(self):
|
|
"""No base (an old server): the number is the desktop's or 0 (no streak animation), never
|
|
another number; and runs that started in view are counted."""
|
|
rng = random.Random(5)
|
|
cases, want = [], []
|
|
for trial in range(10):
|
|
days = fake_days(rng, 200, {i: rng.choice([0.7, 0.85, 0.97]) for i in ITEMS})
|
|
cheat = rng.choice([0, 2, 7])
|
|
held = [d for d in days if d["date"] >= (TODAY - dt.timedelta(days=60)).isoformat()]
|
|
for back in range(0, 40):
|
|
date = (TODAY - dt.timedelta(days=back)).isoformat()
|
|
for iid in ITEMS:
|
|
if not on(model.new_day(SCHEDULE, dt.date.fromisoformat(date)), iid):
|
|
continue
|
|
cases.append({"days": ticked(held, iid, date), "schedule": SCHEDULE, "item": iid,
|
|
"date": date, "cheat": cheat, "base": None})
|
|
want.append(model.streak_ending(ticked(days, iid, date), iid, date, cheat))
|
|
got = Java.run({"ending": cases})["ending"]
|
|
for g, w in zip(got, want):
|
|
self.assertIn(g, (0, w))
|
|
self.assertGreater(sum(1 for g, w in zip(got, want) if g == w and w > 0), 100)
|
|
self.assertGreater(sum(1 for g, w in zip(got, want) if g == 0 and w > 0), 0)
|
|
|
|
def test_stale_base_falls_back(self):
|
|
"""A base older than the oldest day the R1 holds isn't used (0 or the right number)."""
|
|
rng = random.Random(2)
|
|
days = fake_days(rng, 300, {i: 0.99 for i in ITEMS})
|
|
asof = (TODAY - dt.timedelta(days=100)).isoformat()
|
|
base = model.streak_base(days, asof, 7)
|
|
held = [d for d in days if d["date"] >= (TODAY - dt.timedelta(days=60)).isoformat()]
|
|
date = TODAY.isoformat()
|
|
want = model.streak_ending(ticked(days, "am/teeth", date), "am/teeth", date, 7)
|
|
got = Java.run({"ending": [{"days": ticked(held, "am/teeth", date), "schedule": SCHEDULE,
|
|
"item": "am/teeth", "date": date, "cheat": 7, "base": base}]})["ending"]
|
|
self.assertIn(got[0], (0, want))
|
|
|
|
def test_digits(self):
|
|
try:
|
|
from routine.gui.anim_paint import number_strokes
|
|
except ImportError:
|
|
self.skipTest("no Qt binding (PySide6 or PyQt6)")
|
|
args = [[n, cx, cy, h, rot] for n in (7, 21, 42, 66, 100, 150, 254, 365, 465, 1234567890)
|
|
for cx, cy, h, rot in ((0, 0, 1, 0), (0.5, -1.25, 1.6, 0.3), (-2, 1, 0.8, -1.1))]
|
|
got = Java.run({"digits": args})["digits"]
|
|
for a, g in zip(args, got):
|
|
w = number_strokes(*a)
|
|
self.assertEqual(len(g), len(w))
|
|
for gs, ws in zip(g, w):
|
|
self.assertEqual(len(gs), len(ws))
|
|
for x, y in zip(gs, ws):
|
|
self.assertAlmostEqual(x, y, places=4)
|
|
|
|
def close(self, got, want):
|
|
self.assertEqual(len(got), len(want))
|
|
for g, w in zip(got, want):
|
|
if isinstance(w, (list, tuple)):
|
|
self.close(g, w)
|
|
else:
|
|
self.assertAlmostEqual(g, w, places=4)
|
|
|
|
def test_caption(self):
|
|
"""The "<n> days!" caption: strokes, placement, size and pop-in, Java == anim_paint."""
|
|
try:
|
|
from routine.gui import anim_paint as ap
|
|
except ImportError:
|
|
self.skipTest("no Qt binding (PySide6 or PyQt6)")
|
|
ns = (7, 21, 42, 66, 100, 150, 254, 365, 465, 1465, 1234567890)
|
|
caps = [[n, cx, top, h, sc] for n in ns for cx, top, h, sc in ((0, 0, 0.9, 1), (1.5, 2.25, 0.7, 0.5), (-3, -6, 0.6, 1.08))]
|
|
places = [[*b, n, above, h] for n in (7, 365, 1465) for b in ((-1.53, -6.62, 7.0, 0.12), (-9.04, -5.11, 6.17, 0.0),
|
|
(-5.39, -6.28, 6.09, 7.87), (-0.2, -0.3, 0.2, 0.3))
|
|
for above in (0, 1) for h in (0.9, 0.74)]
|
|
hs = [0, 0.05, 0.86, 1.06, 1.3, 2.85]
|
|
pops = [[f, st] for st in (0, 5, 14) for f in range(-1, 20)]
|
|
starts = [[first, n] for first in (-1, 0, 3, 13, 14, 15, 23) for n in (24, 12, 6) if first < n]
|
|
got = Java.run({"caption": caps, "place": places, "captionh": hs, "pop": pops, "start": starts})
|
|
self.close(got["caption"], [ap.caption_strokes(*c) for c in caps])
|
|
want = []
|
|
for pl in places:
|
|
cx, top, ub = ap.caption_place(pl[:4], pl[4], bool(pl[5]), pl[6])
|
|
want.append([cx, top, *ub])
|
|
self.close(got["place"], want)
|
|
self.close(got["captionh"], [ap.caption_h({"frames": [[["n", 0.2, 0, 0, h, 0]]] if h else [[]]}) for h in hs])
|
|
self.close(got["pop"], [ap.caption_scale(f, st) for f, st in pops])
|
|
def anim(first, n):
|
|
return {"frames": [[["n", 0.2, 0, 0, 1, 0]] if first >= 0 and k >= first else [] for k in range(n)]}
|
|
self.assertEqual(got["start"], [ap.caption_start(anim(*a)) for a in starts])
|
|
self.assertEqual(ap.caption_start(anim(5, 24)), 5) # starts with the number ...
|
|
self.assertEqual(ap.caption_start(anim(20, 24)), 14) # ... but shows for the last 10 at least
|
|
self.assertEqual(ap.caption_scale(4, 5), 0.0)
|
|
self.assertEqual(ap.caption_text(21), "21 days!")
|
|
|
|
def test_caption_only_for_streaks(self):
|
|
"""Every streak animation is sized for a caption smaller than its own number; none other has "n"."""
|
|
try:
|
|
from routine.gui import anim_paint as ap
|
|
except ImportError:
|
|
self.skipTest("no Qt binding (PySide6 or PyQt6)")
|
|
with open(os.path.join(ROOT, "shared", "animations.json")) as f:
|
|
data = json.load(f)
|
|
plain = data["animations"] + data.get("calm", []) + ([data["star"]] if data.get("star") else [])
|
|
self.assertFalse(any(sh[0] == "n" for a in plain for fr in a["frames"] for sh in fr))
|
|
st = data.get("streak") or {}
|
|
for a in [a for g in st.get("sets", []) for a in g] + st.get("calm", []):
|
|
tallest = max(sh[4] for fr in a["frames"] for sh in fr if sh[0] == "n")
|
|
self.assertLess(ap.caption_h(a), tallest, a["name"])
|
|
|
|
def test_merge_keeps_streak_plays(self):
|
|
a = model.new_day(SCHEDULE, TODAY)
|
|
b = copy.deepcopy(a)
|
|
a["states"]["am/teeth"] = {"s": "done", "t": 10}
|
|
a["plays"] = [{"t": 10, "a": "x", "i": "am/teeth", "s": model.STREAK_SET, "n": 21}]
|
|
b["states"]["am/tea"] = {"s": "done", "t": 11}
|
|
b["plays"] = [{"t": 11, "a": "y", "i": "am/tea", "s": model.STREAK_CALM_SET, "n": 465},
|
|
{"t": 12, "a": "z", "i": "am/run", "s": 0}]
|
|
got = Java.run({"merge": [[a, b], [b, a]]})["merge"]
|
|
self.assertEqual(got[0], model.merge(a, b))
|
|
self.assertEqual(got[1], model.merge(b, a))
|
|
|
|
|
|
def free_port():
|
|
s = socket.socket()
|
|
s.bind(("127.0.0.1", 0))
|
|
port = s.getsockname()[1]
|
|
s.close()
|
|
return port
|
|
|
|
|
|
class Server:
|
|
"""A routine-server (server/routine_server.py, or another copy of it) on a spare local port."""
|
|
|
|
def __init__(self, source=None):
|
|
self.dir = tempfile.mkdtemp(prefix="routine-server-")
|
|
app = os.path.join(self.dir, "app")
|
|
os.makedirs(app)
|
|
if source is None:
|
|
shutil.copy(os.path.join(ROOT, "server", "routine_server.py"), app)
|
|
else:
|
|
with open(os.path.join(app, "routine_server.py"), "w") as f:
|
|
f.write(source)
|
|
shutil.copy(os.path.join(ROOT, "routine", "model.py"), app)
|
|
self.port = free_port()
|
|
self.proc = subprocess.Popen([sys.executable, os.path.join(app, "routine_server.py"), "--port",
|
|
str(self.port), "--data", os.path.join(self.dir, "data")],
|
|
stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL)
|
|
for _ in range(100):
|
|
try:
|
|
urllib.request.urlopen(self.url + "/health", timeout=1)
|
|
break
|
|
except OSError:
|
|
time.sleep(0.05)
|
|
with open(os.path.join(self.dir, "data", "token")) as f:
|
|
self.token = f.read().strip()
|
|
|
|
@property
|
|
def url(self):
|
|
return "http://127.0.0.1:%d" % self.port
|
|
|
|
def sync(self, body):
|
|
req = urllib.request.Request(self.url + "/sync", json.dumps(body).encode(),
|
|
{"Authorization": "Bearer " + self.token, "Content-Type": "application/json"})
|
|
with urllib.request.urlopen(req, timeout=10) as r:
|
|
return json.load(r)
|
|
|
|
def close(self):
|
|
self.proc.terminate()
|
|
self.proc.wait(5)
|
|
shutil.rmtree(self.dir, ignore_errors=True)
|
|
|
|
|
|
def old_server_source():
|
|
"""server/routine_server.py as last committed before the streak base, if git has it."""
|
|
for rev in ("HEAD:server/routine_server.py",):
|
|
r = subprocess.run(["git", "-C", ROOT, "show", rev], capture_output=True, text=True)
|
|
if r.returncode == 0 and "streaks" not in r.stdout:
|
|
return r.stdout
|
|
return None
|
|
|
|
|
|
class Relay(unittest.TestCase):
|
|
def setUp(self):
|
|
self.srv = Server()
|
|
|
|
def tearDown(self):
|
|
self.srv.close()
|
|
|
|
def doc(self, t, asof="2026-10-15"):
|
|
return {"t": t, "asof": asof, "cheat_after": 7, "anims": True, "items": {"a/x": [12, 5, 0, 0]}}
|
|
|
|
def test_streak_base_relay(self):
|
|
s = self.srv
|
|
r = s.sync({"since": 0, "days": [], "schedule_rev": 0, "streaks": self.doc(100)}) # the desktop
|
|
self.assertEqual(r["streaks_t"], 100)
|
|
self.assertNotIn("streaks", r) # the desktop doesn't ask for it back
|
|
r = s.sync({"since": 0, "days": [], "schedule_rev": 0, "streaks_t": 0}) # the R1
|
|
self.assertEqual(r["streaks"], self.doc(100))
|
|
r = s.sync({"since": 0, "days": [], "schedule_rev": 0, "streaks_t": 100}) # has it
|
|
self.assertNotIn("streaks", r)
|
|
r = s.sync({"since": 0, "days": [], "schedule_rev": 0}) # an old R1
|
|
self.assertNotIn("streaks", r)
|
|
s.sync({"since": 0, "days": [], "schedule_rev": 0, "streaks": self.doc(50, "2026-01-01")}) # older: kept out
|
|
self.assertEqual(s.sync({"since": 0, "days": [], "streaks_t": 0})["streaks"]["t"], 100)
|
|
for bad in ({"t": 200}, {"t": 200, "asof": "x", "items": {}}, {"t": "200", "asof": "2026-10-15", "items": {}},
|
|
[1, 2], "x"):
|
|
r = s.sync({"since": 0, "days": [], "streaks": bad, "streaks_t": 0})
|
|
self.assertEqual(r["streaks"]["t"], 100)
|
|
s.sync({"since": 0, "days": [], "streaks": self.doc(300)})
|
|
self.assertEqual(s.sync({"since": 0, "days": [], "streaks_t": 100})["streaks"]["t"], 300)
|
|
|
|
def test_days_still_sync(self):
|
|
day = model.new_day(SCHEDULE, dt.date.today())
|
|
day["states"]["am/teeth"] = {"s": "done", "t": 5}
|
|
r = self.srv.sync({"since": 0, "days": [day], "schedule_rev": 0, "streaks_t": 0})
|
|
self.assertEqual(r["days"][0]["states"]["am/teeth"]["s"], "done")
|
|
self.assertNotIn("streaks", r)
|
|
self.assertNotIn("streaks_t", r)
|
|
|
|
|
|
class EndToEnd(unittest.TestCase):
|
|
"""The desktop's Syncer (Qt, no window) pushes a fake history and its streak base to a local
|
|
server; the R1's side of it (the request MainActivity sends, then Streaks.ending) gets the
|
|
desktop's number for every task ticked today."""
|
|
|
|
app = None # kept: processEvents does nothing once it's gone
|
|
|
|
def run_desktop(self, srv, store, cfg, sched, times=1):
|
|
from routine.qt.QtCore import QCoreApplication
|
|
from routine.sync import Syncer
|
|
EndToEnd.app = QCoreApplication.instance() or QCoreApplication([])
|
|
sy = Syncer(store, cfg, lambda: sched)
|
|
sy.timer.stop()
|
|
for _ in range(times):
|
|
self.sync_once(sy)
|
|
return sy
|
|
|
|
def sync_once(self, sy):
|
|
sy.sync_now()
|
|
t0 = time.time()
|
|
while sy.busy and time.time() - t0 < 10:
|
|
EndToEnd.app.processEvents()
|
|
time.sleep(0.01)
|
|
self.assertFalse(sy.busy)
|
|
self.assertEqual(sy.last_error, "")
|
|
|
|
def setUp(self):
|
|
try:
|
|
import routine.qt.QtCore # noqa: F401
|
|
except ImportError:
|
|
self.skipTest("no Qt binding (PySide6 or PyQt6)")
|
|
from routine.store import Store
|
|
self.tmp = tempfile.mkdtemp(prefix="routine-e2e-")
|
|
self.store = Store(os.path.join(self.tmp, "routine.db"))
|
|
rng = random.Random(9)
|
|
self.today = model.routine_date(SCHEDULE)
|
|
n = 300
|
|
self.days = []
|
|
for k in range(n):
|
|
d = self.today - dt.timedelta(days=n - 1 - k)
|
|
day = model.new_day(SCHEDULE, d)
|
|
for b in day["blocks"]:
|
|
for it in b["items"]:
|
|
if not it.get("hint") and (rng.random() < 0.97 or it["id"] == "am/teeth") and d != self.today:
|
|
day["states"][it["id"]] = {"s": "done", "t": 1}
|
|
self.days.append(day)
|
|
for day in self.days: # what the server would hold: the last 60 days are pushed
|
|
recent = day["date"] >= (dt.date.today() - dt.timedelta(days=59)).isoformat()
|
|
self.store.put(day, dirty=recent)
|
|
|
|
def tearDown(self):
|
|
if getattr(self, "store", None):
|
|
self.store.db.close()
|
|
shutil.rmtree(getattr(self, "tmp", ""), ignore_errors=True)
|
|
|
|
def r1_numbers(self, srv, cheat=7):
|
|
r = srv.sync({"since": 0, "days": [], "schedule_rev": SCHEDULE["rev"], "streaks_t": 0})
|
|
base = r.get("streaks")
|
|
date = self.today.isoformat()
|
|
cases, want = [], []
|
|
for iid in ITEMS:
|
|
if not on(self.days[-1], iid):
|
|
continue
|
|
held = ticked(r["days"], iid, date)
|
|
cases.append({"days": held, "schedule": SCHEDULE, "item": iid, "date": date, "cheat": cheat, "base": base})
|
|
want.append(model.streak_ending(ticked(self.days, iid, date), iid, date, cheat))
|
|
return base, Java.run({"ending": cases})["ending"], want
|
|
|
|
@unittest.skipUnless(have_java(), "no JDK")
|
|
def test_new_server(self):
|
|
srv = Server()
|
|
try:
|
|
cfg = {"sync_url": srv.url, "sync_token": srv.token, "streak_anims": False, "cheat_day": True, "cheat_after": 7}
|
|
sy = self.run_desktop(srv, self.store, cfg, SCHEDULE, times=2)
|
|
self.assertEqual(sy._streaks_acked, sy._streaks[1]["t"]) # the server has it: not sent again
|
|
base, got, want = self.r1_numbers(srv)
|
|
self.assertIsNotNone(base)
|
|
self.assertFalse(base["anims"])
|
|
self.assertEqual(got, want)
|
|
self.assertTrue(any(n > 60 for n in want))
|
|
cfg["cheat_day"] = False # a setting changed: a new base
|
|
t = sy._streaks[1]["t"]
|
|
self.sync_once(sy)
|
|
self.assertGreater(sy._streaks[1]["t"], t)
|
|
base, got, want = self.r1_numbers(srv, cheat=0)
|
|
self.assertEqual(base["cheat_after"], 0)
|
|
self.assertEqual(got, want)
|
|
finally:
|
|
srv.close()
|
|
|
|
@unittest.skipUnless(have_java(), "no JDK")
|
|
def test_old_server(self):
|
|
src = old_server_source()
|
|
if src is None:
|
|
self.skipTest("no pre-streak server in git")
|
|
srv = Server(src)
|
|
try:
|
|
cfg = {"sync_url": srv.url, "sync_token": srv.token}
|
|
sy = self.run_desktop(srv, self.store, cfg, SCHEDULE, times=2)
|
|
self.assertEqual(sy._streaks_acked, 0) # never acknowledged: still sent
|
|
base, got, want = self.r1_numbers(srv)
|
|
self.assertIsNone(base)
|
|
for g, w in zip(got, want):
|
|
self.assertIn(g, (0, w))
|
|
finally:
|
|
srv.close()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|