"""core/shortcuts.py:解析 + 入口层展开(纯函数 + 文件读)。""" from __future__ import annotations from uuid import uuid4 from core import shortcuts SAMPLE = """\ # 我的快捷指令 | 触发词 | 指令 | |---|---| | 简报 | 给我输出一份昨日的 AI 新闻简报 | | Standup | Summarize yesterday's commits | | 简报 | 这条重复应被首行覆盖 | """ def test_parse_skips_header_and_separator(): m = shortcuts.parse_shortcuts(SAMPLE) # 表头「触发词」、分隔行 |---| 都不进表 assert "触发词" not in m assert "---" not in m assert "简报" in m assert m["简报"] == "给我输出一份昨日的 AI 新闻简报" # 首行赢 def test_parse_case_insensitive_key(): m = shortcuts.parse_shortcuts(SAMPLE) # 触发词归一化用 casefold,英文键存成小写 assert "standup" in m assert m["standup"] == "Summarize yesterday's commits" def test_parse_empty_and_garbage(): assert shortcuts.parse_shortcuts("") == {} assert shortcuts.parse_shortcuts("没有表格\n只是普通文本") == {} # 单元格缺失 / 只有一列 → 跳过 assert shortcuts.parse_shortcuts("| 只有一列 |") == {} def _write(tmp_path, user_id, body): d = tmp_path / "users" / str(user_id) / ".memory" d.mkdir(parents=True, exist_ok=True) (d / "shortcuts.md").write_text(body, encoding="utf-8") def test_expand_exact_match(tmp_path): uid = uuid4() _write(tmp_path, uid, SAMPLE) out, hit = shortcuts.expand(tmp_path, uid, "简报") assert out == "给我输出一份昨日的 AI 新闻简报" assert hit == "简报" # 首尾空格 / 大小写不影响命中 out2, hit2 = shortcuts.expand(tmp_path, uid, " Standup ") assert out2 == "Summarize yesterday's commits" assert hit2 == "Standup" def test_expand_no_partial_match(tmp_path): uid = uuid4() _write(tmp_path, uid, SAMPLE) # 整条不等于触发词 → 原样返回,不展开 out, hit = shortcuts.expand(tmp_path, uid, "帮我出个简报") assert out == "帮我出个简报" assert hit is None def test_expand_missing_file(tmp_path): uid = uuid4() # 没写文件 out, hit = shortcuts.expand(tmp_path, uid, "简报") assert out == "简报" assert hit is None def test_expand_empty_text(tmp_path): uid = uuid4() _write(tmp_path, uid, SAMPLE) out, hit = shortcuts.expand(tmp_path, uid, " ") assert out == " " assert hit is None