#20496: test(utils): add comprehensive unit tests for utility functions
size: S
Cluster:
Test Coverage Enhancements
## 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
#12684: Test: add missing unit tests for src/utils modules
by shaynhornik · 2026-02-09
85.5%
#4137: [AI-assisted] test(cli): add missing test cases for parseDurationMs
by sind00 · 2026-01-29
81.7%
#15569: test: comprehensive test coverage expansion (57 new test files)
by tangcruz · 2026-02-13
81.6%
#9333: Tests: add test coverage for security/audit-fs.ts
by M00N7682 · 2026-02-05
81.5%
#4095: Test/add format duration tests
by TechWizard9999 · 2026-01-29
78.5%
#7507: test(ci): make tests cross-platform (Windows) + add basic sanitizat...
by ThinkIbrokeIt · 2026-02-02
78.0%
#10367: CLI/Ops: resilient browser fill + failover hardening + operations t...
by cluster2600 · 2026-02-06
77.4%
#11421: Tests: add unit tests for infra/format-duration
by PythonUser42 · 2026-02-07
77.3%
#23139: test: fix flaky auth tests when OPENCLAW_GATEWAY_TOKEN is present
by Imccccc · 2026-02-22
77.0%
#21027: fix(tests): resolve TypeScript type errors in CLI test files
by kittipond2365 · 2026-02-19
77.0%