earshot-cli 0.3.1__tar.gz → 0.5.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/PKG-INFO +9 -1
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/README.md +8 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/pyproject.toml +1 -1
- earshot_cli-0.5.0/src/earshot/__init__.py +25 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/src/earshot/auth.py +26 -2
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/src/earshot/cli.py +56 -1
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/src/earshot/deliver.py +2 -1
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/src/earshot/transcribe_cloud.py +2 -1
- earshot_cli-0.5.0/tests/test_credential_lifecycle.py +118 -0
- earshot_cli-0.5.0/tests/test_ua.py +63 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/uv.lock +1 -1
- earshot_cli-0.3.1/src/earshot/__init__.py +0 -7
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/.github/workflows/ci.yml +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/.github/workflows/publish.yml +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/.gitignore +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/LICENSE +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/scripts/download_models.sh +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/src/earshot/capture.py +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/src/earshot/compose.py +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/src/earshot/config.py +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/src/earshot/extract.py +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/src/earshot/live.py +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/src/earshot/models.py +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/src/earshot/subtitle.py +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/src/earshot/transcribe.py +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/tests/conftest.py +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/tests/test_auth.py +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/tests/test_compose.py +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/tests/test_config.py +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/tests/test_deliver.py +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/tests/test_deliver_api.py +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/tests/test_do_deliver.py +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/tests/test_extract.py +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/tests/test_live.py +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/tests/test_me.py +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/tests/test_subtitle.py +0 -0
- {earshot_cli-0.3.1 → earshot_cli-0.5.0}/tests/test_wizard.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: earshot-cli
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.5.0
|
|
4
4
|
Summary: 旁听剪藏:跨平台桌面端系统声录制 → 本地转写 → 投递知识库(默认 ExoMind,可插拔)
|
|
5
5
|
Project-URL: Homepage, https://github.com/helloworldtang/earshot
|
|
6
6
|
Project-URL: Repository, https://github.com/helloworldtang/earshot
|
|
@@ -137,6 +137,14 @@ earshot login # 浏览器打开授权页,点「授权登录」即完成(已
|
|
|
137
137
|
earshot me # 显示凭据状态与服务连通性(「凭据状态:有效」即就绪)
|
|
138
138
|
```
|
|
139
139
|
|
|
140
|
+
凭据生命周期闭环(login → me → update → logout):
|
|
141
|
+
|
|
142
|
+
```bash
|
|
143
|
+
earshot update --api-key <新key> # 替换 API Key(新 key 从 exomind 管理端复制;缺省交互粘贴)
|
|
144
|
+
earshot logout # 清除本地凭据(保留服务器地址;exomind-cli/fireCut 共用该配置会一并失效)
|
|
145
|
+
earshot help [命令] # 查看帮助(如 earshot help listen)
|
|
146
|
+
```
|
|
147
|
+
|
|
140
148
|
没有 ExoMind 账号?投递可换任意 webhook(`--deliver webhook --webhook <url>`),或 `--deliver none` 只本地存档,两者都不需要登录。
|
|
141
149
|
|
|
142
150
|
`earshot` 与 `earshot-cli` 两个命令名等价可用;`earshot doctor` 可做完整环境自检。
|
|
@@ -104,6 +104,14 @@ earshot login # 浏览器打开授权页,点「授权登录」即完成(已
|
|
|
104
104
|
earshot me # 显示凭据状态与服务连通性(「凭据状态:有效」即就绪)
|
|
105
105
|
```
|
|
106
106
|
|
|
107
|
+
凭据生命周期闭环(login → me → update → logout):
|
|
108
|
+
|
|
109
|
+
```bash
|
|
110
|
+
earshot update --api-key <新key> # 替换 API Key(新 key 从 exomind 管理端复制;缺省交互粘贴)
|
|
111
|
+
earshot logout # 清除本地凭据(保留服务器地址;exomind-cli/fireCut 共用该配置会一并失效)
|
|
112
|
+
earshot help [命令] # 查看帮助(如 earshot help listen)
|
|
113
|
+
```
|
|
114
|
+
|
|
107
115
|
没有 ExoMind 账号?投递可换任意 webhook(`--deliver webhook --webhook <url>`),或 `--deliver none` 只本地存档,两者都不需要登录。
|
|
108
116
|
|
|
109
117
|
`earshot` 与 `earshot-cli` 两个命令名等价可用;`earshot doctor` 可做完整环境自检。
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"""earshot:旁听剪藏 —— 听到的都能进知识库。
|
|
2
|
+
|
|
3
|
+
跨平台桌面端:系统声环回录制(macOS 需 BlackHole)→ 端侧 VAD+ASR 转写
|
|
4
|
+
→ 组装 Markdown → 投递(默认 exomind CLI,可插拔 webhook)。
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from importlib import metadata as _metadata
|
|
8
|
+
|
|
9
|
+
try:
|
|
10
|
+
# 单一版本源:pyproject.toml(已安装发行版的元数据);源码 checkout 未安装时回退。
|
|
11
|
+
# 旧写法在此手写 "0.1.1",与 pyproject 的 0.3.x 漂移两月无人察觉——UA 带版本
|
|
12
|
+
# 后漂移会直接暴露在服务端日志里,先根治版本源。
|
|
13
|
+
# 注意发行版名是 earshot-cli(import 名才是 earshot),两个键都试。
|
|
14
|
+
__version__ = _metadata.version("earshot-cli")
|
|
15
|
+
except _metadata.PackageNotFoundError: # pragma: no cover - 仅源码运行路径
|
|
16
|
+
__version__ = "0.0.dev0"
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def ua_headers(**extra: str) -> dict[str, str]:
|
|
20
|
+
"""自有服务(exomind)请求头:显式 UA(可观测性标配)+ 附加头(Authorization 等)。
|
|
21
|
+
|
|
22
|
+
全仓唯一注入点——deliver/auth/transcribe_cloud 旁路端点一律复用,不写两遍;
|
|
23
|
+
第三方 webhook(deliver_webhook)不经过这里(无自有服务端可分桶,不值得改)。
|
|
24
|
+
"""
|
|
25
|
+
return {"User-Agent": f"earshot/{__version__}", **extra}
|
|
@@ -17,6 +17,7 @@ from typing import Any
|
|
|
17
17
|
|
|
18
18
|
import httpx
|
|
19
19
|
|
|
20
|
+
from earshot import ua_headers
|
|
20
21
|
from earshot.config import EXOMIND_CONFIG_FILENAME
|
|
21
22
|
|
|
22
23
|
Poster = Callable[[str, dict[str, Any]], dict[str, Any]]
|
|
@@ -26,13 +27,13 @@ Sleeper = Callable[[float], None]
|
|
|
26
27
|
|
|
27
28
|
|
|
28
29
|
def _post_json(url: str, payload: dict[str, Any]) -> dict[str, Any]:
|
|
29
|
-
resp = httpx.post(url, json=payload, timeout=30.0)
|
|
30
|
+
resp = httpx.post(url, json=payload, headers=ua_headers(), timeout=30.0)
|
|
30
31
|
resp.raise_for_status()
|
|
31
32
|
return dict(resp.json())
|
|
32
33
|
|
|
33
34
|
|
|
34
35
|
def _get_status(url: str, headers: dict[str, str]) -> int:
|
|
35
|
-
return int(httpx.get(url, headers=headers, timeout=30.0).status_code)
|
|
36
|
+
return int(httpx.get(url, headers=ua_headers(**headers), timeout=30.0).status_code)
|
|
36
37
|
|
|
37
38
|
|
|
38
39
|
def _open_browser(url: str) -> bool:
|
|
@@ -147,3 +148,26 @@ def save_credentials(
|
|
|
147
148
|
path.write_text(json.dumps(data, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
|
148
149
|
path.chmod(0o600)
|
|
149
150
|
return path
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def clear_credentials(config_path: Path | None = None) -> Path | None:
|
|
154
|
+
"""清除配置里的 api_key(置空,保留 base_url 等其他字段)——登出只清本地。
|
|
155
|
+
|
|
156
|
+
Args:
|
|
157
|
+
config_path: 配置文件路径,测试注入;缺省 ``~/.exomind/config.json``。
|
|
158
|
+
|
|
159
|
+
Returns:
|
|
160
|
+
实际写入的配置文件路径;文件不存在或本就没有凭据时返回 None(无事可做)。
|
|
161
|
+
"""
|
|
162
|
+
path = config_path if config_path is not None else Path.home() / EXOMIND_CONFIG_FILENAME
|
|
163
|
+
try:
|
|
164
|
+
raw = json.loads(path.read_text(encoding="utf-8"))
|
|
165
|
+
except (OSError, ValueError):
|
|
166
|
+
return None
|
|
167
|
+
if not isinstance(raw, dict) or not raw.get("api_key"):
|
|
168
|
+
return None
|
|
169
|
+
data = {k: v for k, v in raw.items() if isinstance(v, str)}
|
|
170
|
+
data["api_key"] = ""
|
|
171
|
+
path.write_text(json.dumps(data, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
|
|
172
|
+
path.chmod(0o600)
|
|
173
|
+
return path
|
|
@@ -13,7 +13,7 @@ from pathlib import Path
|
|
|
13
13
|
import numpy as np
|
|
14
14
|
|
|
15
15
|
from earshot import __version__
|
|
16
|
-
from earshot.auth import device_login, save_credentials, verify_key
|
|
16
|
+
from earshot.auth import clear_credentials, device_login, save_credentials, verify_key
|
|
17
17
|
from earshot.capture import blackhole_missing_hint, list_capture_devices, pick_device, record_to_wav
|
|
18
18
|
from earshot.compose import NoteMeta, add_punctuation, derive_title, group_paragraphs, outbox_path, render_markdown
|
|
19
19
|
from earshot.config import DEFAULT_EXOMIND_BASE_URL, Settings, load_settings
|
|
@@ -84,6 +84,14 @@ def build_parser() -> argparse.ArgumentParser:
|
|
|
84
84
|
p_login.add_argument("--base-url", default=DEFAULT_EXOMIND_BASE_URL, help="exomind API 地址")
|
|
85
85
|
p_login.add_argument("--force", action="store_true", help="跳过「凭据仍有效」检查,无条件重新登录")
|
|
86
86
|
|
|
87
|
+
p_update = sub.add_parser("update", help="替换 API Key(新 key 从 exomind 管理端复制;获取凭据用 login)")
|
|
88
|
+
p_update.add_argument("--api-key", default=None, help="新 API Key(不填则交互粘贴)")
|
|
89
|
+
|
|
90
|
+
sub.add_parser("logout", help="清除本地凭据(保留服务器地址;exomind-cli/fireCut 共用该配置,会一并失效)")
|
|
91
|
+
|
|
92
|
+
p_help = sub.add_parser("help", help="显示帮助:earshot help [命令]")
|
|
93
|
+
p_help.add_argument("topic", nargs="?", default=None, help="要查看帮助的命令名")
|
|
94
|
+
|
|
87
95
|
sub.add_parser("devices", help="列出可用采集设备")
|
|
88
96
|
sub.add_parser("doctor", help="环境自检")
|
|
89
97
|
p_clean = sub.add_parser("clean", help="清理过期留档(outbox+tmp)")
|
|
@@ -553,6 +561,42 @@ def cmd_login(args: argparse.Namespace, settings: Settings) -> int:
|
|
|
553
561
|
return EXIT_OK
|
|
554
562
|
|
|
555
563
|
|
|
564
|
+
def cmd_update(args: argparse.Namespace, settings: Settings) -> int:
|
|
565
|
+
"""替换 API Key:已拿到新 key(exomind 管理端复制)时本地换上。
|
|
566
|
+
|
|
567
|
+
只管替换,不管新 key 怎么来——获取凭据走 login 的设备流。探活不通过不写入
|
|
568
|
+
(与 cmd_login 同款纪律,避免无效 key 覆盖现有有效配置)。
|
|
569
|
+
"""
|
|
570
|
+
key = args.api_key
|
|
571
|
+
if not key:
|
|
572
|
+
key = _prompt("新 API Key(回车取消)")
|
|
573
|
+
if not key:
|
|
574
|
+
print("未提供凭据,未改动。")
|
|
575
|
+
return EXIT_OK
|
|
576
|
+
base_url = settings.exomind_base_url or DEFAULT_EXOMIND_BASE_URL
|
|
577
|
+
if not verify_key(base_url, key):
|
|
578
|
+
print(f"✗ 凭据探活失败({base_url}),未写入;确认 key 有效后重试")
|
|
579
|
+
return EXIT_FAIL
|
|
580
|
+
path = save_credentials(base_url, key)
|
|
581
|
+
masked = f"{key[:8]}…{key[-4:]}" if len(key) > 12 else key
|
|
582
|
+
print(f"✓ 凭据已替换:{masked}")
|
|
583
|
+
print(f" 已写入 {path}")
|
|
584
|
+
return EXIT_OK
|
|
585
|
+
|
|
586
|
+
|
|
587
|
+
def cmd_logout(_args: argparse.Namespace, _settings: Settings) -> int:
|
|
588
|
+
"""清除本地凭据(保留 base_url);~/.exomind/config.json 三家共用,登出影响一并说清。"""
|
|
589
|
+
path = clear_credentials()
|
|
590
|
+
if path is None:
|
|
591
|
+
print("未登录(本地无凭据),无需登出。")
|
|
592
|
+
return EXIT_OK
|
|
593
|
+
print(f"✓ 已登出,本地凭据已清除({path})")
|
|
594
|
+
print(" 服务器地址保留;重新登录: earshot login")
|
|
595
|
+
print(" 注意: ~/.exomind/config.json 由 earshot/exomind-cli/fireCut 共享,它们的调用也会失效")
|
|
596
|
+
print(" 服务端吊销该 key 请到 exomind 管理端(account 页)操作")
|
|
597
|
+
return EXIT_OK
|
|
598
|
+
|
|
599
|
+
|
|
556
600
|
def main(argv: list[str] | None = None) -> int:
|
|
557
601
|
parser = build_parser()
|
|
558
602
|
args = parser.parse_args(argv)
|
|
@@ -563,11 +607,22 @@ def main(argv: list[str] | None = None) -> int:
|
|
|
563
607
|
"video": cmd_video,
|
|
564
608
|
"deliver": cmd_deliver,
|
|
565
609
|
"login": cmd_login,
|
|
610
|
+
"update": cmd_update,
|
|
611
|
+
"logout": cmd_logout,
|
|
566
612
|
"me": cmd_me,
|
|
567
613
|
"devices": cmd_devices,
|
|
568
614
|
"doctor": cmd_doctor,
|
|
569
615
|
"clean": cmd_clean,
|
|
570
616
|
}
|
|
617
|
+
if args.command == "help":
|
|
618
|
+
if args.topic:
|
|
619
|
+
# 复用 argparse 自己的 -h 渲染:earshot help listen ≡ earshot listen --help
|
|
620
|
+
try:
|
|
621
|
+
parser.parse_args([args.topic, "--help"])
|
|
622
|
+
except SystemExit as exc:
|
|
623
|
+
return int(exc.code or 0)
|
|
624
|
+
parser.print_help()
|
|
625
|
+
return EXIT_OK
|
|
571
626
|
if args.command is None:
|
|
572
627
|
if not _interactive():
|
|
573
628
|
parser.print_help()
|
|
@@ -12,6 +12,7 @@ from typing import Any
|
|
|
12
12
|
|
|
13
13
|
import httpx
|
|
14
14
|
|
|
15
|
+
from earshot import ua_headers
|
|
15
16
|
from earshot.config import Settings
|
|
16
17
|
|
|
17
18
|
EXOMIND_ORIGIN = "earshot"
|
|
@@ -178,7 +179,7 @@ def deliver_exomind_api(
|
|
|
178
179
|
post = poster or _post
|
|
179
180
|
get = getter or _get
|
|
180
181
|
base = settings.exomind_base_url
|
|
181
|
-
headers =
|
|
182
|
+
headers = ua_headers(Authorization=f"Bearer {settings.exomind_api_key}")
|
|
182
183
|
|
|
183
184
|
content = md_path.read_text(encoding="utf-8")
|
|
184
185
|
payload: dict[str, Any] = {"content": content, "origin": EXOMIND_ORIGIN, "client_key": client_key_for(content)}
|
|
@@ -7,6 +7,7 @@ from pathlib import Path
|
|
|
7
7
|
|
|
8
8
|
import httpx
|
|
9
9
|
|
|
10
|
+
from earshot import ua_headers
|
|
10
11
|
from earshot.config import Settings
|
|
11
12
|
|
|
12
13
|
POLL_INTERVAL_S = 3.0
|
|
@@ -36,7 +37,7 @@ def transcribe_cloud(wav: Path, settings: Settings, timeout_s: float = DEFAULT_T
|
|
|
36
37
|
msg = "未配置 exomind 凭据(先 `exomind login` 生成 ~/.exomind/config.json,或设 EXOMIND_API_KEY)"
|
|
37
38
|
raise CloudTranscribeError(msg)
|
|
38
39
|
base = settings.exomind_base_url
|
|
39
|
-
headers =
|
|
40
|
+
headers = ua_headers(Authorization=f"Bearer {settings.exomind_api_key}")
|
|
40
41
|
|
|
41
42
|
try:
|
|
42
43
|
with wav.open("rb") as f:
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
"""凭据生命周期测试:update(替换)/logout(清除)/help(子命令帮助)。
|
|
2
|
+
|
|
3
|
+
update/logout 与 emcli/wxpub 同款语义——update 只管替换(新 key 从管理端复制,
|
|
4
|
+
获取凭据走 login 设备流),logout 只清本地(保留 base_url,三家共用文件的影响要说清)。
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import argparse
|
|
10
|
+
import json
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
|
|
13
|
+
import pytest
|
|
14
|
+
|
|
15
|
+
from earshot.auth import clear_credentials
|
|
16
|
+
from earshot.cli import main
|
|
17
|
+
from earshot.config import Settings
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _settings() -> Settings:
|
|
21
|
+
return Settings(
|
|
22
|
+
home=Path("/tmp/earshot-test"),
|
|
23
|
+
model_root=Path("/tmp/earshot-test/models"),
|
|
24
|
+
exomind_base_url="https://exomind.example",
|
|
25
|
+
exomind_api_key="",
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def _write_config(path: Path, api_key: str = "sk_live_old_key") -> None:
|
|
30
|
+
path.write_text(json.dumps({"base_url": "https://exomind.example", "api_key": api_key}), encoding="utf-8")
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class TestClearCredentials:
|
|
34
|
+
def test_清除_api_key_保留_base_url(self, tmp_path: Path) -> None:
|
|
35
|
+
cfg = tmp_path / "config.json"
|
|
36
|
+
_write_config(cfg)
|
|
37
|
+
assert clear_credentials(cfg) == cfg
|
|
38
|
+
data = json.loads(cfg.read_text(encoding="utf-8"))
|
|
39
|
+
assert data["api_key"] == ""
|
|
40
|
+
assert data["base_url"] == "https://exomind.example"
|
|
41
|
+
|
|
42
|
+
def test_无配置文件或本就无凭据_返回_none(self, tmp_path: Path) -> None:
|
|
43
|
+
assert clear_credentials(tmp_path / "absent.json") is None
|
|
44
|
+
empty = tmp_path / "empty.json"
|
|
45
|
+
_write_config(empty, api_key="")
|
|
46
|
+
assert clear_credentials(empty) is None
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
class TestCmdUpdate:
|
|
50
|
+
def test_新_key_探活通过_替换落盘(
|
|
51
|
+
self,
|
|
52
|
+
monkeypatch: pytest.MonkeyPatch,
|
|
53
|
+
capsys: pytest.CaptureFixture[str],
|
|
54
|
+
tmp_path: Path,
|
|
55
|
+
) -> None:
|
|
56
|
+
monkeypatch.setattr("earshot.cli.verify_key", lambda base, key: True)
|
|
57
|
+
saved: dict[str, str] = {}
|
|
58
|
+
|
|
59
|
+
def fake_save(base_url: str, api_key: str) -> Path:
|
|
60
|
+
saved["base_url"], saved["key"] = base_url, api_key
|
|
61
|
+
return tmp_path / "config.json"
|
|
62
|
+
|
|
63
|
+
monkeypatch.setattr("earshot.cli.save_credentials", fake_save)
|
|
64
|
+
args = argparse.Namespace(api_key="sk_live_new_key")
|
|
65
|
+
from earshot.cli import cmd_update
|
|
66
|
+
|
|
67
|
+
code = cmd_update(args, _settings())
|
|
68
|
+
assert code == 0
|
|
69
|
+
assert saved == {"base_url": "https://exomind.example", "key": "sk_live_new_key"}
|
|
70
|
+
assert "凭据已替换" in capsys.readouterr().out
|
|
71
|
+
|
|
72
|
+
def test_探活失败_不写入(self, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]) -> None:
|
|
73
|
+
monkeypatch.setattr("earshot.cli.verify_key", lambda base, key: False)
|
|
74
|
+
monkeypatch.setattr("earshot.cli.save_credentials", lambda *a, **k: pytest.fail("探活失败不得落盘"))
|
|
75
|
+
args = argparse.Namespace(api_key="sk_live_bad")
|
|
76
|
+
from earshot.cli import cmd_update
|
|
77
|
+
|
|
78
|
+
code = cmd_update(args, _settings())
|
|
79
|
+
assert code == 1
|
|
80
|
+
assert "未写入" in capsys.readouterr().out
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
class TestCmdLogout:
|
|
84
|
+
def test_有凭据_清除并提示共用影响(
|
|
85
|
+
self, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str], tmp_path: Path
|
|
86
|
+
) -> None:
|
|
87
|
+
monkeypatch.setattr("earshot.cli.clear_credentials", lambda: tmp_path / "config.json")
|
|
88
|
+
from earshot.cli import cmd_logout
|
|
89
|
+
|
|
90
|
+
code = cmd_logout(argparse.Namespace(), _settings())
|
|
91
|
+
out = capsys.readouterr().out
|
|
92
|
+
assert code == 0
|
|
93
|
+
assert "已登出" in out
|
|
94
|
+
assert "fireCut" in out, "三家共用 config.json 的影响必须说清"
|
|
95
|
+
|
|
96
|
+
def test_无凭据_无事可做(self, monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]) -> None:
|
|
97
|
+
monkeypatch.setattr("earshot.cli.clear_credentials", lambda: None)
|
|
98
|
+
from earshot.cli import cmd_logout
|
|
99
|
+
|
|
100
|
+
code = cmd_logout(argparse.Namespace(), _settings())
|
|
101
|
+
assert code == 0
|
|
102
|
+
assert "无需登出" in capsys.readouterr().out
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
class TestHelpCommand:
|
|
106
|
+
def test_help_无参数_打印总览(self, capsys: pytest.CaptureFixture[str]) -> None:
|
|
107
|
+
assert main(["help"]) == 0
|
|
108
|
+
out = capsys.readouterr().out
|
|
109
|
+
assert "usage: earshot" in out
|
|
110
|
+
assert "update" in out and "logout" in out, "新命令要在帮助里可见"
|
|
111
|
+
|
|
112
|
+
def test_help_带命令名_打印该命令帮助(self, capsys: pytest.CaptureFixture[str]) -> None:
|
|
113
|
+
assert main(["help", "login"]) == 0
|
|
114
|
+
assert "--force" in capsys.readouterr().out
|
|
115
|
+
|
|
116
|
+
def test_help_未知命令_报错不崩(self, capsys: pytest.CaptureFixture[str]) -> None:
|
|
117
|
+
# argparse 对无效 choice 打错误 + 退出码 2,main 收敛为返回值
|
|
118
|
+
assert main(["help", "no-such-cmd"]) == 2
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"""显式 UA 护栏(可观测性标配,2026-11-09):自有服务(exomind)流量必须带
|
|
2
|
+
earshot/<version>,默认 python-httpx/x 即未收编。
|
|
3
|
+
|
|
4
|
+
质量点:①ua_headers 单一注入点格式与合并;②deliver_exomind_api 的 wire 头
|
|
5
|
+
真实携带(经注入 poster 捕获);③webhook(第三方)不经 ua_headers——无自有
|
|
6
|
+
服务端可分桶,不乱标。
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from pathlib import Path
|
|
10
|
+
from typing import Any
|
|
11
|
+
|
|
12
|
+
import httpx
|
|
13
|
+
import pytest
|
|
14
|
+
|
|
15
|
+
from earshot import __version__, ua_headers
|
|
16
|
+
from earshot.config import Settings
|
|
17
|
+
from earshot.deliver import deliver_exomind_api, deliver_webhook
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def _settings() -> Settings:
|
|
21
|
+
return Settings(
|
|
22
|
+
home=Path("/tmp/earshot-test"),
|
|
23
|
+
model_root=Path("/tmp/earshot-test/models"),
|
|
24
|
+
exomind_base_url="https://api.example.com",
|
|
25
|
+
exomind_api_key="key-1",
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def test_ua_headers_format_and_merge() -> None:
|
|
30
|
+
h = ua_headers(Authorization="Bearer k")
|
|
31
|
+
assert h["User-Agent"] == f"earshot/{__version__}"
|
|
32
|
+
assert h["Authorization"] == "Bearer k"
|
|
33
|
+
assert __version__ not in ("", "0.0.dev0"), "安装态下版本必须来自包元数据,不再是漂移的手写常量"
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
def test_deliver_api_carries_ua_on_wire(tmp_path: Path) -> None:
|
|
37
|
+
md = tmp_path / "n.md"
|
|
38
|
+
md.write_text("内容", encoding="utf-8")
|
|
39
|
+
seen: dict[str, str] = {}
|
|
40
|
+
|
|
41
|
+
def poster(_u: str, _p: dict[str, Any], h: dict[str, str]) -> dict[str, Any]:
|
|
42
|
+
seen.update(h)
|
|
43
|
+
return {"job_id": "j"}
|
|
44
|
+
|
|
45
|
+
def getter(_u: str, _p: dict[str, str], _h: dict[str, str]) -> dict[str, Any]:
|
|
46
|
+
return {"status": "done", "entities": 1}
|
|
47
|
+
|
|
48
|
+
deliver_exomind_api(md, "t", [], _settings(), poster=poster, getter=getter, poll_interval_s=0)
|
|
49
|
+
assert seen["User-Agent"].startswith("earshot/")
|
|
50
|
+
assert seen["Authorization"].startswith("Bearer ")
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def test_webhook_does_not_claim_earshot_ua(monkeypatch: pytest.MonkeyPatch) -> None:
|
|
54
|
+
"""第三方 webhook 不打 earshot UA——收编范围=自有服务,乱标反而污染对方统计。"""
|
|
55
|
+
sent: list[Any] = []
|
|
56
|
+
|
|
57
|
+
def fake_post(url: str, **kw: Any) -> httpx.Response:
|
|
58
|
+
sent.append(kw.get("headers")) # None = 未传头
|
|
59
|
+
return httpx.Response(200)
|
|
60
|
+
|
|
61
|
+
monkeypatch.setattr(httpx, "post", fake_post)
|
|
62
|
+
deliver_webhook("https://example.org/hook", "t", "c", ["a"])
|
|
63
|
+
assert sent == [None]
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|