use super::*; #[tokio::test] #[serial] async fn exact_v5_contract_replays_saved_preview_without_source_locations() { use sha2::{Digest, Sha256}; fn remove_source_locations(value: &mut serde_json::Value) { match value { serde_json::Value::Object(object) => { object.remove("source_location"); for value in object.values_mut() { remove_source_locations(value); } } serde_json::Value::Array(values) => { for value in values { remove_source_locations(value); } } _ => {} } } let registry = test_registry().await; let service = test_service( registry.clone(), test_storage_root("openapi_import_v5_compatibility"), test_auth_settings(), test_secret_crypto(), ); let workspace_id = WorkspaceId::new("ws_default"); let preview = service .preview_openapi_import(&workspace_id, openapi_upload()) .await .unwrap(); let job_id: crank_registry::ImportJobId = preview.job_id.as_str().into(); let mut payload = registry .get_import_job(&workspace_id, &job_id) .await .unwrap() .unwrap() .preview_payload; // A preview persisted under v5 has the then-current projection and digest, // without the source_location fields added by v6. let persisted_preview = payload .get_mut("preview") .expect("newly saved job includes its preview"); assert!(persisted_preview.to_string().contains("source_location")); remove_source_locations(persisted_preview); let digest = format!( "{:x}", Sha256::digest(serde_json::to_vec(persisted_preview).unwrap()) ); payload["normalization"]["normalizer_version"] = "normalized-ir-v5".into(); payload["normalization"]["projection_version"] = "preview-v5".into(); payload["normalization"]["config"]["normalizer_version"] = "normalized-ir-v5".into(); payload["normalization"]["config"]["projection_version"] = "preview-v5".into(); payload["preview_digest"] = digest.into(); // If v5 is reparsed, this deliberately absent dependency makes the replay fail. payload["dependency_snapshots"] = serde_json::json!([{ "source_id": "src_openapi_dep_v5_must_not_be_read", "digest": format!("sha256:{}", "0".repeat(64)), "canonical_uri": "https://schemas.example.test/v5.yaml" }]); sqlx::query("update import_jobs set preview_payload = $1::jsonb where id = $2") .bind(serde_json::to_string(&payload).unwrap()) .bind(job_id.as_str()) .execute(registry.pool()) .await .unwrap(); let result = service .create_openapi_import( &workspace_id, &job_id, OpenApiImportCreatePayload { selected_operation_keys: vec!["GET /v2/latest".to_owned()], server_url: Some("https://api.frankfurter.dev".to_owned()), conflict_mode: "skip".to_owned(), }, ) .await .unwrap(); assert_eq!(result.created.len(), 1); let completed = registry .get_import_job(&workspace_id, &job_id) .await .unwrap() .unwrap(); assert_eq!(completed.status, ImportJobStatus::Completed); } // Deterministic baseline source for the v5/v6 replay characterization. It uses // a local reference so the preview is self-contained (no external dependency // or ephemeral fixture port) while still exercising the reference graph. const URI_PREVIEW_DOCUMENT: &str = r#" openapi: 3.1.0 info: { title: URI preview parity, version: '1' } servers: [{ url: https://api.example.test }] paths: /items: get: operationId: listItems responses: '200': description: ok content: { application/json: { schema: { $ref: '#/components/schemas/Item' } } } components: schemas: Item: { type: object, required: [id], properties: { id: { type: string } } } "#; fn uri_preview_upload() -> OpenApiUpload { OpenApiUpload { bytes: URI_PREVIEW_DOCUMENT.as_bytes().to_vec(), mime_type: "application/yaml".to_owned(), locale: OpenApiUploadLocale::En, } } fn v5_v6_fixture_path() -> std::path::PathBuf { std::path::Path::new(env!("CARGO_MANIFEST_DIR")) .join("tests/integration/openapi_import/fixtures/v5-v6-uri-preview.json") } // AC-4 (history): a preview persisted under the historical (v5, v6) contract // must replay unchanged, and a new preview of the same source must use the // current contract. Captured from baseline execution before the URI change. #[tokio::test] #[serial] async fn v5_v6_uri_preview_replays_unchanged_and_new_preview_uses_current_contract() { let fixture_text = std::fs::read_to_string(v5_v6_fixture_path()) .expect("baseline v5/v6 URI preview fixture must be captured before the change"); let fixture: serde_json::Value = serde_json::from_str(&fixture_text).unwrap(); assert_eq!( fixture["normalization"]["normalizer_version"], serde_json::json!("normalized-ir-v5") ); assert_eq!( fixture["normalization"]["projection_version"], serde_json::json!("preview-v6") ); let fixture_preview = fixture["preview"].clone(); let fixture_digest = fixture["preview_digest"].clone(); let registry = test_registry().await; let service = test_service( registry.clone(), test_storage_root("openapi_import_v5_v6_replay"), test_auth_settings(), test_secret_crypto(), ); let workspace_id = WorkspaceId::new("ws_default"); // A current preview of the same bytes must use the current contract. let fresh = service .preview_openapi_import(&workspace_id, uri_preview_upload()) .await .unwrap(); let job_id: crank_registry::ImportJobId = fresh.job_id.as_str().into(); let fresh_job = registry .get_import_job(&workspace_id, &job_id) .await .unwrap() .unwrap(); assert_eq!( fresh_job.preview_payload["source"]["digest"], fixture["source"]["digest"], "fixture must refer to the exact captured source bytes" ); assert_eq!( fresh_job.preview_payload["normalization"]["normalizer_version"], serde_json::json!(crank_import::rest::NORMALIZER_VERSION) ); assert_eq!( fresh_job.preview_payload["normalization"]["projection_version"], serde_json::json!(crank_import::rest::PROJECTION_VERSION) ); // Bind the historical payload to this run's freshly registered source blob. let mut payload = fixture.clone(); payload["source"]["source_id"] = fresh_job.preview_payload["source"]["source_id"].clone(); sqlx::query("update import_jobs set preview_payload = $1::jsonb where id = $2") .bind(serde_json::to_string(&payload).unwrap()) .bind(job_id.as_str()) .execute(registry.pool()) .await .unwrap(); let result = service .create_openapi_import( &workspace_id, &job_id, OpenApiImportCreatePayload { selected_operation_keys: vec!["GET /items".to_owned()], server_url: None, conflict_mode: "skip".to_owned(), }, ) .await .unwrap(); assert_eq!(result.created.len(), 1); // The historical preview is replayed from its saved projection, never // recomputed under the current normalizer. let completed = registry .get_import_job(&workspace_id, &job_id) .await .unwrap() .unwrap(); assert_eq!(completed.status, ImportJobStatus::Completed); assert_eq!(completed.preview_payload["preview"], fixture_preview); assert_eq!( completed.preview_payload["normalization"]["normalizer_version"], serde_json::json!("normalized-ir-v5") ); assert_eq!( completed.preview_payload["normalization"]["projection_version"], serde_json::json!("preview-v6") ); assert_eq!(completed.preview_payload["preview_digest"], fixture_digest); } // AC-4 (version RED): a fresh preview of the fixture source must be stamped with // the approved v6 normalizer contract. This is the version RED, distinct from // the URI transport RED: it pins the exact approved literals instead of // following the current `NORMALIZER_VERSION`/`PROJECTION_VERSION` constants, // which do not by themselves prove the approved v6 value. Deliberately no // if-current branch and no skip: the assertion is unconditional and is expected // to fail until the implementation bumps the normalizer to `normalized-ir-v6`. #[tokio::test] #[serial] async fn fresh_preview_matches_approved_v6_normalizer_contract() { let registry = test_registry().await; let service = test_service( registry.clone(), test_storage_root("openapi_import_v5_v6_version_contract"), test_auth_settings(), test_secret_crypto(), ); let workspace_id = WorkspaceId::new("ws_default"); let fresh = service .preview_openapi_import(&workspace_id, uri_preview_upload()) .await .unwrap(); let job_id: crank_registry::ImportJobId = fresh.job_id.as_str().into(); let fresh_job = registry .get_import_job(&workspace_id, &job_id) .await .unwrap() .unwrap(); assert_eq!( fresh_job.preview_payload["normalization"]["normalizer_version"], serde_json::json!("normalized-ir-v6"), "approved normalizer contract is normalized-ir-v6" ); assert_eq!( fresh_job.preview_payload["normalization"]["projection_version"], serde_json::json!("preview-v6"), "approved projection contract is preview-v6" ); }