Files
bsodfather e93de7015a
CI / Rust Checks (pull_request) Failing after 1s
CI / UI Checks (pull_request) Skipped
CI / Frontend E2E (pull_request) Skipped
CI / Community Image Smoke (pull_request) Skipped
CI / Deploy (pull_request) Skipped
feat(import): complete OpenAPI onboarding and verify delivery
2026-10-09 17:50:38 +03:00

161 lines
6.4 KiB
Rust

use super::*;
use std::{future::Future, pin::Pin, task::Poll};
use crank_core::{OperationId, ProductEventId, WorkspaceId};
use crank_registry::{
OnboardingPresentationMilestone, PostgresRegistry, RecordOnboardingMilestoneRequest,
RegisterOnboardingImportContextRequest, SelectOnboardingImportDraftRequest,
};
use time::OffsetDateTime;
async fn poll_once_is_pending<F: Future>(mut future: Pin<&mut F>) -> bool {
std::future::poll_fn(|cx| Poll::Ready(future.as_mut().poll(cx).is_pending())).await
}
#[tokio::test(flavor = "multi_thread")]
#[serial]
async fn stale_same_kind_replay_returns_projection_after_concurrent_change() {
let (registry, database_url) = test_registry_with_single_connection().await;
let pool = registry.pool().clone();
let external_registry = PostgresRegistry::connect(&database_url).await.unwrap();
let raw = sqlx::PgPool::connect(&database_url).await.unwrap();
let workspace_id = WorkspaceId::new("ws_default");
insert_admin_import_fixture(&raw).await;
let service = test_service(
registry.clone(),
test_storage_root("onboarding_stale_replay"),
test_auth_settings(),
test_secret_crypto(),
);
let now = OffsetDateTime::now_utc();
let initial = registry
.ensure_onboarding_eligibility_snapshot(&workspace_id, now)
.await
.unwrap();
let registered = registry
.register_onboarding_import_context_snapshot(RegisterOnboardingImportContextRequest {
workspace_id: &workspace_id,
application_idempotency_key: "idem_admin_import",
plan_digest: &"b".repeat(64),
expected_revision: initial.projection.revision,
occurred_at: now,
})
.await
.unwrap();
let selected = registry
.select_onboarding_import_draft_snapshot(SelectOnboardingImportDraftRequest {
workspace_id: &workspace_id,
application_idempotency_key: "idem_admin_import",
plan_digest: &"b".repeat(64),
operation_id: &OperationId::new("op_admin_import"),
operation_version: 1,
expected_revision: registered.projection.revision,
occurred_at: now,
})
.await
.unwrap();
let legacy_key = "legacy-started-replay";
registry
.record_onboarding_milestone_snapshot(RecordOnboardingMilestoneRequest {
workspace_id: &workspace_id,
event_id: &ProductEventId::new("pe_legacy_started_replay"),
milestone: OnboardingPresentationMilestone::Started,
idempotency_key: legacy_key,
expected_revision: selected.projection.revision,
occurred_at: now,
eligible_since: initial.projection.eligible_since,
})
.await
.unwrap();
let before = registry
.ensure_onboarding_eligibility_snapshot(&workspace_id, now)
.await
.unwrap();
assert!(before.import_context.as_ref().unwrap().choices[0].selectable);
let mut service_call = Box::pin(service.record_onboarding_event(
&workspace_id,
admin_api::dto::OnboardingEventPayload {
event: "started".to_owned(),
idempotency_key: legacy_key.to_owned(),
expected_revision: before.projection.revision,
completed_steps: None,
},
));
// Queue the service's workspace lookup ahead of the first connection
// barrier, then prove that the first onboarding snapshot is queued ahead
// of the second barrier. sqlx's pool acquisition queue is FIFO.
let held = pool.acquire().await.unwrap();
assert!(poll_once_is_pending(service_call.as_mut()).await);
let mut first_barrier = Box::pin(pool.acquire());
assert!(poll_once_is_pending(first_barrier.as_mut()).await);
drop(held);
let first_connection = tokio::select! {
connection = first_barrier.as_mut() => connection.unwrap(),
_ = service_call.as_mut() => panic!("service call completed before first snapshot barrier"),
};
assert!(poll_once_is_pending(service_call.as_mut()).await);
let mut second_barrier = Box::pin(pool.acquire());
assert!(poll_once_is_pending(second_barrier.as_mut()).await);
drop(first_connection);
let second_connection = tokio::select! {
connection = second_barrier.as_mut() => connection.unwrap(),
_ = service_call.as_mut() => panic!("service call completed before second snapshot barrier"),
};
assert!(poll_once_is_pending(service_call.as_mut()).await);
// The first snapshot has completed and the event write is now queued
// behind the second barrier. Change the projection and import context from
// an independent registry connection before allowing that write to observe state.
sqlx::query(
"update operations set status = 'archived', updated_at = now()
where workspace_id = 'ws_default' and id = 'op_admin_import'",
)
.execute(&raw)
.await
.unwrap();
let pre_mutation = external_registry
.ensure_onboarding_eligibility_snapshot(&workspace_id, now)
.await
.unwrap();
external_registry
.record_onboarding_milestone_snapshot(RecordOnboardingMilestoneRequest {
workspace_id: &workspace_id,
event_id: &ProductEventId::new("pe_replay_race_mutation"),
milestone: OnboardingPresentationMilestone::Dismissed,
idempotency_key: "mutation-between-snapshots",
expected_revision: pre_mutation.projection.revision,
occurred_at: now,
eligible_since: None,
})
.await
.unwrap();
let fresh = external_registry
.ensure_onboarding_eligibility_snapshot(&workspace_id, now)
.await
.unwrap();
assert_ne!(fresh.projection.revision, pre_mutation.projection.revision);
assert!(!fresh.import_context.as_ref().unwrap().choices[0].selectable);
assert!(fresh.import_context.as_ref().unwrap().drift.is_some());
drop(second_connection);
let response = service_call.await.unwrap();
assert!(!response.accepted);
assert_eq!(
response.onboarding.revision, fresh.projection.revision,
"same-kind idempotency replay must return the latest projection after the concurrent mutation"
);
assert!(!response.onboarding.import_context.as_ref().unwrap().choices[0].selectable);
assert!(
response
.onboarding
.import_context
.as_ref()
.unwrap()
.drift
.is_some()
);
}