design §3.7b 의 thin layer (ParsedBlock 류) 가 4 parser 중 1개 (markdown) 만 lift 를 경유하는 현실 — fan-in/fan-out 모두 1 → layer 의미 잃음. kebab-normalize (1097 LOC) + kebab-parse-types (98 LOC) 둘을 kebab-parse-md 로 흡수. 설계: docs/superpowers/specs/2026-05-26-normalize-absorption-spec.md 플랜: docs/superpowers/plans/2026-05-26-normalize-absorption-plan.md HOTFIXES: tasks/HOTFIXES.md 의 2026-05-26 entry (design deviation) - 5 사용 type + 3 forward-declared struct → kebab-parse-md::types module 의 pub explicit re-export. - build_canonical_document + derive_title + warning_agent → kebab-parse-md::normalize module. - 4 hard-coded agent literal (lib.rs:122/128/134/153) + warning_agent body return + tracing target literal 모두 보존 — stage label 일관성. - kebab-app callsite (lib.rs:51 use + :1119 context string) + Cargo.toml 의 2 dep (regular + dead) 제거. - kebab-chunk + kebab-store-sqlite 의 [dev-dependencies] kebab-normalize → 제거 (kebab-parse-md 로 갈음). 통합 test source 의 use shift. - test file 이동 (kebab-normalize/tests/normalize_snapshot.rs → kebab-parse-md/tests/). - workspace Cargo.toml: Hunk (a) members 2 entry 삭제 + Hunk (b) version 0.18.0 → 0.19.0 (frozen contract 변경). - design §3.7b 4-단락 재작성 (원래 intent 보존 + 현재 상태 + 보존된 surface + future re-extraction trigger). - design §8 graph 갱신 (3 edge 제거 + 2 forbidden bullet 의미 갱신 + commentary). - ARCHITECTURE.md crate graph + directory tree mechanical 갱신. - tasks/INDEX.md L169 closure mention + "Future work / deferred" 섹션 신설 (image/pdf normalize integration entry). - tasks/HOTFIXES.md 신규 entry (4-block — design deviation Symptom). - HANDOFF.md cross-link 한 줄. - 3 dead struct (ParsedImageRegion / ParsedPdfPage / ParsedAudioSegment) 는 보존 — v0.20+ image/pdf normalize integration 의 future surface (spec §11). Wire / surface impact: 0건. CLI / TUI / MCP / --json 출력 / config / XDG path / parser_version 모두 unchanged. wire-invisible provenance.events[].agent + tracing target literal "kb-normalize" 도 보존 — old DB row 와 new DB row 의 audit log 일관성. Verification: cargo test --workspace --no-fail-fast -j 1 → 1313 passed / 0 failed (172 result blocks). cargo clippy --workspace --all-targets -j 1 -- -D warnings → 0 warning (5m 46s). cargo metadata --no-deps --format-version 1 | jq '.workspace_members | length' = 22. cargo tree -p kebab-app --depth 2 | grep -E "kebab_(parse_types|normalize)" = 0 줄.
160 lines
6.1 KiB
Rust
160 lines
6.1 KiB
Rust
//! Snapshot test pinning the full `CanonicalDocument` JSON for the
|
|
//! `code-and-table.md` fixture.
|
|
//!
|
|
//! This is an integration test (it lives under `tests/`) and depends on
|
|
//! `kb-parse-md` only as a dev-dep so the production crate's regular
|
|
//! deps still satisfy the §8 boundary (`cargo tree -p kb-normalize
|
|
//! --depth 1` without `-e dev` does not list any parser implementation).
|
|
//!
|
|
//! Non-deterministic fields are stripped before comparison:
|
|
//!
|
|
//! * `provenance.events[*].at` — each invocation calls `now_utc()` for
|
|
//! the Parsed/Normalized/Warning events. The Discovered event uses
|
|
//! the asset's pinned `discovered_at`, so we keep that one and replace
|
|
//! only indices ≥ 1.
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use kebab_core::{
|
|
AssetId, AssetStorage, Checksum, MediaType, ParserVersion, RawAsset, SourceUri,
|
|
WorkspacePath,
|
|
};
|
|
use kebab_parse_md::{BodyHints, build_canonical_document, parse_blocks, parse_frontmatter};
|
|
use serde_json::Value;
|
|
use time::OffsetDateTime;
|
|
|
|
fn fixtures_dir() -> PathBuf {
|
|
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
.join("..")
|
|
.join("..")
|
|
.join("fixtures")
|
|
.join("markdown")
|
|
}
|
|
|
|
fn fixed_asset(workspace_path: &str) -> RawAsset {
|
|
let wp = WorkspacePath::new(workspace_path.into()).unwrap();
|
|
RawAsset {
|
|
asset_id: AssetId("a".repeat(32)),
|
|
source_uri: SourceUri::File(PathBuf::from("/tmp/code-and-table.md")),
|
|
workspace_path: wp,
|
|
media_type: MediaType::Markdown,
|
|
byte_len: 0,
|
|
checksum: Checksum("0".repeat(64)),
|
|
// Pin discovered_at so the Discovered provenance event is
|
|
// deterministic across runs.
|
|
discovered_at: OffsetDateTime::from_unix_timestamp(1_700_000_000).unwrap(),
|
|
stored: AssetStorage::Reference {
|
|
path: PathBuf::from("/tmp/code-and-table.md"),
|
|
sha: Checksum("0".repeat(64)),
|
|
},
|
|
}
|
|
}
|
|
|
|
fn strip_dynamic(mut v: Value) -> Value {
|
|
if let Some(events) = v
|
|
.get_mut("provenance")
|
|
.and_then(|p| p.get_mut("events"))
|
|
.and_then(|e| e.as_array_mut())
|
|
{
|
|
for (i, ev) in events.iter_mut().enumerate() {
|
|
if i > 0
|
|
&& let Some(obj) = ev.as_object_mut()
|
|
{
|
|
obj.insert("at".into(), Value::String("<stripped>".into()));
|
|
}
|
|
}
|
|
}
|
|
v
|
|
}
|
|
|
|
#[test]
|
|
fn code_and_table_canonical_snapshot() {
|
|
let dir = fixtures_dir();
|
|
let bytes = std::fs::read(dir.join("code-and-table.md")).expect("fixture readable");
|
|
|
|
// Frontmatter parse — code-and-table.md has none, so we provide
|
|
// BodyHints with deterministic timestamps so the lifted Metadata
|
|
// is reproducible. The body offset is 1 (no frontmatter prefix).
|
|
//
|
|
// We pin `first_h1` so the BodyHints → user.title → CanonicalDocument.title
|
|
// lift chain is exercised end-to-end (see `assert_eq!` on
|
|
// `doc.title` below). Without this, `code-and-table.md`'s lack of
|
|
// frontmatter title would leave `title == ""` and the chain would
|
|
// be uncovered by the snapshot.
|
|
let asset = fixed_asset("notes/code-and-table.md");
|
|
let hints = BodyHints {
|
|
first_h1: Some("Code And Table".into()),
|
|
fs_ctime: asset.discovered_at,
|
|
fs_mtime: asset.discovered_at,
|
|
fallback_lang: Some("en".into()),
|
|
};
|
|
let (metadata, fm_span, _fm_warns) =
|
|
parse_frontmatter(&bytes, &hints).expect("frontmatter parses");
|
|
|
|
// No frontmatter → body starts at line 1. With frontmatter, line
|
|
// count of the prelude is computed from the byte span; this fixture
|
|
// has none, so the constant 1 is fine.
|
|
let body_offset_lines: u32 = match fm_span {
|
|
// Defensive: count the newlines in the prelude. The fixture
|
|
// hits the `None` branch so this code path is not exercised
|
|
// by the test, but kept for completeness.
|
|
Some(span) => bytes[..span.end].iter().filter(|b| **b == b'\n').count() as u32 + 1,
|
|
None => 1,
|
|
};
|
|
let (blocks, parse_warns) =
|
|
parse_blocks(&bytes, body_offset_lines).expect("blocks parse");
|
|
|
|
let parser_version = ParserVersion("kb-normalize-snapshot-test-0".into());
|
|
let mut metadata = metadata;
|
|
// The `created_at` / `updated_at` lifted from BodyHints are pinned
|
|
// to `discovered_at` above, so they are already deterministic.
|
|
metadata.aliases.sort();
|
|
metadata.tags.sort();
|
|
|
|
let doc = build_canonical_document(
|
|
&asset,
|
|
metadata,
|
|
blocks,
|
|
&parser_version,
|
|
parse_warns,
|
|
)
|
|
.expect("build_canonical_document");
|
|
|
|
// Assert the BodyHints → first_h1 → user.title → CanonicalDocument.title
|
|
// lift chain end-to-end. Pinned in the snapshot too, but the explicit
|
|
// assertion makes a future drift fail with a clearer message.
|
|
assert_eq!(doc.title, "Code And Table");
|
|
|
|
let actual = strip_dynamic(serde_json::to_value(&doc).unwrap());
|
|
|
|
let baseline_path = dir.join("code-and-table.canonical.snapshot.json");
|
|
let baseline_text = match std::fs::read_to_string(&baseline_path) {
|
|
Ok(s) => s,
|
|
Err(_) if std::env::var("UPDATE_SNAPSHOTS").is_ok() => {
|
|
let pretty = serde_json::to_string_pretty(&actual).unwrap();
|
|
std::fs::write(&baseline_path, format!("{pretty}\n")).unwrap();
|
|
return;
|
|
}
|
|
Err(e) => panic!(
|
|
"missing baseline {}; run with UPDATE_SNAPSHOTS=1 to create: {e}",
|
|
baseline_path.display()
|
|
),
|
|
};
|
|
let expected: Value =
|
|
serde_json::from_str(&baseline_text).expect("baseline parses as json");
|
|
|
|
if actual != expected {
|
|
if std::env::var("UPDATE_SNAPSHOTS").is_ok() {
|
|
let pretty = serde_json::to_string_pretty(&actual).unwrap();
|
|
std::fs::write(&baseline_path, format!("{pretty}\n")).unwrap();
|
|
eprintln!("updated baseline {}", baseline_path.display());
|
|
return;
|
|
}
|
|
let pretty = serde_json::to_string_pretty(&actual).unwrap();
|
|
panic!(
|
|
"canonical snapshot drift\n--- expected ({}) ---\n{baseline_text}\n--- actual ---\n{pretty}\nIf intentional, re-run with UPDATE_SNAPSHOTS=1.",
|
|
baseline_path.display()
|
|
);
|
|
}
|
|
}
|