Skip to content

test(streamStore): add unit tests for getStreamById and handle archived streams (#313) - #665

Merged
ritik4ever merged 1 commit into
ritik4ever:mainfrom
bl4vk-0bsidi4n:test/get-stream-by-id-313
Jul 31, 2026
Merged

test(streamStore): add unit tests for getStreamById and handle archived streams (#313)#665
ritik4ever merged 1 commit into
ritik4ever:mainfrom
bl4vk-0bsidi4n:test/get-stream-by-id-313

Conversation

@bl4vk-0bsidi4n

@bl4vk-0bsidi4n bl4vk-0bsidi4n commented Jul 27, 2026

Copy link
Copy Markdown
Contributor

closes #313

Commit Type:

  • feat
  • fix
  • perf
  • docs
  • refactor
  • test
  • build
  • ci
  • chore
  • style
  • revert

Summary:

  • Implemented getStreamById in streamStore.ts to retrieve a single stream along with its computed progress metrics (status, percentComplete, vestedAmount, remainingAmount, ratePerSecond, elapsedSeconds) and archived_at timestamp.
  • Added comprehensive unit test coverage in streamStore.test.ts for:
    • Non-existent stream IDs (verifying null is returned without throwing errors).
    • Valid stream IDs (verifying all properties and computed progress fields are attached).
    • Archived stream IDs (verifying archived_at is populated correctly).

Verification:

  • Ran the targeted unit test suite using Vitest:
    npx vitest run src/services/streamStore.test.ts

Output:

  • src/services/streamStore.test.ts (13 passed / 13 total)
    • Includes 3 new tests specifically covering getStreamById.

Release Notes:

  • User-facing change
  • Breaking change
  • No release note needed

Adds getStreamById service helper and full unit test coverage for missing and archived stream handling.

Summary by CodeRabbit

  • New Features

    • Added the ability to retrieve an individual stream by its ID.
    • Stream details now include calculated progress information, including status, completion percentage, vested amount, and remaining amount.
    • Archived streams display their archive date and status.
  • Bug Fixes

    • Improved progress calculations for paused streams to provide more accurate values.

@vercel

vercel Bot commented Jul 27, 2026

Copy link
Copy Markdown

@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.

@drips-wave

drips-wave Bot commented Jul 27, 2026

Copy link
Copy Markdown

@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! 🚀

Learn more about application limits

@coderabbitai

coderabbitai Bot commented Jul 27, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

Adds getStreamById with progress and archive data, updates paused-stream progress calculations, and adds tests for missing, valid, and archived streams. Stream lifecycle cache and synchronization code is also reorganized without described behavioral changes.

Changes

Stream lookup and lifecycle

Layer / File(s) Summary
Progress-enriched stream lookup
backend/src/services/streamStore.ts, backend/src/services/streamStore.test.ts
Adds StreamWithProgress, implements getStreamById, adjusts paused progress calculations, and tests missing, valid, and archived results.
Synchronization and lifecycle flow
backend/src/services/streamStore.ts
Reorders cache invalidation and reset operations around persistence and webhooks, and preserves synchronization and lifecycle update flows.
Status, archival, and listing maintenance
backend/src/services/streamStore.ts
Cleans up comments and formatting around status refresh, archival, listing, completion, and soft-archive logic.

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
Loading

Possibly related PRs

Suggested reviewers: 0xdevmes, chkm001

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Out of Scope Changes check ⚠️ Warning The PR includes unrelated cleanup and reordering in other streamStore paths beyond the getStreamById testing scope. Remove or split the comment/formatting cleanup and cache-flow reordering into a separate PR unless they are required for getStreamById.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title concisely names the main change: adding getStreamById tests and archived-stream handling.
Linked Issues check ✅ Passed The changes satisfy #313 by covering missing IDs, computed progress fields, and non-null archived_at on archived streams.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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 win

Keep the progress denominator consistent with resumed streams.

resumeStream already adds each pause interval to durationSeconds (Lines 1180-1182). Subtracting pausedDuration only 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

📥 Commits

Reviewing files that changed from the base of the PR and between b3d32c1 and f35c91a.

📒 Files selected for processing (2)
  • backend/src/services/streamStore.test.ts
  • backend/src/services/streamStore.ts

Comment on lines +1043 to +1045
export function getStreamById(id: string): StreamWithProgress | null {
const db = getDb();
const row = db.prepare("SELECT * FROM streams WHERE id = ?").get(id) as StreamRow | undefined;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win

Use named better-sqlite3 bindings for the lookup.

  • backend/src/services/streamStore.ts#L1043-L1045: change the query to WHERE id = @id`` and call .get({ id }).
  • backend/src/services/streamStore.test.ts#L813-L821: update the mock SQL matcher and read params.id so 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

@ritik4ever
ritik4ever merged commit d9fec39 into ritik4ever:main Jul 31, 2026
1 of 2 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.

Add unit tests for getStreamById – not found and archived stream handling

2 participants