Files
crank/crates/crank-registry/tests/integration/resource_drafts/resource_publications.rs
T
Hermes DevOps 75fe5a123c [skip ci] wip: preserve Hermes crank development checkpoint
Owner-authorized recovery of accumulated source changes.
Includes accepted work and in-progress changes; NOT a release or acceptance.
Parent baseline 1b6db09bd1. Runtime data, build caches and local credentials excluded.
2026-10-25 10:00:22 +00:00

261 lines
11 KiB
Rust

//! Story 3.7 / AC03 registry-level: immutable publication ownership, CAS и отказ
//! без partial writes.
//!
//! Проверяется на реальном PostgreSQL:
//! - публикация создаёт отдельную owning-ссылку на тот же immutable blob;
//! - Draft edit после публикации не отсоединяет publication source и не меняет snapshot;
//! - повторный/stale publish и отказ по несовпавшему snapshot не пишут строк и не
//! сдвигают ревизию карточки.
use super::*;
use crank_core::resource::ResourceError;
use crank_registry::model::resource::{PublishResourceVersionRequest, ResourcePublicationRecord};
async fn publish_current(sandbox: &Sandbox, resource_id: &ResourceId) -> ResourcePublicationRecord {
let current = sandbox
.registry
.get_resource_draft(WORKSPACE_ID, resource_id)
.await
.expect("draft must be readable");
sandbox
.registry
.publish_resource_version(PublishResourceVersionRequest {
workspace_id: WorkspaceId::new(WORKSPACE_ID),
resource_id: resource_id.clone(),
expected_updated_at: current.resource.updated_at,
expected_version: current.version.version,
expected_digest: current.version.digest.clone(),
expected_size_bytes: current.version.size_bytes,
artifact: registered(sandbox, b"registry payload"),
published_at: OffsetDateTime::now_utc(),
})
.await
.expect("a valid current draft must publish")
}
#[tokio::test]
async fn story37_at03_publication_owns_blob_independently_of_draft_edit() {
let sandbox = sandbox("at37_publication_owning").await;
let resource_id = seeded_draft(&sandbox).await;
let publication = publish_current(&sandbox, &resource_id).await;
assert_eq!(publication.version, 1);
assert_eq!(publication.uri, "crank://registry/draft");
assert_eq!(publication.kind, "text");
assert_eq!(publication.mime_type, "text/plain");
assert_eq!(publication.sensitivity, "internal");
assert_eq!(publication.size_bytes, 16);
assert_eq!(publication.digest, sha256_hex(b"registry payload"));
let status: String = sqlx::query_scalar("select status from resources where resource_id = $1")
.bind(resource_id.as_str())
.fetch_one(sandbox.registry.pool())
.await
.expect("resources must be queryable");
assert_eq!(
status, "published",
"publish must switch the card to published"
);
let draft_source: String = sqlx::query_scalar(
"select artifact_source_id from resource_versions where resource_id = $1 and superseded_at is null",
)
.bind(resource_id.as_str())
.fetch_one(sandbox.registry.pool())
.await
.expect("draft source must be queryable");
let publication_source: String = sqlx::query_scalar(
"select artifact_source_id from resource_publications where resource_id = $1 and version = 1",
)
.bind(resource_id.as_str())
.fetch_one(sandbox.registry.pool())
.await
.expect("publication source must be queryable");
assert_ne!(
draft_source, publication_source,
"publication must own a dedicated source, not the mutable draft owning ref"
);
// Draft edit: новая версия, новая draft-ссылка, карточка снова Draft.
let current = sandbox
.registry
.get_resource_draft(WORKSPACE_ID, &resource_id)
.await
.expect("draft must be readable");
sandbox
.registry
.update_resource_draft(UpdateResourceDraftRequest {
workspace_id: WorkspaceId::new(WORKSPACE_ID),
resource_id: resource_id.clone(),
description: "edited after publication".to_owned(),
mime_type: "text/plain".to_owned(),
sensitivity: "public".to_owned(),
expected_updated_at: current.resource.updated_at,
artifact: registered(&sandbox, b"edited payload"),
})
.await
.expect("draft edit after publication must succeed");
let status: String = sqlx::query_scalar("select status from resources where resource_id = $1")
.bind(resource_id.as_str())
.fetch_one(sandbox.registry.pool())
.await
.expect("resources must be queryable");
assert_eq!(
status, "draft",
"a Draft edit must return the card to draft"
);
let publication_lifecycle: String = sqlx::query_scalar(
"select lifecycle from artifact_sources where workspace_id = $1 and source_id = $2",
)
.bind(WORKSPACE_ID)
.bind(&publication_source)
.fetch_one(sandbox.registry.pool())
.await
.expect("publication source must still exist");
assert_eq!(
publication_lifecycle, "active",
"a Draft edit must not detach the publication owning source"
);
// Snapshot остаётся exact и читается через publication owning source.
let resolved = sandbox
.registry
.resolve_resource_publication(WORKSPACE_ID, &resource_id, 1)
.await
.expect("published version must stay resolvable after the edit");
assert_eq!(resolved.publication, publication);
let bytes = sandbox
.store
.read(&resolved.artifact_ref)
.expect("published bytes must stay readable");
assert_eq!(bytes, b"registry payload");
}
async fn totals(sandbox: &Sandbox) -> (i64, i64, i64, i64, i64) {
(
count(sandbox, "select count(*) from resources").await,
count(sandbox, "select count(*) from resource_versions").await,
count(sandbox, "select count(*) from artifact_sources").await,
count(sandbox, "select count(*) from artifact_blobs").await,
count(sandbox, "select count(*) from resource_publications").await,
)
}
#[tokio::test]
async fn story37_at03_stale_repeat_and_refusal_never_write_rows() {
let sandbox = sandbox("at37_publication_stale").await;
let resource_id = seeded_draft(&sandbox).await;
let card_before = sandbox
.registry
.get_resource_draft(WORKSPACE_ID, &resource_id)
.await
.expect("card must be readable");
// Отказ по несовпавшему snapshot (TOCTOU) — stale и без записей, ещё до публикации.
let initial = totals(&sandbox).await;
let mismatched = sandbox
.registry
.publish_resource_version(PublishResourceVersionRequest {
workspace_id: WorkspaceId::new(WORKSPACE_ID),
resource_id: resource_id.clone(),
expected_updated_at: card_before.resource.updated_at,
expected_version: card_before.version.version,
expected_digest: sha256_hex(b"not the stored payload"),
expected_size_bytes: card_before.version.size_bytes,
artifact: registered(&sandbox, b"registry payload"),
published_at: OffsetDateTime::now_utc(),
})
.await
.expect_err("a snapshot mismatch must be refused");
assert!(matches!(mismatched, ResourceError::Stale));
assert_eq!(
totals(&sandbox).await,
initial,
"a refused snapshot must not write rows"
);
let publication = sandbox
.registry
.publish_resource_version(PublishResourceVersionRequest {
workspace_id: WorkspaceId::new(WORKSPACE_ID),
resource_id: resource_id.clone(),
expected_updated_at: card_before.resource.updated_at,
expected_version: card_before.version.version,
expected_digest: card_before.version.digest.clone(),
expected_size_bytes: card_before.version.size_bytes,
artifact: registered(&sandbox, b"registry payload"),
published_at: OffsetDateTime::now_utc(),
})
.await
.expect("a valid current draft must publish");
assert_eq!(publication.version, 1);
let after_publish = totals(&sandbox).await;
let published_card = sandbox
.registry
.get_resource_draft(WORKSPACE_ID, &resource_id)
.await
.expect("card must be readable");
assert!(
published_card.resource.updated_at > card_before.resource.updated_at,
"a successful publish must advance the CAS revision monotonically"
);
// Повтор той же публикации с прежней карточкой: CAS уже сдвинут.
let repeated = sandbox
.registry
.publish_resource_version(PublishResourceVersionRequest {
workspace_id: WorkspaceId::new(WORKSPACE_ID),
resource_id: resource_id.clone(),
expected_updated_at: card_before.resource.updated_at,
expected_version: 1,
expected_digest: card_before.version.digest.clone(),
expected_size_bytes: card_before.version.size_bytes,
artifact: registered(&sandbox, b"registry payload"),
published_at: OffsetDateTime::now_utc(),
})
.await
.expect_err("repeating the same publish must be stale");
assert!(matches!(repeated, ResourceError::Stale));
// Повтор со свежей карточкой без правки Draft: версия уже опубликована (no duplicate).
let duplicate = sandbox
.registry
.publish_resource_version(PublishResourceVersionRequest {
workspace_id: WorkspaceId::new(WORKSPACE_ID),
resource_id: resource_id.clone(),
expected_updated_at: published_card.resource.updated_at,
expected_version: published_card.version.version,
expected_digest: published_card.version.digest.clone(),
expected_size_bytes: published_card.version.size_bytes,
artifact: registered(&sandbox, b"registry payload"),
published_at: OffsetDateTime::now_utc(),
})
.await
.expect_err("re-publishing an unchanged published draft must be stale");
assert!(matches!(duplicate, ResourceError::Stale));
assert_eq!(
totals(&sandbox).await,
after_publish,
"stale/refused publications must not write rows"
);
let card_after = sandbox
.registry
.get_resource_draft(WORKSPACE_ID, &resource_id)
.await
.expect("card must be readable");
assert_eq!(
card_after.resource.updated_at, published_card.resource.updated_at,
"a refused publish must not advance the CAS revision"
);
let resolved = sandbox
.registry
.resolve_resource_publication(WORKSPACE_ID, &resource_id, 1)
.await
.expect("published version must stay readable");
assert_eq!(resolved.publication, publication);
}