Skip to content

知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.

feat(linux): when the AI service needs you to fix something, like a key that stopped working, the top-bar icon shows an orange dot and one notification per meeting says so if the notes window is put away; an outage gets one after 2 minutes

wquguru20b04b6c95fe
diff --git a/linux/README.md b/linux/README.md
index 238149d..d8263c9 100644
--- a/linux/README.md
+++ b/linux/README.md
@@ -55,7 +55,7 @@ linux/vm/vmctl.sh shot shot.png # 截屏(QEMU,不受 Wayland
linux/scripts/build.sh && linux/vm/push.sh # 把 build/cicada-linux 和测试工具放进虚拟机的 ~/cicada-linux
```
-在虚拟机里 `~/cicada-linux/bin/cicada --dev` 启动开发版(记录在 `~/.local/share/cicada-dev`,日志在 `~/.local/state/cicada-dev/debug.log`),再用 `scripts/cicadactl` 驱动它:`state`、`start`、`stop`、`feed <wav>`、`sample`、`view notes|subtitles`、`show records|notes|settings|onboarding`、`windows`、`quit`;结束会议用 `calls zoom=Zoom|none|real`(假装会议软件在通话)、`realcalls`、`endtimes <没人说话秒数> <倒计时> <挂断宽限>|off`、`endkeep`;纪要窗用 `notes-tab notes|transcript`、`notes-search <词>`、`notes-wide on|off`、`notes-end ask|keep|end`(结束前的确认)、`notes-status failed|invalidKey|rateLimited|idle`(看出错横幅);记录用 `rename <id> [标题]`(不给标题即恢复自动标题)、`delete <id>`;还有 `onboarding-step <0-4>`、`settings-scroll <0-1>`;AI 服务检查用 `ai-check`(现在查一次)、`ai-check-sim <原因|lowBalance|off>`(每次检查都按这个原因结束,截各状态的图)、`ask settings|fix|close`(回答开始听时的对话框)、`settings-ai`、`tray-menu`(顶栏菜单的各项)。`scripts/smoke.sh` 一步跑完:开窗口、放一段声音、确认 monitor 听到了。
+在虚拟机里 `~/cicada-linux/bin/cicada --dev` 启动开发版(记录在 `~/.local/share/cicada-dev`,日志在 `~/.local/state/cicada-dev/debug.log`),再用 `scripts/cicadactl` 驱动它:`state`、`start`、`stop`、`feed <wav>`、`sample`、`view notes|subtitles`、`show records|notes|settings|onboarding`、`windows`、`quit`;结束会议用 `calls zoom=Zoom|none|real`(假装会议软件在通话)、`realcalls`、`endtimes <没人说话秒数> <倒计时> <挂断宽限>|off`、`endkeep`;纪要窗用 `notes-tab notes|transcript`、`notes-search <词>`、`notes-wide on|off`、`notes-end ask|keep|end`(结束前的确认)、`notes-status failed|invalidKey|rateLimited|idle`(看出错横幅);记录用 `rename <id> [标题]`(不给标题即恢复自动标题)、`delete <id>`;还有 `onboarding-step <0-4>`、`settings-scroll <0-1>`;AI 服务检查用 `ai-check`(现在查一次)、`ai-check-sim <原因|lowBalance|off>`(每次检查都按这个原因结束,截各状态的图)、`ask settings|fix|close`(回答开始听时的对话框)、`settings-ai`、`tray-menu`(顶栏菜单的各项);只有你能修的问题(key、余额、模型、地址)在顶栏图标上加橙点,纪要窗收起时每场会议发一次通知(连不上的话持续 2 分钟后才发),`hide notes` 收起纪要窗、`problem-wait <秒>|off` 缩短这 2 分钟、`state` 的 `aiProblem` 看结果。`scripts/smoke.sh` 一步跑完:开窗口、放一段声音、确认 monitor 听到了。
焦点:`bin/focus_test.sh` 开一个最大化的“会议 app”(`focus_probe.py`),用 `click.py`(uinput,真实指针)点纪要窗和“字幕”链接,再在 Mac 上 `linux/vm/vmctl.sh type ok`;`/tmp/focus-probe.log` 里不能出现 `active=False`,最后一行应是 `text=…ok`。xdotool 只到 XWayland,测不了这个。
diff --git a/linux/cicada/app/aicheck_view.py b/linux/cicada/app/aicheck_view.py
index 45c448a..a5f9db7 100644
--- a/linux/cicada/app/aicheck_view.py
+++ b/linux/cicada/app/aicheck_view.py
@@ -146,16 +146,23 @@ def strip_words(app):
if f is None:
return r.balance_line(name), L("用完后纪要会停下")
c = ling.config()
- reason = f[1]
- title = {aicheck.UNREACHABLE: lambda: LF("连不上 %@", name),
- aicheck.INVALID_KEY: lambda: LF("%@ 的 key 用不了", name),
- aicheck.MGMT_KEY: lambda: LF("%@ 的 key 用不了", name),
- aicheck.NO_CREDIT: lambda: LF("%@ 账户余额不足。", name),
- aicheck.NO_MODEL: lambda: LF("%@ 上找不到 %@ 这个模型。", name, c.text_model),
- aicheck.DEAF: lambda: LF("%@ 听不了声音。", c.hear_model)}.get(reason, lambda: L("AI 服务的设置要改一下"))()
+ from ..core import problem
+ title = LF("连不上 %@", name) if f[1] == aicheck.UNREACHABLE else \
+ problem.title(f[1], name, c.text_model, c.hear_model) or L("AI 服务的设置要改一下")
return title, ling.message() or ""
+def blocking(app) -> str | None:
+ """What stops Cicada until the user changes something (AIServiceStrip.blocking): the tray's dot
+ and the notification. Not the network, a busy service or one that errs."""
+ from ..core import problem
+ ling = app.ling
+ if not ling.has_key:
+ return None
+ c = ling.config()
+ return problem.title(ling.check_result.reason, ling.service_name, c.text_model, c.hear_model)
+
+
def key_hint(app, key: str):
"""A pasted key that belongs elsewhere: (what it is, the service it fits or None), else None.
A management key can't call models; another service's key goes there with one click."""
diff --git a/linux/cicada/app/debug.py b/linux/cicada/app/debug.py
index ed002e9..35a8658 100644
--- a/linux/cicada/app/debug.py
+++ b/linux/cicada/app/debug.py
@@ -13,7 +13,8 @@ per connection, one JSON line back. Never in release builds.
ask settings|fix|close (answer the 开始听 dialog a failed check put up) | settings-ai (设置 at AI 服务) | tray-menu
pill (the records window capsule's widget sizes)
listen-source system|room|both (听哪里) | room-mic [<source>|auto] | lines [<record id>] (each line's via)
- online both|no (answer 线上也有人?) | room-mic-force <source>|"" (a null sink's monitor as the room mic)"""
+ online both|no (answer 线上也有人?) | room-mic-force <source>|"" (a null sink's monitor as the room mic)
+ hide notes | problem-wait <seconds>|off (how long unreachable lasts before its notification; off: 2 min)"""
from __future__ import annotations
import os
@@ -103,6 +104,14 @@ def _speakers(app, s) -> dict:
return out
+def _problem(app) -> dict:
+ from .aicheck_view import blocking
+ w = app.notes_window
+ return {"blocking": blocking(app), "reason": app.ling.check_result.reason, "sent": sorted(app.notices.sent),
+ "wait": app.notices.wait, "notesShown": bool(w is not None and w.get_visible()),
+ "tray": app.tray.badged[1] if app.tray is not None else None}
+
+
def updates_notified():
from ..core import updates
return updates.notified()
@@ -133,6 +142,7 @@ def command(app, line: str) -> dict:
"speakers": _speakers(app, s),
"ending": {"title": app.ending_title(app.ending), "left": app.ending_left(app.ending),
"reason": repr(app.ending.reason)} if app.ending else None,
+ "aiProblem": _problem(app),
"autoEnded": app.auto_ended, "fakeCalls": app.fake_calls,
"endTimes": list(app.end_times) if app.end_times else None,
"inCall": dict(app.end_watch.in_call) if app.end_watch else None}
@@ -148,6 +158,14 @@ def command(app, line: str) -> dict:
app.ling.simulate = None if arg == "off" else arg
threading.Thread(target=app.ling.check, daemon=True).start()
return {"ok": True, "simulate": app.ling.simulate}
+ if cmd == "hide" and arg == "notes":
+ if app.notes_window is not None:
+ app.notes_window.hide()
+ return {"ok": True}
+ if cmd == "problem-wait":
+ from ..core.problem import UNREACHABLE_AFTER
+ app.notices.wait = UNREACHABLE_AFTER if arg == "off" else float(arg)
+ return {"ok": True, "problem": _problem(app)}
if cmd == "ai-check":
threading.Thread(target=app.ling.check, daemon=True).start()
return {"ok": True}
diff --git a/linux/cicada/app/main.py b/linux/cicada/app/main.py
index e1d24c6..b78fe59 100644
--- a/linux/cicada/app/main.py
+++ b/linux/cicada/app/main.py
@@ -119,14 +119,45 @@ class Controller:
self._calls_busy = False
from .updater import Updates
self.updates = Updates(self) # 新版本: GitHub Releases, the tray menu and 设置 › 关于
+ # Something only the user can fix, said beyond the notes window (problem.py): the tray's dot
+ # and, once a meeting while the window is put away, a notification.
+ from ..core.problem import Notices
+ self.notices = Notices()
+ self._notified = False
GLib.timeout_add_seconds(1, self._tick)
GLib.timeout_add_seconds(2, self._watch_end)
def _tick(self) -> bool:
if self.tray is not None and self.listen.listening:
self.tray.tick()
+ self.check_problem()
return True
+ def check_problem(self) -> None:
+ """Every second and on each change: a notification when problem.Notices says one is due,
+ withdrawn once the service works again. Never a dialog: it would take the focus from the
+ meeting app."""
+ from ..core import aicheck, problem
+ reason = self.ling.check_result.reason if self.ling.has_key else None
+ w = self.notes_window
+ shown = w is not None and w.get_visible() and w.shows_problem(reason)
+ kind = self.notices.tick(time.time(), self.listen.listening, reason, shown)
+ if kind is not None:
+ from .aicheck_view import blocking
+ name = self.ling.service_name
+ title = blocking(self) if kind == "blocking" else LF("连不上 %@", name)
+ n = Gio.Notification.new(title or L("AI 服务的设置要改一下"))
+ n.set_body(self.ling.message() or "")
+ n.set_default_action("app.ai-settings")
+ n.add_button(L("打开设置"), "app.ai-settings")
+ self.application.send_notification("problem", n)
+ self._notified = True
+ Log.d("problem: notified %s (%s)" % (kind, reason))
+ elif self._notified and not (problem.blocking(reason) or reason == aicheck.UNREACHABLE):
+ self.application.withdraw_notification("problem")
+ self._notified = False
+ Log.d("problem: withdrawn")
+
def launch(self, speak: bool = False, background: bool = False) -> None:
ui.install_css()
lang = strings.language
@@ -191,6 +222,7 @@ class Controller:
self.tray.update()
if self.settings_window is not None:
self.settings_window.refresh_version()
+ self.check_problem()
return False
GLib.idle_add(run)
@@ -591,6 +623,7 @@ class Controller:
self.end_watch = meeting_end.MeetingEndWatch(self.end_config(), time.time())
self.online_answered = False
self.online_prompt = None
+ self.notices.start()
room = self._room_side_source() if kind == "both" else None
self.listen.start(source, kind, mic=kind == "room")
if room is not None:
@@ -938,8 +971,9 @@ class App(Gtk.Application):
speak = Gio.SimpleAction.new("speak", None) # `cicada --speak`: 开始 / 停止说
speak.connect("activate", lambda *_: self.controller.toggle_speaking())
self.add_action(speak)
- # The update notification's click and its 更新说明 button.
+ # The update notification's click and its 更新说明 button; a problem's 打开设置.
for name, action in (("install-update", lambda: self.controller.updates.install()),
+ ("ai-settings", lambda: self.controller.show_settings(ai=True)),
("update-notes", lambda: self.controller.updates.open_notes())):
a = Gio.SimpleAction.new(name, None)
a.connect("activate", lambda _a, _p, f=action: f())
diff --git a/linux/cicada/app/notes_window.py b/linux/cicada/app/notes_window.py
index daba7bc..d580772 100644
--- a/linux/cicada/app/notes_window.py
+++ b/linux/cicada/app/notes_window.py
@@ -14,6 +14,7 @@ from datetime import datetime, timezone
from gi.repository import Gdk, GLib, Gtk
from ..core import notes as notesmod
+from ..core import problem
from ..core import roommic, speakers
from ..core.strings import L, LF
from ..core.support import Log, clock
@@ -737,6 +738,19 @@ class NotesWindow(Gtk.Window):
row.pack_end(b, False, False, 0)
box.pack_start(row, False, False, 0)
+ def _reason(self) -> str | None:
+ ling = self.app.ling
+ return ling.check_result.reason if ling.has_key else None
+
+ def shows_problem(self, reason) -> bool:
+ """Whether this window, as it is laid out, says what's wrong (then no notification): the
+ subtitle bar and the live card only what stops hearing; the notes column the rest."""
+ if not problem.blocking(reason) and reason != "unreachable":
+ return True
+ if self.subtitles or getattr(self, "body", None) is None:
+ return problem.stops_hearing(reason) or reason == "unreachable"
+ return self.wide or self.tab == "notes"
+
def _fill_live(self) -> None:
box = self.live
ui.clear(box)
@@ -760,6 +774,12 @@ class NotesWindow(Gtk.Window):
ling = self.app.ling.provider.id == "ling"
self._state_card(box, "dialog-password-symbolic", "warn", listen.problem or L("Ling key 无效"),
L("在设置里更换 Ling key") if ling else L("在 设置 › AI 服务 里换一个"), L("设置"), self.app.show_settings)
+ elif problem.stops_hearing(self._reason()) and kind not in ("network", "busy"):
+ # Nothing is heard until it's fixed (no credit, a bad address…): said where the subtitles
+ # would be, with the way to fix it, not "等有人说话".
+ from .aicheck_view import blocking
+ self._state_card(box, "dialog-warning-symbolic", "warn", blocking(self.app), self.app.ling.message() or "",
+ L("设置"), lambda: self.app.show_settings(ai=True))
elif line is None:
if listen.listening:
hearing = listen.pending > 0 or listen.level >= 0.01
@@ -840,8 +860,21 @@ class NotesWindow(Gtk.Window):
s = self._session()
covered = engine.notes.covered
last = s.lines[covered - 1].t if s is not None and 0 < covered <= len(s.lines) else 0.0
+ reason = self._reason()
if engine.status == notesmod.INVALID_KEY:
text, tone = LF("key 已失效(令牌被删或免费额度用尽),纪要停留在 %@。", clock(last)), "error"
+ elif problem.blocking(reason) and (reason in problem.NOTES_ONLY or not self.live_box.get_visible()):
+ # Only the user can fix it (the live card says it too, when it shows): what, and 打开设置.
+ from .aicheck_view import blocking
+ row = ui.hbox(10)
+ row.pack_start(ui.vbox(2, ui.text(blocking(self.app), 12, "ink", bold=True, width_chars=24),
+ ui.text(self.app.ling.message() or "", 12, "secondary", width_chars=24)), True, True, 0)
+ b = ui.button(L("打开设置"), lambda: self.app.show_settings(ai=True), "darkbtn")
+ b.set_can_focus(False)
+ b.set_valign(Gtk.Align.CENTER)
+ row.pack_end(b, False, False, 0)
+ body.pack_start(ui.pad(ui.panel(ui.pad(row, 12), "notice", "error"), 0, 0, 10, 0), False, False, 0)
+ return
elif engine.status == notesmod.RATE_LIMITED:
text, tone = LF("%@ 很忙,纪要稍后补上。字幕照常。", self.app.ling.service_name), "busy"
elif engine.status == notesmod.FAILED and self.app.ling.check_result.reason == "unreachable":
diff --git a/linux/cicada/app/tray.py b/linux/cicada/app/tray.py
index 764071f..e49e24a 100644
--- a/linux/cicada/app/tray.py
+++ b/linux/cicada/app/tray.py
@@ -50,7 +50,7 @@ class Tray:
lib.app_indicator_set_icon_full.argtypes = [ctypes.c_void_p, ctypes.c_char_p, ctypes.c_char_p]
self.lib = lib
self.icon = icon or "cicada"
- self.badged = False
+ self.badged = (False, None)
# An absolute path works as the icon "name", so no icon theme install is needed.
self.ind = lib.app_indicator_new(b"cicada", (icon or "cicada").encode(), self.CATEGORY_APPLICATION_STATUS)
lib.app_indicator_set_status(self.ind, self.STATUS_ACTIVE)
@@ -72,8 +72,9 @@ class Tray:
menu.append(i)
return i
# The AI service when only the user can fix it (AIServiceStrip.swift, the panel's top): what's
- # wrong, and a click opens 设置 › AI 服务. The icon stays as it is.
- from .aicheck_view import strip_words
+ # wrong, and a click opens 设置 › AI 服务. One that stays until the user changes something also
+ # puts the dot on the icon (below).
+ from .aicheck_view import blocking, strip_words
broken = strip_words(self.app)
if broken:
item("⚠ " + broken[0].rstrip("。.") + " · " + L("打开设置"), lambda: self.app.show_settings(ai=True))
@@ -116,19 +117,22 @@ class Tray:
self.lib.app_indicator_set_menu(self.ind, _gpointer(menu))
self.menu = menu # the indicator holds a ref too; keep ours so Python doesn't free it early
self.lib.app_indicator_set_label(self.ind, (self._clock() if listening else "").encode(), b"")
- self._badge(self.app.updates.update)
-
- def _badge(self, update) -> None:
- """The Mac's update dot on the top-bar icon: the same icon with an orange dot top right."""
- on = update is not None
- if on == self.badged:
+ self._badge(self.app.updates.update, blocking(self.app))
+
+ def _badge(self, update, problem) -> None:
+ """The Mac's menu bar dot on the top-bar icon: the same icon with an orange dot top right, for
+ something only the user can fix or else a new version. The icon's description says which (the
+ Mac's tooltip; screen readers and the indicator read it)."""
+ on = problem is not None or update is not None
+ desc = L("知了") + (" · " + problem if problem else " · " + LF("有新版本 %@", update.version) if update else "")
+ if (on, desc) == self.badged:
return
path = badged_icon(self.icon) if on else self.icon
if path is None:
return
- desc = L("知了") + (" · " + LF("有新版本 %@", update.version) if on else "")
self.lib.app_indicator_set_icon_full(self.ind, path.encode(), desc.encode())
- self.badged = on
+ self.badged = (on, desc)
+ Log.d("tray: badge=%s %s" % ("problem" if problem else "update" if update else "none", desc))
def _clock(self) -> str:
from datetime import datetime, timezone
diff --git a/linux/cicada/core/problem.py b/linux/cicada/core/problem.py
new file mode 100644
index 0000000..5897242
--- /dev/null
+++ b/linux/cicada/core/problem.py
@@ -0,0 +1,71 @@
+"""Something only the user can fix, said beyond the settings (the Mac's AIServiceStrip.blocking and
+its menu bar dot). Plain GNOME has no top bar icon and the notes window may be put away while you
+meet, so on Linux it is also a desktop notification: once a meeting for a failure that stays until
+you change something, and once for a service that has been unreachable for 2 minutes (shorter is
+usually the network catching its breath). Never while the notes window shows it already. Stdlib
+only: the Mac runs these tests too."""
+from __future__ import annotations
+
+from . import aicheck
+from .strings import L, LF
+
+# Stay until the key, the account, a model or the address changes: not the network, a busy service
+# or one that errs. DEAF is the ports' own: the service hears for us, so nothing goes on without it.
+BLOCKING = (aicheck.INVALID_KEY, aicheck.MGMT_KEY, aicheck.NO_CREDIT, aicheck.NO_MODEL,
+ aicheck.BAD_ADDRESS, aicheck.NO_JSON, aicheck.DEAF)
+# Only the notes and the full translations stop; the subtitles go on (the hear model is fine).
+NOTES_ONLY = (aicheck.NO_MODEL, aicheck.NO_JSON)
+UNREACHABLE_AFTER = 120.0
+
+
+def blocking(reason: str | None) -> bool:
+ return reason in BLOCKING
+
+
+def stops_hearing(reason: str | None) -> bool:
+ return blocking(reason) and reason not in NOTES_ONLY
+
+
+def title(reason: str | None, service: str, text_model: str, hear_model: str) -> str | None:
+ """The Mac's words for a blocking failure (AIServiceStrip.blocking), plus 听不了声音; None otherwise."""
+ if not blocking(reason):
+ return None
+ return {aicheck.INVALID_KEY: lambda: LF("%@ 的 key 用不了", service),
+ aicheck.MGMT_KEY: lambda: LF("%@ 的 key 用不了", service),
+ aicheck.NO_CREDIT: lambda: LF("%@ 账户余额不足。", service),
+ aicheck.NO_MODEL: lambda: LF("%@ 上找不到 %@ 这个模型。", service, text_model),
+ aicheck.DEAF: lambda: LF("%@ 听不了声音。", hear_model)}.get(reason, lambda: L("AI 服务的设置要改一下"))()
+
+
+class Notices:
+ """When to notify, per meeting: `start` at 开始听, `tick` every second and on each change."""
+
+ def __init__(self, wait: float = UNREACHABLE_AFTER):
+ self.wait = wait
+ self.sent: set = set()
+ self.since: float | None = None # unreachable, while listening, since then
+
+ def start(self) -> None:
+ self.sent = set()
+ self.since = None
+
+ def tick(self, now: float, listening: bool, reason: str | None, shown: bool) -> str | None:
+ """"blocking" or "unreachable" when a notification is due now (counted as sent), else None.
+ `shown`: the notes window is up and says it."""
+ if reason == aicheck.UNREACHABLE and listening:
+ if self.since is None:
+ self.since = now
+ else:
+ self.since = None
+ if not listening or shown:
+ return None
+ if blocking(reason):
+ kind = "blocking"
+ elif self.since is not None and now - self.since >= self.wait:
+ kind = "unreachable"
+ else:
+ return None
+ if kind in self.sent:
+ return None
+ self.sent.add(kind)
+ return kind
diff --git a/linux/tests/test_problem.py b/linux/tests/test_problem.py
new file mode 100644
index 0000000..377dfa8
--- /dev/null
+++ b/linux/tests/test_problem.py
@@ -0,0 +1,103 @@
+"""Something only the user can fix (problem.py, the Mac's AIServiceStrip.blocking): which reasons
+count, and when the notification is due."""
+from __future__ import annotations
+
+import os
+import sys
+import unittest
+
+sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
+
+from cicada.core import aicheck, problem # noqa: E402
+from cicada.core.problem import Notices # noqa: E402
+
+
+class Reasons(unittest.TestCase):
+ def test_which_reasons_block(self):
+ for r in ("invalidKey", "mgmtKey", "noCredit", "noModel", "badAddress", "noJSON", "deaf"):
+ self.assertTrue(problem.blocking(r), r)
+ for r in ("unreachable", "busy", "serverError", None):
+ self.assertFalse(problem.blocking(r), r)
+ self.assertEqual(set(aicheck.REASONS), set(problem.BLOCKING) | {"unreachable", "busy", "serverError"})
+
+ def test_notes_only_reasons_leave_hearing(self):
+ self.assertFalse(problem.stops_hearing("noModel"))
+ self.assertFalse(problem.stops_hearing("noJSON"))
+ self.assertTrue(problem.stops_hearing("noCredit"))
+ self.assertTrue(problem.stops_hearing("deaf"))
+ self.assertFalse(problem.stops_hearing("unreachable"))
+
+ def test_titles_are_the_macs(self):
+ t = lambda r: problem.title(r, "OpenRouter", "qwen/text", "qwen/omni") # noqa: E731
+ self.assertEqual("OpenRouter 的 key 用不了", t("invalidKey"))
+ self.assertEqual("OpenRouter 的 key 用不了", t("mgmtKey"))
+ self.assertEqual("OpenRouter 账户余额不足。", t("noCredit"))
+ self.assertEqual("OpenRouter 上找不到 qwen/text 这个模型。", t("noModel"))
+ self.assertEqual("AI 服务的设置要改一下", t("badAddress"))
+ self.assertEqual("AI 服务的设置要改一下", t("noJSON"))
+ self.assertEqual("qwen/omni 听不了声音。", t("deaf"))
+ self.assertIsNone(t("unreachable"))
+ self.assertIsNone(t("busy"))
+ self.assertIsNone(t(None))
+
+
+class When(unittest.TestCase):
+ def test_blocking_once_a_meeting_only_while_listening_and_put_away(self):
+ n = Notices()
+ n.start()
+ self.assertIsNone(n.tick(0, False, "invalidKey", False)) # not listening
+ self.assertIsNone(n.tick(1, True, "invalidKey", True)) # the notes window says it
+ self.assertEqual("blocking", n.tick(2, True, "invalidKey", False))
+ self.assertIsNone(n.tick(3, True, "invalidKey", False))
+ self.assertIsNone(n.tick(4, True, "noCredit", False)) # another one, same meeting
+ n.start()
+ self.assertEqual("blocking", n.tick(5, True, "noCredit", False))
+
+ def test_busy_and_server_errors_never(self):
+ n = Notices()
+ n.start()
+ for t in range(0, 600, 10):
+ self.assertIsNone(n.tick(t, True, "busy" if t % 20 else "serverError", False))
+
+ def test_unreachable_after_two_minutes(self):
+ n = Notices()
+ n.start()
+ self.assertIsNone(n.tick(0, True, "unreachable", False))
+ self.assertIsNone(n.tick(119, True, "unreachable", False))
+ self.assertEqual("unreachable", n.tick(120, True, "unreachable", False))
+ self.assertIsNone(n.tick(400, True, "unreachable", False))
+
+ def test_unreachable_clock_restarts_when_it_comes_back(self):
+ n = Notices()
+ n.start()
+ n.tick(0, True, "unreachable", False)
+ n.tick(100, True, None, False) # back
+ self.assertIsNone(n.tick(110, True, "unreachable", False))
+ self.assertIsNone(n.tick(200, True, "unreachable", False))
+ self.assertEqual("unreachable", n.tick(230, True, "unreachable", False))
+
+ def test_unreachable_counts_while_the_window_shows_it(self):
+ # Down for 2 minutes with the window up, then put away: due at once.
+ n = Notices()
+ n.start()
+ n.tick(0, True, "unreachable", True)
+ self.assertIsNone(n.tick(150, True, "unreachable", True))
+ self.assertEqual("unreachable", n.tick(151, True, "unreachable", False))
+
+ def test_unreachable_only_counts_while_listening(self):
+ n = Notices()
+ n.start()
+ n.tick(0, False, "unreachable", False)
+ self.assertIsNone(n.tick(10, True, "unreachable", False))
+ self.assertIsNone(n.tick(129, True, "unreachable", False))
+ self.assertEqual("unreachable", n.tick(130, True, "unreachable", False))
+
+ def test_a_shorter_wait(self):
+ n = Notices(wait=5)
+ n.start()
+ n.tick(0, True, "unreachable", False)
+ self.assertEqual("unreachable", n.tick(5, True, "unreachable", False))
+
+
+if __name__ == "__main__":
+ unittest.main()