"""ja_polish.py の照らし合わせと返事の取り出しのテスト (依頼320・321)。API は呼ばない。 python3 ja_polish/test_ja_polish.py """ import sys import unittest from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) import ja_polish as jp # noqa: E402 BODY = '{"unclear": [{"sentence": "a", "reason": "b"}], "rewritten": "書き直し\\n2行目"}' class TestExtractJson(unittest.TestCase): def test_plain(self): self.assertEqual(jp.extract_json(BODY)["rewritten"], "書き直し\n2行目") def test_fenced_with_text(self): raw = "はい、書き直しました。\n\n```json\n" + BODY + "\n```\n\nほかに直す所があれば言ってください。" self.assertEqual(jp.extract_json(raw)["unclear"][0]["sentence"], "a") def test_text_around_no_fence(self): raw = "次のとおりです。" + BODY + " 以上です。" self.assertIsNotNone(jp.extract_json(raw)) def test_raw_newline_in_string(self): raw = '```json\n{"unclear": [], "rewritten": "1行目\n2行目"}\n```' self.assertEqual(jp.extract_json(raw)["rewritten"], "1行目\n2行目") def test_none(self): self.assertIsNone(jp.extract_json("JSON はありません {壊れた")) class TestCompare(unittest.TestCase): def test_same(self): c = jp.compare("依頼308で10コマ混ぜた。", "依頼308で、10コマかけて混ぜた。", {}) self.assertEqual(c["result"], "合格") def test_irai_list_rewording(self): c = jp.compare("仕組みを作った(依頼314・315)", "仕組みを作った(依頼314と依頼315)", {}) self.assertEqual(c["result"], "合格") self.assertEqual(c["same_by_rewording"]["元"], ["依頼314・315 → 依頼314・依頼315"]) def test_log_list_rewording(self): c = jp.compare("LOG580・581 を見る", "LOG580 と LOG581 を見る", {}) self.assertEqual(c["result"], "合格") def test_comma_and_fullwidth(self): c = jp.compare("3,000ステップ・10°", "3000ステップと10°", {}) self.assertEqual(c["result"], "合格") def test_not_list(self): # 「依頼313と5°」は並びではない。5° はそのまま数字として数える c = jp.compare("依頼313と5°で動かした", "依頼313で5°にして動かした", {}) self.assertEqual(c["result"], "合格") def test_added_only(self): c = jp.compare("お手本で学んだ", "お手本50本で学んだ", {}) self.assertEqual(c["result"], "合格 (足した所あり)") self.assertEqual(c["added"], {"数字": ["50本"]}) self.assertTrue(c["pass"]) def test_lost(self): c = jp.compare("4本で成功2本", "4本で成功した", {}) self.assertEqual(c["result"], "不合格") self.assertEqual(c["lost"], {"数字": ["2本"]}) def test_changed(self): c = jp.compare("跳び4.57°", "跳び4.6°", {}) self.assertEqual(c["result"], "不合格") def test_lost_irai_from_list(self): c = jp.compare("(依頼314・315)", "(依頼314)", {}) self.assertEqual(c["lost"], {"依頼番号": ["依頼315"]}) def test_tick_and_url(self): c = jp.compare("`--blend` と https://a.example/x", "`--blend2` と https://a.example/x", {}) self.assertEqual(c["result"], "不合格") self.assertIn("バッククォートの中身", c["lost"]) def test_mark(self): keep = {"⟦KEEP_A⟧": "```\ncode 123\n```"} self.assertEqual(jp.compare("前\n⟦KEEP_A⟧\n後", "前\n⟦KEEP_A⟧\n後", keep)["result"], "合格") self.assertEqual(jp.compare("前\n⟦KEEP_A⟧\n後", "前\n後", keep)["result"], "不合格") # 依頼321 def test_path_not_log(self): c = jp.compare("記録は KIOXIA lab/so101_Log12/307/REPORT_307.md にある", "KIOXIA lab/so101_Log12/307/REPORT_307.md に記録がある", {}) self.assertEqual(c["result"], "合格") self.assertEqual(c["same_by_rewording"]["元"], []) f = jp.features("lab/so101_Log12/307/ と `lab/so101_Log12/308/` と --log so101_Log12") self.assertEqual(f["LOG番号"], jp.Counter()) self.assertEqual(f["数字"], jp.Counter()) def test_path_lost(self): c = jp.compare("置き場所 lab/so101_Log12/307/ を見る", "置き場所を見る", {}) self.assertEqual(c["lost"], {"ファイルの場所": ["lab/so101_Log12/307/"]}) def test_log_list_still_opened(self): c = jp.compare("LOG580/581 を見る", "LOG580 と LOG581 を見る", {}) self.assertEqual(c["result"], "合格") self.assertEqual(jp.features("LOG580/581")["ファイルの場所"], jp.Counter()) def test_fraction_is_number(self): self.assertEqual(jp.compare("1/30 秒", "1/3 秒", {})["result"], "不合格") def test_tick_log_not_counted(self): f = jp.features("`Log #12` と `依頼314` は数えない。LOG583 は数える") self.assertEqual(f["LOG番号"], jp.Counter({"LOG583": 1})) self.assertEqual(f["依頼番号"], jp.Counter()) def test_pi_same_count(self): c = jp.compare("π0.5 は約0.5秒ごとに出す。π0.5 の答え", "π0.5 は、約0.5秒ごとに出す。π0.5 の答え", {}) self.assertEqual(c["result"], "合格") self.assertEqual(jp.features("π0.5 と π0.5")["数字"], jp.Counter()) def test_pi_fewer_but_present(self): c = jp.compare("π0.5 は速い。π0.5 の答え", "π0.5 は速い。このAIの答え", {}) self.assertEqual(c["result"], "合格") self.assertEqual(c["name_counts"], {"元": {"π0.5": 2}, "書き直し": {"π0.5": 1}}) def test_pi_gone(self): c = jp.compare("π0.5 は約0.5秒", "このAIは約0.5秒", {}) self.assertEqual(c["lost"], {"名前": ["π0.5"]}) def test_pi_does_not_hide_number_loss(self): c = jp.compare("π0.5 は約0.5秒ごと", "π0.5 はすぐ", {}) self.assertEqual(c["lost"], {"数字": ["0.5秒"]}) class TestPrompt(unittest.TestCase): def test_fence_rule(self): out = jp.PROMPT_PATH.read_text(encoding="utf-8").split("## 出力")[1] self.assertTrue(out.rstrip("\n").endswith("JSON は、```json で始まるコードブロック1つの中に入れて返してください。")) class TestRange(unittest.TestCase): def test_lead_body(self): import tempfile with tempfile.TemporaryDirectory() as td: f = Path(td) / "LOG1_article.md" f.write_text("---\ntitle: x\n---\n冒頭\n```\n## 作業ログ\n```\n## 作業ログ\n本文\n", encoding="utf-8") lead, kind, _ = jp.extract_range(f, "lead") body, _, _ = jp.extract_range(f, "body") self.assertEqual(kind, "log") self.assertTrue(lead.endswith("```")) self.assertNotIn("本文", lead) self.assertTrue(body.startswith("冒頭") and body.endswith("本文")) masked, keep = jp.mask(body) self.assertEqual(len(keep), 1) self.assertEqual(jp.unmask(masked, keep), body) if __name__ == "__main__": unittest.main()