mirror of
https://gitee.com/wanwujie/deer-flow
synced 2026-04-03 14:22:13 +08:00
fix: convert crawl_tool dict return to JSON string for type consistency (#636)
Keep fixing #631 This pull request updates the crawl_tool function to return its results as a JSON string instead of a dictionary, and adjusts the unit tests accordingly to handle the new return type. The changes ensure consistent serialization of output and proper validation in tests.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
# Copyright (c) 2025 Bytedance Ltd. and/or its affiliates
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
import json
|
||||
import logging
|
||||
from typing import Annotated
|
||||
|
||||
@@ -22,7 +23,7 @@ def crawl_tool(
|
||||
try:
|
||||
crawler = Crawler()
|
||||
article = crawler.crawl(url)
|
||||
return {"url": url, "crawled_content": article.to_markdown()[:1000]}
|
||||
return json.dumps({"url": url, "crawled_content": article.to_markdown()[:1000]})
|
||||
except BaseException as e:
|
||||
error_msg = f"Failed to crawl. Error: {repr(e)}"
|
||||
logger.error(error_msg)
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import json
|
||||
from unittest.mock import Mock, patch
|
||||
|
||||
from src.tools.crawl import crawl_tool
|
||||
@@ -21,10 +22,11 @@ class TestCrawlTool:
|
||||
result = crawl_tool(url)
|
||||
|
||||
# Assert
|
||||
assert isinstance(result, dict)
|
||||
assert result["url"] == url
|
||||
assert "crawled_content" in result
|
||||
assert len(result["crawled_content"]) <= 1000
|
||||
assert isinstance(result, str)
|
||||
result_dict = json.loads(result)
|
||||
assert result_dict["url"] == url
|
||||
assert "crawled_content" in result_dict
|
||||
assert len(result_dict["crawled_content"]) <= 1000
|
||||
mock_crawler_class.assert_called_once()
|
||||
mock_crawler.crawl.assert_called_once_with(url)
|
||||
mock_article.to_markdown.assert_called_once()
|
||||
@@ -45,7 +47,8 @@ class TestCrawlTool:
|
||||
result = crawl_tool(url)
|
||||
|
||||
# Assert
|
||||
assert result["crawled_content"] == short_content
|
||||
result_dict = json.loads(result)
|
||||
assert result_dict["crawled_content"] == short_content
|
||||
|
||||
@patch("src.tools.crawl.Crawler")
|
||||
@patch("src.tools.crawl.logger")
|
||||
|
||||
Reference in New Issue
Block a user