test

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 8, 2024 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// UseGeneratedKeypair overwrites the pubkey field of an event and the sig field of an event by signing the event with the provided keypair
	UsePreGeneratedKeypair = func(sk, pk string) RandomEventOption {
		return func(e *nostr.Event) error {
			e.PubKey = pk
			return e.Sign(pk)
		}
	}
	// UseKind overwrites the event kind field
	UseKind = func(kind int) RandomEventOption {
		return func(e *nostr.Event) error {
			e.Kind = kind
			return nil
		}
	}
	// UseKinds ensures the event kind field is set to one of the provided kinds
	UseKinds = func(kinds []int) RandomEventOption {
		return func(e *nostr.Event) error {
			e.Kind = kinds[rand.Intn(len(kinds))]
			return nil
		}
	}
	// MustBeNewerThan ensures the event created_at field is greater than the provided since time value
	MustBeNewerThan = func(since nostr.Timestamp) RandomEventOption {
		return func(e *nostr.Event) error {
			e.CreatedAt = nostr.Timestamp(since.Time().Add(time.Duration(RandRange(1, powInt(2, 16))) * time.Second).Unix())
			return nil
		}
	}
	// MustBeOlderThan ensures the event created_at field is less than the provided until time value
	MustBeOlderThan = func(until nostr.Timestamp) RandomEventOption {
		return func(e *nostr.Event) error {
			e.CreatedAt = nostr.Timestamp(until.Time().Add(time.Duration(-1*RandRange(1, powInt(2, 16))) * time.Second).Unix())
			return nil
		}
	}
	// OverwriteID overwrites the event ID. NOTE: use this option last if providing more than one to ensure it is not overwritten
	OverwriteID = func(id string) RandomEventOption {
		return func(e *nostr.Event) error {
			e.ID = id
			return nil
		}
	}
	// AppendSuffixToContent appends a suffix to the events content field
	AppendSuffixToContent = func(suffix string) RandomEventOption {
		return func(e *nostr.Event) error {
			e.Content = e.Content + suffix
			return nil
		}
	}
	// AppendTags appends the contents of a tag map to the events tags field
	AppendTags = func(tagMap nostr.TagMap) RandomEventOption {
		return func(e *nostr.Event) error {
			for letter, values := range tagMap {
				tag := nostr.Tag{letter}
				if len(values) <= 1 {
					tag = append(tag, values...)
				} else {
					tag = append(tag, values[int(math.Max(0, float64(RandRange(1, len(values)))))])
				}
				e.Tags = append(e.Tags, tag)
			}
			return nil
		}
	}
)

Functions

func CloseBytes

func CloseBytes(close nostr.CloseEnvelope) []byte

CloseBytes takes a nostr close message and serializes it

func ClosedBytes

func ClosedBytes(closed nostr.ClosedEnvelope) []byte

ClosedBytes takes a nostr closed message and serializes it

func CompareEnvelope

func CompareEnvelope(t *testing.T, expected, got nostr.Envelope)

CompareEnvelope compares two different nostr envelope messages ensuring they're equal

func CompareEventEnvelope

func CompareEventEnvelope(t *testing.T, expected, got *nostr.EventEnvelope)

CompareEventEnvelope compares two event envelopes and ensures they're equal

func CompareReqEnvelope

func CompareReqEnvelope(t *testing.T, expected, got *nostr.ReqEnvelope)

CompareReqEnvelope compares two req envelopes and ensures they're equal

func CreateRandomEvent

func CreateRandomEvent(opts ...RandomEventOption) nostr.Event

CreateRandomEvent creates an event with random pubkey, kind, value, tags, content and created at fields. The ID and signature are generated from the random content

func CreateRandomKeypair

func CreateRandomKeypair() (string, string, error)

CreateRandomKeypair generates a random key pair

func CreateRandomTags

func CreateRandomTags(totalLen, maxTagLen int) nostr.Tags

CreateRandomTags creates random nostr tags of a given total length and where each tag is up to maxTagLen in length

func EventBytes

func EventBytes(event nostr.EventEnvelope) []byte

EventBytes takes a nostr event and serializes it

func FormatLvlFunc

func FormatLvlFunc(i interface{}) string

FormatLvlFunc formats the log level

func NoticeBytes

func NoticeBytes(notice nostr.NoticeEnvelope) []byte

NoticeBytes takes a nostr notice message and serializes it

func OKBytes

func OKBytes(ok nostr.OKEnvelope) []byte

OKBytes takes a nostr OK message and serializes it

func RandRange

func RandRange(min, max int) int

RandRange creates a random number from a given range

func RandomKind

func RandomKind() int

RandomKind creates a random kind within the range of 0 to 2^32-1

func ReqBytes

func ReqBytes(req nostr.ReqEnvelope) []byte

ReqBytes takes a nostr req message and serializes it

Types

type RandomEventOption

type RandomEventOption func(e *nostr.Event) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL