-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathidentity_detection_test.go
More file actions
149 lines (123 loc) · 4.41 KB
/
Copy pathidentity_detection_test.go
File metadata and controls
149 lines (123 loc) · 4.41 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package nara
import (
"testing"
"github.com/eljojo/nara/identity"
"github.com/eljojo/nara/types"
)
// TestComputeNaraID_Deterministic verifies that the same soul+name always produces the same ID
func TestComputeNaraID_Deterministic(t *testing.T) {
soul := testSoul("test-nara")
name := types.NaraName("test-nara")
id1, err1 := identity.ComputeNaraID(soul, name)
if err1 != nil {
t.Fatalf("Failed to compute ID (attempt 1): %v", err1)
}
id2, err2 := identity.ComputeNaraID(soul, name)
if err2 != nil {
t.Fatalf("Failed to compute ID (attempt 2): %v", err2)
}
if id1 != id2 {
t.Errorf("ID should be deterministic: got %s and %s", id1, id2)
}
// Verify ID is non-empty
if id1 == "" {
t.Error("ID should not be empty")
}
}
// TestComputeNaraID_DifferentSouls verifies that different souls with the same name produce different IDs
func TestComputeNaraID_DifferentSouls(t *testing.T) {
name := types.NaraName("same-name")
soul1 := testSoul("soul1")
soul2 := testSoul("soul2")
id1, err1 := identity.ComputeNaraID(soul1, name)
if err1 != nil {
t.Fatalf("Failed to compute ID for soul1: %v", err1)
}
id2, err2 := identity.ComputeNaraID(soul2, name)
if err2 != nil {
t.Fatalf("Failed to compute ID for soul2: %v", err2)
}
if id1 == id2 {
t.Errorf("Different souls should produce different IDs: both got %s", id1)
}
}
// TestComputeNaraID_DifferentNames verifies that the same soul with different names produces different IDs
func TestComputeNaraID_DifferentNames(t *testing.T) {
soul := testSoul("test-soul")
name1 := types.NaraName("nara1")
name2 := types.NaraName("nara2")
id1, err1 := identity.ComputeNaraID(soul, name1)
if err1 != nil {
t.Fatalf("Failed to compute ID for name1: %v", err1)
}
id2, err2 := identity.ComputeNaraID(soul, name2)
if err2 != nil {
t.Fatalf("Failed to compute ID for name2: %v", err2)
}
if id1 == id2 {
t.Errorf("Different names should produce different IDs: both got %s", id1)
}
}
// TestComputeNaraID_InvalidSoul verifies that invalid soul encoding returns an error
func TestComputeNaraID_InvalidSoul(t *testing.T) {
invalidSoul := "not-valid-base58!!!"
name := types.NaraName("test-nara")
_, err := identity.ComputeNaraID(invalidSoul, name)
if err == nil {
t.Error("Expected error for invalid soul encoding, got nil")
}
}
// TestComputeNaraID_WrongSoulLength verifies that a soul with wrong length returns an error
func TestComputeNaraID_WrongSoulLength(t *testing.T) {
// Create a valid Base58 string but with wrong length (not 40 bytes)
shortSoul := "3vQB7B6MrGQZaxCuFg4oh" // This is valid Base58 but too short
name := types.NaraName("test-nara")
_, err := identity.ComputeNaraID(shortSoul, name)
if err == nil {
t.Error("Expected error for wrong soul length, got nil")
}
}
// TestLocalNara_IDInitialization verifies that LocalNara initializes with correct ID
func TestLocalNara_IDInitialization(t *testing.T) {
name := types.NaraName("test-nara")
soul := testSoul(name.String())
identity := identity.DetermineIdentity(name, soul, name.String(), nil)
profile := DefaultMemoryProfile()
ln, err := NewLocalNara(identity, "", "", "", -1, profile)
if err != nil {
t.Fatalf("Failed to create LocalNara: %v", err)
}
// Verify ID is computed
if ln.ID == "" {
t.Error("LocalNara ID should not be empty")
}
// Verify ID matches identity
if ln.ID != identity.ID {
t.Errorf("LocalNara ID mismatch: got %s, expected %s", ln.ID, identity.ID)
}
// Verify ID is also set in Status
if ln.Me.Status.ID != identity.ID {
t.Errorf("LocalNara Status.ID mismatch: got %s, expected %s", ln.Me.Status.ID, identity.ID)
}
}
// TestLocalNara_IDUniqueness verifies that two LocalNaras with same name but different souls have different IDs
func TestLocalNara_IDUniqueness(t *testing.T) {
name := types.NaraName("same-name")
soul1 := testSoul("soul1")
soul2 := testSoul("soul2")
identity1 := identity.DetermineIdentity(name, soul1, name.String(), nil)
identity2 := identity.DetermineIdentity(name, soul2, name.String(), nil)
profile := DefaultMemoryProfile()
ln1, err := NewLocalNara(identity1, "", "", "", -1, profile)
if err != nil {
t.Fatalf("Failed to create LocalNara: %v", err)
}
profile = DefaultMemoryProfile()
ln2, err := NewLocalNara(identity2, "", "", "", -1, profile)
if err != nil {
t.Fatalf("Failed to create LocalNara: %v", err)
}
if ln1.ID == ln2.ID {
t.Errorf("Two naras with same name but different souls should have different IDs: both got %s", ln1.ID)
}
}