github.com/storacha/go-ucanto@v0.7.2/testing/helpers/printer/printer.go (about)

     1  package printer
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  	"strings"
     7  	"testing"
     8  	"time"
     9  
    10  	"github.com/ipld/go-ipld-prime/printer"
    11  	"github.com/storacha/go-ucanto/core/dag/blockstore"
    12  	"github.com/storacha/go-ucanto/core/delegation"
    13  	"github.com/storacha/go-ucanto/core/ipld"
    14  	"github.com/storacha/go-ucanto/core/receipt"
    15  	"github.com/storacha/go-ucanto/core/result"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func withIndent(t *testing.T, level int) func(format string, args ...any) {
    20  	indent := strings.Repeat("  ", level)
    21  	return func(format string, args ...any) {
    22  		t.Logf(indent+format, args...)
    23  	}
    24  }
    25  
    26  func PrintDelegation(t *testing.T, d delegation.Delegation, level int) {
    27  	t.Helper()
    28  	log := withIndent(t, level)
    29  
    30  	log("%s\n", d.Link())
    31  	log("  Issuer: %s", d.Issuer().DID())
    32  	log("  Audience: %s", d.Audience().DID())
    33  
    34  	log("  Capabilities:")
    35  	for _, c := range d.Capabilities() {
    36  		log("    Can: %s", c.Can())
    37  		log("    With: %s", c.With())
    38  		log("    Nb: %v", c.Nb())
    39  	}
    40  
    41  	if d.Expiration() != nil {
    42  		log("  Expiration: %s", time.Unix(int64(*d.Expiration()), 0).String())
    43  	}
    44  
    45  	bs, err := blockstore.NewBlockReader(blockstore.WithBlocksIterator(d.Blocks()))
    46  	require.NoError(t, err)
    47  
    48  	if len(d.Proofs()) > 0 {
    49  		log("  Proofs:")
    50  		for _, p := range d.Proofs() {
    51  			pd, err := delegation.NewDelegationView(p, bs)
    52  			if err != nil {
    53  				log("    %s\n", p)
    54  				continue
    55  			}
    56  			PrintDelegation(t, pd, level+2)
    57  		}
    58  	}
    59  }
    60  
    61  func PrintReceipt(t *testing.T, r receipt.AnyReceipt) {
    62  	t.Helper()
    63  	t.Logf("%s", r.Root().Link())
    64  	t.Logf("  Issuer: %s", r.Issuer().DID())
    65  	inv, ok := r.Ran().Invocation()
    66  	if ok {
    67  		t.Logf("  Ran:")
    68  		PrintDelegation(t, inv, 2)
    69  	} else {
    70  		t.Logf("  Ran: %s", r.Ran().Link())
    71  	}
    72  	t.Log("  Out:")
    73  	o, x := result.Unwrap(r.Out())
    74  	if x != nil {
    75  		t.Logf("    Error:\n      %s", printer.Sprint(x))
    76  	} else {
    77  		t.Logf("    OK:\n      %s", printer.Sprint(o))
    78  	}
    79  }
    80  
    81  func PrintNode(t *testing.T, n ipld.Node) {
    82  	t.Helper()
    83  	t.Log(printer.Sprint(n))
    84  }
    85  
    86  func SprintBytes(t *testing.T, b int) string {
    87  	t.Helper()
    88  	const unit = 1024
    89  	if b < unit {
    90  		return fmt.Sprintf("%d B", b)
    91  	}
    92  	div, exp := int64(unit), 0
    93  	for n := b / unit; n >= unit; n /= unit {
    94  		div *= unit
    95  		exp++
    96  	}
    97  	return fmt.Sprintf("%.1f %ciB", float64(b)/float64(div), "KMGTPE"[exp])
    98  }
    99  
   100  func PrintHeaders(t *testing.T, h http.Header) {
   101  	t.Helper()
   102  	for name, values := range h {
   103  		for _, value := range values {
   104  			t.Logf("%s: %s", name, value)
   105  		}
   106  	}
   107  }