~james/core

core/spindle/nixcache/name_test.go -rw-r--r-- 926 bytes
452f17b7 — james chore(import): snapshot james.dad.town/core at f70c71c7696a 16 hours ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package nixcache

import (
	"strings"
	"testing"
)

func TestCacheName(t *testing.T) {
	if got, err := CacheName("did:plc:z72i7hdynmk6r22z27h6tvur"); err != nil || got != "plc-z72i7hdynmk6r22z27h6tvur" {
		t.Errorf("CacheName(plc) = %q, %v", got, err)
	}
	if got, err := CacheName(""); err == nil {
		t.Errorf("CacheName(\"\") = %q; want an error", got)
	}

	dids := []string{
		"did:web:example.com",
		"did:plc:UPPER",
		"did:plc:short",
		"did:plc:z72i7hdynmk6r22z27h6tvur-extra",
		"*",
	}
	seen := map[string]string{}
	for _, did := range dids {
		got, err := CacheName(did)
		if err != nil {
			t.Fatalf("CacheName(%q): %v", did, err)
		}
		if !strings.HasPrefix(got, "h-") || len(got) != 34 || !validName.MatchString(got) {
			t.Errorf("CacheName(%q) = %q; want valid h- and 32 chars", did, got)
		}
		if other, ok := seen[got]; ok {
			t.Errorf("CacheName(%q) collides with %q", did, other)
		}
		seen[got] = did
	}
}