|
| 1 | +package ds9 |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | +) |
| 6 | + |
| 7 | +func TestKeyEqual(t *testing.T) { |
| 8 | + tests := []struct { |
| 9 | + key1 *Key |
| 10 | + key2 *Key |
| 11 | + name string |
| 12 | + expected bool |
| 13 | + }{ |
| 14 | + { |
| 15 | + name: "both nil", |
| 16 | + key1: nil, |
| 17 | + key2: nil, |
| 18 | + expected: true, |
| 19 | + }, |
| 20 | + { |
| 21 | + name: "one nil", |
| 22 | + key1: NameKey("Kind", "name", nil), |
| 23 | + key2: nil, |
| 24 | + expected: false, |
| 25 | + }, |
| 26 | + { |
| 27 | + name: "same name keys", |
| 28 | + key1: NameKey("Kind", "name", nil), |
| 29 | + key2: NameKey("Kind", "name", nil), |
| 30 | + expected: true, |
| 31 | + }, |
| 32 | + { |
| 33 | + name: "same ID keys", |
| 34 | + key1: IDKey("Kind", 123, nil), |
| 35 | + key2: IDKey("Kind", 123, nil), |
| 36 | + expected: true, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "different kinds", |
| 40 | + key1: NameKey("Kind1", "name", nil), |
| 41 | + key2: NameKey("Kind2", "name", nil), |
| 42 | + expected: false, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "different names", |
| 46 | + key1: NameKey("Kind", "name1", nil), |
| 47 | + key2: NameKey("Kind", "name2", nil), |
| 48 | + expected: false, |
| 49 | + }, |
| 50 | + { |
| 51 | + name: "different IDs", |
| 52 | + key1: IDKey("Kind", 123, nil), |
| 53 | + key2: IDKey("Kind", 456, nil), |
| 54 | + expected: false, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "with same parent", |
| 58 | + key1: NameKey("Child", "c1", NameKey("Parent", "p1", nil)), |
| 59 | + key2: NameKey("Child", "c1", NameKey("Parent", "p1", nil)), |
| 60 | + expected: true, |
| 61 | + }, |
| 62 | + { |
| 63 | + name: "with different parent", |
| 64 | + key1: NameKey("Child", "c1", NameKey("Parent", "p1", nil)), |
| 65 | + key2: NameKey("Child", "c1", NameKey("Parent", "p2", nil)), |
| 66 | + expected: false, |
| 67 | + }, |
| 68 | + } |
| 69 | + |
| 70 | + for _, tt := range tests { |
| 71 | + t.Run(tt.name, func(t *testing.T) { |
| 72 | + result := tt.key1.Equal(tt.key2) |
| 73 | + if result != tt.expected { |
| 74 | + t.Errorf("Expected %v, got %v", tt.expected, result) |
| 75 | + } |
| 76 | + }) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestKeyIncomplete(t *testing.T) { |
| 81 | + tests := []struct { |
| 82 | + key *Key |
| 83 | + name string |
| 84 | + expected bool |
| 85 | + }{ |
| 86 | + { |
| 87 | + name: "incomplete key", |
| 88 | + key: IncompleteKey("Kind", nil), |
| 89 | + expected: true, |
| 90 | + }, |
| 91 | + { |
| 92 | + name: "name key", |
| 93 | + key: NameKey("Kind", "name", nil), |
| 94 | + expected: false, |
| 95 | + }, |
| 96 | + { |
| 97 | + name: "ID key", |
| 98 | + key: IDKey("Kind", 123, nil), |
| 99 | + expected: false, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "zero ID is incomplete", |
| 103 | + key: &Key{Kind: "Kind", ID: 0, Name: ""}, |
| 104 | + expected: true, |
| 105 | + }, |
| 106 | + } |
| 107 | + |
| 108 | + for _, tt := range tests { |
| 109 | + t.Run(tt.name, func(t *testing.T) { |
| 110 | + result := tt.key.Incomplete() |
| 111 | + if result != tt.expected { |
| 112 | + t.Errorf("Expected %v, got %v", tt.expected, result) |
| 113 | + } |
| 114 | + }) |
| 115 | + } |
| 116 | +} |
| 117 | + |
| 118 | +func TestIncompleteKey(t *testing.T) { |
| 119 | + key := IncompleteKey("TestKind", nil) |
| 120 | + |
| 121 | + if key.Kind != "TestKind" { |
| 122 | + t.Errorf("Expected kind 'TestKind', got '%s'", key.Kind) |
| 123 | + } |
| 124 | + |
| 125 | + if !key.Incomplete() { |
| 126 | + t.Error("Expected key to be incomplete") |
| 127 | + } |
| 128 | + |
| 129 | + if key.Parent != nil { |
| 130 | + t.Error("Expected nil parent") |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +func TestIncompleteKeyWithParent(t *testing.T) { |
| 135 | + parent := NameKey("Parent", "p1", nil) |
| 136 | + key := IncompleteKey("Child", parent) |
| 137 | + |
| 138 | + if key.Kind != "Child" { |
| 139 | + t.Errorf("Expected kind 'Child', got '%s'", key.Kind) |
| 140 | + } |
| 141 | + |
| 142 | + if !key.Incomplete() { |
| 143 | + t.Error("Expected key to be incomplete") |
| 144 | + } |
| 145 | + |
| 146 | + if !key.Parent.Equal(parent) { |
| 147 | + t.Error("Expected parent to match") |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func TestKeyString(t *testing.T) { |
| 152 | + tests := []struct { |
| 153 | + name string |
| 154 | + key *Key |
| 155 | + expected string |
| 156 | + }{ |
| 157 | + { |
| 158 | + name: "nil key", |
| 159 | + key: nil, |
| 160 | + expected: "", |
| 161 | + }, |
| 162 | + { |
| 163 | + name: "simple name key", |
| 164 | + key: NameKey("Kind", "name", nil), |
| 165 | + expected: `/Kind,"name"`, |
| 166 | + }, |
| 167 | + { |
| 168 | + name: "simple ID key", |
| 169 | + key: IDKey("Kind", 123, nil), |
| 170 | + expected: "/Kind,123", |
| 171 | + }, |
| 172 | + { |
| 173 | + name: "incomplete key", |
| 174 | + key: IncompleteKey("Kind", nil), |
| 175 | + expected: "/Kind,incomplete", |
| 176 | + }, |
| 177 | + { |
| 178 | + name: "hierarchical key", |
| 179 | + key: NameKey("Child", "c1", NameKey("Parent", "p1", nil)), |
| 180 | + expected: `/Parent,"p1"/Child,"c1"`, |
| 181 | + }, |
| 182 | + { |
| 183 | + name: "deep hierarchy", |
| 184 | + key: IDKey("GrandChild", 3, NameKey("Child", "c1", NameKey("Parent", "p1", nil))), |
| 185 | + expected: `/Parent,"p1"/Child,"c1"/GrandChild,3`, |
| 186 | + }, |
| 187 | + } |
| 188 | + |
| 189 | + for _, tt := range tests { |
| 190 | + t.Run(tt.name, func(t *testing.T) { |
| 191 | + result := tt.key.String() |
| 192 | + if result != tt.expected { |
| 193 | + t.Errorf("Expected %q, got %q", tt.expected, result) |
| 194 | + } |
| 195 | + }) |
| 196 | + } |
| 197 | +} |
| 198 | + |
| 199 | +func TestKeyEncodeDecode(t *testing.T) { |
| 200 | + tests := []struct { |
| 201 | + key *Key |
| 202 | + name string |
| 203 | + }{ |
| 204 | + { |
| 205 | + name: "name key", |
| 206 | + key: NameKey("Kind", "name", nil), |
| 207 | + }, |
| 208 | + { |
| 209 | + name: "ID key", |
| 210 | + key: IDKey("Kind", 123, nil), |
| 211 | + }, |
| 212 | + { |
| 213 | + name: "hierarchical key", |
| 214 | + key: NameKey("Child", "c1", NameKey("Parent", "p1", nil)), |
| 215 | + }, |
| 216 | + } |
| 217 | + |
| 218 | + for _, tt := range tests { |
| 219 | + t.Run(tt.name, func(t *testing.T) { |
| 220 | + encoded := tt.key.Encode() |
| 221 | + if encoded == "" { |
| 222 | + t.Fatal("Encode returned empty string") |
| 223 | + } |
| 224 | + |
| 225 | + decoded, err := DecodeKey(encoded) |
| 226 | + if err != nil { |
| 227 | + t.Fatalf("DecodeKey failed: %v", err) |
| 228 | + } |
| 229 | + |
| 230 | + if !decoded.Equal(tt.key) { |
| 231 | + t.Errorf("Decoded key doesn't match original.\nOriginal: %s\nDecoded: %s", tt.key.String(), decoded.String()) |
| 232 | + } |
| 233 | + }) |
| 234 | + } |
| 235 | +} |
| 236 | + |
| 237 | +func TestDecodeKeyErrors(t *testing.T) { |
| 238 | + tests := []struct { |
| 239 | + name string |
| 240 | + encoded string |
| 241 | + }{ |
| 242 | + { |
| 243 | + name: "empty string", |
| 244 | + encoded: "", |
| 245 | + }, |
| 246 | + { |
| 247 | + name: "invalid base64", |
| 248 | + encoded: "!!!invalid!!!", |
| 249 | + }, |
| 250 | + { |
| 251 | + name: "invalid JSON", |
| 252 | + encoded: "aW52YWxpZCBqc29u", // "invalid json" in base64 |
| 253 | + }, |
| 254 | + } |
| 255 | + |
| 256 | + for _, tt := range tests { |
| 257 | + t.Run(tt.name, func(t *testing.T) { |
| 258 | + _, err := DecodeKey(tt.encoded) |
| 259 | + if err == nil { |
| 260 | + t.Error("Expected error, got nil") |
| 261 | + } |
| 262 | + }) |
| 263 | + } |
| 264 | +} |
| 265 | + |
| 266 | +func TestKeyEncodeNil(t *testing.T) { |
| 267 | + var key *Key |
| 268 | + encoded := key.Encode() |
| 269 | + if encoded != "" { |
| 270 | + t.Errorf("Expected empty string for nil key, got %q", encoded) |
| 271 | + } |
| 272 | +} |
0 commit comments