Skip to content

refactor: reduce dry4dotnet duplication#222

Open
marandaneto wants to merge 5 commits into
mainfrom
refactor/reduce-dry4dotnet-duplicates
Open

refactor: reduce dry4dotnet duplication#222
marandaneto wants to merge 5 commits into
mainfrom
refactor/reduce-dry4dotnet-duplicates

Conversation

@marandaneto
Copy link
Copy Markdown
Member

@marandaneto marandaneto commented Jun 4, 2026

💡 Motivation and Context

Refactor duplicate code paths found by the dry4dotnet PoC (github.com/marandaneto/dry4dotnet) without changing the public API or behavior.

This reduces repeated implementation/test patterns in group identify helpers, no-op client methods, capture helpers, sample setup, fake HTTP response helpers, and several duplicate-heavy tests.

💚 How did you test it?

  • dotnet build PostHog.sln --no-restore
  • dotnet test PostHog.sln --no-build --no-restore
  • dotnet test tests/UnitTests/UnitTests.csproj --framework net8.0 --no-restore --filter "FullyQualifiedName~SemanticVersionTests"
  • dotnet test tests/UnitTests/UnitTests.csproj --framework net8.0 --no-restore --filter "FullyQualifiedName~TheIdentifyGroupAsyncMethod|FullyQualifiedName~TheCapturePageViewMethod|FullyQualifiedName~TheNoOpPostHogClient"
  • dotnet test tests/UnitTests/UnitTests.csproj --framework net8.0 --no-build --no-restore --filter "FullyQualifiedName~SemanticVersionTests|FullyQualifiedName~LocalEvaluatorTests|FullyQualifiedName~FeatureFlagErrorTracking"
  • Re-ran dry4dotnet PoC and reduced candidates from 284 to 126.

📝 Checklist

  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Ran pnpm changeset to generate a changeset file

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jun 4, 2026

posthog-dotnet Compliance Report

Date: 2026-06-05 10:25:27 UTC
Duration: 610ms

⚠️ Some Tests Failed

2/16 tests passed, 14 failed


Feature_Flags Tests

⚠️ 2/16 tests passed, 14 failed

View Details
Test Status Duration
Request Payload.Request With Person Properties Device Id 43ms
Request Payload.Flags Request Uses V2 Query Param 22ms
Request Payload.Flags Request Hits Flags Path Not Decide 5ms
Request Payload.Flags Request Omits Authorization Header 5ms
Request Payload.Token In Flags Body Matches Init 5ms
Request Payload.Groups Round Trip 5ms
Request Payload.Groups Default To Empty Object 5ms
Request Payload.Person Properties Distinct Id Auto Populated When Caller Omits It 4ms
Request Payload.Disable Geoip False Propagates As Geoip Disable False 12ms
Request Payload.Disable Geoip Omitted Defaults To False 12ms
Request Payload.Flag Keys To Evaluate Contains Only Requested Key 6ms
Request Lifecycle.No Flags Request On Init Alone 4ms
Request Lifecycle.No Flags Request On Normal Capture 186ms
Request Lifecycle.Two Flag Calls Produce Two Remote Requests 7ms
Request Lifecycle.Mock Response Value Is Returned To Caller 5ms
Side Effect Events.Get Feature Flag Captures Feature Flag Called Event 5ms

Failures

request_payload.request_with_person_properties_device_id

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_payload.flags_request_uses_v2_query_param

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_payload.flags_request_hits_flags_path_not_decide

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_payload.flags_request_omits_authorization_header

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_payload.token_in_flags_body_matches_init

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_payload.groups_round_trip

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_payload.groups_default_to_empty_object

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_payload.person_properties_distinct_id_auto_populated_when_caller_omits_it

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_payload.disable_geoip_false_propagates_as_geoip_disable_false

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_payload.disable_geoip_omitted_defaults_to_false

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_payload.flag_keys_to_evaluate_contains_only_requested_key

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_lifecycle.two_flag_calls_produce_two_remote_requests

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

request_lifecycle.mock_response_value_is_returned_to_caller

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

side_effect_events.get_feature_flag_captures_feature_flag_called_event

404, message='Not Found', url='http://sdk-adapter:8080/get_feature_flag'

