diff --git a/pkgs/id/crates/id-import-notion/src/content.rs b/pkgs/id/crates/id-import-notion/src/content.rs index d9bbd9c725..11b0e5d9d8 100644 --- a/pkgs/id/crates/id-import-notion/src/content.rs +++ b/pkgs/id/crates/id-import-notion/src/content.rs @@ -14,8 +14,8 @@ //! unsupported classes are preserved verbatim with their children, so no //! content is lost and no block identity changes when slice 4 upgrades //! their conversion. Database notes and relation claims are slice 3 -//! (#13523); a `child_database` block already emits its reference here, -//! with the database's note ID left for slice 3 to resolve. +//! (#13523); a `child_database` block emits its reference here, with +//! the database's note ID resolved from slice 3's identity pass. use std::collections::{BTreeMap, BTreeSet}; @@ -119,8 +119,9 @@ pub struct TypedReference { /// Notion UUID of the target page or database. pub target_id: String, /// Destination note ID when the target's identity is assigned. Pages - /// are assigned in the identity pass; database note IDs are slice 3, - /// so a child-database reference carries `None` here until then. + /// and databases are assigned in the identity passes; a + /// child-database reference carries `None` only when its database + /// is outside the import set. #[serde(default, skip_serializing_if = "Option::is_none")] pub target_note_id: Option, /// Block selector for block links (§3): the revision-pin slot is @@ -598,10 +599,31 @@ impl Converter<'_> { .get("title") .and_then(Value::as_str) .map(str::to_owned); + // The block's ID is the database's ID (the slice 1 + // inventory rule); slice 3's identity pass assigns + // its note ID. A database in the inventory without + // an assigned note ID means the pass did not run — + // fail closed, as for child pages. A database + // outside the import set keeps `None`: the reference + // still records the target ID. + let target_note_id = self.manifest.database_id_to_note_id.get(&block_id).cloned(); + if target_note_id.is_none() + && self + .manifest + .inventory + .objects + .get(&block_id) + .is_some_and(|object| object.kind == "database") + { + bail!( + "child database {block_id} has no assigned note id; \ + run assign_database_identities first" + ); + } self.references.push(TypedReference { from_block_id: block_id.clone(), target_id: block_id.clone(), - target_note_id: None, + target_note_id, target_block_id: None, surface: ReferenceSurface::ChildDatabase, }); diff --git a/pkgs/id/crates/id-import-notion/src/databases.rs b/pkgs/id/crates/id-import-notion/src/databases.rs index c1bfeeacc3..9571c56e62 100644 --- a/pkgs/id/crates/id-import-notion/src/databases.rs +++ b/pkgs/id/crates/id-import-notion/src/databases.rs @@ -5,12 +5,11 @@ //! on the database note, scoped by `data_source_id` so two sources' claims //! are never merged — even where property names (or property ids) collide. //! -//! This slice covers database-note identity and schema claims only. -//! Row notes, relations as typed references, rollups/formulas, and system -//! properties are follow-up slices on the same issue; they build on the -//! identities assigned here. Resolving `child_database` references to -//! their database note ids (left as `None` by slice 2's [`crate::content`]) -//! is the next slice after this one. +//! This module covers database-note identity and schema claims. Row +//! notes, relations as typed references, rollups/formulas, and system +//! properties live in [`crate::rows`], building on the identities +//! assigned here; `child_database` references resolve from the same +//! identity map in [`crate::content`]. use std::collections::BTreeSet; diff --git a/pkgs/id/crates/id-import-notion/src/lib.rs b/pkgs/id/crates/id-import-notion/src/lib.rs index 8ff4aaa0f5..2c1a14f5ef 100644 --- a/pkgs/id/crates/id-import-notion/src/lib.rs +++ b/pkgs/id/crates/id-import-notion/src/lib.rs @@ -7,10 +7,13 @@ //! Slice 4 comment claims (§2.4) live in [`comments`]. //! Slice 3 (databases: note identity and per-data-source schema claims) //! lives in [`databases`]. +//! Slice 3 remainder (row notes, property claims, relations) lives in +//! [`rows`]. pub mod comments; pub mod content; pub mod databases; +pub mod rows; #[cfg(unix)] use std::fs::File; @@ -606,6 +609,11 @@ pub struct Manifest { /// Absent from pre-slice-4 manifests, so it defaults empty. #[serde(default)] pub comments_coverage: BTreeMap, + /// Row page UUID -> its row conversion record (§2.2), recorded by + /// [`rows::convert_row`]. Absent from pre-slice-3-remainder + /// manifests, so it defaults empty when an older manifest is loaded. + #[serde(default)] + pub row_records: BTreeMap, /// Progress checkpoints exist for page, database, and block UUIDs in /// `inventory.objects`; a database checkpoint represents its complete /// embedded data-source set in `inventory.data_sources`. @@ -667,6 +675,7 @@ impl Manifest { file_blobs: BTreeMap::new(), comment_claims: BTreeMap::new(), comments_coverage: BTreeMap::new(), + row_records: BTreeMap::new(), }) } diff --git a/pkgs/id/crates/id-import-notion/src/rows.rs b/pkgs/id/crates/id-import-notion/src/rows.rs new file mode 100644 index 0000000000..5fc2b3b85c --- /dev/null +++ b/pkgs/id/crates/id-import-notion/src/rows.rs @@ -0,0 +1,845 @@ +//! Row notes and property claims, slice 3 remainder (#13523). +//! +//! Converts inventoried database rows — pages whose parent is a data +//! source — to the §2.2 form: one note per row, a typed reference to +//! the database note, and the row's property values recorded as claims +//! interpreted against the schema of the data source the row was read +//! from. Relations become typed references between row notes (§2.3) +//! whose reference type is the relation's schema identity — (data +//! source, property), never the parent database — in the canonical +//! dual-relation form of §2.2. Rollups and formulas become +//! derived-value provenance claims (computed display value plus the +//! verbatim definition, never silently-authoritative values), system +//! properties become provenance claims, and button / verification +//! properties are named in the conversion's not-imported accounting +//! instead of being imported as values. +//! +//! A row value whose type disagrees with its own source's schema, or +//! that names a property the schema does not carry, fails the +//! conversion closed: reading one source's rows under another source's +//! schema is exactly the conflation failure §2.2 exists to prevent. +//! Relation values resolve only against rows of the schema's target +//! data source; a value naming anything else is recorded as an +//! externalised relation with its reason and counted, never dropped. +//! +//! Records are stored in the manifest keyed by the row's page UUID +//! and replaced wholesale on re-conversion, so an unchanged corpus +//! reproduces identical artifacts. A row's identity is its page UUID +//! alone (§6.1): the database and data source named in its record are +//! membership metadata, updated when a later import reads the row +//! under a new database or source. Superseding a moved data source's +//! prior schema claims on its old database note (§6.1) belongs to the +//! re-import driver, which does not exist in this crate yet; it is +//! the named remainder on #13523. + +use std::collections::BTreeMap; + +use anyhow::{Context, Result, bail}; +use serde::{Deserialize, Serialize}; +use serde_json::Value; + +use crate::{Manifest, Outcome, Stage}; + +/// Which §2.2 claim form a property value took. +#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub enum RowClaimKind { + /// A plain value claim, typed by the schema (rich text, number, + /// select, people, files, the title property, …). + Value, + /// A rollup or formula: the computed display value plus the + /// verbatim definition it was computed from. + Derived, + /// A system property (unique ID, created/last-edited time or by): + /// a provenance fact about the source, handled like a page + /// timestamp. + System, +} + +/// One property value of one row, recorded as a claim against the +/// schema of the data source the row was read from. The claim identity +/// is (`data_source_id`, `property_id`), as for schema claims. +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +pub struct PropertyClaim { + pub kind: RowClaimKind, + pub data_source_id: String, + pub property_id: String, + pub property_name: String, + pub property_type: String, + /// The verbatim property-value object from the row page. + pub value: Value, + /// The verbatim schema definition, for + /// [`RowClaimKind::Derived`] claims (rollup / formula) only: the + /// definition names the inputs the display value was computed + /// from. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub definition: Option, +} + +/// One side of a relation's schema identity: a (data source, property) +/// pair. The parent database is never part of it (§2.2), so a data +/// source moved to another database keeps the same relation identity. +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] +pub struct RelationSide { + pub data_source_id: String, + pub property_id: String, +} + +/// The reference type of a relation (§2.2): a one-way relation's type +/// is its single (data source, property) pair; a two-way (dual) +/// relation's canonical type is the unordered pair of both sides, +/// joined through `dual_property.synced_property_id`, stored sorted +/// so the side that sorts first is the canonical direction and both +/// sides import as the one type. +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq, Eq)] +#[serde(tag = "kind", rename_all = "snake_case")] +pub enum RelationType { + Single { + side: RelationSide, + }, + Dual { + first: RelationSide, + second: RelationSide, + }, +} + +/// A relation value that resolved to a row of the schema's target +/// data source: a typed reference from the owning row's note (the +/// record this sits in) to the target row's note. +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +pub struct RelationReference { + pub property_id: String, + pub property_name: String, + pub reference_type: RelationType, + /// Notion UUID of the target row page. + pub target_page_id: String, + /// Destination note ID of the target row. + pub target_note_id: String, +} + +/// Why a relation value did not become a typed reference. +#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq, Eq)] +#[serde(rename_all = "snake_case")] +pub enum ExternalisedReason { + /// The named page is not in the import set at all. + OutsideImportSet, + /// The named page is in the import set but is not a row of the + /// relation schema's target data source, so the value cannot + /// resolve against the rows the schema names. + NotTargetSourceRow, +} + +/// A relation value that named no row of the target data source, +/// recorded as a provenance claim (the target's UUID) and counted +/// externalised for the fidelity report (§3), never dropped. +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +pub struct ExternalisedRelation { + pub property_id: String, + pub property_name: String, + pub reference_type: RelationType, + pub target_page_id: String, + pub reason: ExternalisedReason, +} + +/// A property the design does not import as a value (button, +/// verification), named in the accounting so the fidelity report can +/// list it per database (§2.2). The schema claim still records that +/// the property existed. +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +pub struct NotImportedProperty { + pub data_source_id: String, + pub property_id: String, + pub property_name: String, + pub property_type: String, +} + +/// The result of converting one row page, and the record the manifest +/// keeps for it: the row note's identity, its typed reference to the +/// database note (via `database_note_id`), and every claim, reference, +/// externalised relation, and not-imported property the row produced. +#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)] +pub struct RowConversion { + pub page_id: String, + pub note_id: String, + pub database_id: String, + pub database_note_id: String, + pub data_source_id: String, + pub claims: Vec, + pub references: Vec, + pub externalised: Vec, + pub not_imported: Vec, +} + +/// One schema property of a data source, keyed by property id. +struct SchemaProperty { + name: String, + property_type: String, + definition: Value, +} + +/// Index one data source's schema by property id, applying the same +/// fail-closed validation as [`crate::databases::convert_database`]. +fn schema_properties(source: &Value, source_id: &str) -> Result> { + let properties = source + .get("properties") + .and_then(Value::as_object) + .with_context(|| format!("data source {source_id} has no properties object"))?; + let mut by_id = BTreeMap::new(); + for (name, definition) in properties { + let property_id = definition + .get("id") + .and_then(Value::as_str) + .with_context(|| format!("data source {source_id} property {name} is missing its id"))? + .to_owned(); + let property_type = definition + .get("type") + .and_then(Value::as_str) + .with_context(|| { + format!("data source {source_id} property {name} is missing its type") + })? + .to_owned(); + let entry = SchemaProperty { + name: name.clone(), + property_type, + definition: definition.clone(), + }; + if by_id.insert(property_id.clone(), entry).is_some() { + bail!("data source {source_id} repeats property id {property_id}"); + } + } + Ok(by_id) +} + +/// Compute a relation property's canonical reference type (§2.2) and +/// the data source its values resolve against. +fn relation_type( + schema: &SchemaProperty, + own_source_id: &str, + property_id: &str, +) -> Result<(RelationType, String)> { + let relation = schema + .definition + .get("relation") + .and_then(Value::as_object) + .with_context(|| { + format!("relation property {property_id} is missing its relation config") + })?; + let target_source_id = relation + .get("data_source_id") + .and_then(Value::as_str) + .with_context(|| { + format!("relation property {property_id} is missing its target data_source_id") + })? + .to_owned(); + let own = RelationSide { + data_source_id: own_source_id.to_owned(), + property_id: property_id.to_owned(), + }; + let reference_type = match relation.get("type").and_then(Value::as_str) { + None | Some("single_property") => RelationType::Single { side: own }, + Some("dual_property") => { + let synced_property_id = relation + .get("dual_property") + .and_then(|dual| dual.get("synced_property_id")) + .and_then(Value::as_str) + .with_context(|| { + format!( + "dual relation property {property_id} is missing \ + dual_property.synced_property_id" + ) + })?; + let other = RelationSide { + data_source_id: target_source_id.clone(), + property_id: synced_property_id.to_owned(), + }; + let (first, second) = if own <= other { + (own, other) + } else { + (other, own) + }; + RelationType::Dual { first, second } + } + Some(other) => { + bail!("relation property {property_id} has unsupported relation type {other}") + } + }; + Ok((reference_type, target_source_id)) +} + +/// Convert one inventoried row page to its §2.2 form, recording the +/// result in the manifest's row records and advancing the row's +/// checkpoint to [`Stage::References`]. +/// +/// The caller must have run [`Manifest::assign_page_identities`] and +/// [`Manifest::assign_database_identities`] first (the §3 two-pass +/// rule); converting a row with no assigned note ID — or whose +/// database has none — fails rather than minting identity +/// mid-content. Conversion is safe to re-run: the row's record is +/// replaced wholesale and progress never rewinds (§6.3). +pub fn convert_row(manifest: &mut Manifest, page_id: &str) -> Result { + let object = manifest + .inventory + .objects + .get(page_id) + .with_context(|| format!("page {page_id} is not in the inventory"))?; + if object.kind != "page" { + bail!( + "inventory object {page_id} is a {}, not a page", + object.kind + ); + } + let source_page = object.source.clone(); + let parent = source_page + .get("parent") + .with_context(|| format!("row page {page_id} is missing its parent"))?; + if parent.get("type").and_then(Value::as_str) != Some("data_source_id") { + bail!("page {page_id} is not a database row (its parent is not a data source)"); + } + let data_source_id = parent + .get("data_source_id") + .and_then(Value::as_str) + .with_context(|| format!("row page {page_id} parent is missing data_source_id"))? + .to_owned(); + let (database_id, schema_source) = manifest + .inventory + .data_sources + .iter() + .find_map(|(database_id, sources)| { + sources + .iter() + .find(|source| source.id == data_source_id) + .map(|source| (database_id.clone(), source.source.clone())) + }) + .with_context(|| { + format!("data source {data_source_id} is not enumerated in the inventory") + })?; + let note_id = manifest + .page_id_to_note_id + .get(page_id) + .with_context(|| { + format!("page {page_id} has no assigned note id; run assign_page_identities first") + })? + .clone(); + let database_note_id = manifest + .database_id_to_note_id + .get(&database_id) + .with_context(|| { + format!( + "database {database_id} has no assigned note id; \ + run assign_database_identities first" + ) + })? + .clone(); + let schema = schema_properties(&schema_source, &data_source_id)?; + let properties = source_page + .get("properties") + .and_then(Value::as_object) + .with_context(|| format!("row page {page_id} has no properties object"))?; + let mut entries: Vec<(&String, &Value)> = properties.iter().collect(); + entries.sort_by(|left, right| left.0.cmp(right.0)); + let mut claims = Vec::new(); + let mut references = Vec::new(); + let mut externalised = Vec::new(); + let mut not_imported = Vec::new(); + for (name, value) in entries { + let property_id = value + .get("id") + .and_then(Value::as_str) + .with_context(|| format!("row page {page_id} property {name} is missing its id"))? + .to_owned(); + let value_type = value + .get("type") + .and_then(Value::as_str) + .with_context(|| format!("row page {page_id} property {name} is missing its type"))? + .to_owned(); + let property = schema.get(&property_id).with_context(|| { + format!( + "row page {page_id} property {name} ({property_id}) is not in the \ + schema of data source {data_source_id}" + ) + })?; + if value_type != property.property_type { + bail!( + "row page {page_id} property {} has type {value_type} but the schema \ + of data source {data_source_id} says {}", + property.name, + property.property_type + ); + } + let claim = |kind, definition| PropertyClaim { + kind, + data_source_id: data_source_id.clone(), + property_id: property_id.clone(), + property_name: property.name.clone(), + property_type: property.property_type.clone(), + value: value.clone(), + definition, + }; + match property.property_type.as_str() { + "relation" => { + let (reference_type, target_source_id) = + relation_type(property, &data_source_id, &property_id)?; + let targets = value + .get("relation") + .and_then(Value::as_array) + .with_context(|| { + format!( + "row page {page_id} relation property {property_id} \ + is missing its relation array" + ) + })?; + for target in targets { + let target_page_id = target + .get("id") + .and_then(Value::as_str) + .with_context(|| { + format!( + "row page {page_id} relation property {property_id} \ + names a target without an id" + ) + })? + .to_owned(); + let target_row = manifest.inventory.objects.get(&target_page_id); + let resolves = target_row.is_some_and(|target_object| { + target_object.kind == "page" + && target_object + .source + .get("parent") + .and_then(|target_parent| target_parent.get("data_source_id")) + .and_then(Value::as_str) + == Some(target_source_id.as_str()) + }); + if target_row.is_none() { + externalised.push(ExternalisedRelation { + property_id: property_id.clone(), + property_name: property.name.clone(), + reference_type: reference_type.clone(), + target_page_id, + reason: ExternalisedReason::OutsideImportSet, + }); + } else if resolves { + let target_note_id = manifest + .page_id_to_note_id + .get(&target_page_id) + .with_context(|| { + format!( + "relation target row {target_page_id} has no \ + assigned note id; run assign_page_identities first" + ) + })? + .clone(); + references.push(RelationReference { + property_id: property_id.clone(), + property_name: property.name.clone(), + reference_type: reference_type.clone(), + target_page_id, + target_note_id, + }); + } else { + externalised.push(ExternalisedRelation { + property_id: property_id.clone(), + property_name: property.name.clone(), + reference_type: reference_type.clone(), + target_page_id, + reason: ExternalisedReason::NotTargetSourceRow, + }); + } + } + } + "rollup" | "formula" => { + claims.push(claim( + RowClaimKind::Derived, + Some(property.definition.clone()), + )); + } + "unique_id" | "created_time" | "last_edited_time" | "created_by" | "last_edited_by" => { + claims.push(claim(RowClaimKind::System, None)); + } + "button" | "verification" => { + not_imported.push(NotImportedProperty { + data_source_id: data_source_id.clone(), + property_id: property_id.clone(), + property_name: property.name.clone(), + property_type: property.property_type.clone(), + }); + } + _ => { + claims.push(claim(RowClaimKind::Value, None)); + } + } + } + let conversion = RowConversion { + page_id: page_id.to_owned(), + note_id, + database_id, + database_note_id, + data_source_id, + claims, + references, + externalised, + not_imported, + }; + manifest + .row_records + .insert(page_id.to_owned(), conversion.clone()); + let outcome = if conversion.externalised.is_empty() && conversion.not_imported.is_empty() { + Outcome::Clean + } else { + Outcome::Degraded + }; + advance(manifest, page_id, Stage::References, outcome, None)?; + Ok(conversion) +} + +/// Record staged progress without rewinding it (§6.3), as in +/// [`crate::databases`]. +fn advance( + manifest: &mut Manifest, + object_id: &str, + stage: Stage, + outcome: Outcome, + reason: Option, +) -> Result<()> { + let rank = |value: &Stage| match value { + Stage::Inventory => 0, + Stage::Files => 1, + Stage::Content => 2, + Stage::References => 3, + Stage::Report => 4, + }; + let current = manifest + .objects + .get(object_id) + .with_context(|| format!("object {object_id} is not in the manifest inventory"))?; + if rank(&stage) >= rank(¤t.stage) { + manifest.record_progress(object_id, stage, outcome, reason)?; + } + Ok(()) +} + +#[cfg(test)] +#[allow(clippy::unwrap_used, clippy::expect_used)] +mod tests { + use super::*; + use crate::{Inventory, NOTION_API_VERSION}; + use serde_json::json; + use std::collections::BTreeMap; + + fn source(id: &str, properties: Value) -> Value { + json!({ + "object": "data_source", + "id": id, + "parent": {"type": "database_id", "database_id": "db-1"}, + "properties": properties, + }) + } + + fn row(id: &str, source_id: &str, properties: Value) -> Value { + json!({ + "object": "page", + "id": id, + "parent": {"type": "data_source_id", "data_source_id": source_id}, + "properties": properties, + }) + } + + fn manifest_for(sources: Vec, pages: Vec) -> Manifest { + let database = json!({ + "object": "database", + "id": "db-1", + "parent": {"type": "workspace", "workspace": true}, + "data_sources": sources.iter().map(|source| { + json!({"id": source["id"].as_str().unwrap()}) + }).collect::>(), + }); + let mut results = vec![database]; + results.extend(pages); + let mut by_database = BTreeMap::new(); + by_database.insert("db-1".into(), sources); + let inventory = Inventory::from_recorded_responses( + &[json!({"object":"list","results":results,"has_more":false})], + &by_database, + vec!["db-1".into()], + BTreeMap::new(), + None, + ) + .unwrap(); + Manifest::new( + "workspace-1".into(), + vec!["api".into()], + NOTION_API_VERSION.into(), + inventory, + ) + .unwrap() + } + + fn assigned(manifest: &mut Manifest) { + manifest + .assign_page_identities(&mut |id| Ok(format!("note-{id}"))) + .unwrap(); + manifest + .assign_database_identities(&mut |id| Ok(format!("note-{id}"))) + .unwrap(); + } + + fn title_property() -> Value { + json!({"Name": {"id": "title", "type": "title", "title": {}}}) + } + + fn title_value(text: &str) -> Value { + json!([{"type": "text", "plain_text": text}]) + } + + #[test] + fn plain_page_is_not_a_row() { + let page = json!({ + "object": "page", + "id": "p-1", + "parent": {"type": "workspace", "workspace": true}, + "properties": {}, + }); + let mut manifest = manifest_for(vec![source("ds-1", title_property())], vec![page]); + assigned(&mut manifest); + let err = convert_row(&mut manifest, "p-1").unwrap_err(); + assert!(err.to_string().contains("not a database row"), "{err:#}"); + } + + #[test] + fn unassigned_row_note_id_fails_closed() { + let mut manifest = manifest_for( + vec![source("ds-1", title_property())], + vec![row( + "row-1", + "ds-1", + json!({"Name": {"id": "title", "type": "title", "title": title_value("One")}}), + )], + ); + let err = convert_row(&mut manifest, "row-1").unwrap_err(); + assert!(err.to_string().contains("no assigned note id"), "{err:#}"); + } + + #[test] + fn value_type_mismatch_against_own_source_fails_closed() { + // The schema says Status is a select; the row carries it with + // the *other* source's type (status). Interpreting the row + // under its own source's schema must refuse, not coerce. + let mut manifest = manifest_for( + vec![source( + "ds-1", + json!({ + "Name": {"id": "title", "type": "title", "title": {}}, + "Status": {"id": "status", "type": "select", "select": {"options": []}}, + }), + )], + vec![row( + "row-1", + "ds-1", + json!({ + "Name": {"id": "title", "type": "title", "title": title_value("One")}, + "Status": {"id": "status", "type": "status", "status": {"name": "Doing"}}, + }), + )], + ); + assigned(&mut manifest); + let err = convert_row(&mut manifest, "row-1").unwrap_err(); + assert!(err.to_string().contains("says select"), "{err:#}"); + } + + #[test] + fn property_absent_from_schema_fails_closed() { + let mut manifest = manifest_for( + vec![source("ds-1", title_property())], + vec![row( + "row-1", + "ds-1", + json!({ + "Name": {"id": "title", "type": "title", "title": title_value("One")}, + "Ghost": {"id": "ghost", "type": "number", "number": 1}, + }), + )], + ); + assigned(&mut manifest); + let err = convert_row(&mut manifest, "row-1").unwrap_err(); + assert!(err.to_string().contains("is not in the schema"), "{err:#}"); + } + + #[test] + fn dual_relation_canonical_type_is_shared_by_both_sides() { + let alpha = source( + "ds-a", + json!({ + "Name": {"id": "title", "type": "title", "title": {}}, + "Rel": {"id": "rel", "type": "relation", "relation": { + "data_source_id": "ds-b", "type": "dual_property", + "dual_property": {"synced_property_id": "rel_back", "synced_property_name": "RelBack"} + }}, + }), + ); + let beta = source( + "ds-b", + json!({ + "Name": {"id": "title", "type": "title", "title": {}}, + "RelBack": {"id": "rel_back", "type": "relation", "relation": { + "data_source_id": "ds-a", "type": "dual_property", + "dual_property": {"synced_property_id": "rel", "synced_property_name": "Rel"} + }}, + }), + ); + // Both sources belong to the one database in this fixture; the + // relation identity is keyed by data source, not database. + let mut manifest = manifest_for( + vec![alpha, beta], + vec![ + row( + "row-a", + "ds-a", + json!({ + "Name": {"id": "title", "type": "title", "title": title_value("A")}, + "Rel": {"id": "rel", "type": "relation", "relation": [{"id": "row-b"}]}, + }), + ), + row( + "row-b", + "ds-b", + json!({ + "Name": {"id": "title", "type": "title", "title": title_value("B")}, + "RelBack": {"id": "rel_back", "type": "relation", "relation": [{"id": "row-a"}]}, + }), + ), + ], + ); + assigned(&mut manifest); + let expected = RelationType::Dual { + first: RelationSide { + data_source_id: "ds-a".into(), + property_id: "rel".into(), + }, + second: RelationSide { + data_source_id: "ds-b".into(), + property_id: "rel_back".into(), + }, + }; + let from_a = convert_row(&mut manifest, "row-a").unwrap(); + assert_eq!(from_a.references.len(), 1); + assert_eq!(from_a.references[0].reference_type, expected); + assert_eq!(from_a.references[0].target_note_id, "note-row-b"); + let from_b = convert_row(&mut manifest, "row-b").unwrap(); + assert_eq!(from_b.references.len(), 1); + // Both sides import as the one canonical type: no duplication. + assert_eq!(from_b.references[0].reference_type, expected); + assert_eq!(from_b.references[0].target_note_id, "note-row-a"); + } + + #[test] + fn relation_value_outside_import_set_is_externalised() { + let mut manifest = manifest_for( + vec![source( + "ds-1", + json!({ + "Name": {"id": "title", "type": "title", "title": {}}, + "Rel": {"id": "rel", "type": "relation", "relation": { + "data_source_id": "ds-1", "type": "single_property", "single_property": {} + }}, + }), + )], + vec![row( + "row-1", + "ds-1", + json!({ + "Name": {"id": "title", "type": "title", "title": title_value("One")}, + "Rel": {"id": "rel", "type": "relation", "relation": [{"id": "row-ghost"}]}, + }), + )], + ); + assigned(&mut manifest); + let conversion = convert_row(&mut manifest, "row-1").unwrap(); + assert!(conversion.references.is_empty()); + assert_eq!(conversion.externalised.len(), 1); + assert_eq!( + conversion.externalised[0].reason, + ExternalisedReason::OutsideImportSet + ); + assert_eq!(conversion.externalised[0].target_page_id, "row-ghost"); + assert_eq!( + conversion.externalised[0].reference_type, + RelationType::Single { + side: RelationSide { + data_source_id: "ds-1".into(), + property_id: "rel".into(), + } + } + ); + assert_eq!(manifest.objects["row-1"].outcome, Outcome::Degraded); + } + + #[test] + fn derived_system_and_not_imported_property_forms() { + let mut manifest = manifest_for( + vec![source( + "ds-1", + json!({ + "Name": {"id": "title", "type": "title", "title": {}}, + "Count": {"id": "count", "type": "rollup", "rollup": { + "relation_property_id": "rel", "rollup_property_id": "title", "function": "count" + }}, + "Label": {"id": "label", "type": "formula", "formula": {"expression": "prop(\"Name\")"}}, + "ID": {"id": "id", "type": "unique_id", "unique_id": {"prefix": "T"}}, + "Action": {"id": "action", "type": "button", "button": {}}, + }), + )], + vec![row( + "row-1", + "ds-1", + json!({ + "Name": {"id": "title", "type": "title", "title": title_value("One")}, + "Count": {"id": "count", "type": "rollup", "rollup": {"type": "number", "number": 2}}, + "Label": {"id": "label", "type": "formula", "formula": {"type": "string", "string": "One"}}, + "ID": {"id": "id", "type": "unique_id", "unique_id": {"prefix": "T", "number": 3}}, + "Action": {"id": "action", "type": "button", "button": {}}, + }), + )], + ); + assigned(&mut manifest); + let conversion = convert_row(&mut manifest, "row-1").unwrap(); + let by_id: BTreeMap<&str, &PropertyClaim> = conversion + .claims + .iter() + .map(|claim| (claim.property_id.as_str(), claim)) + .collect(); + assert_eq!(by_id["count"].kind, RowClaimKind::Derived); + assert_eq!( + by_id["count"].definition.as_ref().unwrap()["rollup"]["function"], + json!("count") + ); + assert_eq!(by_id["label"].kind, RowClaimKind::Derived); + assert_eq!( + by_id["label"].definition.as_ref().unwrap()["formula"]["expression"], + json!("prop(\"Name\")") + ); + assert_eq!(by_id["id"].kind, RowClaimKind::System); + assert_eq!(by_id["title"].kind, RowClaimKind::Value); + assert_eq!(conversion.not_imported.len(), 1); + assert_eq!(conversion.not_imported[0].property_id, "action"); + assert_eq!(conversion.not_imported[0].property_type, "button"); + assert!(!by_id.contains_key("action")); + } + + #[test] + fn rerun_reproduces_identical_record() { + let mut manifest = manifest_for( + vec![source("ds-1", title_property())], + vec![row( + "row-1", + "ds-1", + json!({"Name": {"id": "title", "type": "title", "title": title_value("One")}}), + )], + ); + assigned(&mut manifest); + let first = convert_row(&mut manifest, "row-1").unwrap(); + let second = convert_row(&mut manifest, "row-1").unwrap(); + assert_eq!(first, second); + assert_eq!(manifest.row_records.len(), 1); + assert_eq!(manifest.row_records["row-1"], first); + assert_eq!(manifest.objects["row-1"].stage, Stage::References); + } +} diff --git a/pkgs/id/crates/id-import-notion/tests/fixtures/rows.json b/pkgs/id/crates/id-import-notion/tests/fixtures/rows.json new file mode 100644 index 0000000000..19f5edb97f --- /dev/null +++ b/pkgs/id/crates/id-import-notion/tests/fixtures/rows.json @@ -0,0 +1 @@ +{"source-alpha":[{"created_time":"2026-11-01T10:00:00.000Z","id":"row-a1","last_edited_time":"2026-11-01T11:00:00.000Z","object":"page","parent":{"data_source_id":"source-alpha","type":"data_source_id"},"properties":{"Action":{"button":{},"id":"action","type":"button"},"Created":{"created_time":"2026-11-01T10:00:00.000Z","id":"created","type":"created_time"},"ID":{"id":"id","type":"unique_id","unique_id":{"number":7,"prefix":"ALP"}},"Linked":{"has_more":false,"id":"linked","relation":[{"id":"row-b1"}],"type":"relation"},"LinkedCount":{"id":"linked_count","rollup":{"number":1,"type":"number"},"type":"rollup"},"Name":{"id":"title","title":[{"plain_text":"Alpha One","text":{"content":"Alpha One"},"type":"text"}],"type":"title"},"Owner":{"id":"owner","people":[{"id":"user-1","name":"Drew Example","object":"user"}],"type":"people"},"Status":{"id":"status","status":{"color":"yellow","id":"st-doing","name":"Doing"},"type":"status"},"Summary":{"formula":{"string":"Alpha One / Doing","type":"string"},"id":"summary","type":"formula"}}},{"created_time":"2026-11-02T10:00:00.000Z","id":"row-a2","last_edited_time":"2026-11-02T11:00:00.000Z","object":"page","parent":{"data_source_id":"source-alpha","type":"data_source_id"},"properties":{"Action":{"button":{},"id":"action","type":"button"},"Created":{"created_time":"2026-11-02T10:00:00.000Z","id":"created","type":"created_time"},"ID":{"id":"id","type":"unique_id","unique_id":{"number":8,"prefix":"ALP"}},"Linked":{"has_more":false,"id":"linked","relation":[{"id":"row-b2"},{"id":"row-a1"},{"id":"row-outside"}],"type":"relation"},"LinkedCount":{"id":"linked_count","rollup":{"number":3,"type":"number"},"type":"rollup"},"Name":{"id":"title","title":[{"plain_text":"Alpha Two","text":{"content":"Alpha Two"},"type":"text"}],"type":"title"},"Owner":{"id":"owner","people":[],"type":"people"},"Status":{"id":"status","status":{"color":"gray","id":"st-todo","name":"Todo"},"type":"status"},"Summary":{"formula":{"string":"Alpha Two / Todo","type":"string"},"id":"summary","type":"formula"}}}],"source-beta":[{"created_time":"2026-11-01T12:00:00.000Z","id":"row-b1","last_edited_time":"2026-11-01T12:30:00.000Z","object":"page","parent":{"data_source_id":"source-beta","type":"data_source_id"},"properties":{"Archived":{"checkbox":false,"id":"archived","type":"checkbox"},"Name":{"id":"title","title":[{"plain_text":"Beta One","text":{"content":"Beta One"},"type":"text"}],"type":"title"},"Status":{"id":"status","select":{"color":"blue","id":"opt-open","name":"Open"},"type":"select"}}},{"created_time":"2026-11-02T12:00:00.000Z","id":"row-b2","last_edited_time":"2026-11-02T12:30:00.000Z","object":"page","parent":{"data_source_id":"source-beta","type":"data_source_id"},"properties":{"Archived":{"checkbox":true,"id":"archived","type":"checkbox"},"Name":{"id":"title","title":[{"plain_text":"Beta Two","text":{"content":"Beta Two"},"type":"text"}],"type":"title"},"Status":{"id":"status","select":{"color":"green","id":"opt-done","name":"Done"},"type":"select"}}}]} \ No newline at end of file diff --git a/pkgs/id/crates/id-import-notion/tests/fixtures/rows_data_sources.json b/pkgs/id/crates/id-import-notion/tests/fixtures/rows_data_sources.json new file mode 100644 index 0000000000..d0b72fdc34 --- /dev/null +++ b/pkgs/id/crates/id-import-notion/tests/fixtures/rows_data_sources.json @@ -0,0 +1 @@ +{"has_more":false,"object":"list","results":[{"id":"source-alpha","object":"data_source","parent":{"database_id":"db-alpha","type":"database_id"},"properties":{"Action":{"button":{},"id":"action","type":"button"},"Created":{"created_time":{},"id":"created","type":"created_time"},"ID":{"id":"id","type":"unique_id","unique_id":{"prefix":"ALP"}},"Linked":{"id":"linked","relation":{"data_source_id":"source-beta","single_property":{},"type":"single_property"},"type":"relation"},"LinkedCount":{"id":"linked_count","rollup":{"function":"count","relation_property_id":"linked","relation_property_name":"Linked","rollup_property_id":"title","rollup_property_name":"Name"},"type":"rollup"},"Name":{"id":"title","title":{},"type":"title"},"Owner":{"id":"owner","people":{},"type":"people"},"Status":{"id":"status","status":{},"type":"status"},"Summary":{"formula":{"expression":"prop(\"Name\") + \" / \" + prop(\"Status\")"},"id":"summary","type":"formula"}}},{"id":"source-beta","object":"data_source","parent":{"database_id":"db-alpha","type":"database_id"},"properties":{"Archived":{"checkbox":{},"id":"archived","type":"checkbox"},"Name":{"id":"title","title":{},"type":"title"},"Status":{"id":"status","select":{"options":[{"color":"blue","id":"opt-open","name":"Open"},{"color":"green","id":"opt-done","name":"Done"}]},"type":"select"}}}]} \ No newline at end of file diff --git a/pkgs/id/crates/id-import-notion/tests/recorded_rows.rs b/pkgs/id/crates/id-import-notion/tests/recorded_rows.rs new file mode 100644 index 0000000000..eec37eb1e6 --- /dev/null +++ b/pkgs/id/crates/id-import-notion/tests/recorded_rows.rs @@ -0,0 +1,210 @@ +use std::collections::{BTreeMap, BTreeSet}; + +use id_import_notion::{ + Inventory, Manifest, NOTION_API_VERSION, Outcome, Stage, + databases::convert_database, + rows::{ExternalisedReason, RelationType, RowClaimKind, convert_row}, +}; +use tempfile::tempdir; + +/// Design §8 check 8, second half, plus the check-2 relation subset, +/// over the multi-source fixture database (db-alpha, sources alpha + +/// beta): every row's claims validate against its own source's schema +/// and no row's against the other's; the relation defined in only one +/// source (alpha's `Linked`, targeting source-beta) produces typed +/// references for rows of that source only, resolving to rows of its +/// target data source; values naming anything else are externalised +/// with their reason. +/// +/// The oracle is the recorded fixture: source-alpha carries Status as +/// `status` and source-beta carries the same property name and id as +/// `select`, so an implementation that reads a row under the wrong +/// source's schema — or resolves relations against the wrong source's +/// rows — fails here. Row-a2's relation names one row of the target +/// source (resolves), one row of its own source (externalised, +/// not-target-source-row), and one page outside the import set +/// (externalised, outside-import-set). +#[test] +fn recorded_multi_source_rows_validate_per_source_and_relations_resolve() { + let search: serde_json::Value = + serde_json::from_str(include_str!("fixtures/search.json")).unwrap(); + let database: serde_json::Value = + serde_json::from_str(include_str!("fixtures/database.json")).unwrap(); + let data_sources: serde_json::Value = + serde_json::from_str(include_str!("fixtures/rows_data_sources.json")).unwrap(); + let rows: serde_json::Value = serde_json::from_str(include_str!("fixtures/rows.json")).unwrap(); + let sources = data_sources["results"].as_array().unwrap().clone(); + let mut row_pages = Vec::new(); + for source_id in ["source-alpha", "source-beta"] { + row_pages.extend(rows[source_id].as_array().unwrap().iter().cloned()); + } + let availability = BTreeMap::from([ + ("page-root".into(), true), + ("row-a1".into(), true), + ("row-a2".into(), true), + ("row-b1".into(), true), + ("row-b2".into(), true), + ]); + let inventory = Inventory::from_recorded_responses( + &[ + search, + serde_json::json!({"object":"list","results":[database],"has_more":false}), + serde_json::json!({"object":"list","results":row_pages,"has_more":false}), + ], + &BTreeMap::from([("db-alpha".into(), sources)]), + vec!["db-alpha".into(), "page-root".into()], + availability, + Some("business".into()), + ) + .unwrap(); + let mut manifest = Manifest::new( + "workspace-alpha".into(), + vec!["api".into()], + NOTION_API_VERSION.into(), + inventory, + ) + .unwrap(); + assert_eq!( + manifest + .assign_page_identities(&mut |page_id| Ok(format!("note-{page_id}"))) + .unwrap(), + 5 + ); + assert_eq!( + manifest + .assign_database_identities(&mut |database_id| Ok(format!("note-{database_id}"))) + .unwrap(), + 1 + ); + + let database_conversion = convert_database(&mut manifest, "db-alpha").unwrap(); + // Schema claims per source, keyed (data_source_id, property_id). + let schema_keys: BTreeMap<&str, BTreeSet<(&str, &str)>> = { + let mut map: BTreeMap<&str, BTreeSet<(&str, &str)>> = BTreeMap::new(); + for claim in &database_conversion.schema_claims { + map.entry(claim.data_source_id.as_str()) + .or_default() + .insert((claim.property_id.as_str(), claim.property_type.as_str())); + } + map + }; + assert_eq!(schema_keys["source-alpha"].len(), 9); + assert_eq!(schema_keys["source-beta"].len(), 3); + + let mut conversions = BTreeMap::new(); + for row_id in ["row-a1", "row-a2", "row-b1", "row-b2"] { + conversions.insert(row_id, convert_row(&mut manifest, row_id).unwrap()); + } + + // Every row's record names the one database note and its own + // source; every claim validates against its own source's schema. + for (row_id, conversion) in &conversions { + assert_eq!(conversion.database_note_id, "note-db-alpha", "{row_id}"); + assert_eq!(conversion.note_id, format!("note-{row_id}")); + for claim in &conversion.claims { + assert_eq!(claim.data_source_id, conversion.data_source_id); + assert!( + schema_keys[conversion.data_source_id.as_str()] + .contains(&(claim.property_id.as_str(), claim.property_type.as_str())), + "{row_id} claim {claim:?} must validate against {} schema", + conversion.data_source_id + ); + } + assert_eq!(manifest.objects[*row_id].stage, Stage::References); + } + // The colliding Status property discriminates the sources: no + // row's Status claim validates against the other source's schema. + for (row_id, other) in [ + ("row-a1", "source-beta"), + ("row-a2", "source-beta"), + ("row-b1", "source-alpha"), + ("row-b2", "source-alpha"), + ] { + let status = conversions[row_id] + .claims + .iter() + .find(|claim| claim.property_id == "status") + .unwrap(); + assert!( + !schema_keys[other].contains(&("status", status.property_type.as_str())), + "{row_id} Status claim must not validate against {other}" + ); + } + + // The relation is defined in source-alpha only: alpha rows produce + // references, beta rows produce none. + let a1 = &conversions["row-a1"]; + assert_eq!(a1.references.len(), 1); + assert_eq!(a1.references[0].target_page_id, "row-b1"); + assert_eq!(a1.references[0].target_note_id, "note-row-b1"); + assert_eq!( + a1.references[0].reference_type, + RelationType::Single { + side: id_import_notion::rows::RelationSide { + data_source_id: "source-alpha".into(), + property_id: "linked".into(), + } + } + ); + assert!(a1.externalised.is_empty()); + for beta_row in ["row-b1", "row-b2"] { + assert!(conversions[beta_row].references.is_empty(), "{beta_row}"); + assert!(conversions[beta_row].externalised.is_empty(), "{beta_row}"); + } + + // Check-2 relation subset: row-a2's three values are one typed + // reference and two externalised items with the expected reasons. + let a2 = &conversions["row-a2"]; + assert_eq!(a2.references.len(), 1); + assert_eq!(a2.references[0].target_page_id, "row-b2"); + assert_eq!(a2.externalised.len(), 2); + assert_eq!(a2.externalised[0].target_page_id, "row-a1"); + assert_eq!( + a2.externalised[0].reason, + ExternalisedReason::NotTargetSourceRow + ); + assert_eq!(a2.externalised[1].target_page_id, "row-outside"); + assert_eq!( + a2.externalised[1].reason, + ExternalisedReason::OutsideImportSet + ); + assert_eq!(manifest.objects["row-a2"].outcome, Outcome::Degraded); + assert_eq!(manifest.objects["row-b1"].outcome, Outcome::Clean); + + // Claim forms on row-a1: rollup and formula are derived claims + // carrying their verbatim definitions; the unique ID is a system + // claim; the people value is a plain provenance claim; the button + // is named in the not-imported accounting, never claimed. + let claims: BTreeMap<&str, _> = a1 + .claims + .iter() + .map(|claim| (claim.property_id.as_str(), claim)) + .collect(); + assert_eq!(claims["linked_count"].kind, RowClaimKind::Derived); + assert_eq!( + claims["linked_count"].definition.as_ref().unwrap()["rollup"]["function"], + serde_json::json!("count") + ); + assert_eq!(claims["summary"].kind, RowClaimKind::Derived); + assert_eq!( + claims["summary"].definition.as_ref().unwrap()["formula"]["expression"], + serde_json::json!("prop(\"Name\") + \" / \" + prop(\"Status\")") + ); + assert_eq!(claims["id"].kind, RowClaimKind::System); + assert_eq!(claims["created"].kind, RowClaimKind::System); + assert_eq!(claims["owner"].kind, RowClaimKind::Value); + assert_eq!(claims["status"].kind, RowClaimKind::Value); + assert!(!claims.contains_key("action")); + assert_eq!(a1.not_imported.len(), 1); + assert_eq!(a1.not_imported[0].property_id, "action"); + assert_eq!(a1.not_imported[0].property_type, "button"); + + // The row records survive a manifest round-trip unchanged. + assert_eq!(manifest.row_records.len(), 4); + let dir = tempdir().unwrap(); + let path = dir.path().join("manifest.json"); + manifest.save_atomic(&path).unwrap(); + let loaded = Manifest::load(&path).unwrap(); + assert_eq!(loaded, manifest); + assert_eq!(loaded.row_records["row-a1"].references.len(), 1); +}