Skip to content

Phase 2: Testing seams (database, CLI handlers, connections) - #36

Merged
soorya-u merged 3 commits into
mainfrom
phase-2-testing-seams
Jul 11, 2026
Merged

Phase 2: Testing seams (database, CLI handlers, connections)#36
soorya-u merged 3 commits into
mainfrom
phase-2-testing-seams

Conversation

@soorya-u

@soorya-u soorya-u commented Jul 11, 2026

Copy link
Copy Markdown
Owner

Implements #31

Summary

  • Add @cyrus/database Turso integration tests with isolated temp databases
  • Extract runTurn from controller chat handler and add CLI unit coverage
  • Add @cyrus/connections signaling/session unit tests
  • Add server createPostgresDb() factory and docker-compose.test.yml for future Postgres integration
  • Add CI test-integration job

Test plan

  • bun test:unit
  • bun test:integration
  • bun check:types

Stacked on #35

Closes #31
Part of #29

Summary by CodeRabbit

  • New Features

    • Added integration test coverage for database repositories and CLI workflows.
    • Added unit test commands for CLI and connection packages.
    • Improved runtime chat event handling, including streamed tokens and turn completion or interruption states.
    • Added WebSocket host normalization support.
  • Documentation

    • Documented isolated database setup for integration testing.
  • Chores

    • CI now runs integration tests automatically.

@vercel

vercel Bot commented Jul 11, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
cyrus Ready Ready Preview, Comment Jul 11, 2026 7:05am

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown

Review Change Stack

Warning

Review limit reached

@soorya-u, you've reached your PR review limit, so we couldn't start this review.

Next review available in: 45 minutes

Enable usage-based reviews in Billing to review now. Otherwise, wait until the next included review is available.
You're only billed for reviews past your plan's rate limits ($0.25/file).

How can I continue?

After more reviews become available, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

To avoid repeated limits, reduce automatic review volume by pausing incremental auto-reviews earlier, using label-based review opt-in, excluding WIP or generated PR titles, or requesting reviews manually when the PR is ready. If your team needs uninterrupted high-volume reviews, an organization admin can enable usage-based reviews.

How do review limits work?

CodeRabbit enforces per-developer PR review limits for each organization. Most developers receive the normal plan review availability.

For paid Pro and Pro+ PR reviews, CodeRabbit uses adaptive limits for sustained high-volume activity. When a developer's recent PR review activity reaches the 95th percentile or higher among CodeRabbit users, additional reviews become available more gradually as earlier reviews age out of the rolling window.

Please refer docs for additional details.

Review details
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: d248063b-35ca-4ae5-bb14-5ede090d26be

📥 Commits

Reviewing files that changed from the base of the PR and between 529cc09 and 2266ab2.

📒 Files selected for processing (9)
  • .github/workflows/ci.yml
  • apps/cli/src/utils/run-turn.test.ts
  • apps/cli/src/utils/run-turn.ts
  • docs/testing.md
  • shared/connections/src/rtc/peer.test.ts
  • shared/connections/src/rtc/session.test.ts
  • shared/connections/src/rtc/session.ts
  • shared/database/__tests__/helpers/turso.ts
  • shared/database/__tests__/integration/repositories.test.ts
📝 Walkthrough

Walkthrough

The PR extracts CLI turn orchestration into a testable utility, adds CLI and connections unit tests, adds isolated database repository integration tests, exposes host normalization, and configures integration-test scripts, CI execution, and testing-environment documentation.

Changes

CLI testing seams

Layer / File(s) Summary
CLI turn and stream testing
apps/cli/src/utils/run-turn.ts, apps/cli/src/handlers/controller/chat.ts, apps/cli/src/utils/*.test.ts, apps/cli/src/core/acp/events.test.ts, apps/cli/package.json
runTurn now owns turn lifecycle emissions and streamed runtime handling, with tests covering success, interruption, stream persistence helpers, ACP event mapping, and the unit-test script.

Connections coverage

Layer / File(s) Summary
Connections helper and signaling coverage
shared/connections/src/rtc/*.ts, shared/connections/src/rtc/*.test.ts, shared/connections/package.json
normalizeHost is exported and tested for protocol normalization, while signaling event fan-out and immediate closure behavior receive Bun tests.

Database integration

Layer / File(s) Summary
Isolated database repository integration
shared/database/__tests__/**, shared/database/package.json, shared/database/tsconfig.json
Temporary database setup and cleanup support integration tests for projects, threads, names, renames, and persisted conversation events.

CI and documentation

Layer / File(s) Summary
Integration test execution setup
.github/workflows/ci.yml, docs/testing.md
CI adds a dedicated integration-test job, and testing notes document isolated Turso and Neon test environments.

Estimated code review effort: 3 (Moderate) | ~25 minutes

Possibly related issues

  • Issue 29 — Covers the layered Bun unit/integration testing strategy, database isolation, CI gates, and CLI runTurn extraction implemented here.
  • Issue 32 — Relates to CLI runTurn and ACP event-stream tests plus integration-test CI wiring.

Possibly related PRs

  • soorya-u/cyrus#18 — Introduces the streaming, persistence-event, and ACP mapper code now covered by these tests.
  • soorya-u/cyrus#23 — Overlaps with the extraction of chat turn-streaming orchestration into a runTurn utility.
🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Linked Issues check ⚠️ Warning Database, CLI, and connections work is present, but the required server/Postgres Workers-pool integration pieces from #31 are absent. Implement the apps/server factory, Postgres test setup/migrations, and Workers-pool tests; add fake-deps CLI handler integration coverage if still required.
Out of Scope Changes check ⚠️ Warning docs/testing.md adds Neon-branch guidance, which the issue marks as optional later-phase work rather than Phase 2 scope. Move that Neon guidance to the later-phase docs or rewrite it to match the Phase 2 Docker Postgres requirement.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly reflects the main Phase 2 testing-seams work across database, CLI handlers, and connections.
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
  • Commit unit tests in branch phase-2-testing-seams

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.

@soorya-u

Copy link
Copy Markdown
Owner Author

Tracks sub-issue #31 (epic #29).

Base automatically changed from phase-1-testing-foundation to main July 11, 2026 06:30
@soorya-u soorya-u linked an issue Jul 11, 2026 that may be closed by this pull request
23 tasks
Introduce Turso repository integration tests, extract controller runTurn, add stream and ACP mapper coverage, test signaling helpers, and add a postgres-js factory for future server integration work.

Co-authored-by: Cursor <cursoragent@cursor.com>
@soorya-u
soorya-u force-pushed the phase-2-testing-seams branch from a7aed5f to 83d89a6 Compare July 11, 2026 06:36
@soorya-u

Copy link
Copy Markdown
Owner Author

Rebased onto merged #35 (main @ 9dddff2). Phase 2 branch now contains only the Phase 2 commit cherry-picked on top of main — conflicts resolved.

Move runTurn out of handlers into utils, and drop postgres-js/Docker
Postgres in favor of Neon branches with the production neon-http driver.

Co-authored-by: Cursor <cursoragent@cursor.com>

@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: 4

Caution

Some comments are outside the diff and can’t be posted inline due to platform limitations.

⚠️ Outside diff range comments (1)
shared/connections/src/rtc/session.ts (1)

36-47: 🔒 Security & Privacy | 🟠 Major | ⚡ Quick win

Pre-existing wss:// protocol downgrade bug now exposed by export.

normalizeHost maps url.protocol === "https:" to wss and everything else to ws. If a caller passes wss://cyrus.example.com, url.protocol is wss: (not https:), so the function returns protocol: "ws" — silently downgrading a secure WebSocket URL to insecure. This is a security concern now that the function is exported and part of the public API surface.

Consider mapping both https: and wss: to wss:

🔒 Proposed fix
 export function normalizeHost(host: string): {
 	host: string;
 	protocol: "ws" | "wss";
 } {
 	if (host.includes("://")) {
 		const url = new URL(host);
 		return {
 			host: url.host,
-			protocol: url.protocol === "https:" ? "wss" : "ws",
+			protocol: url.protocol === "https:" || url.protocol === "wss:" ? "wss" : "ws",
 		};
 	}
 	return { host, protocol: "ws" };
 }
🤖 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 `@shared/connections/src/rtc/session.ts` around lines 36 - 47, Update
normalizeHost to preserve secure WebSocket URLs: map both "https:" and "wss:"
protocols to "wss", while mapping only non-secure protocols to "ws". Keep the
existing host extraction and return shape unchanged.
🧹 Nitpick comments (4)
shared/database/__tests__/integration/repositories.test.ts (1)

20-38: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Test creates /tmp/cyrus directory without cleanup.

createProject("Cyrus", "/tmp/cyrus") triggers mkdir("/tmp/cyrus", { recursive: true }) inside createProject, but withTempDatabase only cleans up the temp DB directory — not the project's cwd. Consider using a path inside the temp directory or an empty cwd to avoid leaving artifacts behind.

♻️ Suggested change
-			const created = await createProject("Cyrus", "/tmp/cyrus");
+			const created = await createProject("Cyrus");

Or pass a path under os.tmpdir() that the test manages itself.

🤖 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 `@shared/database/__tests__/integration/repositories.test.ts` around lines 20 -
38, Update the “creates and lists projects” test to avoid using the unmanaged
“/tmp/cyrus” path: use an empty cwd or a path created under the temporary
directory managed by withTempDatabase, and update the expected cwd accordingly.
Keep the existing assertions for project creation and listing.
apps/cli/src/utils/run-turn.test.ts (1)

1-71: 📐 Maintainability & Code Quality | 🔵 Trivial | ⚡ Quick win

Add test coverage for the initial emit failure path.

The two existing tests cover the happy path and streaming failure, but the started.isErr() branch (initial emit calls for user_message/thread_started) is untested. This is the same branch flagged in run-turn.ts where no terminal event is emitted. Once that fix is applied, a test verifying turn_interrupted is emitted on initial emit failure would close the gap.

Suggested test for initial emit failure
+	test("emits interrupted terminal event when initial emits fail", async () => {
+		const terminal: ChatChunk["event"][] = [];
+
+		const result = await runTurn({
+			agentName: "claude",
+			threadId: "thread-1",
+			projectId: "project-1",
+			message: "hello",
+			emit: () => Promise.reject(new Error("emit failed")),
+			emitTerminal: (event) => {
+				terminal.push(event);
+				return Promise.resolve();
+			},
+			runtime: {
+				threadCoordinator: {
+					prompt: () => prompt(),
+				},
+			} as never,
+		});
+
+		expect(result.isErr()).toBe(true);
+		expect(terminal).toEqual([{ type: "turn_interrupted" }]);
+	});
🤖 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 `@apps/cli/src/utils/run-turn.test.ts` around lines 1 - 71, Add a test in the
runTurn describe block covering failure of the initial emit calls for
user_message or thread_started. Make emit reject with an error, invoke runTurn
with the existing required inputs, assert the result is an error, and verify
emitTerminal receives a single turn_interrupted event.
shared/connections/src/rtc/session.test.ts (1)

4-25: 🎯 Functional Correctness | 🔵 Trivial | ⚡ Quick win

Add test coverage for wss:// and ws:// protocol inputs.

The current tests cover https://, http://, and bare hosts, but not wss:// or ws:// inputs. The wss:// case would expose the downgrade bug flagged in session.ts. Adding these cases would strengthen the test suite and guard against regressions.

🧪 Suggested additional test cases
 	test("defaults bare hosts to ws", () => {
 		expect(normalizeHost("localhost:8787")).toEqual({
 			host: "localhost:8787",
 			protocol: "ws",
 		});
 	});
+
+	test("parses wss urls as wss", () => {
+		expect(normalizeHost("wss://cyrus.example.com")).toEqual({
+			host: "cyrus.example.com",
+			protocol: "wss",
+		});
+	});
+
+	test("parses ws urls as ws", () => {
+		expect(normalizeHost("ws://localhost:8787")).toEqual({
+			host: "localhost:8787",
+			protocol: "ws",
+		});
+	});
 });
🤖 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 `@shared/connections/src/rtc/session.test.ts` around lines 4 - 25, Add test
cases in the normalizeHost suite for wss:// and ws:// inputs, asserting that the
host is preserved and protocols remain wss and ws respectively. Use the existing
normalizeHost tests as the pattern and ensure the wss:// case prevents protocol
downgrading.
shared/connections/src/rtc/peer.test.ts (1)

10-39: 🩺 Stability & Availability | 🔵 Trivial | 💤 Low value

Timing-based assertions with Bun.sleep(10) may be flaky in CI.

Both tests rely on Bun.sleep(10) to allow async microtask delivery. While 10ms is likely sufficient for synchronous generators, this pattern is inherently timing-dependent. Consider using a deterministic approach — e.g., polling with await new Promise(r => setTimeout(r, 0)) to flush microtasks, or asserting on the received array after the stream's async iteration has settled via a promise or callback signal.

This is a minor concern given the synchronous generator, but worth noting for future tests that may use real async streams.

🤖 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 `@shared/connections/src/rtc/peer.test.ts` around lines 10 - 39, Replace the
timing-dependent Bun.sleep(10) calls in the “fans out events to subscribers” and
“stops delivering after close” tests with deterministic synchronization, such as
awaiting a promise/callback that signals async iteration completion or a
zero-delay event-loop flush. Ensure assertions run only after delivery has
settled while still verifying no events arrive after events.close().
🤖 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 @.github/workflows/ci.yml:
- Around line 58-68: Add persist-credentials: false to the checkout step in the
test-integration job to prevent GitHub token persistence, and add needs:
check-types to gate integration tests on successful type checking, matching
test-unit.

In `@apps/cli/src/utils/run-turn.ts`:
- Line 33: When runTurn’s initial emit fails, it returns without notifying the
client, leaving the subscription open. Update the started.isErr() branch in
runTurn to call emitTerminal with a turn_interrupted event before returning the
error, matching the streamed.isErr() handling; preserve the existing error
return.

In `@shared/connections/src/rtc/peer.test.ts`:
- Around line 5-7: Change the eventStream generator to an async generator so it
returns AsyncIterable<ServerEvent> as required by createSignalingEvents,
preserving its existing event iteration behavior.

In `@shared/database/__tests__/helpers/turso.ts`:
- Around line 11-17: Guard the cleanup in the helper’s try/finally block so
connection.close() is only called after a successful connection.open(), or catch
and suppress close errors when opening fails; preserve the original error while
still removing the temporary directory.

---

Outside diff comments:
In `@shared/connections/src/rtc/session.ts`:
- Around line 36-47: Update normalizeHost to preserve secure WebSocket URLs: map
both "https:" and "wss:" protocols to "wss", while mapping only non-secure
protocols to "ws". Keep the existing host extraction and return shape unchanged.

---

Nitpick comments:
In `@apps/cli/src/utils/run-turn.test.ts`:
- Around line 1-71: Add a test in the runTurn describe block covering failure of
the initial emit calls for user_message or thread_started. Make emit reject with
an error, invoke runTurn with the existing required inputs, assert the result is
an error, and verify emitTerminal receives a single turn_interrupted event.

In `@shared/connections/src/rtc/peer.test.ts`:
- Around line 10-39: Replace the timing-dependent Bun.sleep(10) calls in the
“fans out events to subscribers” and “stops delivering after close” tests with
deterministic synchronization, such as awaiting a promise/callback that signals
async iteration completion or a zero-delay event-loop flush. Ensure assertions
run only after delivery has settled while still verifying no events arrive after
events.close().

In `@shared/connections/src/rtc/session.test.ts`:
- Around line 4-25: Add test cases in the normalizeHost suite for wss:// and
ws:// inputs, asserting that the host is preserved and protocols remain wss and
ws respectively. Use the existing normalizeHost tests as the pattern and ensure
the wss:// case prevents protocol downgrading.

In `@shared/database/__tests__/integration/repositories.test.ts`:
- Around line 20-38: Update the “creates and lists projects” test to avoid using
the unmanaged “/tmp/cyrus” path: use an empty cwd or a path created under the
temporary directory managed by withTempDatabase, and update the expected cwd
accordingly. Keep the existing assertions for project creation and listing.
🪄 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

Run ID: fce6a678-1b51-4c19-b5e2-fc0fa475484c

📥 Commits

Reviewing files that changed from the base of the PR and between 9dddff2 and 529cc09.

⛔ Files ignored due to path filters (1)
  • bun.lock is excluded by !**/*.lock
