test(streamStore): add unit tests for getStreamById and handle archived streams (#313) - #665
Conversation
|
@bl4vk-0bsidi4n is attempting to deploy a commit to the ritik4ever's projects Team on Vercel. A member of the Team first needs to authorize it. |
|
@bl4vk-0bsidi4n Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
📝 WalkthroughWalkthroughAdds ChangesStream lookup and lifecycle
Estimated code review effort: 3 (Moderate) | ~20 minutes Sequence Diagram(s)sequenceDiagram
participant Caller
participant getStreamById
participant Database
participant calculateProgress
Caller->>getStreamById: request stream ID
getStreamById->>Database: query stream row
Database-->>getStreamById: row or undefined
getStreamById->>calculateProgress: compute progress
calculateProgress-->>getStreamById: progress fields
getStreamById-->>Caller: stream result or null
Possibly related PRs
Suggested reviewers: 🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 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.
Actionable comments posted: 1
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
backend/src/services/streamStore.ts (1)
451-461: 🎯 Functional Correctness | 🟠 Major | ⚡ Quick winKeep the progress denominator consistent with resumed streams.
resumeStreamalready adds each pause interval todurationSeconds(Lines 1180-1182). SubtractingpausedDurationonly from elapsed time makes progress regress after resume: a 100-second stream paused at 50 seconds for 20 seconds reports 41.67% at resume and remains only 83.33% vested when its extended 120-second schedule completes.Proposed fix
const elapsed = Math.max(0, Math.max(0, effectiveAt - stream.startAt) - stream.pausedDuration); - const ratio = stream.durationSeconds <= 0 ? 1 : Math.min(1, elapsed / stream.durationSeconds); - const elapsedSeconds = stream.durationSeconds <= 0 ? 0 : Math.min(elapsed, stream.durationSeconds); + const activeDurationSeconds = Math.max( + 0, + stream.durationSeconds - stream.pausedDuration, + ); + const ratio = + activeDurationSeconds <= 0 + ? 1 + : Math.min(1, elapsed / activeDurationSeconds); + const elapsedSeconds = + activeDurationSeconds <= 0 + ? 0 + : Math.min(elapsed, activeDurationSeconds); const vestedAmount = stream.totalAmount * ratio; return { status: computeStatus(stream, at), - ratePerSecond: stream.durationSeconds <= 0 ? Infinity : round(stream.totalAmount / stream.durationSeconds), + ratePerSecond: activeDurationSeconds <= 0 + ? Infinity + : round(stream.totalAmount / activeDurationSeconds),Add a pause/resume regression test covering unchanged progress at resume and full vesting at the extended completion time.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@backend/src/services/streamStore.ts` around lines 451 - 461, Update the vesting calculation around effectiveAt, elapsed, ratio, and elapsedSeconds to use a denominator consistent with resumeStream’s pause-extended durationSeconds: progress must not regress when resuming and must reach full vesting at the extended completion time. Preserve existing handling for paused streams and non-positive durations, and add a regression test covering unchanged progress at resume and complete vesting after the extended schedule.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@backend/src/services/streamStore.ts`:
- Around line 1043-1045: The getStreamById lookup uses positional instead of
named better-sqlite3 bindings. In backend/src/services/streamStore.ts lines
1043-1045, change the query to use the `@id` binding and call get with an object
containing id; in backend/src/services/streamStore.test.ts lines 813-821, update
the SQL matcher and mock parameter access to params.id so the test matches the
production binding contract.
---
Outside diff comments:
In `@backend/src/services/streamStore.ts`:
- Around line 451-461: Update the vesting calculation around effectiveAt,
elapsed, ratio, and elapsedSeconds to use a denominator consistent with
resumeStream’s pause-extended durationSeconds: progress must not regress when
resuming and must reach full vesting at the extended completion time. Preserve
existing handling for paused streams and non-positive durations, and add a
regression test covering unchanged progress at resume and complete vesting after
the extended schedule.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: defaults
Review profile: CHILL
Plan: Pro Plus
Run ID: c91e034a-9efa-4efb-9a12-612a9a1e26e0
📒 Files selected for processing (2)
backend/src/services/streamStore.test.tsbackend/src/services/streamStore.ts
| export function getStreamById(id: string): StreamWithProgress | null { | ||
| const db = getDb(); | ||
| const row = db.prepare("SELECT * FROM streams WHERE id = ?").get(id) as StreamRow | undefined; |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
Use named better-sqlite3 bindings for the lookup.
backend/src/services/streamStore.ts#L1043-L1045: change the query toWHERE id =@id`` and call.get({ id }).backend/src/services/streamStore.test.ts#L813-L821: update the mock SQL matcher and readparams.idso tests cover the production binding contract.
As per coding guidelines, “In backend TypeScript code, use @name parameter binding syntax for better-sqlite3 prepared statements instead of ? placeholders.”
📍 Affects 2 files
backend/src/services/streamStore.ts#L1043-L1045(this comment)backend/src/services/streamStore.test.ts#L813-L821
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@backend/src/services/streamStore.ts` around lines 1043 - 1045, The
getStreamById lookup uses positional instead of named better-sqlite3 bindings.
In backend/src/services/streamStore.ts lines 1043-1045, change the query to use
the `@id` binding and call get with an object containing id; in
backend/src/services/streamStore.test.ts lines 813-821, update the SQL matcher
and mock parameter access to params.id so the test matches the production
binding contract.
Source: Coding guidelines
closes #313
Commit Type:
Summary:
Verification:
npx vitest run src/services/streamStore.test.ts
Output:
Release Notes:
Adds getStreamById service helper and full unit test coverage for missing and archived stream handling.
Summary by CodeRabbit
New Features
Bug Fixes