Skip to content

Add recommended guards helper#12

Merged
jnsdls merged 3 commits into
mainfrom
t3code/add-recommended-guards-helper
Apr 27, 2026
Merged

Add recommended guards helper#12
jnsdls merged 3 commits into
mainfrom
t3code/add-recommended-guards-helper

Conversation

@jnsdls
Copy link
Copy Markdown
Member

@jnsdls jnsdls commented Apr 27, 2026

Summary

  • add public recommendedGuards() helper for unpackaged + env-var MCP opt-in checks
  • export the helper and its options type from the root and types entrypoints
  • update README quickstart/security snippets to use the package helper

Verification

  • pnpm run lint
  • pnpm run typecheck
  • pnpm run test
  • pnpm run build
  • pnpm test:electron if Electron/CDP behavior changed

Closes #5

Summary by CodeRabbit

  • New Features
    • Added a public helper to opt into MCP when the app is unpackaged and a specified environment variable equals "1".
  • Documentation
    • Quickstart and guidance updated with example usage and clearer behavior and edge-case notes.
  • Tests
    • Unit tests added covering packaged vs unpackaged states, env-var opt-in, and error conditions.
  • Chores
    • Changeset added documenting a minor version bump.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented Apr 27, 2026

🦋 Changeset detected

Latest commit: 884f455

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Apr 27, 2026

📝 Walkthrough

Walkthrough

Adds a new exported helper recommendedGuards() that determines if MCP runtime guards should be active (based on packaging detection and an opt-in env var), re-exports its type from the package root/types, documents usage in the README, and adds unit tests and a changeset.

Changes

Cohort / File(s) Summary
Guards implementation
src/guards.ts
New module exporting RecommendedGuardsOptions and recommendedGuards() which resolves packaged from isPackaged or app.isPackaged, throws if undeterminable, and returns true only when not packaged and the configured env var equals "1".
Public surface
src/index.ts, src/types.ts
Re-exports recommendedGuards and the RecommendedGuardsOptions type from the package root and types entry.
Tests
src/index.test.ts
Adds unit tests covering: missing packaging source throws, unpackaged + env opt-in returns true, missing env returns false, packaged returns false, and accepts an Electron-like app object.
Documentation
README.md
Replaces local helper example with import from @nebula-agents/electron-mcp, updates Quickstart and "Recommended production guard" snippets, and clarifies env-var and packaging behavior.
Release metadata
.changeset/recommended-guards.md
New changeset declaring a minor release and noting the added recommendedGuards() helper.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title "Add recommended guards helper" clearly and concisely summarizes the main change—introduction of a new public helper function.
Description check ✅ Passed The PR description covers the main changes (helper addition, exports, README updates) and includes comprehensive verification checkmarks.
Linked Issues check ✅ Passed All requirements from issue #5 are met: recommendedGuards() is implemented and exported, behavior is defined (env var + isPackaged checks), and README is updated.
Out of Scope Changes check ✅ Passed All changes are within scope—new guards.ts, test coverage, exports, README updates, and changeset file all directly support the stated objective.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch t3code/add-recommended-guards-helper

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
src/index.test.ts (1)

175-183: Add one regression case for zero-argument invocation.

Once the helper accepts a default options object, include a recommendedGuards() test to lock in the intended custom error behavior for JavaScript callers.

