# Validation Scope and Source Accounting

## Verification Matrix

1. **Multi-target unit tests (`moon test --deny-warn`)**
   - Covered targets: `wasm`, `wasm-gc`, `js`, `native`.
   - `decoder_test.mbt`: binary `.pb` and gzip `.pb.gz` round-trip encoding/decoding, empty input rejection, corrupted gzip stream rejection, truncated varint rejection, and `profile.proto` invariant checks (`string_table[0] == ""`, sample value arity).
   - `analysis_test.mbt`: leaf-first inlined frame expansion (`Location.lines`), recursive stack deduplication for cumulative (`cum`) attribution, explicit vs default `sample_type` selection, and deterministic Brendan Gregg folded stack collapse.
   - `diff_test.mbt`: absolute vs duration-normalized (`--normalize`) comparisons, `regressed`/`improved`/`added`/`removed`/`unchanged` classifications, and CI gate policy violations returning exit code `1`.
   - `flamegraph_test.mbt`: single-profile SVG, Brendan Gregg red/blue differential SVG legend & delta metadata, self-contained interactive HTML (search + zoom controls), and GitHub-flavored Markdown reports.
   - `merge_test.mbt`: multi-profile string-table remapping, function/location deduplication, coalesced sample totals, `Sample.label` slicing & grouping, and `focus`/`ignore`/`hide` filters.
   - `fuzz_test.mbt`: 256 deterministic pseudo-random malformed inputs (including gzip-prefixed garbage) and all truncated prefixes of valid `.pb` and `.pb.gz` streams verifying panic-free error handling.

2. **Official `go tool pprof` oracle cross-check (`demo/verify_oracle.py`)**
   - Real `.pb.gz` CPU and Heap profiles generated by `demo/gen_go_profiles.go` using Go standard library `runtime/pprof`.
   - Asserts exact integer equality (0 error) against `go tool pprof -top` on CPU (`180,000,000 ns` & `490,000,000 ns`, 6 functions each), Heap `alloc_space` (`3,247,944 B` / 22 functions & `6,543,480 B` / 25 functions), and `moon-pprof merge` against `go tool pprof -proto` (`670,000,000 ns`, 6 functions).
   - Verifies end-to-end CLI `diff --hide runtime.` regression gating (exit code `1`), differential SVG generation, interactive HTML generation, and Markdown report generation.

3. **Independent consumer check**
   - CI creates a separate temporary MoonBit module importing `Sam-Hui-dot/moon-pprof` and executes `encode_profile` -> `decode_profile` -> `compute_top` -> `render_flamegraph_svg` on both `js` and `wasm` targets.

## Known Boundaries

- Supports Google `perftools.profiles.Profile` (`profile.proto`) binary `.pb` and RFC 1952 `.pb.gz` streams (unknown protobuf fields are skipped safely).
- Does not disassemble raw binaries or symbolize un-symbolized instruction addresses from external DWARF/ELF files; locations without `Line` metadata are rendered by hexadecimal address (`0x...`).
- Filtering currently uses deterministic substring matching on function name or filename rather than full RE2 regular expressions.

## Source Accounting

Non-test MoonBit source lines are counted across root library files (`*.mbt` excluding `*_test.mbt`) and CLI files (`cmd/main/*.mbt`), excluding blank lines, `//` comment lines, generated `.mbti` files, and `demo/` scripts.
