Repository navigation
test(parser): add shared golden-doc + alignment helpers - #18098
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughThe parser test helpers now load legacy and metadata-bearing golden documents. They expose accepted-divergence and document-type filtering. They also add text/code and HTML alignment presets with delimiter, heading-marker, tag, and whitespace normalization. ChangesGolden alignment helpers
Estimated code review effort: 3 (Moderate) | ~20 minutes Possibly related PRs
Suggested labels: Suggested reviewers: Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/parser/parser/align_test.go`:
- Around line 200-215: Update gen_markdown_golden.py and
markdown.python.golden.json to generate and consume the {meta, items} document
format, then simplify parseGolden to unmarshal only GoldenDoc and remove its
legacy bare-array fallback. Leave unified_html_cases.json unchanged as a shared
array fixture, and retain array compatibility only if it represents a documented
external contract.
🪄 Autofix
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: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: 0d02f56f-ac6a-49b3-b338-2f010f1c4118
📒 Files selected for processing (1)
internal/parser/parser/align_test.go
| // parseGolden unmarshals a golden file that may be either a bare JSON array of | ||
| // items (legacy format) or a {meta, items} document (current format). It | ||
| // returns the full document either way. Tolerant parsing keeps older | ||
| // callers/tests working after the format gained a meta block. | ||
| func parseGolden(t *testing.T, data []byte) *GoldenDoc { | ||
| t.Helper() | ||
| var doc GoldenDoc | ||
| if err := json.Unmarshal(data, &doc); err == nil && doc.Items != nil { | ||
| return &doc | ||
| } | ||
| // Legacy flat-array format: treat the whole file as the items list. | ||
| var items []map[string]any | ||
| if err := json.Unmarshal(data, &items); err != nil { | ||
| t.Fatalf("parse golden: %v", err) | ||
| } | ||
| return &GoldenDoc{Items: items} |
There was a problem hiding this comment.
📐 Maintainability & Code Quality | 🟠 Major | ⚡ Quick win
🧩 Analysis chain
🏁 Script executed:
#!/bin/bash
set -euo pipefail
# Find alignment fixtures that still use a top-level JSON array.
fd -e json . internal/parser/parser/testdata | while IFS= read -r file; do
python3 - "$file" <<'PY'
import json
import sys
path = sys.argv[1]
try:
with open(path, encoding="utf-8") as f:
value = json.load(f)
except (OSError, json.JSONDecodeError):
raise SystemExit(0)
if isinstance(value, list):
print(path)
PY
done
# Inspect remaining loader call sites before removing compatibility.
rg -n -C 3 --glob '*.go' '\bLoadGolden(Doc)?\s*\(' internal/parserRepository: infiniflow/ragflow
Length of output: 2123
🏁 Script executed:
#!/bin/bash
set -euo pipefail
python3 - <<'PY'
import json
from pathlib import Path
for path in [
Path("internal/parser/parser/testdata/markdown.python.golden.json"),
Path("internal/parser/parser/testdata/unified_html_cases.json"),
]:
value = json.loads(path.read_text(encoding="utf-8"))
kind = "array" if isinstance(value, list) else type(value).__name__
print(f"{path}: {kind}, length/keys={len(value)}")
if isinstance(value, list) and value:
print(f" first item keys: {sorted(value[0])}")
elif isinstance(value, dict):
print(f" keys: {sorted(value)}")
PY
sed -n '180,270p' internal/parser/parser/align_test.go
rg -n -C 4 --glob '*.go' '\b(parseGolden|LoadGolden|LoadGoldenDoc)\s*\(' internal/parser
rg -n -i -C 2 'golden|accepted_divergences|meta.*items|items.*meta' --glob '*.md' --glob '*.go' --glob '*.json' internal/parser README.md 2>/dev/null | head -250Repository: infiniflow/ragflow
Length of output: 16156
🏁 Script executed:
#!/bin/bash
set -euo pipefail
rg -n -C 5 'markdown\.python\.golden\.json|unified_html_cases\.json|json\.(load|dump)|accepted_divergences' \
internal/parser/parser/testdata internal/parser/parser --glob '*.py' --glob '*.go' --glob '*.md'
sed -n '1,240p' internal/parser/parser/testdata/gen_markdown_golden.py
sed -n '1,80p' internal/parser/parser/testdata/markdown.python.golden.json
sed -n '1,60p' internal/parser/parser/testdata/unified_html_cases.jsonRepository: infiniflow/ragflow
Length of output: 16898
Converge the alignment golden format.
markdown.python.golden.json is still generated and consumed as a bare array. Update gen_markdown_golden.py and the fixture to use {meta, items}, then remove the array branch from parseGolden. Keep unified_html_cases.json unchanged because Go and Python share it as an array fixture. Retain bare-array support only for a documented external contract.
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@internal/parser/parser/align_test.go` around lines 200 - 215, Update
gen_markdown_golden.py and markdown.python.golden.json to generate and consume
the {meta, items} document format, then simplify parseGolden to unmarshal only
GoldenDoc and remove its legacy bare-array fallback. Leave
unified_html_cases.json unchanged as a shared array fixture, and retain array
compatibility only if it represents a documented external contract.
Source: Coding guidelines
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@internal/parser/parser/align_test.go`:
- Around line 429-433: Update the “corrupt” case in the relevant parser test so
it does not invoke parseGolden through a subtest expecting failure. Prefer
exposing or reusing a parsing-core path that returns an error and assert that
error directly; otherwise isolate the t.Fatalf contract in a subprocess while
keeping the parent test successful.
🪄 Autofix
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: Organization UI
Review profile: CHILL
Plan: Pro Plus
Run ID: df5f3f64-b345-4f58-bf1a-c95842d88981
📒 Files selected for processing (1)
internal/parser/parser/align_test.go
Extract the alignment scaffolding so the format-specific PRs (text&code,
markdown golden, HTML) don't conflict on align_test.go.
Adds (test-only, no production behavior change):
- GoldenDoc{Meta,Items} and parseGolden: tolerant of both the legacy bare
array and the new {meta, items} golden format.
- LoadGoldenDoc, AcceptedDivergences(meta), FilterOutDocTypes(items, drop):
meta-driven divergence handling, no hardcoded divergence lists.
- TextCodeAlignOptions / DefaultTextCodeDelimiter, HTMLAlignOptions /
StripHTMLHeadingMarker: normalizer presets reused by the alignment tests.
- LoadGolden now tolerates the {meta, items} format.
Stacks on #18014 (the align_test.go framework is already in main).
DefaultTextCodeDelimiter and DefaultMarkdownDelimiter are shared by both the production parsers and the alignment tests. They previously lived in align_test.go (a _test.go file), so production text_parser.go had to keep a private duplicate that could drift silently. Move them to a non-test delimiter.go as the single source of truth.
f2e724d to
1e39d24
Compare
parseGolden now returns (*GoldenDoc, error) instead of calling t.Fatalf internally on a malformed golden. Callers (LoadGolden/LoadGoldenDoc and TestParseGolden) handle the error. The corrupt subtest asserts err != nil directly, so the intended loud failure no longer propagates to and fails TestParseGolden itself.
|
LGTM |
Summary
Centralize the shared golden-doc + alignment helpers in
align_test.goso theformat-specific PRs (text&code, markdown golden, HTML) reuse one implementation
instead of each carrying their own copy of the scaffolding.
Adds (no production behavior change — consts/helpers only):
delimiter.go(non-test):DefaultTextCodeDelimiterandDefaultMarkdownDelimiter, the single source of truth for the flow parser'sdefault delimiter sets. Production parsers and the alignment tests both
reference these, so there is no duplicated hard-coded copy that could drift.
GoldenDoc{Meta,Items}andparseGolden: tolerant of both the legacy barearray and the new
{meta, items}golden format.LoadGoldenDoc,AcceptedDivergences(meta),FilterOutDocTypes(items, drop):meta-driven divergence handling, no hardcoded divergence lists.
TextCodeAlignOptions,HTMLAlignOptions/StripHTMLHeadingMarker:normalizer presets reused by the alignment tests.
LoadGoldennow tolerates the{meta, items}format.TestParseGolden: unit test locking the two tolerated golden formats (legacyarray,
{meta, items}) and the corrupt-input failure path, so future changesto the golden format cannot silently break parsing.
Dependency
The
align_test.goframework (CompareAlignment, Normalizer, LoadGolden) comesfrom #18014, which is already in
main. This PR only layers the shared helpersand delimiter consts on top. Merge this first; PR-2/3/4 branch from it.