-
-
Notifications
You must be signed in to change notification settings - Fork 28
fix: don't report negative timings in TCP ping #335
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
Conversation
WalkthroughThe PR refactors TCP ping probe types by introducing a base probe type and distinct success/failure variants: successful probes include Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
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.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/command/handlers/ping/tcp-ping.ts (1)
245-247: Consider explicit type predicate for improved type safety.While TypeScript should narrow the type correctly after filtering by
t.success, using an explicit type predicate can make the type narrowing more robust:-const timings = probeData.filter(t => t.success).map(probe => ({ +const timings = probeData.filter((t): t is TcpPingSuccessProbeData => t.success).map(probe => ({ rtt: roundNumber(probe.rtt, 2), })).filter(t => !Number.isNaN(t.rtt));This ensures TypeScript explicitly recognizes that after the filter, all items are
TcpPingSuccessProbeDatawith therttproperty available.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/command/handlers/ping/tcp-ping.ts(4 hunks)test/unit/command/handlers/ping/tcp-ping.test.ts(3 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{vue,ts,js}
⚙️ CodeRabbit configuration file
We use Nuxt with auto imports enabled. Don't warn about missing imports.
Files:
test/unit/command/handlers/ping/tcp-ping.test.tssrc/command/handlers/ping/tcp-ping.ts
⏰ 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). (6)
- GitHub Check: Test code / Run e2e tests (20.x)
- GitHub Check: Test docker / Publish Docker image
- GitHub Check: Test code / Run tests (20.x)
- GitHub Check: Test code / Run tests (18.x)
- GitHub Check: Test docker / Publish Docker image
- GitHub Check: Test code / Run e2e tests (20.x)
🔇 Additional comments (9)
test/unit/command/handlers/ping/tcp-ping.test.ts (4)
160-165: LGTM - Proper type narrowing for successful probes.The conditional check ensures RTT is only asserted when the probe succeeds, aligning with the new type structure.
178-183: LGTM - Consistent with success-based assertions.
198-198: LGTM - Verifies absence of RTT on failures.This assertion directly validates the PR objective.
509-515: LGTM - Test data matches the new type structure.src/command/handlers/ping/tcp-ping.ts (5)
24-40: LGTM - Well-structured discriminated union.The type definitions cleanly separate successful and failed probe data, with the literal
successproperty enabling proper type discrimination.
85-86: LGTM - Error handling excludes RTT.
90-91: LGTM - Timeout handling excludes RTT.
116-116: LGTM - Refined typing for success timings.
149-151: LGTM - Correct filtering of successful probes.
|
@alexey-yarmosh, please see if you can fix the tests. The code should be correct, TS builds it, but ts-node reports an error. |
|
Turns out inferred type predicates require |
TSC doesn't check the test code, otherwise, it would report errors too. And having the tests with no type checking at all would cause them to become a bigger and bigger mess over time, at which point I'd rather write them in plain JS. But the fix here turned out to be quite simple - strict comparison is enough to trigger the predicate. |
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.
Actionable comments posted: 0
🧹 Nitpick comments (1)
src/command/handlers/ping/tcp-ping.ts (1)
149-151: Minor: Consider consistent success checking.Line 149 uses
result.success(truthy check) while line 245 usest.success === true(explicit check). Both work correctly, but consistency would be slightly clearer.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
src/command/handlers/ping/tcp-ping.ts(4 hunks)test/unit/command/ping-command.test.ts(1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.{vue,ts,js}
⚙️ CodeRabbit configuration file
We use Nuxt with auto imports enabled. Don't warn about missing imports.
Files:
test/unit/command/ping-command.test.tssrc/command/handlers/ping/tcp-ping.ts
⏰ 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: Test docker / Publish Docker image
- GitHub Check: Test code / Run e2e tests (20.x)
- GitHub Check: Test docker / Publish Docker image
- GitHub Check: Test code / Run e2e tests (20.x)
🔇 Additional comments (4)
src/command/handlers/ping/tcp-ping.ts (3)
24-40: LGTM! Clean discriminated union design.The type structure correctly ensures that
rttis only present on successful probes, directly addressing the PR objective of not reporting negative timings.
73-99: Correct implementation of success/failure distinction.The function properly omits
rttfrom error and timeout cases, ensuring only successful connections report timing data.
245-247: Correct filtering logic.The explicit
success === truecheck properly narrows the type toTcpPingSuccessProbeData, ensuring type-safe access tortt.test/unit/command/ping-command.test.ts (1)
127-127: Correct type refinement.Changing from
nevertounknownproperly reflects that the mock can receive any options type, even if unused. This aligns with TypeScript best practices.
|
🎉 This PR is included in version 0.41.3 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
Related to jsdelivr/globalping#686