|
1 | 1 | package torrentjob |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
| 5 | + "strings" |
4 | 6 | "testing" |
5 | 7 |
|
| 8 | + "github.com/tanq16/danzo/internal/highway" |
6 | 9 | "github.com/tanq16/danzo/utils" |
7 | 10 | ) |
8 | 11 |
|
9 | | -func TestTorrentJobCompile(t *testing.T) { |
10 | | - job := New("magnet:?xt=urn:btih:123", "", 0, utils.HTTPClientConfig{}) |
11 | | - _ = job |
| 12 | +func TestTorrentJobID(t *testing.T) { |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + uri string |
| 16 | + outputPath string |
| 17 | + want string |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "Magnet link with no output path", |
| 21 | + uri: "magnet:?xt=urn:btih:12345", |
| 22 | + outputPath: "", |
| 23 | + want: "magnet-link", |
| 24 | + }, |
| 25 | + { |
| 26 | + name: "Torrent file with no output path", |
| 27 | + uri: "https://example.com/file.torrent", |
| 28 | + outputPath: "", |
| 29 | + want: "file.torrent", |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "Magnet link with explicit output path", |
| 33 | + uri: "magnet:?xt=urn:btih:12345", |
| 34 | + outputPath: "my-download-dir", |
| 35 | + want: "my-download-dir", |
| 36 | + }, |
| 37 | + { |
| 38 | + name: "Magnet link with dot output path", |
| 39 | + uri: "magnet:?xt=urn:btih:12345", |
| 40 | + outputPath: ".", |
| 41 | + want: "magnet-link", // dot is ignored for ID |
| 42 | + }, |
| 43 | + } |
| 44 | + |
| 45 | + for _, tt := range tests { |
| 46 | + t.Run(tt.name, func(t *testing.T) { |
| 47 | + job := New(tt.uri, tt.outputPath, 0, utils.HTTPClientConfig{}) |
| 48 | + if got := job.ID(); got != tt.want { |
| 49 | + t.Errorf("ID() = %v, want %v", got, tt.want) |
| 50 | + } |
| 51 | + }) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func TestTorrentJobInvalidURI(t *testing.T) { |
| 56 | + // Provide a completely invalid URI or non-existent file for a torrent, to verify it fails fast |
| 57 | + job := New("not-a-magnet-or-file.torrent", "", 0, utils.HTTPClientConfig{}) |
| 58 | + |
| 59 | + ctx, cancel := context.WithCancel(context.Background()) |
| 60 | + defer cancel() |
| 61 | + |
| 62 | + progressCh := make(chan highway.Progress, 10) |
| 63 | + |
| 64 | + err := job.Run(ctx, progressCh) |
| 65 | + if err == nil { |
| 66 | + t.Error("expected error for invalid torrent URI, got nil") |
| 67 | + } |
| 68 | + |
| 69 | + // Ensure error string complains about file addition |
| 70 | + if err != nil && !strings.Contains(err.Error(), "failed to add torrent") { |
| 71 | + t.Errorf("expected 'failed to add torrent' in error message, got: %v", err) |
| 72 | + } |
12 | 73 | } |
0 commit comments