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 줄.
135 lines
5.2 KiB
Rust
135 lines
5.2 KiB
Rust
//! Contract: drive the full pipeline (`kb-parse-md` → `kb-normalize` →
|
|
//! `kb-chunk`) on a real fixture and prove `DocumentStore` round-trips
|
|
//! the resulting `CanonicalDocument` + `Vec<Chunk>` losslessly.
|
|
//!
|
|
//! `kb-parse-md`, `kb-normalize`, `kb-chunk` are dev-deps only — see the
|
|
//! crate's `Cargo.toml`. The store crate's production tree (visible via
|
|
//! `cargo tree -p kb-store-sqlite --depth 1`) does NOT include them.
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use kebab_chunk::MdHeadingV1Chunker;
|
|
use kebab_core::{
|
|
AssetId, AssetStorage, Checksum, ChunkPolicy, ChunkerVersion, Chunker, DocumentStore,
|
|
MediaType, ParserVersion, RawAsset, SourceUri, WorkspacePath,
|
|
};
|
|
use kebab_parse_md::{BodyHints, build_canonical_document, parse_blocks, parse_frontmatter};
|
|
use kebab_store_sqlite::SqliteStore;
|
|
use time::OffsetDateTime;
|
|
|
|
mod common;
|
|
|
|
fn fixtures_dir() -> PathBuf {
|
|
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
.join("..")
|
|
.join("..")
|
|
.join("fixtures")
|
|
.join("markdown")
|
|
}
|
|
|
|
#[test]
|
|
fn document_and_chunks_round_trip_through_sqlite() {
|
|
let env = common::TestEnv::new();
|
|
let store = SqliteStore::open(&env.config()).unwrap();
|
|
store.run_migrations().unwrap();
|
|
|
|
// ── Build inputs from the fixture ───────────────────────────────
|
|
let dir = fixtures_dir();
|
|
let bytes = std::fs::read(dir.join("code-and-table.md")).expect("read fixture");
|
|
let cs = blake3::hash(&bytes).to_hex().to_string();
|
|
|
|
let asset = RawAsset {
|
|
asset_id: AssetId("a".repeat(32)),
|
|
source_uri: SourceUri::File(dir.join("code-and-table.md")),
|
|
workspace_path: WorkspacePath::new("notes/code-and-table.md".into()).unwrap(),
|
|
media_type: MediaType::Markdown,
|
|
byte_len: bytes.len() as u64,
|
|
checksum: Checksum(cs.clone()),
|
|
discovered_at: OffsetDateTime::from_unix_timestamp(1_700_000_000).unwrap(),
|
|
stored: AssetStorage::Reference {
|
|
path: dir.join("code-and-table.md"),
|
|
sha: Checksum(cs.clone()),
|
|
},
|
|
};
|
|
|
|
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 (mut metadata, _fm_span, _fm_warns) =
|
|
parse_frontmatter(&bytes, &hints).unwrap();
|
|
let (parsed_blocks, parse_warns) = parse_blocks(&bytes, 1).unwrap();
|
|
|
|
metadata.aliases.sort();
|
|
metadata.tags.sort();
|
|
let parser_version = ParserVersion("kb-store-sqlite-roundtrip".into());
|
|
let doc = build_canonical_document(
|
|
&asset,
|
|
metadata,
|
|
parsed_blocks,
|
|
&parser_version,
|
|
parse_warns,
|
|
)
|
|
.unwrap();
|
|
|
|
let policy = ChunkPolicy {
|
|
target_tokens: 200,
|
|
overlap_tokens: 40,
|
|
respect_markdown_headings: true,
|
|
chunker_version: ChunkerVersion("md-heading-v1".into()),
|
|
};
|
|
let chunks = MdHeadingV1Chunker.chunk(&doc, &policy).unwrap();
|
|
assert!(!chunks.is_empty(), "fixture must produce ≥1 chunk");
|
|
|
|
// ── Persist via the store ────────────────────────────────────────
|
|
store
|
|
.put_asset_with_bytes(&asset, &bytes)
|
|
.expect("put_asset_with_bytes");
|
|
store.put_document(&doc).expect("put_document");
|
|
store
|
|
.put_blocks(&doc.doc_id, &doc.blocks)
|
|
.expect("put_blocks");
|
|
store
|
|
.put_chunks(&doc.doc_id, &chunks)
|
|
.expect("put_chunks");
|
|
|
|
// ── Read back ────────────────────────────────────────────────────
|
|
let loaded = store
|
|
.get_document(&doc.doc_id)
|
|
.expect("get_document err")
|
|
.expect("get_document Some");
|
|
|
|
// Document-level fields must match. doc_version is bumped by the
|
|
// UPSERT path even on first put (the trigger runs on conflict
|
|
// only; first-insert lands the caller-supplied 1). updated_at is
|
|
// re-stamped server-side and is NOT round-tripped (the loaded
|
|
// CanonicalDocument carries `metadata.updated_at` from the
|
|
// metadata_json blob, which is the input value). So we compare
|
|
// the field-by-field copies that ARE deterministic:
|
|
assert_eq!(loaded.doc_id, doc.doc_id);
|
|
assert_eq!(loaded.workspace_path, doc.workspace_path);
|
|
assert_eq!(loaded.title, doc.title);
|
|
assert_eq!(loaded.lang, doc.lang);
|
|
assert_eq!(loaded.parser_version, doc.parser_version);
|
|
assert_eq!(loaded.schema_version, doc.schema_version);
|
|
assert_eq!(loaded.metadata, doc.metadata, "metadata round-trip");
|
|
assert_eq!(loaded.provenance, doc.provenance, "provenance round-trip");
|
|
assert_eq!(
|
|
loaded.blocks.len(),
|
|
doc.blocks.len(),
|
|
"block count round-trip"
|
|
);
|
|
assert_eq!(loaded.blocks, doc.blocks, "block stream round-trip");
|
|
|
|
// Chunks: get_chunk for each id.
|
|
for c in &chunks {
|
|
let back = store
|
|
.get_chunk(&c.chunk_id)
|
|
.expect("get_chunk err")
|
|
.expect("get_chunk Some");
|
|
assert_eq!(&back, c, "chunk round-trip mismatch for {}", c.chunk_id.0);
|
|
}
|
|
}
|