test_rag_chat_static.py 1.41 KB
from pathlib import Path
import unittest


RAG_PAGE = Path("frontend/src/app/rag/page.tsx")
RAG_API = Path("frontend/src/lib/rag-api.ts")


class RagChatStaticTest(unittest.TestCase):
    def test_clear_history_does_not_enter_chat_loading_state(self):
        source = RAG_PAGE.read_text(encoding="utf-8")
        clear_start = source.index("const clearHistoryV2 = async () => {")
        clear_end = source.index("const openSourcePreview", clear_start)
        clear_block = source[clear_start:clear_end]

        self.assertNotIn("runAction", clear_block)
        self.assertIn("setLoading(false)", clear_block)
        self.assertIn("setMessages([])", clear_block)
        self.assertIn("const sessionToClear = sessionId", clear_block)
        self.assertIn("clearRagHistory(sessionToClear)", clear_block)

    def test_aborted_rag_stream_is_not_rendered_as_an_error_message(self):
        source = RAG_API.read_text(encoding="utf-8")

        self.assertIn('error.name === "AbortError"', source)
        self.assertIn("return;", source[source.index('error.name === "AbortError"'):])

    def test_rag_reasoning_steps_render_elapsed_time_per_step(self):
        source = RAG_PAGE.read_text(encoding="utf-8")

        self.assertIn("function formatElapsedSeconds", source)
        self.assertIn("time: formatElapsedSeconds(step.elapsed_ms)", source)
        self.assertIn("`耗时 ${", source)


if __name__ == "__main__":
    unittest.main()