Skip to content

Conversation

@jy-tan
Copy link
Contributor

@jy-tan jy-tan commented Jan 31, 2026

Summary

Fixes non-deterministic HTML class attribute ordering that caused replay comparison failures in Django apps (e.g., django-unfold admin). Also fixes a critical bug in protobuf Value conversion that caused empty mock data during replay.

Context

The HTML class ordering issue was discovered during Django replay where templates (likely from django-unfold) rendered CSS classes in non-deterministic order. The protobuf bug was causing mock database rows to be empty during replay, leading to Django's StopIteration error when populating models.

Changes

HTML Normalization for Django Replay

  • Add html_utils.py with normalize_html_class_ordering() to sort CSS classes alphabetically within class="..." attributes
  • Consolidate CSRF token normalization into html_utils.py (deleted separate csrf_utils.py)
  • Add normalize_html_body() and normalize_html_response() helpers to reduce code duplication
  • Update middleware.py to use consolidated helpers for both RECORD and REPLAY modes
  • Add 37 unit tests for HTML normalization

Protobuf Utils Refactor

  • Extract shared value_to_python() and struct_to_dict() into new protobuf_utils.py
  • Fix bug where hasattr() was incorrectly used to check protobuf oneof fields (always returns True for protobuf attributes, causing all values to return None)
  • Properly handle: Python native types, Google protobuf Value (WhichOneof), and betterproto Value (is_set)
  • Remove ~130 lines of duplicated code from communicator.py and types.py
  • Add 27 unit tests for protobuf conversion

Copy link

@cubic-dev-ai cubic-dev-ai bot left a comment

Choose a reason for hiding this comment

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

1 issue found across 8 files

Prompt for AI agents (all issues)

Check if these issues are valid — if so, understand the root cause of each and fix them.


<file name="tests/unit/test_html_utils.py">

<violation number="1" location="tests/unit/test_html_utils.py:286">
P2: Deleting `response.content` on a MagicMock with `spec=["get"]` raises `AttributeError`, so this test will fail before it runs. The mock already lacks a `content` attribute; removing the delete fixes the test.</violation>
</file>

Reply with feedback, questions, or to request a fix. Tag @cubic-dev-ai to re-run a review.

@jy-tan jy-tan merged commit 17cab49 into main Feb 1, 2026
22 checks passed
@jy-tan jy-tan deleted the html-norm-fix-protobuf branch February 1, 2026 00:21
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.

3 participants