📒 Files selected for processing (16)
  • .github/workflows/ci.yml
  • apps/cli/package.json
  • apps/cli/src/core/acp/events.test.ts
  • apps/cli/src/handlers/controller/chat.ts
  • apps/cli/src/utils/run-turn.test.ts
  • apps/cli/src/utils/run-turn.ts
  • apps/cli/src/utils/streams.test.ts
  • docs/testing.md
  • shared/connections/package.json
  • shared/connections/src/rtc/peer.test.ts
  • shared/connections/src/rtc/session.test.ts
  • shared/connections/src/rtc/session.ts
  • shared/database/__tests__/helpers/turso.ts
  • shared/database/__tests__/integration/repositories.test.ts
  • shared/database/package.json
  • shared/database/tsconfig.json

Comment thread .github/workflows/ci.yml
Comment thread apps/cli/src/utils/run-turn.ts Outdated
Comment thread shared/connections/src/rtc/peer.test.ts Outdated
Comment thread shared/database/__tests__/helpers/turso.ts
Emit turn_interrupted on initial emit failure, fix wss:// host
normalization, harden test helpers, and align integration CI with unit
test job settings.

Co-authored-by: Cursor <cursoragent@cursor.com>
@soorya-u

Copy link
Copy Markdown
Owner Author

Addressed CodeRabbit inline review in 2266ab2:

  • CItest-integration now has needs: check-types and persist-credentials: false
  • runTurn — emits turn_interrupted when initial emit fails; added regression test
  • normalizeHost — preserves wss:// (no downgrade to ws); added wss:///ws:// tests
  • peer.test — async generator + setImmediate flush instead of Bun.sleep(10)
  • turso helper — in-memory shared DB, guarded close, temp cwd so WAL files stay out of repo
  • repositories test — dropped hardcoded /tmp/cyrus cwd

Intentionally unchanged (scope):

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.

Phase 2: Testing seams (database, server Workers pool, CLI handlers)

1 participant