|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "testing" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +func TestNormalizeForDedup(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + a, b string |
| 14 | + wantSame bool |
| 15 | + }{ |
| 16 | + { |
| 17 | + name: "exact duplicates", |
| 18 | + a: "Lead-only commit eliminates merge conflicts", |
| 19 | + b: "Lead-only commit eliminates merge conflicts", |
| 20 | + wantSame: true, |
| 21 | + }, |
| 22 | + { |
| 23 | + name: "case difference", |
| 24 | + a: "Lead-Only Commit Pattern", |
| 25 | + b: "lead-only commit pattern", |
| 26 | + wantSame: true, |
| 27 | + }, |
| 28 | + { |
| 29 | + name: "whitespace difference", |
| 30 | + a: " Lead-only commit pattern ", |
| 31 | + b: "Lead-only commit pattern", |
| 32 | + wantSame: true, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "trailing ellipsis stripped", |
| 36 | + a: "Workers write files but never commit...", |
| 37 | + b: "Workers write files but never commit", |
| 38 | + wantSame: true, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "distinct content with same 80-char prefix", |
| 42 | + a: "Topological wave decomposition extracts parallelism from dependency graphs by grouping leaves — this is about WAVES", |
| 43 | + b: "Topological wave decomposition extracts parallelism from dependency graphs by grouping leaves — this is about SORTING", |
| 44 | + wantSame: false, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "short similar strings are distinct", |
| 48 | + a: "Use content hashing for dedup", |
| 49 | + b: "Use content hashing for dedup detection", |
| 50 | + wantSame: false, |
| 51 | + }, |
| 52 | + { |
| 53 | + name: "completely different", |
| 54 | + a: "Workers should never commit", |
| 55 | + b: "Wave sizing follows dependency graph", |
| 56 | + wantSame: false, |
| 57 | + }, |
| 58 | + } |
| 59 | + |
| 60 | + for _, tt := range tests { |
| 61 | + t.Run(tt.name, func(t *testing.T) { |
| 62 | + keyA := normalizeForDedup(tt.a) |
| 63 | + keyB := normalizeForDedup(tt.b) |
| 64 | + gotSame := keyA == keyB |
| 65 | + if gotSame != tt.wantSame { |
| 66 | + t.Errorf("normalizeForDedup(%q) == normalizeForDedup(%q): got %v, want %v\n keyA=%s\n keyB=%s", |
| 67 | + tt.a, tt.b, gotSame, tt.wantSame, keyA, keyB) |
| 68 | + } |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +func TestDedupSimilar(t *testing.T) { |
| 74 | + tests := []struct { |
| 75 | + name string |
| 76 | + input []string |
| 77 | + wantCount int |
| 78 | + }{ |
| 79 | + { |
| 80 | + name: "nil input", |
| 81 | + input: nil, |
| 82 | + wantCount: 0, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "empty input", |
| 86 | + input: []string{}, |
| 87 | + wantCount: 0, |
| 88 | + }, |
| 89 | + { |
| 90 | + name: "no duplicates", |
| 91 | + input: []string{"alpha", "beta", "gamma"}, |
| 92 | + wantCount: 3, |
| 93 | + }, |
| 94 | + { |
| 95 | + name: "exact duplicates removed", |
| 96 | + input: []string{"alpha", "beta", "alpha", "gamma", "beta"}, |
| 97 | + wantCount: 3, |
| 98 | + }, |
| 99 | + { |
| 100 | + name: "case-insensitive dedup", |
| 101 | + input: []string{ |
| 102 | + "Lead-only commit pattern", |
| 103 | + "lead-only commit pattern", |
| 104 | + "LEAD-ONLY COMMIT PATTERN", |
| 105 | + }, |
| 106 | + wantCount: 1, |
| 107 | + }, |
| 108 | + { |
| 109 | + name: "preserves distinct long strings", |
| 110 | + input: []string{ |
| 111 | + "Topological wave decomposition extracts parallelism from dependency graphs by grouping leaves — approach A", |
| 112 | + "Topological wave decomposition extracts parallelism from dependency graphs by grouping leaves — approach B", |
| 113 | + }, |
| 114 | + wantCount: 2, |
| 115 | + }, |
| 116 | + } |
| 117 | + |
| 118 | + for _, tt := range tests { |
| 119 | + t.Run(tt.name, func(t *testing.T) { |
| 120 | + result := dedupSimilar(tt.input) |
| 121 | + if len(result) != tt.wantCount { |
| 122 | + t.Errorf("dedupSimilar() returned %d items, want %d. Items: %v", len(result), tt.wantCount, result) |
| 123 | + } |
| 124 | + }) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func TestFindPendingTranscripts(t *testing.T) { |
| 129 | + // Create temp directory with test transcript files |
| 130 | + tmpDir, err := os.MkdirTemp("", "batch_forge_test") |
| 131 | + if err != nil { |
| 132 | + t.Fatal(err) |
| 133 | + } |
| 134 | + defer os.RemoveAll(tmpDir) |
| 135 | + |
| 136 | + // Create some test JSONL files (must be > 100 bytes to pass the size filter) |
| 137 | + for _, name := range []string{"session1.jsonl", "session2.jsonl"} { |
| 138 | + content := []byte(`{"role":"user","content":"hello world, this is a test message with enough content"}` + "\n" + |
| 139 | + `{"role":"assistant","content":"hi there, this is a sufficiently long response to exceed the 100 byte minimum"}` + "\n") |
| 140 | + if err := os.WriteFile(filepath.Join(tmpDir, name), content, 0644); err != nil { |
| 141 | + t.Fatal(err) |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + // Create a file too small to be a transcript |
| 146 | + if err := os.WriteFile(filepath.Join(tmpDir, "tiny.jsonl"), []byte("{}"), 0644); err != nil { |
| 147 | + t.Fatal(err) |
| 148 | + } |
| 149 | + |
| 150 | + // Create a non-JSONL file |
| 151 | + if err := os.WriteFile(filepath.Join(tmpDir, "readme.md"), []byte("# Hello"), 0644); err != nil { |
| 152 | + t.Fatal(err) |
| 153 | + } |
| 154 | + |
| 155 | + // Create a subagents directory that should be skipped |
| 156 | + subDir := filepath.Join(tmpDir, "subagents") |
| 157 | + if err := os.MkdirAll(subDir, 0755); err != nil { |
| 158 | + t.Fatal(err) |
| 159 | + } |
| 160 | + if err := os.WriteFile(filepath.Join(subDir, "sub.jsonl"), []byte(`{"role":"user","content":"skip me please"}`+"\n"), 0644); err != nil { |
| 161 | + t.Fatal(err) |
| 162 | + } |
| 163 | + |
| 164 | + candidates, err := findPendingTranscripts(tmpDir) |
| 165 | + if err != nil { |
| 166 | + t.Fatalf("findPendingTranscripts: %v", err) |
| 167 | + } |
| 168 | + |
| 169 | + if len(candidates) != 2 { |
| 170 | + t.Errorf("got %d candidates, want 2", len(candidates)) |
| 171 | + for _, c := range candidates { |
| 172 | + t.Logf(" candidate: %s (size=%d)", c.path, c.size) |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + // Verify sorted by mod time (oldest first) |
| 177 | + if len(candidates) >= 2 { |
| 178 | + if candidates[0].modTime.After(candidates[1].modTime) { |
| 179 | + t.Error("candidates not sorted by modification time (oldest first)") |
| 180 | + } |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +func TestFindPendingTranscriptsEmptyDir(t *testing.T) { |
| 185 | + tmpDir, err := os.MkdirTemp("", "batch_forge_empty") |
| 186 | + if err != nil { |
| 187 | + t.Fatal(err) |
| 188 | + } |
| 189 | + defer os.RemoveAll(tmpDir) |
| 190 | + |
| 191 | + candidates, err := findPendingTranscripts(tmpDir) |
| 192 | + if err != nil { |
| 193 | + t.Fatalf("unexpected error: %v", err) |
| 194 | + } |
| 195 | + if len(candidates) != 0 { |
| 196 | + t.Errorf("got %d candidates, want 0", len(candidates)) |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +func TestHumanSize(t *testing.T) { |
| 201 | + tests := []struct { |
| 202 | + bytes int64 |
| 203 | + want string |
| 204 | + }{ |
| 205 | + {0, "0 B"}, |
| 206 | + {512, "512 B"}, |
| 207 | + {1024, "1.0 KB"}, |
| 208 | + {1536, "1.5 KB"}, |
| 209 | + {1048576, "1.0 MB"}, |
| 210 | + } |
| 211 | + |
| 212 | + for _, tt := range tests { |
| 213 | + got := humanSize(tt.bytes) |
| 214 | + if got != tt.want { |
| 215 | + t.Errorf("humanSize(%d) = %q, want %q", tt.bytes, got, tt.want) |
| 216 | + } |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +func TestTranscriptCandidateFields(t *testing.T) { |
| 221 | + now := time.Now() |
| 222 | + c := transcriptCandidate{ |
| 223 | + path: "/tmp/test.jsonl", |
| 224 | + modTime: now, |
| 225 | + size: 1234, |
| 226 | + } |
| 227 | + |
| 228 | + if c.path != "/tmp/test.jsonl" { |
| 229 | + t.Error("path mismatch") |
| 230 | + } |
| 231 | + if c.size != 1234 { |
| 232 | + t.Error("size mismatch") |
| 233 | + } |
| 234 | + if !c.modTime.Equal(now) { |
| 235 | + t.Error("modTime mismatch") |
| 236 | + } |
| 237 | +} |
0 commit comments