Skip to content

Conversation

@tisnik
Copy link
Contributor

@tisnik tisnik commented Nov 9, 2025

Description

LCORE-740: minor fixes in endpoint unit tests

Type of change

  • Refactor
  • New feature
  • Bug fix
  • CVE fix
  • Optimization
  • Documentation Update
  • Configuration Update
  • Bump-up service version
  • Bump-up dependent library
  • Bump-up library or tool used for development (does not change the final image)
  • CI configuration change
  • Konflux configuration change
  • Unit tests improvement
  • Integration tests improvement
  • End to end tests improvement

Related Tickets & Documents

  • Related Issue #LCORE-740

Summary by CodeRabbit

  • Tests
    • Updated test mocks to align with asynchronous operation patterns
    • Extended authorization validation across multiple endpoint test suites
    • Enhanced streaming payload test coverage with additional fields and nested structures
    • Added type annotations to test functions for improved code quality

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Nov 9, 2025

Walkthrough

This PR updates unit test files to align with authentication tuple shape changes (from 3-tuple to 4-tuple), adds async mocking for the metrics endpoint, refines type annotations in test utilities, and expands streaming payload structures with richer nested fields in the streaming query tests.

Changes

Cohort / File(s) Summary
Authentication tuple expansion
tests/unit/app/endpoints/test_metrics.py, tests/unit/app/endpoints/test_models.py
Updated test fixtures to use expanded 4-element AuthTuple (adding user_id and token/boolean flag) instead of 3-tuple; AsyncMock applied to setup_model_metrics; updated mock import path for AsyncLlamaStackClientHolder in models tests.
Type annotation enhancements
tests/unit/app/endpoints/test_query_v2.py
Added explicit typing annotations (MockerFixture, Any, None return types); enhanced error-raising helper with type hints and Request object construction before raising APIConnectionError.
Streaming payload restructuring
tests/unit/app/endpoints/test_streaming_query.py
Converted static list-based async iterator to iter([...]) pattern; expanded mock payloads to include step_details (turn_id, step_id, step_type, tool_responses), tool_calls, and enriched turn_complete fields (session_id, started_at, completed_at, output_attachments); updated assertions to validate new fields; introduced list[str] typing for streaming_content.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

  • AuthTuple public API change: Review the 4-element tuple expansion across test_metrics.py and test_models.py to ensure consistent adoption and verify backward compatibility implications.
  • Streaming payload complexity: test_streaming_query.py introduces substantial mock payload restructuring with nested fields; carefully verify that the expanded payloads align with actual endpoint streaming behavior.
  • Mock import path shift: Verify that the updated AsyncLlamaStackClientHolder import path in test_models.py correctly reflects the production code structure.

Possibly related PRs

Suggested reviewers

  • manstis
  • umago

Poem

🐰 Four tuples where three once stood,
Async mocks and types—all good!
Streaming payloads rich and nested,
Test scaffolds carefully tested.
burrows deeper into review 🏐

Pre-merge checks and finishing touches

✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the changeset scope—minor fixes in endpoint unit tests across four test files. It is concise, specific enough to convey the primary intent, and directly related to the actual changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing touches
  • 📝 Generate docstrings
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 4f39c33 and 1352820.

📒 Files selected for processing (4)
  • tests/unit/app/endpoints/test_metrics.py (1 hunks)
  • tests/unit/app/endpoints/test_models.py (6 hunks)
  • tests/unit/app/endpoints/test_query_v2.py (12 hunks)
  • tests/unit/app/endpoints/test_streaming_query.py (2 hunks)
🧰 Additional context used
📓 Path-based instructions (3)
**/*.py

📄 CodeRabbit inference engine (CLAUDE.md)

**/*.py: All modules start with descriptive module-level docstrings explaining purpose
Use logger = logging.getLogger(name) for module logging after import logging
Define type aliases at module level for clarity
All functions require docstrings with brief descriptions
Provide complete type annotations for all function parameters and return types
Use typing_extensions.Self in model validators where appropriate
Use modern union syntax (str | int) and Optional[T] or T | None consistently
Function names use snake_case with descriptive, action-oriented prefixes (get_, validate_, check_)
Avoid in-place parameter modification; return new data structures instead of mutating arguments
Use appropriate logging levels: debug, info, warning, error with clear messages
All classes require descriptive docstrings explaining purpose
Class names use PascalCase with conventional suffixes (Configuration, Error/Exception, Resolver, Interface)
Abstract base classes should use abc.ABC and @AbstractMethod for interfaces
Provide complete type annotations for all class attributes
Follow Google Python docstring style for modules, classes, and functions, including Args, Returns, Raises, Attributes sections as needed

Files:

  • tests/unit/app/endpoints/test_streaming_query.py
  • tests/unit/app/endpoints/test_query_v2.py
  • tests/unit/app/endpoints/test_models.py
  • tests/unit/app/endpoints/test_metrics.py
tests/{unit,integration}/**/*.py

📄 CodeRabbit inference engine (CLAUDE.md)

tests/{unit,integration}/**/*.py: Use pytest for all unit and integration tests
Do not use unittest in tests; pytest is the standard

Files:

  • tests/unit/app/endpoints/test_streaming_query.py
  • tests/unit/app/endpoints/test_query_v2.py
  • tests/unit/app/endpoints/test_models.py
  • tests/unit/app/endpoints/test_metrics.py
tests/**/*.py

