← Back to PRs

#20496: test(utils): add comprehensive unit tests for utility functions

by masifislamm open 2026-02-19 01:09 View on GitHub →
size: S
## Summary Describe the problem and fix in 2–5 bullets: - Problem: Several core utility functions ([clamp](cci:1://file:///Users/xavitar/OpenSource/openclaw/src/utils.ts:31:0-33:1), [escapeRegExp](cci:1://file:///Users/xavitar/OpenSource/openclaw/src/utils.ts:38:0-43:1), [safeParseJson](cci:1://file:///Users/xavitar/OpenSource/openclaw/src/utils.ts:45:0-54:1), and UTF-16 string handlers) lacked unit test coverage. - Why it matters: Ensures reliability and prevents regressions, especially for edge cases like emoji handling (surrogate pairs) and invalid JSON. - What changed: Added comprehensive unit tests in [src/utils.test.ts](cci:7://file:///Users/xavitar/OpenSource/openclaw/src/utils.test.ts:0:0-0:0) covering [clamp](cci:1://file:///Users/xavitar/OpenSource/openclaw/src/utils.ts:31:0-33:1), [clampInt](cci:1://file:///Users/xavitar/OpenSource/openclaw/src/utils.ts:31:0-33:1), [escapeRegExp](cci:1://file:///Users/xavitar/OpenSource/openclaw/src/utils.ts:38:0-43:1), [safeParseJson](cci:1://file:///Users/xavitar/OpenSource/openclaw/src/utils.ts:45:0-54:1), [isPlainObject](cci:1://file:///Users/xavitar/OpenSource/openclaw/src/utils.ts:56:0-67:1), [isRecord](cci:1://file:///Users/xavitar/OpenSource/openclaw/src/utils.ts:69:0-75:1), [sliceUtf16Safe](cci:1://file:///Users/xavitar/OpenSource/openclaw/src/utils.ts:257:0-284:1), [truncateUtf16Safe](cci:1://file:///Users/xavitar/OpenSource/openclaw/src/utils.ts:286:0-292:1), and [formatTerminalLink](cci:1://file:///Users/xavitar/OpenSource/openclaw/src/utils.ts:383:0-397:1). - What did NOT change (scope boundary): No production code changes; only tests were added. ## Change Type (select all) - [ ] Bug fix - [ ] Feature - [ ] Refactor - [ ] Docs - [ ] Security hardening - [x] Chore/infra ## Scope (select all touched areas) - [ ] Gateway / orchestration - [ ] Skills / tool execution - [ ] Auth / tokens - [ ] Memory / storage - [ ] Integrations - [ ] API / contracts - [ ] UI / DX - [x] CI/CD / infra ## Linked Issue/PR - Closes # None ## User-visible / Behavior Changes None. ## Security Impact (required) - New permissions/capabilities? No - Secrets/tokens handling changed? No - New/changed network calls? No - Command/tool execution surface changed? No - Data access scope changed? No ## Repro + Verification ### Environment - OS: macOS - Runtime/container: pnpm workspace - Model/provider: N/A ### Steps 1. Checkout branch `test/utils-enhancement` 2. Run `pnpm install` 3. Run `pnpm test src/utils.test.ts` ### Expected - All 42 tests pass, specifically confirming correct behavior for: - UTF-16 surrogate slicing - Invalid JSON parsing (safe return) - Regex escaping - Type guards ### Actual - All 42 tests passed locally. ## Evidence ```bash > openclaw@2026.2.18 test /Users/xavitar/OpenSource/openclaw > node scripts/test-parallel.mjs src/utils.test.ts RUN v4.0.18 /Users/xavitar/OpenSource/openclaw ✓ src/utils.test.ts (42 tests) 11ms ... ✓ clamp (1) ✓ clampInt (1) ✓ escapeRegExp (2) ✓ safeParseJson (2) ✓ isPlainObject (2) ✓ isRecord (2) ✓ sliceUtf16Safe (2) ✓ truncateUtf16Safe (2) ✓ formatTerminalLink (2) Test Files 1 passed (1) Tests 42 passed (42) <!-- greptile_comment --> <h3>Greptile Summary</h3> Added comprehensive unit tests for utility functions including `clamp`, `clampInt`, `escapeRegExp`, `safeParseJson`, type guards, UTF-16 safe string operations, and terminal link formatting. Tests cover edge cases like emoji surrogate pair handling and invalid JSON parsing. <h3>Confidence Score: 5/5</h3> - This PR is safe to merge with no risk - Score reflects test-only changes with comprehensive coverage. Tests are well-structured, properly mock file system operations, use appropriate Vitest patterns, and verify edge cases. No production code changes. - No files require special attention <sub>Last reviewed commit: 22d699e</sub> <!-- greptile_other_comments_section --> <sub>(2/5) Greptile learns from your feedback when you react with thumbs up/down!</sub> <!-- /greptile_comment -->

Most Similar PRs