Skip to content

添加了对FastAPI的测试;修改WebSocket的控制类#17

Merged
qwasd7680 merged 6 commits intomainfrom
dev
Oct 30, 2025
Merged

添加了对FastAPI的测试;修改WebSocket的控制类#17
qwasd7680 merged 6 commits intomainfrom
dev

Conversation

@qwasd7680
Copy link
Copy Markdown
Owner

No description provided.

self.disconnect(client_id)
await websocket.close()
except Exception:
pass
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exception handling in the _send_and_close method is too broad, catching all exceptions without any specific action or logging. This could obscure the root cause of errors during WebSocket closure, making debugging difficult.

Recommendation:
Refine the exception handling to catch more specific exceptions related to WebSocket operations. Additionally, log the exception details to aid in troubleshooting.

Comment on lines 161 to +166
else:
# 任务失败,发送 WebSocket 错误通知
asyncio.run(manager.send_personal_message(client_id, {
message = {
"status": "error",
"file_name": file_title,
"message": f"文件 '{file_title}' 未找到或处理失败。"
}))

}
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error handling logic in the sync_download_and_zip_task function assumes that if the zip file does not exist, it is due to a processing failure. This might not always be the case as there could be other reasons for the absence of the file, such as issues in the file creation process or file system errors.

Recommendation:
Improve the robustness of the error handling by checking specific conditions that could lead to the absence of the file and logging these conditions. This would provide clearer diagnostics and allow for more targeted troubleshooting.

Comment on lines +15 to +22
response = client.get("/v1/{0}".format(nowtimestamp))
timedelta = int(time.time() * 1000) - nowtimestamp
ms = int(timedelta)
assert response.status_code == 200
assert response.json().get("status") == "ok"
assert response.json().get("app") == "jmcomic_server_api"
assert int(response.json().get("latency")) <= ms
assert int(response.json().get("latency")) > 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The repeated calls to response.json() within the same test function can be optimized. Each call to response.json() parses the JSON response anew, which is inefficient if the response does not change. To improve performance, parse the response once and reuse the result:

json_response = response.json()
assert json_response.get("status") == "ok"
assert json_response.get("app") == "jmcomic_server_api"
assert int(json_response.get("latency")) <= ms
assert int(json_response.get("latency")) > 0

Comment on lines +53 to +55
assert file_path.exists()
file_path.unlink()
assert file_path.exists() == False
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file deletion check assert file_path.exists() == False immediately after file_path.unlink() is redundant and could be removed to clean up the code. The unlink() method will remove the file if it exists, and if it fails to do so, it will raise an exception. Therefore, the subsequent existence check is unnecessary and does not contribute to the test's effectiveness.

Comment on lines 6 to 7
FILE_PATH = Path(f"{current_dir}/temp")
os.makedirs(FILE_PATH, exist_ok=True)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potential Security Risk with Directory Creation

The code uses os.getcwd() combined with a relative path to create a directory for temporary files. This approach can pose a security risk, especially if the script is run in an environment where the current directory can be influenced by an external user or if it's a shared environment.

Recommendation:

  • Consider using a dedicated temporary directory that is not directly tied to the current working directory, such as tempfile.gettempdir() in Python, which provides a more secure and predictable temporary directory.

Comment on lines 26 to 27
assert page2.page_size > 0
assert page3.page_size > 0
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Limited Assertion Scope in Ranking Tests

The assertions in the test_rank_comic function only check if the page_size is greater than zero. This is a very basic check and might not be sufficient to catch all potential issues with the ranking functionality.

Recommendation:

  • Expand the assertions to include checks for the correctness of the content of the rankings, such as verifying specific expected results or properties of the results. This would help ensure the functionality not only returns results but returns correct and expected results.

@qwasd7680 qwasd7680 merged commit 9e56b9e into main Oct 30, 2025
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant