"""P2:一份起点/番茄导出 → 一份归因报告。""" from __future__ import annotations import pathlib import sys import tempfile import unittest ROOT = next( parent for parent in (pathlib.Path(__file__).resolve().parent, *pathlib.Path(__file__).resolve().parents) if (parent / "AGENTS.md").is_file() and (parent / ".git").exists() ) if str(ROOT) not in sys.path: sys.path.insert(0, str(ROOT)) from muse.feedback import import_ranking, write_attribution_report # noqa: E402 from muse.store import connect # noqa: E402 class RankingAttributionTest(unittest.TestCase): def test_import_produces_non_empty_report(self) -> None: tmp = pathlib.Path(tempfile.mkdtemp()) db = tmp / "muse.db" with connect(db) as conn: conn.execute( """INSERT INTO runs(id, created_at, kind, input_json, output_text, meta_json, skill_set_hash) VALUES ('run-rank', datetime('now'), 'agent.writer', '{}', '稿', '{}', 'skills-abc')""" ) conn.commit() csv_path = tmp / "qidian.csv" csv_path.write_text( "platform,title,rank,votes,date\n起点,深空之影,8,12000,2026-08-01\n番茄,深空之影,3,88000,2026-08-01\n", encoding="utf-8", ) report = import_ranking(csv_path, sqlite_path=db) self.assertEqual(report["imported"], 2) dest = write_attribution_report(report, tmp / "attribution.md") text = dest.read_text(encoding="utf-8") self.assertIn("深空之影", text) self.assertIn("run-rank", text) self.assertIn("skills-abc", text) self.assertGreater(len(text.strip()), 20) if __name__ == "__main__": unittest.main()