forked from
tangled.org/core
Monorepo for Tangled — https://tangled.org
1package userutil
2
3import "testing"
4
5func TestUnflattenDid(t *testing.T) {
6 unflattenedMap := map[string]string{
7 "did-plc-abcdefghijklmnopqrstuvwxyz": "did:plc:abcdefghijklmnopqrstuvwxyz",
8 "did-plc-1234567890": "did:plc:1234567890",
9 "did-key-z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK": "did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK",
10 "did-plc-abcdefghi-jklmnopqr-stuvwxyz": "did:plc:abcdefghi-jklmnopqr-stuvwxyz",
11 "plc-abcdefghijklmnopqrstuvwxyz": "plc-abcdefghijklmnopqrstuvwxyz",
12 "didplc-abcdefghijklmnopqrstuvwxyz": "didplc-abcdefghijklmnopqrstuvwxyz",
13 "": "",
14 "did-": "did-",
15 "did:plc:abcdefghijklmnopqrstuvwxyz": "did:plc:abcdefghijklmnopqrstuvwxyz",
16 "did-invalid$format:something": "did-invalid$format:something",
17 }
18
19 tests := []struct {
20 name string
21 input string
22 expected string
23 }{}
24
25 for _, tc := range isFlattenedDidTests {
26 tests = append(tests, struct {
27 name string
28 input string
29 expected string
30 }{
31 name: tc.name,
32 input: tc.input,
33 expected: unflattenedMap[tc.input],
34 })
35 }
36
37 for _, tc := range tests {
38 t.Run(tc.name, func(t *testing.T) {
39 result := UnflattenDid(tc.input)
40 if result != tc.expected {
41 t.Errorf("unflattenDid(%q) = %q, want %q", tc.input, result, tc.expected)
42 }
43 })
44 }
45}
46
47var isFlattenedDidTests = []struct {
48 name string
49 input string
50 expected bool
51}{
52 {
53 name: "valid flattened DID",
54 input: "did-plc-abcdefghijklmnopqrstuvwxyz",
55 expected: true,
56 },
57 {
58 name: "valid flattened DID with numbers",
59 input: "did-plc-1234567890",
60 expected: true,
61 },
62 {
63 name: "valid flattened DID with special characters",
64 input: "did-key-z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK",
65 expected: true,
66 },
67 {
68 name: "valid flattened DID with dashes",
69 input: "did-plc-abcdefghi-jklmnopqr-stuvwxyz",
70 expected: true,
71 },
72
73 {
74 name: "doesn't start with did-",
75 input: "plc-abcdefghijklmnopqrstuvwxyz",
76 expected: false,
77 },
78 {
79 name: "no hyphen after did",
80 input: "didplc-abcdefghijklmnopqrstuvwxyz",
81 expected: false,
82 },
83 {
84 name: "empty string",
85 input: "",
86 expected: false,
87 },
88 {
89 name: "only did-",
90 input: "did-",
91 expected: false,
92 },
93 {
94 name: "standard DID format, not flattened",
95 input: "did:plc:abcdefghijklmnopqrstuvwxyz",
96 expected: false,
97 },
98 {
99 name: "invalid reconstructed DID format",
100 input: "did-invalid$format:something",
101 expected: false,
102 },
103}
104
105func TestIsFlattenedDid(t *testing.T) {
106 for _, tc := range isFlattenedDidTests {
107 t.Run(tc.name, func(t *testing.T) {
108 result := IsFlattenedDid(tc.input)
109 if result != tc.expected {
110 t.Errorf("isFlattenedDid(%q) = %v, want %v", tc.input, result, tc.expected)
111 }
112 })
113 }
114}