|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "os" |
| 6 | + "path/filepath" |
| 7 | + "strings" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/steveyegge/wasteland/internal/federation" |
| 12 | +) |
| 13 | + |
| 14 | +func TestRunList_NoWastelands(t *testing.T) { |
| 15 | + tmpDir := t.TempDir() |
| 16 | + t.Setenv("XDG_CONFIG_HOME", tmpDir) |
| 17 | + |
| 18 | + var stdout, stderr bytes.Buffer |
| 19 | + err := runList(&stdout, &stderr) |
| 20 | + if err != nil { |
| 21 | + t.Fatalf("runList() error: %v", err) |
| 22 | + } |
| 23 | + if !strings.Contains(stdout.String(), "No wastelands joined") { |
| 24 | + t.Errorf("output = %q, want to contain 'No wastelands joined'", stdout.String()) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +func TestRunList_SingleWasteland(t *testing.T) { |
| 29 | + tmpDir := t.TempDir() |
| 30 | + t.Setenv("XDG_CONFIG_HOME", tmpDir) |
| 31 | + |
| 32 | + store := federation.NewConfigStore() |
| 33 | + cfg := &federation.Config{ |
| 34 | + Upstream: "hop/wl-commons", |
| 35 | + ForkOrg: "alice", |
| 36 | + ForkDB: "wl-commons", |
| 37 | + LocalDir: "/tmp/test/wl-commons", |
| 38 | + RigHandle: "alice", |
| 39 | + JoinedAt: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC), |
| 40 | + } |
| 41 | + if err := store.Save(cfg); err != nil { |
| 42 | + t.Fatalf("Save() error: %v", err) |
| 43 | + } |
| 44 | + |
| 45 | + var stdout, stderr bytes.Buffer |
| 46 | + err := runList(&stdout, &stderr) |
| 47 | + if err != nil { |
| 48 | + t.Fatalf("runList() error: %v", err) |
| 49 | + } |
| 50 | + |
| 51 | + got := stdout.String() |
| 52 | + if !strings.Contains(got, "hop/wl-commons") { |
| 53 | + t.Errorf("output missing upstream: %q", got) |
| 54 | + } |
| 55 | + if !strings.Contains(got, "alice") { |
| 56 | + t.Errorf("output missing handle: %q", got) |
| 57 | + } |
| 58 | + if !strings.Contains(got, "1") { |
| 59 | + t.Errorf("output missing count: %q", got) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func TestRunList_MultipleWastelands(t *testing.T) { |
| 64 | + tmpDir := t.TempDir() |
| 65 | + t.Setenv("XDG_CONFIG_HOME", tmpDir) |
| 66 | + |
| 67 | + store := federation.NewConfigStore() |
| 68 | + for _, cfg := range []*federation.Config{ |
| 69 | + { |
| 70 | + Upstream: "hop/wl-commons", |
| 71 | + ForkOrg: "alice", |
| 72 | + ForkDB: "wl-commons", |
| 73 | + LocalDir: "/tmp/test1", |
| 74 | + RigHandle: "alice", |
| 75 | + JoinedAt: time.Date(2025, 1, 15, 0, 0, 0, 0, time.UTC), |
| 76 | + }, |
| 77 | + { |
| 78 | + Upstream: "bob/wl-commons", |
| 79 | + ForkOrg: "alice", |
| 80 | + ForkDB: "wl-commons", |
| 81 | + LocalDir: "/tmp/test2", |
| 82 | + RigHandle: "alice", |
| 83 | + JoinedAt: time.Date(2025, 2, 20, 0, 0, 0, 0, time.UTC), |
| 84 | + }, |
| 85 | + } { |
| 86 | + if err := store.Save(cfg); err != nil { |
| 87 | + t.Fatalf("Save() error: %v", err) |
| 88 | + } |
| 89 | + } |
| 90 | + |
| 91 | + var stdout, stderr bytes.Buffer |
| 92 | + err := runList(&stdout, &stderr) |
| 93 | + if err != nil { |
| 94 | + t.Fatalf("runList() error: %v", err) |
| 95 | + } |
| 96 | + |
| 97 | + got := stdout.String() |
| 98 | + if !strings.Contains(got, "hop/wl-commons") { |
| 99 | + t.Errorf("output missing first upstream: %q", got) |
| 100 | + } |
| 101 | + if !strings.Contains(got, "bob/wl-commons") { |
| 102 | + t.Errorf("output missing second upstream: %q", got) |
| 103 | + } |
| 104 | + if !strings.Contains(got, "2") { |
| 105 | + t.Errorf("output missing count '2': %q", got) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func TestRunList_CorruptConfig(t *testing.T) { |
| 110 | + tmpDir := t.TempDir() |
| 111 | + t.Setenv("XDG_CONFIG_HOME", tmpDir) |
| 112 | + |
| 113 | + // Create a valid config first so List() finds an upstream |
| 114 | + dir := filepath.Join(tmpDir, "wasteland", "wastelands", "hop") |
| 115 | + if err := os.MkdirAll(dir, 0o755); err != nil { |
| 116 | + t.Fatal(err) |
| 117 | + } |
| 118 | + // Write corrupt JSON |
| 119 | + if err := os.WriteFile(filepath.Join(dir, "wl-commons.json"), []byte("{bad json"), 0o644); err != nil { |
| 120 | + t.Fatal(err) |
| 121 | + } |
| 122 | + |
| 123 | + var stdout, stderr bytes.Buffer |
| 124 | + err := runList(&stdout, &stderr) |
| 125 | + if err != nil { |
| 126 | + t.Fatalf("runList() should not error on corrupt config: %v", err) |
| 127 | + } |
| 128 | + // Error should be printed to stderr |
| 129 | + if !strings.Contains(stderr.String(), "error loading config") { |
| 130 | + t.Errorf("stderr = %q, want error message about corrupt config", stderr.String()) |
| 131 | + } |
| 132 | +} |
0 commit comments