test(chat-filter): add coverage for ALLOWED_CHAT_IDS propagation#1289
test(chat-filter): add coverage for ALLOWED_CHAT_IDS propagation#1289
Conversation
#896) Extract chat ID parsing/filtering from telegram-bridge.js into a testable bin/lib/chat-filter.js module and add comprehensive tests: - Integration: env var survives nohup in start-services.sh - E2E: nohup child parses + filters using chat-filter module - Source assertion: telegram-bridge.js uses the module correctly - Unit: parsing edge cases (whitespace, trailing commas, single ID, unset) Also fixes a minor bug where trailing commas produced empty entries in the allowlist (now filtered via filter(Boolean)). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
📝 WalkthroughWalkthroughA new chat-filter module is added to parse and enforce ALLOWED_CHAT_IDS; the Telegram bridge now uses it instead of inline parsing. Tests are added to verify parsing, allowlist behavior, and that ALLOWED_CHAT_IDS is propagated into child processes started by start-services.sh. Changes
Sequence DiagramsequenceDiagram
participant User as User/Admin
participant StartServices as start-services.sh
participant ChildProcess as telegram-bridge.js (child)
participant ChatFilter as chat-filter module
participant TelegramAPI as Telegram API
participant IncomingMsg as Incoming Message
User->>StartServices: Set ALLOWED_CHAT_IDS env var
StartServices->>ChildProcess: Start child (nohup) with env vars
ChildProcess->>ChatFilter: parseAllowedChatIds(process.env.ALLOWED_CHAT_IDS)
ChatFilter-->>ChildProcess: parsed array or null
IncomingMsg->>TelegramAPI: User sends message
TelegramAPI->>ChildProcess: Deliver message with chatId
ChildProcess->>ChatFilter: isChatAllowed(ALLOWED_CHATS, chatId)
alt allowed (whitelist match or null)
ChatFilter-->>ChildProcess: true
ChildProcess->>ChildProcess: process message
else denied
ChatFilter-->>ChildProcess: false
ChildProcess->>ChildProcess: ignore/reject message
end
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@test/service-env.test.js`:
- Around line 191-193: The PATH in the test wrapper still includes system
directories (/usr/bin, /bin, /usr/local/bin) which exposes real
openshell/cloudflared; update the PATH construction used in both wrapper blocks
(the lines setting NODE_DIR and export PATH) to exclude those system directories
and only include the computed NODE_DIR plus the test fake-bridge/bin directory
(or explicitly prepend the fake-bridge path and NODE_DIR), ensuring both
occurrences (around the current diff and the second block at lines ~266-267) are
changed so the fake wrappers are isolated from real system binaries.
🪄 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
Run ID: 698edb18-81e9-4630-accb-9a80126b60d7
📒 Files selected for processing (3)
bin/lib/chat-filter.jsscripts/telegram-bridge.jstest/service-env.test.js
Stub openshell and disable cloudflared via sed in test wrappers so real system binaries don't interfere with test isolation. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
There was a problem hiding this comment.
♻️ Duplicate comments (1)
test/service-env.test.js (1)
157-160:⚠️ Potential issue | 🟠 MajorCoverage is still one layer short of the reported regression path.
Line 160 and Line 233 run patched
start-services.shdirectly; issue#896reproduces fromnemoclaw start. These tests can still pass if the outer CLI layer dropsALLOWED_CHAT_IDS. Please add one end-to-end assertion through the realnemoclaw startentrypoint (or its launcher module).Also applies to: 233-234
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@test/service-env.test.js` around lines 157 - 160, The tests for "ALLOWED_CHAT_IDS propagation (issue `#896`)" only invoke the patched start-services.sh directly and miss the outer CLI path; update the test in test/service-env.test.js to call the real nemoclaw start entrypoint (or require and invoke the launcher module used by the CLI) and assert end-to-end that ALLOWED_CHAT_IDS is present in the environment of the spawned nohup child (for example by spawning the CLI via child_process.exec/spawn or calling the launcher function, then inspecting the child process env or the nohup output file for the variable). Keep the existing direct-start-services.sh tests, but add this single end-to-end assertion using the CLI entrypoint name "nemoclaw start" (or the launcher module) and the same ALLOWED_CHAT_IDS value used elsewhere in the suite.
🧹 Nitpick comments (1)
test/service-env.test.js (1)
224-230: Source-string assertions here are brittle to harmless refactors.These checks are tightly coupled to exact formatting/import text. Consider regex/semantic assertions (e.g., behavior-level checks only) so equivalent refactors don’t cause false failures.
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@test/service-env.test.js` around lines 224 - 230, The assertions in test/service-env.test.js are brittle because they assert exact source strings; update the checks that reference bridgeSrc to use regex or behavior-level assertions instead of exact text: replace expect(bridgeSrc).toContain('require("../bin/lib/chat-filter")') and the exact parseAllowedChatIds/process.env and isChatAllowed/ALLOWED_CHATS checks with regex matches (e.g., match parseAllowedChatIds\s*\(\s*process\.env\.ALLOWED_CHAT_IDS\s*\) and isChatAllowed\s*\(\s*ALLOWED_CHATS\s*,\s*chatId\s*\)) and replace the inline-split negative assertion with a regex that ignores whitespace/quote variations (e.g., ensure no /\.split\(\s*['"].+?['"]\s*\)\.map\(/). Alternatively, convert these source-string checks into behavior-level assertions by importing/execing the module and asserting parseAllowedChatIds and isChatAllowed are called/used correctly when given ALLOWED_CHAT_IDS.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Duplicate comments:
In `@test/service-env.test.js`:
- Around line 157-160: The tests for "ALLOWED_CHAT_IDS propagation (issue `#896`)"
only invoke the patched start-services.sh directly and miss the outer CLI path;
update the test in test/service-env.test.js to call the real nemoclaw start
entrypoint (or require and invoke the launcher module used by the CLI) and
assert end-to-end that ALLOWED_CHAT_IDS is present in the environment of the
spawned nohup child (for example by spawning the CLI via
child_process.exec/spawn or calling the launcher function, then inspecting the
child process env or the nohup output file for the variable). Keep the existing
direct-start-services.sh tests, but add this single end-to-end assertion using
the CLI entrypoint name "nemoclaw start" (or the launcher module) and the same
ALLOWED_CHAT_IDS value used elsewhere in the suite.
---
Nitpick comments:
In `@test/service-env.test.js`:
- Around line 224-230: The assertions in test/service-env.test.js are brittle
because they assert exact source strings; update the checks that reference
bridgeSrc to use regex or behavior-level assertions instead of exact text:
replace expect(bridgeSrc).toContain('require("../bin/lib/chat-filter")') and the
exact parseAllowedChatIds/process.env and isChatAllowed/ALLOWED_CHATS checks
with regex matches (e.g., match
parseAllowedChatIds\s*\(\s*process\.env\.ALLOWED_CHAT_IDS\s*\) and
isChatAllowed\s*\(\s*ALLOWED_CHATS\s*,\s*chatId\s*\)) and replace the
inline-split negative assertion with a regex that ignores whitespace/quote
variations (e.g., ensure no /\.split\(\s*['"].+?['"]\s*\)\.map\(/).
Alternatively, convert these source-string checks into behavior-level assertions
by importing/execing the module and asserting parseAllowedChatIds and
isChatAllowed are called/used correctly when given ALLOWED_CHAT_IDS.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: ab462a35-970a-4a11-bc37-e7b0a2bb7604
📒 Files selected for processing (1)
test/service-env.test.js
…DIA#1289) ## Summary - Extracts chat ID parsing/filtering from `telegram-bridge.js` into a testable `bin/lib/chat-filter.js` module - Adds 8 tests covering the full `ALLOWED_CHAT_IDS` path: shell env → nohup → node process → parsing → filtering - Fixes minor bug where trailing commas (e.g. `"111,,222,"`) produced empty entries in the allowlist Fixes NVIDIA#896 ## Test plan - [x] `npm test` — 32/32 passing - [x] All pre-commit hooks pass - [x] Integration test: env var survives `nohup` in `start-services.sh` - [x] E2E test: nohup child parses + filters using `chat-filter` module - [x] Source assertion: `telegram-bridge.js` uses the module with correct env var name - [x] Unit tests: whitespace trimming, single ID, trailing commas, empty/undefined input 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added configurable chat-ID allowlist for the Telegram bridge, allowing admins to restrict access via an environment variable; parsing handles commas, whitespace, and empty entries, and unset values disable filtering. * **Tests** * Added unit and end-to-end tests verifying environment propagation, parsing behavior, trimming/empty-entry handling, and allow/deny enforcement. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
…DIA#1289) ## Summary - Extracts chat ID parsing/filtering from `telegram-bridge.js` into a testable `bin/lib/chat-filter.js` module - Adds 8 tests covering the full `ALLOWED_CHAT_IDS` path: shell env → nohup → node process → parsing → filtering - Fixes minor bug where trailing commas (e.g. `"111,,222,"`) produced empty entries in the allowlist Fixes NVIDIA#896 ## Test plan - [x] `npm test` — 32/32 passing - [x] All pre-commit hooks pass - [x] Integration test: env var survives `nohup` in `start-services.sh` - [x] E2E test: nohup child parses + filters using `chat-filter` module - [x] Source assertion: `telegram-bridge.js` uses the module with correct env var name - [x] Unit tests: whitespace trimming, single ID, trailing commas, empty/undefined input 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **New Features** * Added configurable chat-ID allowlist for the Telegram bridge, allowing admins to restrict access via an environment variable; parsing handles commas, whitespace, and empty entries, and unset values disable filtering. * **Tests** * Added unit and end-to-end tests verifying environment propagation, parsing behavior, trimming/empty-entry handling, and allow/deny enforcement. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --------- Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Summary
telegram-bridge.jsinto a testablebin/lib/chat-filter.jsmoduleALLOWED_CHAT_IDSpath: shell env → nohup → node process → parsing → filtering"111,,222,") produced empty entries in the allowlistFixes #896
Test plan
npm test— 32/32 passingnohupinstart-services.shchat-filtermoduletelegram-bridge.jsuses the module with correct env var name🤖 Generated with Claude Code
Summary by CodeRabbit
New Features
Tests