📄 CodeRabbit inference engine (CLAUDE.md)

tests/**/*.py: Use pytest-mock to create AsyncMock objects for async interactions in tests
Use the shared auth mock constant: MOCK_AUTH = ("mock_user_id", "mock_username", False, "mock_token") in tests

Files:

  • tests/unit/app/endpoints/test_streaming_query.py
  • tests/unit/app/endpoints/test_query_v2.py
  • tests/unit/app/endpoints/test_models.py
  • tests/unit/app/endpoints/test_metrics.py
🧠 Learnings (7)
📓 Common learnings
Learnt from: CR
Repo: lightspeed-core/lightspeed-stack PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-09-18T16:46:33.353Z
Learning: Applies to tests/**/*.py : Use the shared auth mock constant: MOCK_AUTH = ("mock_user_id", "mock_username", False, "mock_token") in tests
📚 Learning: 2025-09-18T16:46:33.353Z
Learnt from: CR
Repo: lightspeed-core/lightspeed-stack PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-09-18T16:46:33.353Z
Learning: Applies to **/*.py : Provide complete type annotations for all function parameters and return types

Applied to files:

  • tests/unit/app/endpoints/test_query_v2.py
📚 Learning: 2025-09-18T16:46:33.353Z
Learnt from: CR
Repo: lightspeed-core/lightspeed-stack PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-09-18T16:46:33.353Z
Learning: Applies to src/{models/**/*.py,configuration.py} : Use precise type hints in configuration (e.g., Optional[FilePath], PositiveInt, SecretStr)

Applied to files:

  • tests/unit/app/endpoints/test_query_v2.py
📚 Learning: 2025-09-18T16:46:33.353Z
Learnt from: CR
Repo: lightspeed-core/lightspeed-stack PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-09-18T16:46:33.353Z
Learning: Applies to tests/**/*.py : Use pytest-mock to create AsyncMock objects for async interactions in tests

Applied to files:

  • tests/unit/app/endpoints/test_query_v2.py
  • tests/unit/app/endpoints/test_models.py
  • tests/unit/app/endpoints/test_metrics.py
📚 Learning: 2025-09-18T16:46:33.353Z
Learnt from: CR
Repo: lightspeed-core/lightspeed-stack PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-09-18T16:46:33.353Z
Learning: Applies to src/client.py : Handle Llama Stack APIConnectionError when interacting with the Llama client

Applied to files:

  • tests/unit/app/endpoints/test_query_v2.py
  • tests/unit/app/endpoints/test_models.py
📚 Learning: 2025-09-18T16:46:33.353Z
Learnt from: CR
Repo: lightspeed-core/lightspeed-stack PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-09-18T16:46:33.353Z
Learning: Applies to tests/**/*.py : Use the shared auth mock constant: MOCK_AUTH = ("mock_user_id", "mock_username", False, "mock_token") in tests

Applied to files:

  • tests/unit/app/endpoints/test_models.py
  • tests/unit/app/endpoints/test_metrics.py
📚 Learning: 2025-09-18T16:46:33.353Z
Learnt from: CR
Repo: lightspeed-core/lightspeed-stack PR: 0
File: CLAUDE.md:0-0
Timestamp: 2025-09-18T16:46:33.353Z
Learning: Applies to src/client.py : Use Llama Stack client import: from llama_stack_client import AsyncLlamaStackClient

Applied to files:

  • tests/unit/app/endpoints/test_models.py
🧬 Code graph analysis (2)
tests/unit/app/endpoints/test_streaming_query.py (1)
src/models/responses.py (1)
  • ToolCall (157-162)
tests/unit/app/endpoints/test_query_v2.py (1)
src/configuration.py (1)
  • llama_stack_configuration (87-91)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (4)
  • GitHub Check: build-pr
  • GitHub Check: Konflux kflux-prd-rh02 / lightspeed-stack-on-pull-request
  • GitHub Check: e2e_tests (ci)
  • GitHub Check: e2e_tests (azure)

Comment on lines +358 to 365
streaming_content: list[str] = []
# response.body_iterator is an async generator, iterate over it directly
async for chunk in response.body_iterator:
streaming_content.append(chunk)
streaming_content.append(str(chunk))

# Convert to string for assertions
full_content = "".join(streaming_content) # type: ignore
full_content = "".join(streaming_content)

Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🟠 Major

Decode streaming chunks instead of coercing to repr strings

Casting chunk with str() turns the byte payload into its repr ("b'data: …'"). Later assertions slice the string and feed it to json.loads, which now blows up because of the leading b'/trailing '. Decode the bytes (and fall back to the original string) so the test still exercises the SSE payload rather than its repr.

Apply this diff:

-    streaming_content.append(str(chunk))
+    streaming_content.append(
+        chunk if isinstance(chunk, str) else chunk.decode("utf-8")
+    )
🤖 Prompt for AI Agents
In tests/unit/app/endpoints/test_streaming_query.py around lines 358 to 365, the
test currently uses str(chunk) which produces the Python repr (e.g. "b'...'" )
and breaks later JSON parsing; update the loop to decode chunk when it's bytes
(e.g. chunk.decode('utf-8')) and fall back to using the original chunk if it's
already a str, appending the decoded/normalized string to streaming_content so
the assembled full_content contains the actual SSE payload instead of its repr.

@tisnik tisnik merged commit 3b0bc86 into lightspeed-core:main Nov 9, 2025
21 of 23 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