Skip to content

Commit 143e4c7

Browse files
loganjLarryclaude
committed
test(relay): first AUTH is not held by a contended roster lock
Recording an agent owner republishes the agent's rosters in the background. Cover that a held roster lock does not delay first NIP-OA AUTH, and that the roster shows the agent as `bot` once the lock frees. Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Logan Johnson <[email protected]>
1 parent 02ccba9 commit 143e4c7

1 file changed

Lines changed: 118 additions & 0 deletions

File tree

  • crates/buzz-relay/src/handlers

‎crates/buzz-relay/src/handlers/auth.rs‎

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2396,6 +2396,124 @@ mod tests {
23962396
assert!(!watcher.cancel.is_cancelled(), "the bystander stays");
23972397
}
23982398

2399+
/// First AUTH with a NIP-OA credential records the owner, which makes
2400+
/// the agent's ordinary rows show `bot`. The rosters must follow, but
2401+
/// a contended roster lock must not hold AUTH: the pending AUTH
2402+
/// expires after 5 s, the same as the default lock timeout.
2403+
///
2404+
/// Mutation: await the roster republish inside
2405+
/// `materialize_nip_oa_owner` → AUTH waits on the held lock → RED.
2406+
#[tokio::test]
2407+
#[ignore = "requires Postgres — runs in postgres-ci nextest lane"]
2408+
async fn first_auth_is_not_held_by_a_contended_roster_lock() {
2409+
use buzz_core::channel::{ChannelType, ChannelVisibility, MemberRole};
2410+
use std::sync::Arc;
2411+
let state = auth_test_state_real_db_expect().await;
2412+
let community = seeded_community(&state).await;
2413+
let (creator, owner, agent) = (Keys::generate(), Keys::generate(), Keys::generate());
2414+
for key in [&creator, &agent] {
2415+
state
2416+
.db
2417+
.ensure_user(community, &key.public_key().to_bytes())
2418+
.await
2419+
.expect("seed user");
2420+
}
2421+
let channel = state
2422+
.db
2423+
.create_channel(
2424+
community,
2425+
"roster-lock",
2426+
ChannelType::Stream,
2427+
ChannelVisibility::Open,
2428+
None,
2429+
&creator.public_key().to_bytes(),
2430+
None,
2431+
)
2432+
.await
2433+
.expect("create channel")
2434+
.id;
2435+
state
2436+
.db
2437+
.add_member(
2438+
community,
2439+
channel,
2440+
&agent.public_key().to_bytes(),
2441+
MemberRole::Member,
2442+
Some(&creator.public_key().to_bytes()),
2443+
)
2444+
.await
2445+
.expect("add unowned agent");
2446+
2447+
// Hold the roster lock the republish needs.
2448+
let relay_pubkey = state.relay_keypair.public_key().to_bytes();
2449+
let held = state
2450+
.db
2451+
.lock_member_snapshot(community, channel, &relay_pubkey)
2452+
.await
2453+
.expect("hold roster lock");
2454+
2455+
let auth_tag = buzz_sdk::nip_oa::compute_auth_tag(&owner, &agent.public_key(), "")
2456+
.expect("sign NIP-OA credential");
2457+
let auth_tag: Vec<String> = serde_json::from_str(&auth_tag).expect("tag JSON");
2458+
let (conn, _ctrl) = registered_pending_conn(&state, community, "roster-lock");
2459+
tokio::time::timeout(
2460+
std::time::Duration::from_secs(2),
2461+
handle_auth(
2462+
signed_auth(&agent, "roster-lock", Some(auth_tag)),
2463+
Arc::clone(&conn),
2464+
Arc::clone(&state),
2465+
),
2466+
)
2467+
.await
2468+
.expect("AUTH must not wait on the roster lock");
2469+
assert!(
2470+
matches!(conn.auth_state_snapshot(), AuthState::Authenticated(_)),
2471+
"the agent is admitted while the roster lock is held"
2472+
);
2473+
assert!(state
2474+
.db
2475+
.is_agent_owner(
2476+
community,
2477+
agent.public_key().as_bytes(),
2478+
owner.public_key().as_bytes(),
2479+
)
2480+
.await
2481+
.expect("read owner"));
2482+
2483+
// Once the lock is free, the background republish names the agent
2484+
// `bot` in the live roster.
2485+
held.release().await.expect("release roster lock");
2486+
let agent_hex = agent.public_key().to_hex();
2487+
let deadline = Instant::now() + std::time::Duration::from_secs(10);
2488+
loop {
2489+
let live: Option<serde_json::Value> = sqlx::query_scalar(
2490+
"SELECT tags FROM events WHERE community_id = $1 AND channel_id = $2 \
2491+
AND kind = 39002 AND deleted_at IS NULL",
2492+
)
2493+
.bind(community.as_uuid())
2494+
.bind(channel)
2495+
.fetch_optional(state.db.pool())
2496+
.await
2497+
.expect("read live roster");
2498+
let is_bot = live
2499+
.as_ref()
2500+
.and_then(|tags| tags.as_array())
2501+
.is_some_and(|tags| {
2502+
tags.iter().any(|tag| {
2503+
tag[0] == "p" && tag[1] == agent_hex.as_str() && tag[3] == "bot"
2504+
})
2505+
});
2506+
if is_bot {
2507+
break;
2508+
}
2509+
assert!(
2510+
Instant::now() < deadline,
2511+
"the roster must converge to bot after the lock is released: {live:?}"
2512+
);
2513+
tokio::time::sleep(std::time::Duration::from_millis(50)).await;
2514+
}
2515+
}
2516+
23992517
/// An untagged AUTH that read a NULL stored owner just before a
24002518
/// concurrent owner link commits must not end up admitted ownerless:
24012519
/// either the link's disconnect closes it, or it carries the owner.

0 commit comments

Comments
 (0)