프로젝트 이름 `kb` → `kebab` rename 의 첫 단계. - workspace `Cargo.toml`: members `crates/kb-*` → `crates/kebab-*`, repository URL `altair823/kb` → `altair823/kebab`. - 18 crate 폴더 rename via `git mv` (history 보존). - 각 crate `Cargo.toml`: `name = "kb-*"` → `"kebab-*"`, path deps `../kb-*` → `../kebab-*`. - 모든 `.rs`: `kb_<id>` snake-case 모듈 path 18 개 (`kb_core`, `kb_config`, `kb_app`, `kb_cli`, `kb_eval`, `kb_search`, `kb_chunk`, `kb_normalize`, `kb_source_fs`, `kb_parse_md`, `kb_parse_types`, `kb_store_sqlite`, `kb_store_vector`, `kb_embed`, `kb_embed_local`, `kb_llm`, `kb_llm_local`, `kb_rag`) → `kebab_<id>` 일괄 sed (단어 경계 \\b 사용해 영어 문장 안의 "kb" 약어 미오염). CLI binary 이름 (`[[bin]] name = "kb"`), 환경변수 `KB_*`, XDG paths, tracing target, 그리고 docs sweep 은 다음 commit 에서. ## 검증 - `cargo check --workspace` clean — 모든 crate 빌드 통과 후 commit. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
161 lines
6.1 KiB
Rust
161 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_normalize::build_canonical_document;
|
|
use kebab_parse_md::{BodyHints, 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()
|
|
);
|
|
}
|
|
}
|