✅ Suggested test addition
 describe("recommendedGuards", () => {
+  it("throws a packaged-state error when called without options", () => {
+    expect(() => recommendedGuards()).toThrow(/isPackaged/i);
+  });
+
   it("allows startup when the app is unpackaged and the opt-in env var is enabled", () => {
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/index.test.ts` around lines 175 - 183, Add a regression test that calls
recommendedGuards() with no arguments and asserts it throws the same
"isPackaged" error (e.g., expect(() =>
recommendedGuards()).toThrow(/isPackaged/i)) so JavaScript zero-argument callers
keep the intended custom error behavior; place the test alongside the existing
suite that already tests packaged-state cases and reference the
recommendedGuards function to locate where to add it.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@src/guards.ts`:
- Around line 10-15: The exported function recommendedGuards currently
destructures its parameter and will throw if called with no arguments; modify
its signature to accept a default empty object (e.g., recommendedGuards({ ... }:
RecommendedGuardsOptions = {})) so destructuring never receives undefined while
preserving the RecommendedGuardsOptions type and existing runtime behavior and
error handling inside the function.

---

Nitpick comments:
In `@src/index.test.ts`:
- Around line 175-183: Add a regression test that calls recommendedGuards() with
no arguments and asserts it throws the same "isPackaged" error (e.g., expect(()
=> recommendedGuards()).toThrow(/isPackaged/i)) so JavaScript zero-argument
callers keep the intended custom error behavior; place the test alongside the
existing suite that already tests packaged-state cases and reference the
recommendedGuards function to locate where to add it.
🪄 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: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: 4f95814c-d97d-495c-9ab4-a8a130f96bbf

📥 Commits

Reviewing files that changed from the base of the PR and between 115d110 and b4b078f.

📒 Files selected for processing (5)
  • README.md
  • src/guards.ts
  • src/index.test.ts
  • src/index.ts
  • src/types.ts

Comment thread src/guards.ts Outdated
Copy link
Copy Markdown

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
src/index.test.ts (1)

124-187: Add a precedence test for isPackaged vs app.isPackaged.

Line 124+ covers most behavior, but there’s no regression test proving explicit isPackaged overrides app.isPackaged when both are provided.

Proposed test addition
 describe("recommendedGuards", () => {
+  it("prefers explicit isPackaged over app.isPackaged when both are provided", () => {
+    expect(
+      recommendedGuards({
+        isPackaged: true,
+        app: { isPackaged: false },
+        env: { MY_APP_MCP: "1" },
+        envVar: "MY_APP_MCP",
+      }),
+    ).toBe(false);
+  });
+
   it("throws a packaged-state error when called without options", () => {
     expect(() => recommendedGuards()).toThrow(/isPackaged/i);
   });
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@src/index.test.ts` around lines 124 - 187, Add a unit test in the
"recommendedGuards" suite that asserts an explicit isPackaged option overrides
an app-like object's app.isPackaged: call recommendedGuards with both isPackaged
and app: { isPackaged: ... } provided (two variants if you want full coverage)
and assert the return matches the explicit isPackaged value (e.g., when
isPackaged: true but app.isPackaged: false expect false/startup blocked, and
vice versa), using the same expect(...).toBe(...) style as the surrounding tests
to prove precedence for the recommendedGuards function.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@src/index.test.ts`:
- Around line 124-187: Add a unit test in the "recommendedGuards" suite that
asserts an explicit isPackaged option overrides an app-like object's
app.isPackaged: call recommendedGuards with both isPackaged and app: {
isPackaged: ... } provided (two variants if you want full coverage) and assert
the return matches the explicit isPackaged value (e.g., when isPackaged: true
but app.isPackaged: false expect false/startup blocked, and vice versa), using
the same expect(...).toBe(...) style as the surrounding tests to prove
precedence for the recommendedGuards function.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: CHILL

Plan: Pro Plus

Run ID: d91c92a0-a0ea-4309-92ae-05e5ac7a6ebe

📥 Commits

Reviewing files that changed from the base of the PR and between 63067b3 and 884f455.

📒 Files selected for processing (2)
  • src/guards.ts
  • src/index.test.ts
✅ Files skipped from review due to trivial changes (1)
  • src/guards.ts

@jnsdls jnsdls merged commit e2095ff into main Apr 27, 2026
3 checks passed
@jnsdls jnsdls deleted the t3code/add-recommended-guards-helper branch April 27, 2026 07:59
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 recommendedGuards() helper + README snippet

1 participant