"""测「预设快捷入口」+ 打开 Activity。 ★ 关键测的是**回退链**:深链失败 → 退回主界面,而不是"点了没反应"。 emulator 上没装微信/支付宝,所以正好能测"没装"那条路。 同时用**装了的 App** 测正常路径(图库 / 设置 / 浏览器)。 """ import json import sys import urllib.error import urllib.request try: sys.stdout.reconfigure(encoding="utf-8", errors="replace") except Exception: pass URL = "http://127.0.0.1:15199" fails = [] def call(path, payload=None, method="POST", timeout=90): data = json.dumps(payload, ensure_ascii=False).encode("utf-8") if payload is not None else None req = urllib.request.Request( URL + path, data=data, method=method, headers={"Content-Type": "application/json; charset=utf-8"}, ) try: with urllib.request.urlopen(req, timeout=timeout) as r: return json.loads(r.read().decode("utf-8")) except urllib.error.HTTPError as e: return json.loads(e.read().decode("utf-8", "replace")) def tool(name, args): return call("/api/tools/run", {"name": name, "args": args}) def err_of(r): if not isinstance(r, dict): return "" return str(r.get("error") or (r.get("result") or {}).get("error") or "") def check(cond, label, detail=""): print(f" {'✓' if cond else '✗'} {label}" + (f" {detail}" if detail else "")) if not cond: fails.append(label) print("=== 1. 列出预设 ===") r = tool("apps", {"action": "shortcuts"}) res = (r or {}).get("result") or {} items = res.get("shortcuts") or [] check(len(items) >= 10, f"有 {len(items)} 个预设") for it in items[:6]: mark = "装了" if it.get("installed") else "没装" print(f" [{mark}] {it.get('id'):18s} {it.get('label')}") print() print("=== 2. 预设里要有用户点名的那两个 ===") ids = [it.get("id") for it in items] labels = " ".join(it.get("label") or "" for it in items) check("wechat-chat" in ids, "有「微信 · 聊天列表」", "wechat-chat") check("alipay-cainiao" in ids, "有「支付宝 · 菜鸟裹裹」", "alipay-cainiao") check("菜鸟" in labels, "菜鸟那个标签里有「菜鸟」二字") check("扫一扫" in labels, "也有扫一扫这类常用的") print() print("=== 3. ★ 用关键词找预设(用户会说「打开微信聊天」)===") for kw in ("微信聊天", "朋友圈", "菜鸟", "支付宝扫一扫", "抖音"): r = tool("apps", {"action": "shortcut", "shortcut": kw}) res = (r or {}).get("result") or {} err = err_of(r) if res.get("opened"): check(True, f"「{kw}」→ {res.get('label')}", f"how={res.get('how')}") else: # 没装这些 App,所以必然失败 —— 但**必须是"没装"这种可解释的失败** check("没装" in err or "没成功" in err, f"「{kw}」失败但理由可解释", err[:80]) print() print("=== 4. ★★ 失败时必须说明「试过哪些、各自为什么失败」 ===") r = tool("apps", {"action": "shortcut", "shortcut": "朋友圈"}) err = err_of(r) check("试过" in err, "错误里列出了试过哪些候选", err[:110]) check("没装" in err, "说清了是没装(而不是笼统的'打不开')") check("hint" in json.dumps(r, ensure_ascii=False) or "action=open" in err, "给了下一步建议") print() print("=== 5. 不存在的预设要给可选项 ===") r = tool("apps", {"action": "shortcut", "shortcut": "根本不存在的预设xyz"}) err = err_of(r) check("没有叫" in err, "说清了没有这个预设", err[:70]) check("wechat" in err or "可以用的有" in err, "列出了可用的预设") print() print("=== 6. 打开 Activity:只给包名也能开(自己查 launcher activity)===") r = tool("apps", {"action": "list"}) res = (r or {}).get("result") or {} apps = res.get("apps") or [] if apps: pkg = apps[0].get("package") r2 = tool("apps", {"action": "open", "package": pkg}) res2 = (r2 or {}).get("result") or {} check(bool(res2.get("opened")) or bool(err_of(r2)), f"用包名打开 {pkg}", f"how={res2.get('how')} act={res2.get('activity')}") check(bool(res2.get("activity")) or bool(err_of(r2)), "返回了自己查到的 activity 名", str(res2.get("activity"))[:60]) print() print("=== 7. 给了 activity 就直达(activity 没导出时退回主界面)===") if apps: pkg = apps[0].get("package") r3 = tool("apps", {"action": "open", "package": pkg, "activity": pkg + ".NoSuchActivity"}) res3 = (r3 or {}).get("result") or {} err3 = err_of(r3) if res3.get("opened"): check(True, "坏 activity 名 → 自动退回主界面", f"how={res3.get('how')} note={res3.get('note')}") else: check(bool(err3), "或者如实报错", err3[:80]) print() if fails: print(f"❌ {len(fails)} 项不合格:") for f in fails: print(" -", f) sys.exit(1) print("✅ 全部通过")