diff --git a/docs/DEMO.md b/docs/DEMO.md index 194f1e81c..caaa5bc1c 100644 --- a/docs/DEMO.md +++ b/docs/DEMO.md @@ -11,7 +11,7 @@ Everything it writes is invented: ten made-up people, numbers in the 555-01xx ra | Contacts (in `omaphone`), and the names in Phone and Messages | A vdir, one vCard per contact: `~/.local/share/contacts//*.vcf`. New contacts go in the `default` book. A favorite is `CATEGORIES:Starred` | `default/omamobile-demo-.vcf`, ten contacts, three of them starred | | Messages (`omamessages`) | `~/.local/share/omatel/messages.db` (SQLite, WAL): `conversations` (keyed by the E.164 `address`), `participants`, `messages` (`direction` in/out, `state`, `read`, times in ms), and an FTS5 index that triggers keep up to date | Six one-to-one threads over the last four days. Three incoming messages are unread, in two threads | | Phone (`omaphone`) | `~/.local/share/omatel/calls.db`: `calls` (direction, outcome answered/missed/rejected/blocked/unanswered/failed, start, answer and end times, duration, `seen`) and `favorites` | Nine calls (incoming, outgoing, missed), one missed call not yet seen | -| Notifications | Omarchy's notification service keeps one JSON file per notification in `~/.local/state/omarchy/notifications/`, and in `history/` once the toast has gone (the newest ten). The quick-settings shade and the lock glance read these files | Five notifications, sent through `org.freedesktop.Notifications` as Messages, Phone, Calendar and Omarchy | +| Notifications | Omarchy's notification service keeps them in `~/.local/state/omarchy/notifications/`: on Omarchy 4.5, the toasts on screen in `live.json` and the closed ones in `history.json`; on 4.0, one JSON file per notification, and in `history/` once the toast has gone (the newest ten). The quick-settings shade and the lock glance read these files, and the tool handles both layouts | Five notifications, sent through `org.freedesktop.Notifications` as Messages, Phone, Calendar and Omarchy | `omatel-agent` writes these stores and the apps read them directly, so the history shows without a modem or ModemManager. If the agent isn't running, the apps show a "service isn't running" banner, but the data still appears. With no SIM the status bar shows no cellular icon. The Call and Send buttons are live whenever the agent runs, but without a modem a call fails and a text is stored as failed, so don't press them during a take. A failed text typed into a demo thread is removed by clear along with the thread. diff --git a/tests/all b/tests/all index 17d5dd43d..1afbe163c 100755 --- a/tests/all +++ b/tests/all @@ -82,3 +82,5 @@ echo "# tests/review-lessons" tests/review-lessons echo "# tests/factory-index" # the build index of Google's factory images, against a made-up zip tests/factory-index +echo "# tests/phone-demo-notes" +tests/phone-demo-notes diff --git a/tests/phone-demo-notes b/tests/phone-demo-notes new file mode 100755 index 000000000..68f52836e --- /dev/null +++ b/tests/phone-demo-notes @@ -0,0 +1,92 @@ +#!/bin/bash +# SPDX-License-Identifier: MIT +# tools/phone-demo's notification code against both of Omarchy's stores, in a +# made-up home with no phone: 4.0's one file per notification, and 4.5's +# live.json, history.json and quiet.json. The demo's rows are found, taken +# away through Omarchy's forget call when the shell answers and directly +# when it doesn't, a real notification is never touched, Do Not Disturb is +# read from the right file, and nothing from a backup goes into a 4.5 store. +set -euo pipefail +cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.." +t=$(mktemp -d); trap 'rm -rf "$t"' EXIT +sed -n "/^cat > \"\$stage\/demo.py\" <<'PY'\$/,/^PY\$/p" tools/phone-demo | sed '1d;$d' > "$t/demo.py" +[[ -s $t/demo.py ]] || { echo "not ok - found the phone side of tools/phone-demo" >&2; exit 1; } +mkdir -p "$t/bin" "$t/stage" "$t/data" +DEMO_STAGE=$t/stage DEMO_DATA=$t/data DEMO_HOME=$t/home T=$t PATH="$t/bin:$PATH" python3 -B - <<'PY' +import importlib.util, io, json, os, stat, sys, contextlib +t = os.environ["T"] +spec = importlib.util.spec_from_file_location("demo", t + "/demo.py"); d = importlib.util.module_from_spec(spec); spec.loader.exec_module(d) +d.time.sleep = lambda s: None +def check(ok, what): + print(("ok - " if ok else "not ok - ") + what) + if not ok: sys.exit(1) +def quiet(f): + out = io.StringIO() + with contextlib.redirect_stdout(out): f() + return out.getvalue() +notes = d.NOTES; state = os.path.dirname(notes) +demo = [dict(app=a, summary=s, body=b) for a, _, _, s, b, _ in d.note_list(None)] +real = dict(app="Signal", summary="Dana", body="see you at six") +def fresh(): + import shutil; shutil.rmtree(os.path.join(t, "home"), ignore_errors=True); os.makedirs(notes) +def shell_script(body): + p = os.path.join(t, "bin", "omarchy-shell") + if body is None: + if os.path.exists(p): os.remove(p) + return + open(p, "w").write("#!/bin/sh\n" + body + "\n"); os.chmod(p, 0o755) +def write45(live, hist, quiet_text=""): + for name, rows in (("live.json", live), ("history.json", hist)): + open(os.path.join(notes, name), "w").write(json.dumps(rows, separators=(",", ":")) + "\n") + open(os.path.join(notes, "quiet.json"), "w").write(quiet_text) +def rows(name): return json.load(open(os.path.join(notes, name))) + +# Omarchy 4.5, the shell not answering: the rows go out of the two files directly +fresh(); shell_script(None) +write45([dict(demo[0], key="k1"), dict(real, key="r1")], [dict(demo[1], key="k2"), dict(real, key="r2"), dict(demo[2], key="k3")]) +check(d.store_45() and sorted(n for n, p in d.demo_notes()) == ["k1", "k2", "k3"] and all(p is None for n, p in d.demo_notes()), + "4.5: the demo's rows are found by key in live.json and history.json") +out = quiet(d.forget_notes) +check([r["key"] for r in rows("live.json")] == ["r1"] and [r["key"] for r in rows("history.json")] == ["r2"], + "4.5, no shell: the demo's rows are taken out and the real notifications stay") +check("took away 3 demo notification(s)" in out and "still there" not in out, "4.5, no shell: it says how many it took away") +check(stat.S_IMODE(os.stat(os.path.join(notes, "history.json")).st_mode) == 0o600 and open(os.path.join(notes, "history.json")).read().endswith("]\n"), + "4.5, no shell: the files stay 0600 and compact, as the service writes them") + +# Omarchy 4.5, the shell answering: forget is called with each key and the store is left to the service +fresh(); shell_script('echo "$*" >> "%s/calls"; echo ok' % t) +write45([dict(demo[0], key="k1")], [dict(demo[1], key="k2"), dict(real, key="r2")]) +before = open(os.path.join(notes, "history.json")).read() +out = quiet(d.forget_notes) +calls = open(t + "/calls").read().split("\n") +check("notifications forget k1" in calls and "notifications forget k2" in calls and "notifications forget r2" not in calls, + "4.5, the shell answers: Omarchy's forget is called with each demo row's key, never a real one's") +check(open(os.path.join(notes, "history.json")).read() == before and "2 still there" in out, + "4.5, the shell answers: the store is the service's to write, and rows it leaves are counted, not hidden") + +# Do Not Disturb on 4.5 is quiet.json; empty means off +check(d.dnd_on() is False, "4.5: an empty quiet.json is Do Not Disturb off") +open(os.path.join(notes, "quiet.json"), "w").write('{"dnd":true}\n') +open(os.path.join(state, "notifications.json"), "w").write('{"dnd":false}') +check(d.dnd_on() is True, "4.5: Do Not Disturb is read from quiet.json, not 4.0's notifications.json") + +# Nothing from a backup goes into a 4.5 store +src = os.path.join(t, "stage", "notes", "notifications", "history"); os.makedirs(src) +open(os.path.join(src, "1-1.json"), "w").write(json.dumps(real)) +out = quiet(d.restore_notes) +check(not os.path.exists(os.path.join(notes, "history")) and "left as it is" in out, "4.5: restore-notes copies no 4.0 history into the store") + +# Omarchy 4.0: one file per notification, by file name +fresh(); shell_script(None); os.makedirs(os.path.join(notes, "history")) +open(os.path.join(notes, "5-1.json"), "w").write(json.dumps(demo[0]) + "\n") +open(os.path.join(notes, "history", "4-1.json"), "w").write(json.dumps(demo[1]) + "\n") +open(os.path.join(notes, "history", "3-1.json"), "w").write(json.dumps(real) + "\n") +check(not d.store_45() and sorted(n for n, p in d.demo_notes()) == ["4-1.json", "5-1.json"], "4.0: the demo's files are found by name") +quiet(d.forget_notes) +check(sorted(os.listdir(os.path.join(notes, "history"))) == ["3-1.json"] and not os.path.exists(os.path.join(notes, "5-1.json")), + "4.0, no shell: the demo's files are removed and the real one stays") +open(os.path.join(state, "notifications.json"), "w").write('{"dnd":true}') +check(d.dnd_on() is True, "4.0: Do Not Disturb is read from notifications.json") +out = quiet(d.restore_notes) +check(os.path.exists(os.path.join(notes, "history", "1-1.json")), "4.0: restore-notes still puts a pushed-out history entry back") +PY diff --git a/tools/phone-demo b/tools/phone-demo index 8a9721186..655a28bc9 100755 --- a/tools/phone-demo +++ b/tools/phone-demo @@ -631,9 +631,35 @@ def note_list(m): ] -def demo_note_files(): - """Omarchy's files (toasts on screen and history) holding a demo notification.""" +def store_45(): + """Omarchy 4.5 keeps the toasts on screen in live.json and the closed ones in + history.json, each one JSON array of rows with a key; 4.0 kept one file + per notification. history.json is there from the 4.5 service's first + start, so it tells the two apart (as the shade does, omamobile-shell 0.2.125).""" + return os.path.isfile(os.path.join(NOTES, "history.json")) + + +def rows_45(name): + try: + with open(os.path.join(NOTES, name), encoding="utf-8") as f: + v = json.load(f) + except (OSError, ValueError): + return [] + return [r for r in v if isinstance(r, dict)] if isinstance(v, list) else [] + + +def demo_notes(): + """The demo notifications Omarchy still holds, on screen or in history: + (the name its forget call takes, the 4.0 file or None on 4.5).""" wanted = {(a, s, b) for a, _, _, s, b, _ in note_list(None)} + is_demo = lambda v: isinstance(v, dict) and (v.get("app"), v.get("summary"), v.get("body")) in wanted + if store_45(): + keys = [] + for name in ("live.json", "history.json"): + for r in rows_45(name): + if is_demo(r) and r.get("key") and str(r["key"]) not in keys: + keys.append(str(r["key"])) + return [(k, None) for k in keys] found = [] for path in glob.glob(os.path.join(NOTES, "*.json")) + glob.glob(os.path.join(NOTES, "history", "*.json")): try: @@ -641,8 +667,8 @@ def demo_note_files(): v = json.loads(f.readline()) except (OSError, ValueError): continue - if isinstance(v, dict) and (v.get("app"), v.get("summary"), v.get("body")) in wanted: - found.append(path) + if is_demo(v): + found.append((os.path.basename(path), path)) return found @@ -653,32 +679,54 @@ def shell(*args): return "" +def drop_rows_45(keys): + """Without the shell nothing else writes the 4.5 store: take the rows out + of its two files directly, as the service writes them (compact, 0600).""" + for name in ("live.json", "history.json"): + path = os.path.join(NOTES, name) + rows = rows_45(name) + kept = [r for r in rows if str(r.get("key")) not in keys] + if len(kept) == len(rows): + continue + tmp = path + ".demo-tmp" + with open(os.open(tmp, os.O_WRONLY | os.O_CREAT | os.O_TRUNC, 0o600), "w", encoding="utf-8") as f: + f.write(json.dumps(kept, separators=(",", ":"), ensure_ascii=False) + "\n") + os.replace(tmp, path) + + def forget_notes(): - files = demo_note_files() - for path in files: - # Omarchy's own forget (patch 0019) closes a toast on screen and - # deletes the entry; without the shell, the file goes directly. - if shell("notifications", "forget", os.path.basename(path)) != "ok": + notes = demo_notes() + # Omarchy's own forget closes a toast on screen and deletes the entry (by + # file name on 4.0, patch 0019; by key on 4.5). Only when the shell doesn't + # answer is the store touched directly: the running service owns it. + unanswered = [(name, path) for name, path in notes if shell("notifications", "forget", name) != "ok"] + for name, path in unanswered: + if path: try: os.remove(path) except FileNotFoundError: pass + if unanswered and store_45(): + drop_rows_45({name for name, _ in unanswered}) for _ in range(20): - if not demo_note_files(): + if not demo_notes(): break time.sleep(0.25) - left = demo_note_files() - for path in left: - try: - os.remove(path) - except FileNotFoundError: - pass - print("took away %d demo notification(s)" % len(files)) + for name, path in demo_notes(): + if path: + try: + os.remove(path) + except FileNotFoundError: + pass + left = len(demo_notes()) + print("took away %d demo notification(s)%s" % (len(notes) - left, ", %d still there" % left if left else "")) def dnd_on(): + # 4.5: quiet.json ({"dnd": true}; empty while it has never been on). 4.0: notifications.json. + name = "notifications/quiet.json" if store_45() else "notifications.json" try: - with open(os.path.join(HOME, ".local/state/omarchy/notifications.json")) as f: + with open(os.path.join(HOME, ".local/state/omarchy", name)) as f: return json.load(f).get("dnd") is True except (OSError, ValueError, AttributeError): return False @@ -711,6 +759,12 @@ def notify(): def restore_notes(): """Put back history entries (and their images) the demo pushed out of the newest ten.""" src = os.path.join(STAGE, "notes", "notifications") + # Omarchy 4.5's history is one file the running service owns and rewrites, + # and it never read 4.0's history/ and images/ directories: nothing from + # a backup of either layout is copied into a 4.5 store. + if store_45() or os.path.isfile(os.path.join(src, "history.json")): + print("notification history left as it is (Omarchy 4.5 keeps it in one file its own service writes)") + return n = 0 for sub in ("history", "images"): for path in glob.glob(os.path.join(src, sub, "*")): @@ -743,7 +797,7 @@ def status(): print(" calls: %d of %d present" % ( present(CALLS_DB, "SELECT count(*) FROM calls WHERE id IN (%s)", m["calls"]), len(m["calls"]))) print(" backup: %s" % (m.get("backup") or "?")) - print("demo notifications on the phone: %d" % len(demo_note_files())) + print("demo notifications on the phone: %d" % len(demo_notes())) print("do not disturb: %s" % ("on (the lock glance shows no notifications)" if dnd_on() else "off")) if os.path.exists(os.path.join(HOME, ".local/state/omarchy/toggles/lock-hide-notification-text")): print("lock-hide-notification-text: on (the glance shows \"Unlock to read\")")