kind_test.mx raw

   1  package protocol
   2  
   3  import "testing"
   4  
   5  func TestKindNames(t *testing.T) {
   6  	if n := KindName(KindTrack); n != "track" {
   7  		t.Fatalf("KindName(32210) = %q", n)
   8  	}
   9  	if n := KindName(KindAccessFilter); n != "access-filter" {
  10  		t.Fatalf("KindName(32217) = %q", n)
  11  	}
  12  	if n := KindName(12345); n != "unknown" {
  13  		t.Fatalf("KindName(12345) = %q", n)
  14  	}
  15  }
  16  
  17  func TestKindBlocks(t *testing.T) {
  18  	addressable := []uint16{
  19  		KindTrack, KindAlbum, KindPlaylist, KindSetlist,
  20  		KindReleaseCredits, KindTrackCredits, KindEdition, KindAccessFilter,
  21  	}
  22  	regular := []uint16{
  23  		KindPlayReport, KindPurchaseOrder, KindDeliveryReceipt,
  24  		KindHostAggregate, KindPublisherRollup,
  25  	}
  26  	for _, k := range addressable {
  27  		if !IsMusiquay(k) || !IsAddressable(k) || IsRegular(k) {
  28  			t.Errorf("kind %d is not classified as addressable-only", k)
  29  		}
  30  	}
  31  	for _, k := range regular {
  32  		if !IsMusiquay(k) || !IsRegular(k) || IsAddressable(k) {
  33  			t.Errorf("kind %d is not classified as regular-only", k)
  34  		}
  35  	}
  36  	if IsMusiquay(KindCreditLabel) || IsMusiquay(KindStall) {
  37  		t.Fatal("a reused NIP kind is reported as a Musiquay kind")
  38  	}
  39  }
  40  
  41  func TestAddressableBlockIsContiguous(t *testing.T) {
  42  	for k := uint16(32210); k <= 32217; k++ {
  43  		if !IsAddressable(k) {
  44  			t.Fatalf("kind %d in the addressable block is not addressable", k)
  45  		}
  46  		if !InReplaceableRange(k) {
  47  			t.Fatalf("addressable kind %d is outside NIP-01's replaceable range", k)
  48  		}
  49  	}
  50  }
  51  
  52  // The registry puts three regular, counted kinds inside NIP-01's replaceable
  53  // range. That is a hazard, not a feature: a relay applying the range rule
  54  // without an exception would keep one receipt per host. This test pins the
  55  // exact set so a renumbering is visible, and so storage policy has a list.
  56  func TestRegularKindsInReplaceableRange(t *testing.T) {
  57  	ks := RegularInReplaceableRange()
  58  	want := []uint16{KindDeliveryReceipt, KindHostAggregate, KindPublisherRollup}
  59  	if len(ks) != len(want) {
  60  		t.Fatalf("RegularInReplaceableRange() = %v", ks)
  61  	}
  62  	for i, k := range ks {
  63  		if k != want[i] {
  64  			t.Fatalf("RegularInReplaceableRange()[%d] = %d, want %d", i, k, want[i])
  65  		}
  66  		if !IsRegular(k) {
  67  			t.Fatalf("kind %d listed as regular is not regular", k)
  68  		}
  69  		if !InReplaceableRange(k) {
  70  			t.Fatalf("kind %d listed as in-range is not", k)
  71  		}
  72  	}
  73  	// The counted kinds outside the range must not be listed.
  74  	if InReplaceableRange(KindPlayReport) || InReplaceableRange(KindPurchaseOrder) {
  75  		t.Fatal("a regular kind outside the replaceable range is reported inside it")
  76  	}
  77  }
  78  
  79  func TestPrivateKinds(t *testing.T) {
  80  	if !IsPrivate(KindPurchaseOrder) || !IsPrivate(KindDeliveryReceipt) {
  81  		t.Fatal("the two wrapped kinds must be private")
  82  	}
  83  	if IsPrivate(KindPlayReport) || IsPrivate(KindHostAggregate) {
  84  		t.Fatal("a public kind is reported as private")
  85  	}
  86  }
  87  
  88  func TestKnownRole(t *testing.T) {
  89  	if !KnownRole("sound-foh") {
  90  		t.Fatal("roster role not known")
  91  	}
  92  	if !KnownRole("mastering-engineer") {
  93  		t.Fatal("credit role not known")
  94  	}
  95  	// Instruments are open-ended inside the role token.
  96  	if !KnownRole("performer-cello") {
  97  		t.Fatal("performer-<instrument> not accepted")
  98  	}
  99  	if KnownRole("wizard") {
 100  		t.Fatal("an unknown role reported as known")
 101  	}
 102  }
 103  
 104  func TestAccessTier(t *testing.T) {
 105  	for _, s := range []string{TierFan, TierRetail, TierSub} {
 106  		if !AccessTier(s) {
 107  			t.Errorf("tier %q not accepted", s)
 108  		}
 109  	}
 110  	if AccessTier("presale") {
 111  		t.Fatal("presale is not an access-filter tier")
 112  	}
 113  }
 114