|
| 1 | +package app |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/morluto/gitcontribute/internal/cli" |
| 11 | + "github.com/morluto/gitcontribute/internal/config" |
| 12 | + "github.com/morluto/gitcontribute/internal/github" |
| 13 | +) |
| 14 | + |
| 15 | +func TestEndToEndSyncSearchDossier(t *testing.T) { |
| 16 | + t.Parallel() |
| 17 | + ctx := context.Background() |
| 18 | + srv := newTestServer("octocat", "test") |
| 19 | + defer srv.Close() |
| 20 | + |
| 21 | + svc := newTestService(t, srv) |
| 22 | + defer func() { _ = svc.Close() }() |
| 23 | + |
| 24 | + syncRes, err := svc.Sync(ctx, cli.RepoRef{Owner: "octocat", Repo: "test"}) |
| 25 | + if err != nil { |
| 26 | + t.Fatalf("sync: %v", err) |
| 27 | + } |
| 28 | + if syncRes.Updated != 2 { |
| 29 | + t.Fatalf("updated = %d, want 2", syncRes.Updated) |
| 30 | + } |
| 31 | + |
| 32 | + searchRes, err := svc.Search(ctx, "searchable", cli.SearchOptions{Kind: "issues", Limit: 10}) |
| 33 | + if err != nil { |
| 34 | + t.Fatalf("search: %v", err) |
| 35 | + } |
| 36 | + if searchRes.Total != 1 || len(searchRes.Matches) != 1 { |
| 37 | + t.Fatalf("search results = %+v", searchRes) |
| 38 | + } |
| 39 | + if searchRes.Matches[0].Number != 1 { |
| 40 | + t.Fatalf("unexpected match: %+v", searchRes.Matches[0]) |
| 41 | + } |
| 42 | + |
| 43 | + dossierRes, err := svc.Dossier(ctx, cli.RepoRef{Owner: "octocat", Repo: "test"}) |
| 44 | + if err != nil { |
| 45 | + t.Fatalf("dossier: %v", err) |
| 46 | + } |
| 47 | + if dossierRes.Stars != 42 { |
| 48 | + t.Fatalf("stars = %d, want 42", dossierRes.Stars) |
| 49 | + } |
| 50 | + if dossierRes.OpenIssues != 1 { |
| 51 | + t.Fatalf("open issues = %d, want 1", dossierRes.OpenIssues) |
| 52 | + } |
| 53 | + if dossierRes.Summary != "A test repository" { |
| 54 | + t.Fatalf("summary = %q", dossierRes.Summary) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +func TestTailSourceRunsOneIdempotentIteration(t *testing.T) { |
| 59 | + t.Parallel() |
| 60 | + ctx := context.Background() |
| 61 | + srv := newTestServer("octocat", "tail") |
| 62 | + defer srv.Close() |
| 63 | + svc := newTestService(t, srv) |
| 64 | + defer func() { _ = svc.Close() }() |
| 65 | + |
| 66 | + if _, err := svc.AddRepoSource(ctx, "explicit", []cli.RepoRef{{Owner: "octocat", Repo: "tail"}}); err != nil { |
| 67 | + t.Fatalf("add source: %v", err) |
| 68 | + } |
| 69 | + result, err := svc.TailSource(ctx, "explicit", cli.TailOptions{ |
| 70 | + Since: time.Hour, Budget: 1, Interval: time.Minute, Once: true, |
| 71 | + }) |
| 72 | + if err != nil { |
| 73 | + t.Fatalf("tail source: %v", err) |
| 74 | + } |
| 75 | + if result.Iterations != 1 || result.Last == nil || result.Last.Repositories != 1 { |
| 76 | + t.Fatalf("tail result = %+v", result) |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func TestDiscoveryCrawlDoesNotAdvanceCheckpointWhenBudgetExhausted(t *testing.T) { |
| 81 | + t.Parallel() |
| 82 | + ctx := context.Background() |
| 83 | + srv := newTestServer("octocat", "discovered") |
| 84 | + defer srv.Close() |
| 85 | + svc := newTestService(t, srv) |
| 86 | + defer func() { _ = svc.Close() }() |
| 87 | + if _, err := svc.AddSearchSource(ctx, "bounded", "language:go"); err != nil { |
| 88 | + t.Fatal(err) |
| 89 | + } |
| 90 | + if _, err := svc.Crawl(ctx, "bounded", cli.CrawlOptions{Since: time.Hour, Budget: 1}); err == nil || !strings.Contains(err.Error(), "budget") { |
| 91 | + t.Fatalf("crawl error = %v, want budget exhaustion", err) |
| 92 | + } |
| 93 | + c, err := svc.openCorpus(ctx) |
| 94 | + if err != nil { |
| 95 | + t.Fatal(err) |
| 96 | + } |
| 97 | + if checkpoint, exists, err := c.GetTime(ctx, "source:bounded"); err != nil || exists { |
| 98 | + t.Fatalf("checkpoint = %v exists=%v err=%v", checkpoint, exists, err) |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +func TestAddSearchSourceRejectsUnstableName(t *testing.T) { |
| 103 | + t.Parallel() |
| 104 | + ctx := context.Background() |
| 105 | + srv := newTestServer("octocat", "test") |
| 106 | + defer srv.Close() |
| 107 | + svc := newTestService(t, srv) |
| 108 | + defer func() { _ = svc.Close() }() |
| 109 | + if _, err := svc.AddSearchSource(ctx, "contains spaces", "language:go"); err == nil { |
| 110 | + t.Fatal("expected invalid source name error") |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +func TestSearchReportsDefaultLimit(t *testing.T) { |
| 115 | + t.Parallel() |
| 116 | + ctx := context.Background() |
| 117 | + srv := newTestServer("octocat", "test") |
| 118 | + defer srv.Close() |
| 119 | + svc := newTestService(t, srv) |
| 120 | + defer func() { _ = svc.Close() }() |
| 121 | + if _, err := svc.Sync(ctx, cli.RepoRef{Owner: "octocat", Repo: "test"}); err != nil { |
| 122 | + t.Fatal(err) |
| 123 | + } |
| 124 | + result, err := svc.Search(ctx, "searchable", cli.SearchOptions{}) |
| 125 | + if err != nil { |
| 126 | + t.Fatal(err) |
| 127 | + } |
| 128 | + if result.Limit != 20 { |
| 129 | + t.Fatalf("reported limit = %d, want 20", result.Limit) |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func TestLocalInitializationDoesNotResolveKeyringAuth(t *testing.T) { |
| 134 | + t.Parallel() |
| 135 | + ctx := context.Background() |
| 136 | + paths := config.NewPaths(&config.Env{Home: t.TempDir()}) |
| 137 | + configPath, err := paths.ConfigFile() |
| 138 | + if err != nil { |
| 139 | + t.Fatal(err) |
| 140 | + } |
| 141 | + cfg := config.Default() |
| 142 | + cfg.TokenSource.Method = "keyring" |
| 143 | + cfg.TokenSource.Key = "test-account" |
| 144 | + if err := config.ApplyDefaults(cfg, paths); err != nil { |
| 145 | + t.Fatal(err) |
| 146 | + } |
| 147 | + if err := config.Save(configPath, cfg); err != nil { |
| 148 | + t.Fatal(err) |
| 149 | + } |
| 150 | + svc, err := New(paths, "test", nil) |
| 151 | + if err != nil { |
| 152 | + t.Fatalf("local service construction resolved GitHub auth: %v", err) |
| 153 | + } |
| 154 | + defer func() { _ = svc.Close() }() |
| 155 | + if _, err := svc.Init(ctx); err != nil { |
| 156 | + t.Fatalf("local init resolved GitHub auth: %v", err) |
| 157 | + } |
| 158 | +} |
| 159 | + |
| 160 | +func TestConfiguredAuthenticationIsRequired(t *testing.T) { |
| 161 | + t.Setenv("GITCONTRIBUTE_TEST_MISSING_TOKEN", "") |
| 162 | + cfg := config.Default() |
| 163 | + cfg.TokenSource = config.TokenSource{ |
| 164 | + Method: "env", |
| 165 | + Key: "GITCONTRIBUTE_TEST_MISSING_TOKEN", |
| 166 | + } |
| 167 | + |
| 168 | + _, err := tokenSource(cfg).Token(context.Background()) |
| 169 | + if !errors.Is(err, github.ErrRequiredToken) { |
| 170 | + t.Fatalf("configured auth error = %v, want ErrRequiredToken", err) |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +func TestNewRejectsInvalidConfiguredTokenSource(t *testing.T) { |
| 175 | + t.Parallel() |
| 176 | + paths := config.NewPaths(&config.Env{Home: t.TempDir()}) |
| 177 | + configPath, err := paths.ConfigFile() |
| 178 | + if err != nil { |
| 179 | + t.Fatal(err) |
| 180 | + } |
| 181 | + cfg := config.Default() |
| 182 | + cfg.TokenSource.Method = "keyrign" |
| 183 | + if err := config.ApplyDefaults(cfg, paths); err != nil { |
| 184 | + t.Fatal(err) |
| 185 | + } |
| 186 | + if err := config.Save(configPath, cfg); err != nil { |
| 187 | + t.Fatal(err) |
| 188 | + } |
| 189 | + |
| 190 | + _, err = New(paths, "test", nil) |
| 191 | + if err == nil || !strings.Contains(err.Error(), "invalid token_source method") { |
| 192 | + t.Fatalf("New error = %v, want invalid token source", err) |
| 193 | + } |
| 194 | +} |
0 commit comments