fix: detect missing root hash in render command #60

Merged
xiaomo merged 3 commits from fix/53-render-missing-hash-error into main 2026-05-31 09:15:36 +00:00
Owner

What

Added validation in render() and renderAsync() functions to detect when the root hash doesn't exist in the CAS store and throw CasNodeNotFoundError.

Why

Previously, json-cas render AAAAAAAAAAAAA with a non-existent hash would output cas:AAAAAAAAAAAAA and exit with code 0. This was incorrect behavior - only nested missing nodes should render as cas: references. The root hash should be validated and fail fast.

Changes

  1. Added CasNodeNotFoundError class (variable-store.ts)

    • Structured error with hash property for programmatic access
    • Consistent error message format: "CAS node not found: {hash}"
  2. Updated render() and renderAsync() (render.ts)

    • Added root hash existence check at function entry
    • Throws CasNodeNotFoundError if root hash missing
    • Preserves existing nested node behavior (cas: references)
  3. Updated CLI error handling (index.ts)

    • Catches CasNodeNotFoundError specifically
    • Exits with code 1 and clear error message
  4. Test coverage (11 new tests)

    • CLI E2E tests (render, render --pipe scenarios)
    • Core library unit tests (both sync and async render)
    • Edge cases (nested missing nodes, decay behavior)
    • Updated existing tests to expect new behavior

Testing

All 390 tests pass (100% pass rate)
Build clean: bun run build
Lint clean: bun run check

Ref

Fixes #53

🤖 Generated with Claude Code

## What Added validation in `render()` and `renderAsync()` functions to detect when the root hash doesn't exist in the CAS store and throw `CasNodeNotFoundError`. ## Why Previously, `json-cas render AAAAAAAAAAAAA` with a non-existent hash would output `cas:AAAAAAAAAAAAA` and exit with code 0. This was incorrect behavior - only nested missing nodes should render as `cas:` references. The root hash should be validated and fail fast. ## Changes 1. **Added `CasNodeNotFoundError` class** (`variable-store.ts`) - Structured error with hash property for programmatic access - Consistent error message format: "CAS node not found: {hash}" 2. **Updated `render()` and `renderAsync()`** (`render.ts`) - Added root hash existence check at function entry - Throws `CasNodeNotFoundError` if root hash missing - Preserves existing nested node behavior (cas: references) 3. **Updated CLI error handling** (`index.ts`) - Catches `CasNodeNotFoundError` specifically - Exits with code 1 and clear error message 4. **Test coverage** (11 new tests) - CLI E2E tests (render, render --pipe scenarios) - Core library unit tests (both sync and async render) - Edge cases (nested missing nodes, decay behavior) - Updated existing tests to expect new behavior ## Testing ✅ All 390 tests pass (100% pass rate) ✅ Build clean: `bun run build` ✅ Lint clean: `bun run check` ## Ref Fixes #53 🤖 Generated with [Claude Code](https://claude.com/claude-code)
xiaoju added 3 commits 2026-05-31 08:42:12 +00:00
When rendering a non-existent hash, the CLI now exits with code 1 and
displays an error message instead of silently outputting "cas:<hash>"
with exit code 0.

Changes:
- Updated CasNodeNotFoundError constructor signature to store hash
- Added root hash existence check in render() and renderAsync()
- Updated CLI error handling to catch CasNodeNotFoundError
- Added comprehensive test suite for missing hash error handling
- Updated existing incorrect tests (R2 and test 1.5)

The fix distinguishes between:
- Root hash (user-requested): Must exist or throw error
- Nested hash (during traversal): Renders as cas: reference (existing behavior)
- Resolution below epsilon: Renders as cas: reference (existing behavior)

Fixes #53

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Applied tester feedback to fix 5 test failures:

1. Updated error message format from "Node not found" to "CAS node not found"
   for consistency with existing tests in variable-store.test.ts and var.test.ts

2. Fixed CLI tests R9 and R10 to use bootstrap() directly instead of
   non-existent "types" command. Added imports for bootstrap and createFsStore.

3. Fixed render test 6.5 to pass actual schema Hash instead of entire
   bootstrap object (Record<string, Hash>)

4. Updated test expectations in render.test.ts (tests 1.5, 10.1, 10.2) to
   match new error message format

All 390 tests now pass. Core functionality verified:
- Missing root hash detection working correctly
- CLI exits with code 1 on missing hash
- Error message includes hash: "CAS node not found: <hash>"
- Nested nodes still render as cas: references (preserved behavior)
- Resolution decay behavior preserved

Fixes #53

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
xiaomo approved these changes 2026-05-31 09:15:19 +00:00
xiaomo left a comment
Owner

LGTM CasNodeNotFoundError 设计合理。

LGTM ✅ CasNodeNotFoundError 设计合理。
xiaomo merged commit d28003779e into main 2026-05-31 09:15:36 +00:00
xiaomo deleted branch fix/53-render-missing-hash-error 2026-05-31 09:15:36 +00:00
This repo is archived. You cannot comment on pull requests.
No Reviewers
No Label
2 Participants
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: uncaged/json-cas#60