-
Notifications
You must be signed in to change notification settings - Fork 0
Add tests for logging utilities #377
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zhongkaifu
wants to merge
1
commit into
main
Choose a base branch
from
codex/add-tests-for-logging-utility-functions
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import json | ||
|
|
||
| from velvetflow import logging_utils | ||
|
|
||
|
|
||
| def test_configure_run_logging_sets_and_resets_run_id(): | ||
| assert logging_utils.current_run_id() is None | ||
|
|
||
| token = logging_utils.configure_run_logging(run_id="run-123") | ||
| assert token is not None | ||
| assert logging_utils.current_run_id() == "run-123" | ||
|
|
||
| logging_utils._RUN_ID.reset(token) | ||
| assert logging_utils.current_run_id() is None | ||
|
|
||
|
|
||
| def test_use_trace_context_sets_current_and_resets(): | ||
| context = logging_utils.TraceContext("trace-1", "span-1", "span-name") | ||
|
|
||
| with logging_utils.use_trace_context(context): | ||
| current = logging_utils.current_trace_context() | ||
| assert current is not None | ||
| assert current.trace_id == "trace-1" | ||
| assert current.span_id == "span-1" | ||
| assert current.span_name == "span-name" | ||
|
|
||
| assert logging_utils.current_trace_context() is None | ||
|
|
||
|
|
||
| def test_log_event_includes_context_and_payload_override(tmp_path, capsys): | ||
| log_path = tmp_path / "events.jsonl" | ||
| token = logging_utils.configure_run_logging(run_id="run-123", log_file=log_path) | ||
| context = logging_utils.TraceContext("trace-1", "span-1", "span-name") | ||
|
|
||
| with logging_utils.use_trace_context(context): | ||
| logging_utils.log_event( | ||
| "hello", | ||
| payload={"node_id": "node-1", "action_id": "action-1", "extra": "data"}, | ||
| ) | ||
|
|
||
| output = capsys.readouterr().out.strip().splitlines()[-1] | ||
| record = json.loads(output) | ||
| assert record["workflow_run_id"] == "run-123" | ||
| assert record["trace_id"] == "trace-1" | ||
| assert record["span_id"] == "span-1" | ||
| assert record["node_id"] == "node-1" | ||
| assert record["action_id"] == "action-1" | ||
| assert record["payload"]["extra"] == "data" | ||
|
|
||
| logging_utils._RUN_ID.reset(token) | ||
|
|
||
|
|
||
| def test_log_llm_message_serializes_tool_calls(tmp_path, capsys): | ||
| class DummyFunction: | ||
| def __init__(self, name, arguments): | ||
| self.name = name | ||
| self.arguments = arguments | ||
|
|
||
| class DummyToolCall: | ||
| def __init__(self, tool_id, tool_type, function): | ||
| self.id = tool_id | ||
| self.type = tool_type | ||
| self.function = function | ||
|
|
||
| class DummyMessage: | ||
| def __init__(self, role, content, tool_calls): | ||
| self.role = role | ||
| self.content = content | ||
| self.tool_calls = tool_calls | ||
|
|
||
| logging_utils.configure_run_logging(log_file=tmp_path / "llm.jsonl") | ||
|
|
||
| function = DummyFunction("search", {"query": "hello"}) | ||
| tool_call = DummyToolCall("tool-1", "function", function) | ||
| message = DummyMessage("assistant", "hi", [tool_call]) | ||
| logging_utils.log_llm_message("gpt-test", message, operation="demo") | ||
|
|
||
| output = capsys.readouterr().out.strip().splitlines()[-1] | ||
| record = json.loads(output) | ||
| tool_calls = record["payload"]["message"]["tool_calls"] | ||
| assert tool_calls[0]["function"]["name"] == "search" | ||
| assert tool_calls[0]["function"]["arguments"] == {"query": "hello"} | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These assertions assume
log_eventwill copynode_id/action_idfrom the payload when the explicit args are omitted, butlog_eventcurrently initializes those keys toNoneand then usesrecord.setdefault(...), which does not overwrite existing keys. As a result, the emitted JSON line will keepnode_id/action_idasnulland this test will fail unless the production code is updated to override or the test passesnode_id/action_idexplicitly. Consider adjusting the test expectations or the logging logic to match the intended behavior.Useful? React with 👍 / 👎.