@@ -412,3 +412,292 @@ async fn generic_d_lookup_matches_before_limit() {
412412 expected. sort ( ) ;
413413 assert_eq ! ( ids, expected, "{body}" ) ;
414414}
415+
416+ #[ tokio:: test]
417+ #[ ignore = "requires Postgres" ]
418+ async fn migration_schema_artifact_content_search_finds_current_revision ( ) {
419+ // NIP-AR docs/nips/NIP-AR.md#access: readable artifacts participate in search.
420+ let pool = sqlx:: PgPool :: connect ( & crate :: test_support:: database_url ( ) )
421+ . await
422+ . unwrap ( ) ;
423+ buzz_db:: migration:: run_migrations ( & pool) . await . unwrap ( ) ;
424+ let f = Fixture :: new ( ) . await ;
425+ let create = f. revision ( Uuid :: new_v4 ( ) , "create" , None ) ;
426+ f. publish ( & create) . await ;
427+ let ( status, current) = f
428+ . request (
429+ "/query" ,
430+ json ! ( [
431+ { "artifact" : "current" , "#d" : [ create. tags. identifier( ) . unwrap( ) ] }
432+ ] ) ,
433+ )
434+ . await ;
435+ assert ! ( status. is_success( ) , "{status}: {current}" ) ;
436+ assert_eq ! ( current[ 0 ] [ "id" ] , create. id. to_hex( ) ) ;
437+ let ( status, body) = f
438+ . request (
439+ "/query" ,
440+ json ! ( [
441+ { "kinds" : [ 45010 ] , "#h" : [ f. home] , "search" : "review searchable" }
442+ ] ) ,
443+ )
444+ . await ;
445+ assert ! ( status. is_success( ) , "{status}: {body}" ) ;
446+ assert_eq ! ( body. as_array( ) . unwrap( ) . len( ) , 1 , "{body}" ) ;
447+ assert_eq ! ( body[ 0 ] [ "id" ] , create. id. to_hex( ) , "{body}" ) ;
448+ }
449+
450+ #[ tokio:: test]
451+ #[ ignore = "requires Postgres" ]
452+ async fn non_moderator_revision_author_redaction_preserves_artifact_current_state ( ) {
453+ // NIP-AR docs/nips/NIP-AR.md#moving-and-deleting: writer deletion uses a restorable tombstone (moderation-policy tension).
454+ let f = Fixture :: new ( ) . await ;
455+ let d = Uuid :: new_v4 ( ) ;
456+ let create = f. revision ( d, "create" , None ) ;
457+ f. publish ( & create) . await ;
458+ let edit = f. revision_as ( & f. peer , f. home , d, "update" , Some ( & create) ) ;
459+ f. publish ( & edit) . await ;
460+ let redact = EventBuilder :: new ( Kind :: Custom ( 9005 ) , "" )
461+ . tags ( [
462+ Tag :: parse ( [ "e" , & edit. id . to_hex ( ) ] ) . unwrap ( ) ,
463+ Tag :: parse ( [ "h" , & f. home . to_string ( ) ] ) . unwrap ( ) ,
464+ ] )
465+ . sign_with_keys ( & f. peer )
466+ . unwrap ( ) ;
467+ let ( status, redaction) = f. try_publish ( & redact) . await ;
468+ let ( query_status, current) = f
469+ . request ( "/query" , json ! ( [ { "artifact" : "current" , "#d" : [ d] } ] ) )
470+ . await ;
471+ let ( count_status, history) = f
472+ . request ( "/count" , json ! ( [ { "artifact" : "history" , "#d" : [ d] } ] ) )
473+ . await ;
474+ assert ! ( query_status. is_success( ) , "{query_status}: {current}" ) ;
475+ assert ! ( count_status. is_success( ) , "{count_status}: {history}" ) ;
476+ let recreate = EventBuilder :: new ( create. kind , "recreated task" )
477+ . tags ( create. tags . iter ( ) . cloned ( ) )
478+ . sign_with_keys ( & f. owner )
479+ . unwrap ( ) ;
480+ let ( _, recreate) = f. try_publish ( & recreate) . await ;
481+ assert_eq ! (
482+ current. as_array( ) . unwrap( ) . len( ) ,
483+ 1 ,
484+ "nonmember 9005 ({status}: {redaction}) retired the owner's artifact; \
485+ history count {history}; owner re-create: {recreate}"
486+ ) ;
487+ }
488+
489+ #[ tokio:: test]
490+ #[ ignore = "requires Postgres" ]
491+ async fn retired_artifact_cannot_be_revived_by_a_new_reader ( ) {
492+ // NIP-AR docs/nips/NIP-AR.md#moderation-and-retention: clients without the retired head cannot edit further.
493+ let f = Fixture :: new ( ) . await ;
494+ let d = Uuid :: new_v4 ( ) ;
495+ let create = f. revision ( d, "create" , None ) ;
496+ f. publish ( & create) . await ;
497+ let removal = EventBuilder :: new ( Kind :: Custom ( 9005 ) , "" )
498+ . tags ( [
499+ Tag :: parse ( [ "h" , & f. home . to_string ( ) ] ) . unwrap ( ) ,
500+ Tag :: parse ( [ "e" , & create. id . to_hex ( ) ] ) . unwrap ( ) ,
501+ ] )
502+ . sign_with_keys ( & f. owner )
503+ . unwrap ( ) ;
504+ f. publish ( & removal) . await ;
505+ // Peer has never fetched a revision. Its only source is post-redaction replay.
506+ let ( status, body) = f
507+ . request_as ( & f. peer , "/query" , json ! ( [ { "kinds" : [ 9005 ] , "#h" : [ f. home] } ] ) )
508+ . await ;
509+ assert ! ( status. is_success( ) , "{status}: {body}" ) ;
510+ let returned: Event = serde_json:: from_value ( body[ 0 ] . clone ( ) ) . unwrap ( ) ;
511+ let leaked_prev = returned
512+ . tags
513+ . iter ( )
514+ . find_map ( |t| {
515+ let p = t. as_slice ( ) ;
516+ ( p. first ( ) . map ( String :: as_str) == Some ( "e" ) ) . then ( || p[ 1 ] . clone ( ) )
517+ } )
518+ . unwrap ( ) ;
519+ let edit = EventBuilder :: new ( Kind :: Custom ( 45010 ) , "resurrected by fresh reader" )
520+ . tags ( [
521+ Tag :: parse ( [ "ar" , "1" ] ) . unwrap ( ) ,
522+ Tag :: parse ( [ "d" , & d. to_string ( ) ] ) . unwrap ( ) ,
523+ Tag :: parse ( [ "h" , & f. home . to_string ( ) ] ) . unwrap ( ) ,
524+ Tag :: parse ( [ "type" , "buzz.task" ] ) . unwrap ( ) ,
525+ Tag :: parse ( [ "title" , "resurrected" ] ) . unwrap ( ) ,
526+ Tag :: parse ( [ "op" , "update" ] ) . unwrap ( ) ,
527+ Tag :: parse ( [ "prev" , & leaked_prev] ) . unwrap ( ) ,
528+ ] )
529+ . sign_with_keys ( & f. peer )
530+ . unwrap ( ) ;
531+ let ( status, body) = f. try_publish ( & edit) . await ;
532+ assert ! (
533+ !status. is_success( ) || body[ "accepted" ] != true ,
534+ "fresh reader recovered retired prev from 9005 and revived artifact: {status}: {body}"
535+ ) ;
536+ }
537+
538+ #[ tokio:: test]
539+ #[ ignore = "requires Postgres" ]
540+ async fn generic_artifact_tag_matching_precedes_pagination ( ) {
541+ // NIP-AR docs/nips/NIP-AR.md#current-state-and-queries: tag matching applies before pagination.
542+ let f = Fixture :: new ( ) . await ;
543+ let now = nostr:: Timestamp :: now ( ) . as_secs ( ) ;
544+ let older = resign (
545+ & f. revision ( Uuid :: new_v4 ( ) , "create" , None ) ,
546+ & f. owner ,
547+ & [ [ "t" , "wanted" ] ] ,
548+ now - 60 ,
549+ ) ;
550+ let newer = resign (
551+ & f. revision ( Uuid :: new_v4 ( ) , "create" , None ) ,
552+ & f. owner ,
553+ & [ [ "t" , "other" ] ] ,
554+ now,
555+ ) ;
556+ f. publish ( & older) . await ;
557+ f. publish ( & newer) . await ;
558+ let ( status, exact) = f
559+ . request (
560+ "/query" ,
561+ json ! ( [
562+ { "artifact" : "current" , "#t" : [ "wanted" ] , "limit" : 1 }
563+ ] ) ,
564+ )
565+ . await ;
566+ assert ! ( status. is_success( ) , "{status}: {exact}" ) ;
567+ assert_eq ! ( exact[ 0 ] [ "id" ] , older. id. to_hex( ) ) ;
568+ let ( status, body) = f
569+ . request (
570+ "/query" ,
571+ json ! ( [
572+ { "kinds" : [ 45010 ] , "#h" : [ f. home] , "#t" : [ "wanted" ] , "limit" : 1 }
573+ ] ) ,
574+ )
575+ . await ;
576+ assert ! ( status. is_success( ) , "{status}: {body}" ) ;
577+ assert_eq ! ( body. as_array( ) . unwrap( ) . len( ) , 1 , "{body}" ) ;
578+ assert_eq ! ( body[ 0 ] [ "id" ] , older. id. to_hex( ) , "{body}" ) ;
579+ }
580+
581+ #[ tokio:: test]
582+ #[ ignore = "requires Postgres" ]
583+ async fn out_of_range_kinds_cannot_silently_drop_artifact_predicates ( ) {
584+ // NIP-AR docs/nips/NIP-AR.md#current-state-and-queries: unsupported predicates fail explicitly, never silently ignored.
585+ let f = Fixture :: new ( ) . await ;
586+ for project in [ "A" , "B" ] {
587+ let create = f. revision ( Uuid :: new_v4 ( ) , "create" , None ) ;
588+ let create = resign (
589+ & create,
590+ & f. owner ,
591+ & [ [ "project" , project] ] ,
592+ create. created_at . as_secs ( ) ,
593+ ) ;
594+ f. publish ( & create) . await ;
595+ }
596+ let filter = json ! ( { "kinds" : [ 110546 ] , "#h" : [ f. home] , "#project" : [ "A" ] } ) ;
597+ let ws = json ! ( [ "REQ" , "board" , filter. clone( ) ] ) . to_string ( ) ;
598+ let ws_parse = crate :: protocol:: ClientMessage :: parse ( & ws) . is_ok ( ) ;
599+ let ( query_status, query) = f. request ( "/query" , json ! ( [ filter. clone( ) ] ) ) . await ;
600+ let ( count_status, count) = f. request ( "/count" , json ! ( [ filter] ) ) . await ;
601+ assert ! (
602+ query_status == axum:: http:: StatusCode :: BAD_REQUEST
603+ && count_status == axum:: http:: StatusCode :: BAD_REQUEST
604+ && !ws_parse,
605+ "unsupported #project must fail explicitly: /query {query_status}: {query}; \
606+ /count {count_status}: {count}; WebSocket REQ accepted: {ws_parse}"
607+ ) ;
608+ }
609+
610+ #[ tokio:: test]
611+ #[ ignore = "requires Postgres" ]
612+ async fn generic_artifact_identity_matches_only_the_d_tag_first_value ( ) {
613+ // NIP-AR docs/nips/NIP-AR.md#current-state-and-queries: match the tag name and its first value in the same tag.
614+ let f = Fixture :: new ( ) . await ;
615+ let a = Uuid :: new_v4 ( ) ;
616+ let now = nostr:: Timestamp :: now ( ) . as_secs ( ) ;
617+ let older = resign ( & f. revision ( a, "create" , None ) , & f. owner , & [ ] , now - 60 ) ;
618+ f. publish ( & older) . await ;
619+ let other = f. revision ( Uuid :: new_v4 ( ) , "create" , None ) ;
620+ let newer = EventBuilder :: new ( other. kind , other. content . clone ( ) )
621+ . tags (
622+ other
623+ . tags
624+ . iter ( )
625+ . cloned ( )
626+ . chain ( [ Tag :: parse ( [ "related" , "d" , & a. to_string ( ) ] ) . unwrap ( ) ] ) ,
627+ )
628+ . custom_created_at ( nostr:: Timestamp :: from ( now) )
629+ . sign_with_keys ( & f. owner )
630+ . unwrap ( ) ;
631+ f. publish ( & newer) . await ;
632+ // Exact artifact queries correctly reject an annotation-value match.
633+ let ( status, exact) = f
634+ . request (
635+ "/query" ,
636+ json ! ( [
637+ { "artifact" : "current" , "#d" : [ a] , "limit" : 1 }
638+ ] ) ,
639+ )
640+ . await ;
641+ assert ! ( status. is_success( ) , "{status}: {exact}" ) ;
642+ assert_eq ! ( exact[ 0 ] [ "id" ] , older. id. to_hex( ) ) ;
643+ // Generic lookup must apply the same positional check before LIMIT.
644+ let ( status, body) = f
645+ . request (
646+ "/query" ,
647+ json ! ( [
648+ { "kinds" : [ 45010 ] , "#h" : [ f. home] , "#d" : [ a] , "limit" : 1 }
649+ ] ) ,
650+ )
651+ . await ;
652+ assert ! ( status. is_success( ) , "{status}: {body}" ) ;
653+ assert_eq ! ( body. as_array( ) . unwrap( ) . len( ) , 1 , "{body}" ) ;
654+ assert_eq ! ( body[ 0 ] [ "id" ] , older. id. to_hex( ) , "{body}" ) ;
655+ }
656+
657+ #[ tokio:: test]
658+ #[ ignore = "requires Postgres" ]
659+ async fn kind5_address_deletion_is_rejected_for_artifacts ( ) {
660+ // NIP-AR docs/nips/NIP-AR.md#moderation-and-retention: kind-5 requests targeting artifact revisions are explicitly rejected.
661+ let f = Fixture :: new ( ) . await ;
662+ let d = Uuid :: new_v4 ( ) ;
663+ let create = f. revision ( d, "create" , None ) ;
664+ f. publish ( & create) . await ;
665+ let coordinate = format ! ( "45010:{}:{d}" , f. owner. public_key( ) . to_hex( ) ) ;
666+ let deletion = EventBuilder :: new ( Kind :: Custom ( 5 ) , "" )
667+ . tags ( [
668+ Tag :: parse ( [ "a" , & coordinate] ) . unwrap ( ) ,
669+ Tag :: parse ( [ "k" , "45010" ] ) . unwrap ( ) ,
670+ ] )
671+ . sign_with_keys ( & f. owner )
672+ . unwrap ( ) ;
673+ let ( status, body) = f. try_publish ( & deletion) . await ;
674+ assert ! (
675+ !status. is_success( ) || body[ "accepted" ] == false ,
676+ "kind 5 {coordinate} accepted: {status}: {body}"
677+ ) ;
678+ assert ! (
679+ body. to_string( )
680+ . contains( "artifacts cannot be deleted with kind 5" ) ,
681+ "kind 5 artifact deletion needs an explicit reason: {status}: {body}"
682+ ) ;
683+ }
684+
685+ #[ tokio:: test]
686+ #[ ignore = "requires Postgres" ]
687+ async fn readable_artifact_conflict_names_current_head ( ) {
688+ // Agreed requirement 1 / crates/buzz-db/src/store/artifact.rs ArtifactOutcome contract: stale-prev conflicts name the readable head.
689+ let f = Fixture :: new ( ) . await ;
690+ let d = Uuid :: new_v4 ( ) ;
691+ let create = f. revision ( d, "create" , None ) ;
692+ f. publish ( & create) . await ;
693+ let update = f. revision ( d, "update" , Some ( & create) ) ;
694+ f. publish ( & update) . await ;
695+ let stale = f. revision_as ( & f. peer , f. home , d, "update" , Some ( & create) ) ;
696+ let ( status, body) = f. try_publish ( & stale) . await ;
697+ assert ! ( body. to_string( ) . contains( "conflict" ) , "{status}: {body}" ) ;
698+ assert ! (
699+ body. to_string( ) . contains( & update. id. to_hex( ) ) ,
700+ "conflict omits the readable head {}: {body}" ,
701+ update. id. to_hex( )
702+ ) ;
703+ }
0 commit comments