Skip to content

test: CLI error formatting + YAML frontmatter sanitization#423

Closed
anandgupta42 wants to merge 1 commit intomainfrom
test/hourly-20260323-2312
Closed

test: CLI error formatting + YAML frontmatter sanitization#423
anandgupta42 wants to merge 1 commit intomainfrom
test/hourly-20260323-2312

Conversation

@anandgupta42
Copy link
Contributor

@anandgupta42 anandgupta42 commented Mar 23, 2026

What does this PR do?

Adds 31 new tests across two completely untested areas that produce user-facing output. Both modules had zero test coverage prior to this PR.

1. FormatError + FormatUnknownErrorsrc/cli/error.ts (21 new tests)

These are the only functions that format error messages shown to CLI users. When a user hits a provider misconfiguration, broken config JSON, MCP server failure, or model-not-found error, these functions produce the human-readable message. Zero tests existed, meaning any refactoring of NamedError names or addition of new error types could silently break user-facing output.

New coverage includes:

  • All 8 known NamedError branches (MCPFailed, ProviderModelNotFoundError, ProviderInitError, ConfigJsonError, ConfigDirectoryTypoError, ConfigFrontmatterError, ConfigInvalidError, UICancelledError)
  • ModelNotFoundError with and without suggestions (different output paths)
  • InvalidError with issues list and with the special "config" path that suppresses location display
  • FormatUnknownError edge cases: Error with/without stack, plain objects, circular objects (unserializable fallback), primitives (string, number, undefined, null)
  • Unknown error types returning undefined (fallthrough to caller)

2. ConfigMarkdown.fallbackSanitizationsrc/config/markdown.ts (10 new tests, added to existing file)

This function is the safety net for users importing CLAUDE.md, .cursorrules, or other config files from other AI tools that contain messy YAML frontmatter (unquoted colons in URLs, model IDs like org/repo:tag). It converts problematic values to YAML block scalars so gray-matter can parse them. Previously only tested indirectly through parse() with fixture files — edge cases in the sanitizer itself were uncovered.

New coverage includes:

  • Colon-containing values converted to block scalars + verified parseable by gray-matter
  • Already-quoted values (double and single) preserved correctly
  • Empty values, YAML comments, and indented continuations left untouched
  • Multiple colon-containing values in the same frontmatter
  • No-frontmatter input returns unchanged
  • Body content after --- is never modified by the sanitizer

Type of change

  • New feature (non-breaking change which adds functionality)

Issue for this PR

N/A — proactive test coverage for zero-coverage user-facing code

How did you verify your code works?

bun test test/cli/error-format.test.ts    # 21 pass, 33 expect() calls
bun test test/config/markdown.test.ts     # 47 pass (37 existing + 10 new), 72 expect() calls

All tests validated by a critic agent before commit. Three error name mismatches (McpFailedMCPFailed, ModelNotFoundErrorProviderModelNotFoundError) were caught and fixed during review.

Checklist

  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes

https://claude.ai/code/session_01KYDdfMxVRzntwFi66wMnEr

Summary by CodeRabbit

  • Tests
    • Added comprehensive test coverage for CLI error formatting utilities and configuration markdown/YAML frontmatter handling.

…sts)

Cover two zero-test-coverage areas that produce user-facing output:
FormatError/FormatUnknownError (error messages) and
ConfigMarkdown.fallbackSanitization (messy YAML from other AI tools).

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>

https://claude.ai/code/session_01KYDdfMxVRzntwFi66wMnEr
Copy link

@claude claude bot left a comment

Choose a reason for hiding this comment

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

Claude Code Review

This repository is configured for manual code reviews. Comment @claude review to trigger a review.

Tip: disable this comment in your organization's Code Review settings.

@coderabbitai
Copy link

coderabbitai bot commented Mar 23, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro

Run ID: bb279905-ec6f-4df3-97e1-75ff8fd03a7b

📥 Commits

Reviewing files that changed from the base of the PR and between 528af75 and 808c12c.

📒 Files selected for processing (2)
  • packages/opencode/test/cli/error-format.test.ts
  • packages/opencode/test/config/markdown.test.ts

📝 Walkthrough

Walkthrough

Two new test suites were added to the opencode package: one for CLI error-formatting utilities covering multiple error types and fallback behavior, and another for Markdown/YAML frontmatter sanitization functionality.

Changes

Cohort / File(s) Summary
CLI Error Formatting Tests
packages/opencode/test/cli/error-format.test.ts
Tests for FormatError covering MCPFailed, ProviderModelNotFoundError, ProviderInitError, ConfigJsonError, ConfigDirectoryTypoError, ConfigFrontmatterError, ConfigInvalidError, and UICancelledError. Also includes FormatUnknownError tests for fallback formatting of various input types.
Markdown Sanitization Tests
packages/opencode/test/config/markdown.test.ts
Tests for ConfigMarkdown.fallbackSanitization verifying handling of YAML frontmatter with colons, quoted values, empty values, comments, lists, and content preservation in various scenarios.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Suggested labels

contributor

Poem

🐰 With tests so thorough, errors now sing,
From frontmatter colons to messages clean,
Each test case a safeguard, each assertion keen,
Coverage blooms like carrots in spring! 🥕✨

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the two main areas of test coverage added: CLI error formatting and YAML frontmatter sanitization, matching the changeset.
Description check ✅ Passed The description covers all required template sections: comprehensive summary of changes with rationale, detailed test plan with verification results, and completed checklist confirming tests added.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ 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 test/hourly-20260323-2312

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 and usage tips.

@anandgupta42
Copy link
Contributor Author

Superseded by #439 which consolidates all 12 test PRs into one, deduplicates overlapping tests, and fixes bugs found during review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants