92 lines
2.9 KiB
Go
92 lines
2.9 KiB
Go
package github
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
)
|
|
|
|
type mentionSearchClient struct {
|
|
*MockClient
|
|
issueQuery string
|
|
prQuery string
|
|
issues []*Issue
|
|
prs []*PR
|
|
}
|
|
|
|
func (c *mentionSearchClient) ListIssuesPaged(
|
|
_ context.Context,
|
|
_, customQuery string,
|
|
page, perPage int,
|
|
) (*IssueSearchPage, error) {
|
|
c.issueQuery = customQuery
|
|
return &IssueSearchPage{Issues: c.issues, TotalCount: len(c.issues), Page: page, PerPage: perPage}, nil
|
|
}
|
|
|
|
func (c *mentionSearchClient) SearchPRsPaged(
|
|
_ context.Context,
|
|
_, customQuery string,
|
|
page, perPage int,
|
|
) (*PRSearchPage, error) {
|
|
c.prQuery = customQuery
|
|
return &PRSearchPage{PRs: c.prs, TotalCount: len(c.prs), Page: page, PerPage: perPage}, nil
|
|
}
|
|
|
|
func TestServiceSearchMentionsUsesServerBuiltTitleOnlyQuery(t *testing.T) {
|
|
client := &mentionSearchClient{
|
|
MockClient: NewMockClient(),
|
|
issues: []*Issue{{ID: 101, NodeID: "I_kwDOA", Number: 7, Title: "Fix auth", RepoOwner: "acme", RepoName: "web"}},
|
|
prs: []*PR{{ID: 202, NodeID: "PR_kwDOB", Number: 8, Title: "Fix auth flow", RepoOwner: "acme", RepoName: "web"}},
|
|
}
|
|
service := &Service{client: client, searchCache: newTTLCache()}
|
|
// Mention search is a workspace-scoped personal-read operation. Seed the
|
|
// workspace's human automation identity explicitly rather than relying on
|
|
// the legacy, process-wide test client.
|
|
configureTestWorkspaceAuth(t, service, client, "workspace-1")
|
|
|
|
issues, err := service.SearchMentionIssuesForWorkspace(
|
|
context.Background(),
|
|
"workspace-1",
|
|
`fix type:pr is:issue repo:foreign/private "auth" \\ flow`,
|
|
3,
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("search issue mentions: %v", err)
|
|
}
|
|
prs, err := service.SearchMentionPullRequestsForWorkspace(
|
|
context.Background(),
|
|
"workspace-1",
|
|
`fix type:pr is:issue repo:foreign/private "auth" \\ flow`,
|
|
4,
|
|
)
|
|
if err != nil {
|
|
t.Fatalf("search PR mentions: %v", err)
|
|
}
|
|
wantIssueQuery := `type:issue in:title "fix type:pr is:issue repo:foreign/private \"auth\" \\\\ flow"`
|
|
wantPRQuery := `type:pr in:title "fix type:pr is:issue repo:foreign/private \"auth\" \\\\ flow"`
|
|
if client.issueQuery != wantIssueQuery || client.prQuery != wantPRQuery {
|
|
t.Fatalf(
|
|
"queries = issue %q PR %q, want issue %q PR %q",
|
|
client.issueQuery,
|
|
client.prQuery,
|
|
wantIssueQuery,
|
|
wantPRQuery,
|
|
)
|
|
}
|
|
if len(issues) != 1 || issues[0].NodeID != "I_kwDOA" {
|
|
t.Fatalf("issues = %#v", issues)
|
|
}
|
|
if len(prs) != 1 || prs[0].NodeID != "PR_kwDOB" {
|
|
t.Fatalf("PRs = %#v", prs)
|
|
}
|
|
}
|
|
|
|
func TestServiceSearchMentionsRequiresExplicitWorkspaceAndQuery(t *testing.T) {
|
|
service := &Service{client: NewMockClient(), searchCache: newTTLCache()}
|
|
if _, err := service.SearchMentionIssuesForWorkspace(context.Background(), "", "auth", 5); err == nil {
|
|
t.Fatal("blank workspace was accepted")
|
|
}
|
|
if _, err := service.SearchMentionPullRequestsForWorkspace(context.Background(), "workspace-1", " ", 5); err == nil {
|
|
t.Fatal("blank query was accepted")
|
|
}
|
|
}
|