ingraph-codegen 0.1.0

The emission engine behind ingraph-derive: a plain declaration model in, the ingraph framework faces out.
Documentation
//! **THE WIRE CITIZENSHIP** a retrieving declaration mints — `<Decl>Queries` and the [`Graphql`]
//! binding that seats it.
//!
//! ```text
//!   #[ingraph::vector(…, graphql(async_graphql))]
//!     │
//!     ├─ pub struct ShotsQueries;                     the UNIT a root folds
//!     └─ impl Graphql for Shots {                     the SEAT
//!          type Input = ();  type Output = ();  type Query = ShotsQueries;
//!        }
//! ```
//!
//! Beside [`tiers`](super::tiers) on this directory's one-file-per-thing precedent. What is proved
//! here is the MOUNTING — that the unit lands, that the binding is ungated, that both record-shaped
//! halves are the empty product, and that a declaration naming no backend mints none of it. The
//! CITIZEN itself — the door a resolver reads its slot through — is `ingraph`'s `graphql::search`.

use crate::prefix::EmissionPrefix;
use quote::ToTokens as _;

use super::{
  IndexGraphql, IndexModel, IndexRename,
  tests::{document, embedding_index},
};
use crate::model::{GraphqlKind, NodeKind, Role};

fn ident(name: &str) -> syn::Ident {
  syn::Ident::new(name, proc_macro2::Span::call_site())
}

/// The emission with its spaces taken out — what a claim about an impl is read off.
fn tight(model: &IndexModel) -> String {
  model.emit().to_string().split_whitespace().collect()
}

/// EVERY TYPE the whole expansion lands at MODULE SCOPE, by name.
fn landed(model: &IndexModel) -> Vec<String> {
  let file: syn::File = syn::parse2(model.emit()).expect("the emission parses");
  file
    .items
    .into_iter()
    .filter_map(|item| match item {
      syn::Item::Struct(declared) => Some(declared.ident.to_string()),
      syn::Item::Enum(declared) => Some(declared.ident.to_string()),
      _ => None,
    })
    .collect()
}

/// A declaration wearing no backend word — the absence half of every claim below.
fn undressed() -> IndexModel {
  IndexModel {
    prefix: EmissionPrefix::default(),
    graphql: None,
    ..document()
  }
}

/// **THE UNIT LANDS IN THE DECLARING SCOPE**, named from the declaration, and the binding seats it.
#[test]
fn a_dressed_declaration_mints_its_unit_and_binds_the_seat() {
  let model = document();
  assert!(
    landed(&model).contains(&"SearchDocumentQueries".to_owned()),
    "{:?}",
    landed(&model),
  );

  let emitted = tight(&model);
  assert!(
    emitted.contains("impl::ingraph::__private::transform::GraphqlforSearchDocument"),
    "{emitted}",
  );
  assert!(
    emitted.contains("typeQuery=SearchDocumentQueries;"),
    "{emitted}",
  );
}

/// **BOTH RECORD-SHAPED HALVES BIND THE EMPTY PRODUCT.**
///
/// A retrieving declaration is asked by no predicate and ordered by no column: its request surface
/// is the question and options packages its fusion tree mints, and its answer surface is the hits
/// its doors hand back. So `Input` and `Output` have nothing to name, and `()` is the answer the
/// framework's absent forms are already spelled with — asserted rather than assumed, because
/// minting two empty types per declaration is the road this one did NOT take.
#[test]
fn the_two_record_shaped_halves_are_the_empty_product() {
  let emitted = tight(&document());
  assert!(emitted.contains("typeInput=();"), "{emitted}");
  assert!(emitted.contains("typeOutput=();"), "{emitted}");
}

/// **THE BINDING IS UNGATED AND THE UNIT IS A PLAIN ITEM.**
///
/// `type Query = <Decl>Queries` is bound in an impl no feature guards, so a unit that existed only
/// where async-graphql does would leave that binding naming nothing in every other build — the
/// record's own split, read at a new tier. The claim is that neither token sits inside the gate
/// macro's own invocation.
#[test]
fn the_unit_and_its_binding_stand_outside_the_binding_gate() {
  let file: syn::File = syn::parse2(document().emit()).expect("the emission parses");
  let seated = file.items.iter().any(|item| {
    matches!(item, syn::Item::Impl(block) if block
      .trait_
      .as_ref()
      .is_some_and(|traited| traited.0.to_token_stream().to_string().contains("Graphql")))
  });
  assert!(seated, "the binding is a module-scope item");

  let minted = file.items.iter().any(
    |item| matches!(item, syn::Item::Struct(declared) if declared.ident == "SearchDocumentQueries"),
  );
  assert!(minted, "the unit is a module-scope item");
}

