62159
Sy
symb / files
symb / internal / hashline / hashline_test.go
6.6 KB
1package hashline 2 3import ( 4 "strings" 5 "testing" 6) 7 8func TestLineHash(t *testing.T) { 9 // Deterministic: same input → same hash 10 h1 := LineHash("hello world") 11 h2 := LineHash("hello world") 12 if h1 != h2 { 13 t.Errorf("same input produced different hashes: %s vs %s", h1, h2) 14 } 15 16 // Different input → (very likely) different hash 17 h3 := LineHash("hello world!") 18 if h1 == h3 { 19 t.Errorf("different inputs produced same hash: %s", h1) 20 } 21 22 // Always 2 hex chars 23 if len(h1) != HashLen { 24 t.Errorf("expected hash length %d, got %d", HashLen, len(h1)) 25 } 26 27 // Empty line gets a hash too 28 h4 := LineHash("") 29 if len(h4) != HashLen { 30 t.Errorf("empty line hash length: expected %d, got %d", HashLen, len(h4)) 31 } 32} 33 34func TestTagLines(t *testing.T) { 35 content := "func hello() {\n return \"world\"\n}" 36 37 tagged := TagLines(content, 1) 38 if len(tagged) != 3 { 39 t.Fatalf("expected 3 tagged lines, got %d", len(tagged)) 40 } 41 42 // Check numbering 43 for i, tl := range tagged { 44 if tl.Num != i+1 { 45 t.Errorf("line %d: expected Num=%d, got %d", i, i+1, tl.Num) 46 } 47 if len(tl.Hash) != HashLen { 48 t.Errorf("line %d: expected hash length %d, got %d", i, HashLen, len(tl.Hash)) 49 } 50 } 51 52 if tagged[0].Content != "func hello() {" { 53 t.Errorf("line 0 content: %q", tagged[0].Content) 54 } 55 if tagged[2].Content != "}" { 56 t.Errorf("line 2 content: %q", tagged[2].Content) 57 } 58} 59 60func TestTagLinesWithOffset(t *testing.T) { 61 content := "line a\nline b" 62 tagged := TagLines(content, 10) 63 64 if tagged[0].Num != 10 { 65 t.Errorf("expected first line num 10, got %d", tagged[0].Num) 66 } 67 if tagged[1].Num != 11 { 68 t.Errorf("expected second line num 11, got %d", tagged[1].Num) 69 } 70} 71 72func TestFormatTagged(t *testing.T) { 73 tagged := []TaggedLine{ 74 {Num: 1, Hash: "a3", Content: "func hello() {"}, 75 {Num: 2, Hash: "f1", Content: " return \"world\""}, 76 {Num: 3, Hash: "0e", Content: "}"}, 77 } 78 79 output := FormatTagged(tagged) 80 expected := "1:a3|func hello() {\n2:f1| return \"world\"\n3:0e|}" 81 if output != expected { 82 t.Errorf("FormatTagged:\ngot: %q\nwant: %q", output, expected) 83 } 84} 85 86func TestAnchorValidate(t *testing.T) { 87 lines := []string{"func hello() {", " return \"world\"", "}"} 88 89 // Valid anchor 90 hash := LineHash(lines[0]) 91 a := Anchor{Num: 1, Hash: hash} 92 if err := a.Validate(lines); err != nil { 93 t.Errorf("valid anchor failed: %v", err) 94 } 95 96 // Out of range 97 a2 := Anchor{Num: 0, Hash: "ff"} 98 if err := a2.Validate(lines); err == nil { 99 t.Error("line 0 should be out of range") 100 } 101 102 a3 := Anchor{Num: 4, Hash: "ff"} 103 if err := a3.Validate(lines); err == nil { 104 t.Error("line 4 should be out of range") 105 } 106 107 // Wrong hash (file changed) — error should include actual line content 108 a4 := Anchor{Num: 1, Hash: "ff"} 109 err := a4.Validate(lines) 110 if err == nil { 111 t.Error("wrong hash should fail validation") 112 } 113 errMsg := err.Error() 114 if !strings.Contains(errMsg, "actual:") { 115 t.Errorf("error should contain actual line content: %s", errMsg) 116 } 117 if !strings.Contains(errMsg, "func hello()") { 118 t.Errorf("error should contain the line text: %s", errMsg) 119 } 120 if !strings.Contains(errMsg, "re-Read") { 121 t.Errorf("error should suggest re-reading: %s", errMsg) 122 } 123} 124 125func TestValidateRange(t *testing.T) { 126 lines := []string{"aaa", "bbb", "ccc"} 127 h1 := LineHash(lines[0]) 128 h2 := LineHash(lines[1]) 129 h3 := LineHash(lines[2]) 130 131 // Valid range 132 s, e := Anchor{1, h1}, Anchor{3, h3} 133 if err := ValidateRange(lines, &s, &e); err != nil { 134 t.Errorf("valid range failed: %v", err) 135 } 136 137 // Single line range 138 s2, e2 := Anchor{2, h2}, Anchor{2, h2} 139 if err := ValidateRange(lines, &s2, &e2); err != nil { 140 t.Errorf("single line range failed: %v", err) 141 } 142 143 // Inverted range 144 s3, e3 := Anchor{3, h3}, Anchor{1, h1} 145 if err := ValidateRange(lines, &s3, &e3); err == nil { 146 t.Error("inverted range should fail") 147 } 148 149 // Bad start hash 150 s4, e4 := Anchor{1, "ff"}, Anchor{3, h3} 151 if err := ValidateRange(lines, &s4, &e4); err == nil { 152 t.Error("bad start hash should fail") 153 } 154 155 // Bad end hash 156 s5, e5 := Anchor{1, h1}, Anchor{3, "ff"} 157 if err := ValidateRange(lines, &s5, &e5); err == nil { 158 t.Error("bad end hash should fail") 159 } 160} 161 162func TestRoundTrip(t *testing.T) { 163 // Simulate the full flow: tag lines, extract anchors, validate 164 content := "package main\n\nimport \"fmt\"\n\nfunc main() {\n\tfmt.Println(\"hello\")\n}" 165 tagged := TagLines(content, 1) 166 167 lines := make([]string, len(tagged)) 168 for i, tl := range tagged { 169 lines[i] = tl.Content 170 } 171 172 // Every tagged anchor should validate 173 for _, tl := range tagged { 174 a := Anchor{Num: tl.Num, Hash: tl.Hash} 175 if err := a.Validate(lines); err != nil { 176 t.Errorf("round-trip validation failed for line %d: %v", tl.Num, err) 177 } 178 } 179} 180 181func TestParseAnchor(t *testing.T) { 182 a, err := ParseAnchor("5:ab") 183 if err != nil { 184 t.Fatal(err) 185 } 186 if a.Num != 5 || a.Hash != "ab" { 187 t.Errorf("got %+v", a) 188 } 189 190 // errors 191 for _, bad := range []string{"", "5", ":ab", "ab:5", "5:abc", "5:a"} { 192 if _, err := ParseAnchor(bad); err == nil { 193 t.Errorf("expected error for %q", bad) 194 } 195 } 196} 197 198func TestAnchorRelocate(t *testing.T) { 199 lines := []string{"alpha", "beta", "gamma", "delta"} 200 betaHash := LineHash("beta") 201 202 // Wrong line number, unique hash → relocate succeeds. 203 a := Anchor{Num: 4, Hash: betaHash} 204 if err := a.Validate(lines); err != nil { 205 t.Fatalf("expected relocation, got error: %v", err) 206 } 207 if a.Num != 2 { 208 t.Errorf("expected relocated to line 2, got %d", a.Num) 209 } 210 211 // Duplicate hash → relocation fails. 212 dupes := []string{"same", "other", "same"} 213 sameHash := LineHash("same") 214 a2 := Anchor{Num: 2, Hash: sameHash} 215 if err := a2.Validate(dupes); err == nil { 216 t.Error("expected error for ambiguous hash") 217 } 218 219 // Out of range but relocatable. 220 a3 := Anchor{Num: 99, Hash: betaHash} 221 if err := a3.Validate(lines); err != nil { 222 t.Fatalf("expected relocation for out-of-range, got: %v", err) 223 } 224 if a3.Num != 2 { 225 t.Errorf("expected relocated to line 2, got %d", a3.Num) 226 } 227} 228 229func TestValidateRangeRelocate(t *testing.T) { 230 lines := []string{"alpha", "beta", "gamma", "delta"} 231 betaHash := LineHash("beta") 232 gammaHash := LineHash("gamma") 233 234 // Both anchors have wrong line numbers but unique hashes. 235 start := Anchor{Num: 10, Hash: betaHash} 236 end := Anchor{Num: 11, Hash: gammaHash} 237 if err := ValidateRange(lines, &start, &end); err != nil { 238 t.Fatalf("expected range relocation, got: %v", err) 239 } 240 if start.Num != 2 || end.Num != 3 { 241 t.Errorf("expected 2..3, got %d..%d", start.Num, end.Num) 242 } 243 244 // Relocated range inverted → error. 245 s2 := Anchor{Num: 10, Hash: gammaHash} 246 e2 := Anchor{Num: 11, Hash: betaHash} 247 if err := ValidateRange(lines, &s2, &e2); err == nil { 248 t.Error("expected error for inverted relocated range") 249 } 250}