upleb.uk

Public git repos — served from a NIP-34 GRASP relay at git.upleb.uk

summaryrefslogtreecommitdiff
path: root/tests/archive_grasp_services.rs
diff options
context:
space:
mode:
Diffstat (limited to 'tests/archive_grasp_services.rs')
-rw-r--r--tests/archive_grasp_services.rs225
1 files changed, 224 insertions, 1 deletions
diff --git a/tests/archive_grasp_services.rs b/tests/archive_grasp_services.rs
index a47fc55..9f13d2a 100644
--- a/tests/archive_grasp_services.rs
+++ b/tests/archive_grasp_services.rs
@@ -29,7 +29,11 @@
29 29
30mod common; 30mod common;
31 31
32use common::TestRelay; 32use common::{
33 check_ref_at_commit, create_repo_announcement, create_state_event,
34 create_test_repo_with_commit, push_to_relay, wait_for_event_served, wait_for_sync_connection,
35 CommitVariant, TestRelay,
36};
33use nostr_sdk::prelude::*; 37use nostr_sdk::prelude::*;
34use std::path::PathBuf; 38use std::path::PathBuf;
35use std::process::{Child, Command, Stdio}; 39use std::process::{Child, Command, Stdio};
@@ -376,3 +380,222 @@ async fn test_archive_multiple_grasp_services() {
376 let _ = process.kill(); 380 let _ = process.kill();
377 let _ = process.wait(); 381 let _ = process.wait();
378} 382}
383
384/// Test that archive_read_only mode creates bare git repositories and syncs data
385/// via relay-to-relay sync (purgatory sync infrastructure).
386///
387/// Scenario:
388/// 1. Start source relay with full repository (announcement + state + git data)
389/// 2. Start archive relay with archive_all=true, archive_read_only=true, syncing from source
390/// 3. Archive relay syncs announcement and state events from source
391/// 4. State events trigger purgatory sync which fetches git data from source's clone URL
392/// 5. Verify bare repository is created and git data is synced
393/// 6. Verify git pushes are rejected (read-only mode)
394#[tokio::test]
395async fn test_archive_read_only_creates_bare_repo() {
396 // 1. Start source relay
397 let source_relay = TestRelay::start().await;
398 let keys = Keys::generate();
399 let identifier = "archive-test-repo";
400
401 // Pre-allocate archive relay port so we can include it in announcement
402 let archive_port = TestRelay::find_free_port();
403 let archive_domain = format!("127.0.0.1:{}", archive_port);
404
405 // 2. Create test repository locally with deterministic commit
406 let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
407 let commit_hash = create_test_repo_with_commit(temp_dir.path(), CommitVariant::StateTest)
408 .expect("Failed to create test repo");
409
410 let npub = keys.public_key().to_bech32().expect("Failed to get npub");
411
412 // 3. Create and send announcement listing BOTH relays
413 // This ensures the archive relay will accept the state event when it syncs
414 let announcement = create_repo_announcement(
415 &keys,
416 &[&source_relay.domain(), &archive_domain],
417 identifier,
418 );
419
420 let source_client = Client::new(keys.clone());
421 source_client
422 .add_relay(source_relay.url())
423 .await
424 .expect("Failed to add source relay");
425 source_client.connect().await;
426
427 // Wait for connection
428 tokio::time::sleep(Duration::from_millis(500)).await;
429
430 // Send announcement to source relay
431 source_client
432 .send_event(&announcement)
433 .await
434 .expect("Failed to send announcement to source");
435
436 tokio::time::sleep(Duration::from_millis(200)).await;
437
438 // 4. Create and send state event
439 let clone_urls = [
440 format!(
441 "http://{}/{}/{}.git",
442 source_relay.domain(),
443 npub,
444 identifier
445 ),
446 format!("http://{}/{}/{}.git", archive_domain, npub, identifier),
447 ];
448 let relay_urls = [
449 source_relay.url().to_string(),
450 format!("ws://{}", archive_domain),
451 ];
452
453 let state_event = create_state_event(
454 &keys,
455 identifier,
456 &[("main", &commit_hash)],
457 &[],
458 &[&clone_urls[0], &clone_urls[1]],
459 &[&relay_urls[0], &relay_urls[1]],
460 )
461 .expect("Failed to create state event");
462
463 let state_event_id = state_event.id;
464
465 // Send state event to source relay (goes to purgatory - no git data yet)
466 source_client
467 .send_event(&state_event)
468 .await
469 .expect("Failed to send state event to source");
470
471 tokio::time::sleep(Duration::from_millis(200)).await;
472
473 // 5. Push git data to source relay
474 // The state event in purgatory authorizes this push
475 push_to_relay(temp_dir.path(), &source_relay.domain(), &npub, identifier)
476 .expect("Push to source should succeed");
477
478 // After push, state event should be released from purgatory on source relay
479 wait_for_event_served(source_relay.url(), &state_event_id, Duration::from_secs(5))
480 .await
481 .expect("State event should be served on source relay after push");
482
483 // 6. Start archive relay with archive_all=true, archive_read_only=true, syncing from source
484 let archive_relay = TestRelay::start_with_archive_and_sync(
485 archive_port,
486 Some(source_relay.url().to_string()),
487 false, // negentropy enabled
488 true, // archive_all
489 true, // archive_read_only
490 )
491 .await;
492
493 // Wait for sync connection to establish
494 wait_for_sync_connection(archive_relay.url(), 1, Duration::from_secs(5))
495 .await
496 .expect("Sync connection should establish");
497
498 // 7. Wait for state event to be released on archive relay
499 // The sync should:
500 // a) Fetch the announcement and state event from source relay
501 // b) Accept announcement (creates bare repo structure) - via archive mode
502 // c) Put state event in purgatory (git data missing on archive relay)
503 // d) Fetch git data from source relay's clone URL
504 // e) Release the state event from purgatory
505
506 let found = wait_for_event_served(
507 archive_relay.url(),
508 &state_event_id,
509 Duration::from_secs(30), // Allow time for sync + git fetch
510 )
511 .await;
512
513 assert!(
514 found.is_ok(),
515 "State event should be served after sync fetches git data: {:?}",
516 found.err()
517 );
518
519 // 8. Verify bare repository was created
520 let repo_path = archive_relay
521 .git_data_path()
522 .join(format!("{}/{}.git", npub, identifier));
523
524 assert!(
525 repo_path.exists(),
526 "Bare repository should be created at {:?} for archive announcement",
527 repo_path
528 );
529
530 // 9. Verify it's a bare repository (check for config file with bare = true)
531 let config_path = repo_path.join("config");
532 assert!(
533 config_path.exists(),
534 "Git config should exist at {:?}",
535 config_path
536 );
537
538 let config_content = tokio::fs::read_to_string(&config_path)
539 .await
540 .expect("Should read git config");
541 assert!(
542 config_content.contains("bare = true"),
543 "Repository at {:?} should be bare (config should contain 'bare = true')",
544 repo_path
545 );
546
547 // 10. Verify refs are correct on archive relay
548 let ref_correct = check_ref_at_commit(
549 &archive_domain,
550 &npub,
551 identifier,
552 "refs/heads/main",
553 &commit_hash,
554 )
555 .await
556 .expect("Failed to check ref");
557
558 assert!(ref_correct, "main branch should point to correct commit");
559
560 // 11. Verify git pushes are rejected (read-only mode)
561 // Create a new commit in the source repo
562 tokio::fs::write(temp_dir.path().join("new_file.txt"), "new content")
563 .await
564 .expect("Failed to write new file");
565
566 let output = tokio::process::Command::new("git")
567 .args(["add", "."])
568 .current_dir(temp_dir.path())
569 .output()
570 .await
571 .expect("Failed to git add");
572 assert!(output.status.success());
573
574 let output = tokio::process::Command::new("git")
575 .args(["commit", "-m", "New commit for push test"])
576 .current_dir(temp_dir.path())
577 .output()
578 .await
579 .expect("Failed to git commit");
580 assert!(output.status.success());
581
582 // Try to push to archive relay (should fail in read-only mode)
583 let push_url = format!("http://{}/{}/{}.git", archive_domain, npub, identifier);
584 let output = tokio::process::Command::new("git")
585 .args(["push", &push_url, "main"])
586 .current_dir(temp_dir.path())
587 .output()
588 .await
589 .expect("Failed to run git push");
590
591 assert!(
592 !output.status.success(),
593 "Git push should be rejected in archive_read_only mode. stderr: {}",
594 String::from_utf8_lossy(&output.stderr)
595 );
596
597 // Cleanup
598 source_client.disconnect().await;
599 archive_relay.stop().await;
600 source_relay.stop().await;
601}