/// **A DECLARATION THAT NAMED NO BACKEND MINTS NO CITIZENSHIP**, and does not even reach for the
/// macro that asks whether the feature is on.
///
/// The presence law this family's skins already hold, at the tier that mints the unit: a
/// backend-only search pipeline keeps every door and package it had and gets no schema.
#[test]
fn an_undressed_declaration_mints_no_citizenship() {
  let model = undressed();
  assert!(!model.mounts());
  assert!(model.citizen().is_empty());
  assert!(
    !landed(&model).contains(&"SearchDocumentQueries".to_owned()),
    "{:?}",
    landed(&model),
  );
  assert!(
    !tight(&model).contains("transform::GraphqlforSearchDocument"),
    "an undressed declaration bound the wire seat",
  );
}

/// **A BLOB MINTS NONE OF IT EITHER** — the kind fence, which is where a family that retrieves
/// nothing has nothing for a root to mount.
#[test]
fn an_artifact_family_mints_no_query_unit() {
  let mut blob = document();
  blob.kind = NodeKind::Blob;
  blob.graphql = None;
  assert!(!blob.mounts());
  assert!(blob.citizen().is_empty());
}

/// **THE UNIT JOINS THE ROSTER**, so the front end's collision refusals are measured against a name
/// the expansion actually lands.
///
/// A mint that reached the emission and missed the enumeration would compile and would refuse
/// declarations for names it no longer minted — the roster law, at a name the wire road added.
#[test]
fn the_unit_is_on_the_roster_and_carries_its_own_role() {
  let model = document();
  let rostered: Vec<String> = model
    .emitted_types()
    .iter()
    .map(|declared| declared.name.to_string())
    .collect();
  assert!(
    rostered.contains(&"SearchDocumentQueries".to_owned()),
    "{rostered:?}"
  );
  assert!(
    model
      .emitted_types()
      .iter()
      .any(|declared| matches!(declared.role, Role::Queries)),
  );

  assert!(
    !undressed()
      .emitted_types()
      .iter()
      .any(|declared| matches!(declared.role, Role::Queries)),
    "an undressed declaration rostered a name it never mints",
  );
}

/// **THE BONES RENAME MOVES IT WITH THE REST OF THE FAMILY.**
///
/// One stem per declaring scope: a rename that reached the packages and left the unit behind would
/// put two in one scope. It takes no piece word of its own, on the record unit's precedent — the
/// unit is a Rust type a root spells once and a schema never sees.
#[test]
fn the_base_rename_moves_the_unit_with_the_family() {
  let renamed = IndexModel {
    prefix: EmissionPrefix::default(),
    rename: Some(IndexRename::Base(ident("ShotSearch"))),
    ..document()
  };
  assert_eq!(renamed.queries_name().to_string(), "ShotSearchQueries");
  assert!(landed(&renamed).contains(&"ShotSearchQueries".to_owned()));
  assert!(
    tight(&renamed).contains("typeQuery=ShotSearchQueries;"),
    "the binding kept the old stem",
  );
}

/// **THE SKIN'S OWN PREFIX DOES NOT REACH IT**, which is the axis law at the one name that is Rust
/// and never schema: the unit is folded by `MergedObject` and its SDL name appears in no schema, so
/// a word about the schema has nothing here to move.
#[test]
fn a_skin_prefix_leaves_the_unit_where_the_bones_put_it() {
  let mut model = document();
  let mut skin = super::IndexBackend::of(GraphqlKind::AsyncGraphql);
  skin.prefix = Some(syn::LitStr::new("wireFace", proc_macro2::Span::call_site()));
  model.graphql = Some(IndexGraphql {
    backends: vec![skin],
  });
  assert_eq!(model.queries_name().to_string(), "SearchDocumentQueries");
  assert!(landed(&model).contains(&"SearchDocumentQueries".to_owned()));
}

/// **A VECTOR DECLARATION IS A CITIZEN ON THE SAME TERMS** — one law, both retrieving kinds.
#[test]
fn both_retrieving_kinds_mint_the_same_citizenship() {
  let vector = embedding_index();
  assert!(vector.mounts());
  assert!(
    tight(&vector).contains("typeQuery=SegmentEmbeddingQueries;"),
    "{}",
    tight(&vector),
  );
}