@marandaneto marandaneto marked this pull request as ready for review June 4, 2026 21:29
@marandaneto marandaneto requested review from a team and haacked as code owners June 4, 2026 21:29
@greptile-apps
Copy link
Copy Markdown
Contributor

greptile-apps Bot commented Jun 4, 2026

Comments Outside Diff (2)

  1. tests/UnitTests/Library/SemanticVersionTests.cs, line 1068 (link)

    P2 Literal tab characters reduce readability

    The escape sequence "\t1.2.3\t" was replaced with a string containing literal tab characters. Both are functionally identical, but the literal form is nearly invisible in most editors and code-review tools — it looks like ordinary indentation rather than whitespace that is part of the test value. The escaped form was far more self-documenting.

    Prompt To Fix With AI
    This is a comment left during a code review.
    Path: tests/UnitTests/Library/SemanticVersionTests.cs
    Line: 1068
    
    Comment:
    **Literal tab characters reduce readability**
    
    The escape sequence `"\t1.2.3\t"` was replaced with a string containing literal tab characters. Both are functionally identical, but the literal form is nearly invisible in most editors and code-review tools — it looks like ordinary indentation rather than whitespace that is part of the test value. The escaped form was far more self-documenting.
    
    How can I resolve this? If you propose a fix, please make it concise.

    Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

  2. tests/UnitTests/Library/SemanticVersionTests.cs, line 1554-1560 (link)

    P2 Silent catch-all in switch could mask a test-data typo

    The _ => arm silently evaluates >= for any operatorName value that is not <, <=, or >. If a future test case accidentally uses "=>" (reversed) or ">= " (trailing space), the switch executes >= without warning, producing a passing test that exercises the wrong operator. Consider throwing in the _ arm so any mismatched string fails loudly. The same silent-fallback pattern applies to the kind == "tilde" ? ... : ... dispatch in CalculatesCorrectBounds and BoundsMatchCorrectly.

    Prompt To Fix With AI
    This is a comment left during a code review.
    Path: tests/UnitTests/Library/SemanticVersionTests.cs
    Line: 1554-1560
    
    Comment:
    **Silent catch-all in switch could mask a test-data typo**
    
    The `_ =>` arm silently evaluates `>=` for any `operatorName` value that is not `<`, `<=`, or `>`. If a future test case accidentally uses `"=>"` (reversed) or `">= "` (trailing space), the switch executes `>=` without warning, producing a passing test that exercises the wrong operator. Consider throwing in the `_` arm so any mismatched string fails loudly. The same silent-fallback pattern applies to the `kind == "tilde" ? ... : ...` dispatch in `CalculatesCorrectBounds` and `BoundsMatchCorrectly`.
    
    How can I resolve this? If you propose a fix, please make it concise.
Prompt To Fix All With AI
Fix the following 2 code review issues. Work through them one at a time, proposing concise fixes.

---

### Issue 1 of 2
tests/UnitTests/Library/SemanticVersionTests.cs:1068
**Literal tab characters reduce readability**

The escape sequence `"\t1.2.3\t"` was replaced with a string containing literal tab characters. Both are functionally identical, but the literal form is nearly invisible in most editors and code-review tools — it looks like ordinary indentation rather than whitespace that is part of the test value. The escaped form was far more self-documenting.

### Issue 2 of 2
tests/UnitTests/Library/SemanticVersionTests.cs:1554-1560
**Silent catch-all in switch could mask a test-data typo**

The `_ =>` arm silently evaluates `>=` for any `operatorName` value that is not `<`, `<=`, or `>`. If a future test case accidentally uses `"=>"` (reversed) or `">= "` (trailing space), the switch executes `>=` without warning, producing a passing test that exercises the wrong operator. Consider throwing in the `_` arm so any mismatched string fails loudly. The same silent-fallback pattern applies to the `kind == "tilde" ? ... : ...` dispatch in `CalculatesCorrectBounds` and `BoundsMatchCorrectly`.

Reviews (1): Last reviewed commit: "refactor: reduce dry4dotnet duplication" | Re-trigger Greptile

@marandaneto
Copy link
Copy Markdown
Member Author

Addressed Greptile feedback in 0c6c389: restored escaped tab test data for readability and made the test dispatch fail loudly for unknown range/operator strings. Verified with dotnet test tests/UnitTests/UnitTests.csproj --framework net8.0 --no-restore --filter "FullyQualifiedName~SemanticVersionTests".

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