|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "sort" |
| 9 | + "strings" |
| 10 | + "testing" |
| 11 | + "time" |
| 12 | + |
| 13 | + "github.com/substantialcattle5/sietch/internal/config" |
| 14 | + dedup "github.com/substantialcattle5/sietch/internal/deduplication" |
| 15 | + lsui "github.com/substantialcattle5/sietch/internal/ls" |
| 16 | +) |
| 17 | + |
| 18 | +// Helper: capture stdout while running fn() |
| 19 | +func captureStdout(t *testing.T, fn func()) string { |
| 20 | + old := os.Stdout |
| 21 | + r, w, err := os.Pipe() |
| 22 | + if err != nil { |
| 23 | + t.Fatalf("pipe: %v", err) |
| 24 | + } |
| 25 | + os.Stdout = w |
| 26 | + |
| 27 | + fn() |
| 28 | + |
| 29 | + // Close writer and restore stdout before reading |
| 30 | + w.Close() |
| 31 | + var buf bytes.Buffer |
| 32 | + _, err = io.Copy(&buf, r) |
| 33 | + if err != nil { |
| 34 | + os.Stdout = old |
| 35 | + t.Fatalf("copy: %v", err) |
| 36 | + } |
| 37 | + os.Stdout = old |
| 38 | + return buf.String() |
| 39 | +} |
| 40 | + |
| 41 | +func TestFilterAndSortFiles_Basic(t *testing.T) { |
| 42 | + now := time.Now().UTC().Format(time.RFC3339) |
| 43 | + f1 := config.FileManifest{FilePath: "a.txt", Destination: "docs/", Size: 100, ModTime: now} |
| 44 | + f2 := config.FileManifest{FilePath: "b.txt", Destination: "docs/", Size: 200, ModTime: now} |
| 45 | + f3 := config.FileManifest{FilePath: "c.txt", Destination: "data/", Size: 50, ModTime: now} |
| 46 | + |
| 47 | + files := []config.FileManifest{f1, f2, f3} |
| 48 | + |
| 49 | + // sort by name |
| 50 | + out := filterAndSortFiles(files, "", "name") |
| 51 | + if out[0].FilePath != "a.txt" || out[1].FilePath != "b.txt" || out[2].FilePath != "c.txt" { |
| 52 | + t.Fatalf("unexpected order by name: %v", []string{out[0].FilePath, out[1].FilePath, out[2].FilePath}) |
| 53 | + } |
| 54 | + |
| 55 | + // sort by size (desc) |
| 56 | + out = filterAndSortFiles(files, "", "size") |
| 57 | + if out[0].Size < out[1].Size || out[1].Size < out[2].Size { |
| 58 | + t.Fatalf("unexpected order by size: %v", []int64{out[0].Size, out[1].Size, out[2].Size}) |
| 59 | + } |
| 60 | + |
| 61 | + // filter by destination prefix |
| 62 | + out = filterAndSortFiles(files, "docs/", "path") |
| 63 | + if len(out) != 2 { |
| 64 | + t.Fatalf("expected 2 files in docs/, got %d", len(out)) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func TestBuildChunkIndexAndComputeDedupStats(t *testing.T) { |
| 69 | + now := time.Now().UTC().Format(time.RFC3339) |
| 70 | + |
| 71 | + // file1 has chunks c1 and c2 |
| 72 | + f1 := config.FileManifest{ |
| 73 | + FilePath: "a.txt", |
| 74 | + Destination: "test/", |
| 75 | + Size: 1024, |
| 76 | + ModTime: now, |
| 77 | + Chunks: []config.ChunkRef{ |
| 78 | + {Hash: "c1", EncryptedSize: 128}, |
| 79 | + {Hash: "c2", EncryptedSize: 256}, |
| 80 | + }, |
| 81 | + } |
| 82 | + // file2 shares c1 |
| 83 | + f2 := config.FileManifest{ |
| 84 | + FilePath: "b.txt", |
| 85 | + Destination: "test/", |
| 86 | + Size: 1024, |
| 87 | + ModTime: now, |
| 88 | + Chunks: []config.ChunkRef{ |
| 89 | + {Hash: "c1", EncryptedSize: 128}, |
| 90 | + }, |
| 91 | + } |
| 92 | + // file3 no share |
| 93 | + f3 := config.FileManifest{ |
| 94 | + FilePath: "c.txt", |
| 95 | + Destination: "other/", |
| 96 | + Size: 512, |
| 97 | + ModTime: now, |
| 98 | + Chunks: []config.ChunkRef{ |
| 99 | + {Hash: "c3", EncryptedSize: 64}, |
| 100 | + }, |
| 101 | + } |
| 102 | + |
| 103 | + files := []config.FileManifest{f1, f2, f3} |
| 104 | + |
| 105 | + idx := buildChunkIndex(files) |
| 106 | + |
| 107 | + // verify chunk index |
| 108 | + if len(idx["c1"]) != 2 { |
| 109 | + t.Fatalf("expected c1 refs length 2, got %d", len(idx["c1"])) |
| 110 | + } |
| 111 | + if len(idx["c2"]) != 1 { |
| 112 | + t.Fatalf("expected c2 refs length 1, got %d", len(idx["c2"])) |
| 113 | + } |
| 114 | + |
| 115 | + sharedChunks, savedBytes, sharedWith := dedup.ComputeDedupStatsForFile(f1, idx) |
| 116 | + if sharedChunks != 1 { |
| 117 | + t.Fatalf("expected sharedChunks 1 for f1, got %d", sharedChunks) |
| 118 | + } |
| 119 | + if savedBytes != 128 { |
| 120 | + t.Fatalf("expected savedBytes 128 for f1, got %d", savedBytes) |
| 121 | + } |
| 122 | + if len(sharedWith) != 1 { |
| 123 | + t.Fatalf("expected sharedWith length 1, got %d", len(sharedWith)) |
| 124 | + } |
| 125 | + if sharedWith[0] != "test/b.txt" { |
| 126 | + t.Fatalf("expected shared with test/b.txt got %v", sharedWith) |
| 127 | + } |
| 128 | + |
| 129 | + // file with no shared chunks |
| 130 | + sc, sb, sw := dedup.ComputeDedupStatsForFile(f3, idx) |
| 131 | + if sc != 0 || sb != 0 || len(sw) != 0 { |
| 132 | + t.Fatalf("expected no shared chunks for f3, got sc=%d sb=%d sw=%v", sc, sb, sw) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +func TestFormatSharedWith_Truncation(t *testing.T) { |
| 137 | + list := make([]string, 0, 12) |
| 138 | + for i := 0; i < 12; i++ { |
| 139 | + // use numeric suffixes to avoid rune/int confusion |
| 140 | + list = append(list, fmt.Sprintf("file%d", i)) |
| 141 | + } |
| 142 | + out := lsui.FormatSharedWith(list, 10) |
| 143 | + if !strings.Contains(out, "(+2 more)") { |
| 144 | + t.Fatalf("expected truncation info (+2 more) in '%s'", out) |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +func TestDisplayShortAndLongFormat_OutputContainsStats(t *testing.T) { |
| 149 | + now := time.Now().UTC().Format(time.RFC3339) |
| 150 | + |
| 151 | + f1 := config.FileManifest{ |
| 152 | + FilePath: "a.txt", |
| 153 | + Destination: "test/", |
| 154 | + Size: 100, |
| 155 | + ModTime: now, |
| 156 | + Chunks: []config.ChunkRef{{Hash: "c1", EncryptedSize: 128}}, |
| 157 | + } |
| 158 | + f2 := config.FileManifest{ |
| 159 | + FilePath: "b.txt", |
| 160 | + Destination: "test/", |
| 161 | + Size: 200, |
| 162 | + ModTime: now, |
| 163 | + Chunks: []config.ChunkRef{{Hash: "c1", EncryptedSize: 128}}, |
| 164 | + } |
| 165 | + files := []config.FileManifest{f1, f2} |
| 166 | + chunkRefs := buildChunkIndex(files) |
| 167 | + |
| 168 | + // short format capture |
| 169 | + outShort := captureStdout(t, func() { |
| 170 | + lsui.DisplayShortFormat(files, true, true, chunkRefs) |
| 171 | + }) |
| 172 | + if !strings.Contains(outShort, "shared_chunks:") || !strings.Contains(outShort, "saved:") { |
| 173 | + t.Fatalf("short output missing dedup info: %s", outShort) |
| 174 | + } |
| 175 | + |
| 176 | + // long format capture |
| 177 | + outLong := captureStdout(t, func() { |
| 178 | + displayLongFormat(files, false, true, chunkRefs) |
| 179 | + }) |
| 180 | + if !strings.Contains(outLong, "SIZE") || !strings.Contains(outLong, "shared_chunks:") { |
| 181 | + t.Fatalf("long output missing dedup info: %s", outLong) |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +func TestBuildChunkIndex_DeterministicOrder(t *testing.T) { |
| 186 | + now := time.Now().UTC().Format(time.RFC3339) |
| 187 | + |
| 188 | + f1 := config.FileManifest{ |
| 189 | + FilePath: "a.txt", |
| 190 | + Destination: "x/", |
| 191 | + Size: 10, |
| 192 | + ModTime: now, |
| 193 | + Chunks: []config.ChunkRef{{Hash: "c1", EncryptedSize: 10}}, |
| 194 | + } |
| 195 | + f2 := config.FileManifest{ |
| 196 | + FilePath: "b.txt", |
| 197 | + Destination: "y/", |
| 198 | + Size: 20, |
| 199 | + ModTime: now, |
| 200 | + Chunks: []config.ChunkRef{{Hash: "c1", EncryptedSize: 10}}, |
| 201 | + } |
| 202 | + files := []config.FileManifest{f1, f2} |
| 203 | + idx := buildChunkIndex(files) |
| 204 | + |
| 205 | + // ensure entries are present |
| 206 | + if len(idx["c1"]) != 2 { |
| 207 | + t.Fatalf("expected 2 refs for c1; got %d", len(idx["c1"])) |
| 208 | + } |
| 209 | + |
| 210 | + // ensure computeDedupStatsForFile sorts sharedWith deterministically |
| 211 | + _, _, sw := dedup.ComputeDedupStatsForFile(f1, idx) |
| 212 | + // sw should be sorted (we call sort.Strings), check monotonic property |
| 213 | + if !sort.StringsAreSorted(sw) { |
| 214 | + t.Fatalf("sharedWith not sorted: %v", sw) |
| 215 | + } |
| 216 | +} |
0 commit comments