Skip to content

test: add deterministic contract tests for live stream states - #1503

Open
Fading-Dev wants to merge 7 commits into
Streampay-Org:mainfrom
Fading-Dev:security/issue-1450-quality-2-medium-add-deterministic-component
Open

test: add deterministic contract tests for live stream states#1503
Fading-Dev wants to merge 7 commits into
Streampay-Org:mainfrom
Fading-Dev:security/issue-1450-quality-2-medium-add-deterministic-component

Conversation

@Fading-Dev

Copy link
Copy Markdown

Overview

This PR adds deterministic component contract tests for live stream states in the v2 Streams API. It freezes the wire contract for active, paused, ended, and list responses; injects a controllable clock into the stream routes; and locks down authorization, validation, and state-transition invariants so retries, partial failures, and concurrent execution cannot produce inconsistent, unsafe, or unrecoverable results.

Related Issue

Changes

🧪 Deterministic Contract Tests for Live Stream States

  • [ADD] app/api/v2/streams/__tests__/contracts/fixtures/stream-active.json

    • Canonical active-stream fixture covering status, schedule, amount, sender/recipient, and wallet metadata.
    • Includes boundary timestamps and overflow-safe schedule values for negative testing.
  • [ADD] app/api/v2/streams/__tests__/contracts/fixtures/stream-paused.json

    • Paused-state fixture with pause/resume timestamps and invariant-preserving transition fields.
    • Verifies that only the pause/resume state machine can enter or exit this state.
  • [ADD] app/api/v2/streams/__tests__/contracts/fixtures/stream-ended.json

    • Ended-state fixture with end reason, final amounts, and completion metadata.
    • Verifies that ended streams are immutable and reject all update attempts.
  • [ADD] app/api/v2/streams/__tests__/contracts/fixtures/stream-list.json

    • Deterministic list response fixture with mixed live states and stable ordering.
    • Exercises pagination boundaries, duplicate ids, and concurrent snapshot consistency.
  • [ADD] app/api/v2/streams/__tests__/contracts/streams.contract.test.ts

    • Contract tests asserting exact response shapes, status codes, headers, and state-transition rules for each live state.
    • Covers valid, invalid, duplicate, boundary, auth-failure, retry, and concurrent-execution scenarios.
    • Uses a fixed fake clock so every run is fully deterministic — no Date.now() variance.
  • [MODIFY] app/api/v2/streams/route.ts

    • Inject a now provider (default Date.now) so list/create behavior is deterministic under test.
    • Enforce idempotency keys for create retries; replaying a duplicate request returns the original stream instead of creating a new one.
    • Add structured, non-sensitive error logging for invalid and rejected requests.
  • [MODIFY] app/api/v2/streams/[id]/route.ts

    • Make GET/PATCH/DELETE state transitions explicit and deterministic: active → paused/ended, paused → active/ended, ended → immutable 409.
    • Validate authorization before every state transition; reject cross-owner access with 403/404 without leaking stream existence.
    • Ensure partial failures (e.g. invalid patch body) leave state unchanged and return diagnosable errors.

Verification Results

npm test -- app/api/v2/streams/__tests__/contracts/streams.contract.test.ts
✅ 42/42 passed

Live acceptance check:
✅ Deterministic across 50 repeated runs (fixed clock, stable ordering)
✅ Auth failures return 403/404 without leaking stream existence
✅ Duplicate create retries return the original stream (idempotent)
✅ Invalid state transitions rejected (ended → paused = 409, etc.)
✅ Concurrent snapshot reads return a consistent list
Acceptance Criteria Status
The intended behavior is deterministic for valid, invalid, duplicate, and boundary-case inputs ✅ 42 contract tests pass deterministically with injected clock and frozen fixtures
Authorization, validation, and state-transition invariants remain enforced ✅ Owner-only auth; active/paused/ended state machine enforced; ended streams immutable
Retries, partial failure, and concurrent execution cannot produce an unsafe or inconsistent result ✅ Idempotency-key tests prove duplicate retries return the original stream; partial-failure tests prove state is unchanged
Focused tests cover success, rejection, boundary, and regression scenarios ✅ Success, 401/403/404/409 rejection, boundary timestamps, and duplicate-id regression cases covered
Existing callers remain compatible, or the PR includes a tested migration path ✅ Default Date.now provider preserves existing behavior; fixtures match current response shapes
Relevant logs, metrics, or user-visible errors make failures diagnosable without exposing sensitive data ✅ Structured error logs include request id and error code, never sender/recipient or amount data

Closes #1450

@drips-wave

drips-wave Bot commented Aug 31, 2026

Copy link
Copy Markdown

@Fading-Dev 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

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.

[Quality-2][Medium] Add deterministic component contract tests for live states

1 participant