mod support;
use async_trait::async_trait;
use std::time::SystemTime;
use futures::executor::block_on;
use support::{body_bytes, catalog_id, fixture_writer, request, served_from};
use turul_ai_catalog::identity::{EntryKey, WELL_KNOWN_PATH};
use turul_ai_catalog::parse::parse;
use turul_ai_catalog::validate::{Conformance, Severity, validate};
use turul_ai_catalog_lambda::{
ColdStartError, ServedCatalog, catalog_id as id_from, handle, max_age,
};
use turul_ai_catalog_storage::{
AssembleError, CatalogId, CatalogStore, Condition, EntryRecord, Etag, HostRecord, ImportError,
InMemoryCatalogStore, Provenance, StoreError, Stored, StoredEntry, import,
};
const SPEC_EXAMPLE: &str = include_str!("fixtures/ai-catalog.json");
const ENTRY: &str = r#"{"identifier":"urn:example:a","type":"application/a2a-agent-card+json","url":"https://example.com/a.json"}"#;
fn doc(entry: &str) -> String {
format!(r#"{{"specVersion":"1.0","entries":[{entry}]}}"#)
}
fn with_jws(jws: &str) -> String {
let start = SPEC_EXAMPLE.find(r#""jws": ""#).expect("jws member") + r#""jws": ""#.len();
let end = start + SPEC_EXAMPLE[start..].find('"').expect("jws end");
format!("{}{}{}", &SPEC_EXAMPLE[..start], jws, &SPEC_EXAMPLE[end..])
}
fn assert_starts_and_serves(document: &str) {
let served = served_from(document);
let r = handle(&served, &request("GET", WELL_KNOWN_PATH));
assert_eq!(r.status().as_u16(), 200);
let body = body_bytes(&r);
assert!(!validate(&parse(&body).expect("body parses")).has_errors());
}
struct Unreachable;
#[async_trait]
impl CatalogStore for Unreachable {
fn backend_name(&self) -> &'static str {
"unreachable"
}
async fn put_entry(
&self,
_: &CatalogId,
_: &EntryRecord,
_: &Provenance,
_: Condition,
) -> Result<Etag, StoreError> {
Err(StoreError::Backend("unreachable".into()))
}
async fn get_entry(
&self,
_: &CatalogId,
_: &EntryKey,
) -> Result<Option<StoredEntry>, StoreError> {
Err(StoreError::Backend("unreachable".into()))
}
async fn delete_entry(
&self,
_: &CatalogId,
_: &EntryKey,
_: Option<&Etag>,
) -> Result<(), StoreError> {
Err(StoreError::Backend("unreachable".into()))
}
async fn list_entries(&self, _: &CatalogId) -> Result<Vec<StoredEntry>, StoreError> {
Err(StoreError::Backend("unreachable".into()))
}
async fn put_host(
&self,
_: &CatalogId,
_: &HostRecord,
_: Condition,
) -> Result<Etag, StoreError> {
Err(StoreError::Backend("unreachable".into()))
}
async fn get_host(&self, _: &CatalogId) -> Result<Option<Stored<HostRecord>>, StoreError> {
Err(StoreError::Backend("unreachable".into()))
}
}
struct WrittenBeforeTheGuard(Vec<EntryRecord>);
fn written_before_the_guard() -> WrittenBeforeTheGuard {
let entry = |document: &str| {
let entry = parse(document.as_bytes())
.expect("parses")
.entries
.remove(0);
EntryRecord::new(entry).expect("entry passes the record gate")
};
WrittenBeforeTheGuard(vec![
entry(&doc(&ENTRY.replace("}", r#","version":"1"}"#))),
entry(&doc(ENTRY)),
])
}
#[async_trait]
impl CatalogStore for WrittenBeforeTheGuard {
fn backend_name(&self) -> &'static str {
"written-before-the-guard"
}
async fn put_entry(
&self,
_: &CatalogId,
_: &EntryRecord,
_: &Provenance,
_: Condition,
) -> Result<Etag, StoreError> {
unreachable!()
}
async fn get_entry(
&self,
_: &CatalogId,
_: &EntryKey,
) -> Result<Option<StoredEntry>, StoreError> {
unreachable!()
}
async fn delete_entry(
&self,
_: &CatalogId,
_: &EntryKey,
_: Option<&Etag>,
) -> Result<(), StoreError> {
unreachable!()
}
async fn list_entries(&self, _: &CatalogId) -> Result<Vec<StoredEntry>, StoreError> {
Ok(self
.0
.iter()
.map(|r| {
let provenance = Provenance::Curated {
writer: fixture_writer(),
};
StoredEntry {
record: r.clone(),
etag: Etag::of_entry(r, &provenance).unwrap(),
provenance,
}
})
.collect())
}
async fn put_host(
&self,
_: &CatalogId,
_: &HostRecord,
_: Condition,
) -> Result<Etag, StoreError> {
unreachable!()
}
async fn get_host(&self, _: &CatalogId) -> Result<Option<Stored<HostRecord>>, StoreError> {
Ok(None)
}
}
#[test]
fn empty_store_serves_a_valid_empty_catalog() {
let served = block_on(ServedCatalog::assemble(
&InMemoryCatalogStore::new(),
&catalog_id(),
SystemTime::now(),
))
.expect("empty store assembles");
let r = handle(&served, &request("GET", WELL_KNOWN_PATH));
assert_eq!(body_bytes(&r), br#"{"entries":[],"specVersion":"1.0"}"#);
}
#[test]
fn invalid_store_fails_cold_start() {
match block_on(ServedCatalog::assemble(
&written_before_the_guard(),
&catalog_id(),
SystemTime::now(),
)) {
Err(ColdStartError::Assemble(AssembleError::Invalid(report))) => {
assert!(report.has_errors())
}
Err(e) => panic!("wrong error: {e}"),
Ok(_) => panic!("an error diagnostic must fail cold start"),
}
match block_on(ServedCatalog::assemble(
&Unreachable,
&catalog_id(),
SystemTime::now(),
)) {
Err(ColdStartError::Assemble(AssembleError::Store(StoreError::Backend(_)))) => {}
Err(e) => panic!("wrong error: {e}"),
Ok(_) => panic!("an unreachable store must fail cold start"),
}
}
#[test]
fn cold_start_error_names_each_error_code_and_pointer() {
let store = written_before_the_guard();
let Err(err) = block_on(ServedCatalog::assemble(
&store,
&catalog_id(),
SystemTime::now(),
)) else {
panic!("must fail cold start");
};
let ColdStartError::Assemble(AssembleError::Invalid(report)) = &err else {
panic!("wrong error: {err}");
};
let text = err.to_string();
let errors: Vec<_> = report
.diagnostics()
.iter()
.filter(|d| d.severity == Severity::Error)
.collect();
assert!(!errors.is_empty());
for d in errors {
assert!(text.contains(d.code.as_str()), "{text}");
assert!(text.contains(&d.path.to_string()), "{text}");
}
}
#[cfg(feature = "dynamodb")]
#[test]
fn missing_table_configuration_fails_cold_start() {
use turul_ai_catalog_lambda::table_name;
for raw in [None, Some("")] {
match table_name(raw) {
Err(ColdStartError::Config(m)) => assert!(m.contains("TURUL_AI_CATALOG_TABLE"), "{m}"),
other => panic!("{other:?}"),
}
}
assert_eq!(table_name(Some("t")).expect("named"), "t");
match id_from(None) {
Err(ColdStartError::Config(m)) => assert!(m.contains("TURUL_AI_CATALOG_ID"), "{m}"),
other => panic!("{other:?}"),
}
}
#[test]
fn catalog_id_and_refresh_interval_are_validated() {
assert_eq!(id_from(Some("prod.v1")).expect("valid").as_str(), "prod.v1");
assert!(matches!(
id_from(Some("a b")),
Err(ColdStartError::Config(_))
));
assert_eq!(max_age(None).expect("default").as_secs(), 60);
assert_eq!(max_age(Some("0")).expect("zero").as_secs(), 0);
assert!(matches!(
max_age(Some("soon")),
Err(ColdStartError::Config(_))
));
assert!(matches!(
max_age(Some("-1")),
Err(ColdStartError::Config(_))
));
}
#[test]
fn warnings_do_not_fail_cold_start() {
let unknown_digest = doc(&format!(
r#"{{"identifier":"urn:example:a","type":"application/a2a-agent-card+json","url":"https://example.com/a.json","digest":"sha3-256:{}"}}"#,
"a".repeat(64)
));
let report = validate(&parse(unknown_digest.as_bytes()).expect("parses"));
assert!(
report
.diagnostics()
.iter()
.any(|d| d.code.as_str() == "digest-algorithm-unknown"
&& d.severity == Severity::Warning)
);
assert_starts_and_serves(&unknown_digest);
let mut nested = format!(r#"{{"specVersion":"1.0","entries":[{ENTRY}]}}"#);
for n in 0..4 {
nested = format!(
r#"{{"specVersion":"1.0","entries":[{{"identifier":"urn:example:n{n}","type":"application/ai-catalog+json","data":{nested}}}]}}"#
);
}
let report = validate(&parse(nested.as_bytes()).expect("parses"));
assert_eq!(report.conformance(), Conformance::Undetermined);
assert!(!report.has_errors());
assert_starts_and_serves(&nested);
}
#[test]
fn signature_defects_do_not_fail_cold_start() {
assert_starts_and_serves(&with_jws("e30..detached-jws-signature"));
assert_starts_and_serves(&with_jws("eyJhbGciOiJub25lIn0..detached-jws-signature"));
let signer = SPEC_EXAMPLE
.replacen(
r#""contributor": "did:web:acme-corp.com""#,
r#""contributor": "did:web:Example.com""#,
1,
)
.replacen(
r#""signer": "did:web:acme-corp.com""#,
r#""signer": "did:web:Example.com""#,
1,
);
assert_eq!(signer.matches("did:web:Example.com").count(), 2);
assert_starts_and_serves(&signer);
}
#[test]
fn url_nested_children_are_not_fetched_at_cold_start() {
assert_starts_and_serves(&doc(
r#"{"identifier":"urn:example:child","type":"application/ai-catalog+json","url":"https://127.0.0.1/x.json"}"#,
));
}
#[test]
fn non_i_json_numbers_are_refused_before_they_can_be_served() {
let document = parse(
doc(r#"{"identifier":"urn:example:a","type":"application/vnd.example+json","data":{"n":1e400}}"#)
.as_bytes(),
)
.expect("parses");
let store = InMemoryCatalogStore::new();
assert!(matches!(
block_on(import(&store, &catalog_id(), &document, &fixture_writer())),
Err(ImportError::Record(_))
));
}