...

Source file src/edge-infra.dev/pkg/f8n/devinfra/jack/plugin/size/handler_test.go

Documentation: edge-infra.dev/pkg/f8n/devinfra/jack/plugin/size

     1  package size
     2  
     3  import (
     4  	"context"
     5  	"reflect"
     6  	"testing"
     7  
     8  	"edge-infra.dev/pkg/lib/logging"
     9  
    10  	"github.com/google/go-github/v47/github"
    11  
    12  	"edge-infra.dev/pkg/f8n/devinfra/jack/constants"
    13  	"edge-infra.dev/pkg/f8n/devinfra/jack/plugin"
    14  )
    15  
    16  var c = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{}}
    17  var hp = &plugin.HandlerParams{
    18  	Ctx:    context.Background(),
    19  	Client: c,
    20  	Org:    "some-org",
    21  	Repo:   "some-repo",
    22  	Log:    *logging.NewLogger(),
    23  }
    24  
    25  func TestAddSizeToNewIssue(t *testing.T) {
    26  	// Test if issue is an epic.
    27  	c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Epic)}}
    28  	ie := github.IssuesEvent{
    29  		Action: github.String("opened"),
    30  		Issue:  &github.Issue{Labels: []*github.Label{{Name: github.String(string(constants.Epic))}}},
    31  	}
    32  
    33  	handleIssue(*hp, ie)
    34  
    35  	ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Epic)}}}
    36  	if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
    37  		t.Error("Failed: TestAddSizeToNewIssue -> Test if issue is an epic.")
    38  	}
    39  
    40  	// Test if a size label already exists.
    41  	c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Task), constants.SmallLabel}}
    42  	ie = github.IssuesEvent{
    43  		Action: github.String("opened"),
    44  		Issue:  &github.Issue{Labels: []*github.Label{{Name: github.String(string(constants.Task))}, {Name: github.String(constants.SmallLabel)}}},
    45  	}
    46  
    47  	handleIssue(*hp, ie)
    48  
    49  	ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Task), constants.SmallLabel}}}
    50  	if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
    51  		t.Error("Failed: TestAddSizeToNewIssue -> Test if a size label already exists.")
    52  	}
    53  
    54  	// Test if a size command exists in the issue body
    55  	c.GithubIssues = &plugin.MockIssueService{
    56  		Labels: []string{string(constants.Task)},
    57  		Body:   "No relevant command here",
    58  	}
    59  	ie = github.IssuesEvent{
    60  		Action: github.String("opened"),
    61  		Issue: &github.Issue{
    62  			Labels: []*github.Label{{Name: github.String(string(constants.Task))}},
    63  			Body:   github.String("No relevant command here"),
    64  		},
    65  	}
    66  
    67  	handleIssue(*hp, ie)
    68  
    69  	ct = &plugin.MockGithubClient{
    70  		GithubIssues: &plugin.MockIssueService{
    71  			Labels: []string{string(constants.Task), constants.TriageSizeLabel},
    72  			Body:   "No relevant command here",
    73  		},
    74  	}
    75  	if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
    76  		t.Error("Failed: TestAddSizeToNewIssue -> Test if a size command exists in the issue body.")
    77  	}
    78  
    79  	// Test if the size command has "extra" in it, but is an invalid command
    80  	c.GithubIssues = &plugin.MockIssueService{
    81  		Labels: []string{string(constants.Task)},
    82  		Body:   "This issue is of size extra medismell",
    83  	}
    84  	ie = github.IssuesEvent{
    85  		Action: github.String("opened"),
    86  		Issue: &github.Issue{
    87  			Labels: []*github.Label{{Name: github.String(string(constants.Task))}},
    88  			Body:   github.String("This issue is of size extra medismell"),
    89  		},
    90  	}
    91  
    92  	handleIssue(*hp, ie)
    93  
    94  	ct = &plugin.MockGithubClient{
    95  		GithubIssues: &plugin.MockIssueService{
    96  			Labels: []string{string(constants.Task), constants.TriageSizeLabel},
    97  			Body:   "This issue is of size extra medismell",
    98  		},
    99  	}
   100  	if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
   101  		t.Error("Failed: TestAddSizeToNewIssue -> Test if the size command has 'extra' in it, but is an invalid command.")
   102  	}
   103  
   104  	// Test if the size command is invalid
   105  	c.GithubIssues = &plugin.MockIssueService{
   106  		Labels: []string{string(constants.Task)},
   107  		Body:   "This issue is of size medismell",
   108  	}
   109  	ie = github.IssuesEvent{
   110  		Action: github.String("opened"),
   111  		Issue: &github.Issue{
   112  			Labels: []*github.Label{{Name: github.String(string(constants.Task))}},
   113  			Body:   github.String("This issue is of size medismell"),
   114  		},
   115  	}
   116  
   117  	handleIssue(*hp, ie)
   118  
   119  	ct = &plugin.MockGithubClient{
   120  		GithubIssues: &plugin.MockIssueService{
   121  			Labels: []string{string(constants.Task), constants.TriageSizeLabel},
   122  			Body:   "This issue is of size medismell",
   123  		},
   124  	}
   125  	if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
   126  		t.Error("Failed: TestAddSizeToNewIssue -> Test if the size command is invalid.")
   127  	}
   128  
   129  	// Test if the size command with "extra" is valid
   130  	c.GithubIssues = &plugin.MockIssueService{
   131  		Labels: []string{string(constants.Task)},
   132  		Body:   "This issue is of size extra large",
   133  	}
   134  	ie = github.IssuesEvent{
   135  		Action: github.String("opened"),
   136  		Issue: &github.Issue{
   137  			Labels: []*github.Label{{Name: github.String(string(constants.Task))}},
   138  			Body:   github.String("This issue is of size extra large"),
   139  		},
   140  	}
   141  
   142  	handleIssue(*hp, ie)
   143  
   144  	ct = &plugin.MockGithubClient{
   145  		GithubIssues: &plugin.MockIssueService{
   146  			Labels: []string{string(constants.Task), constants.ExtraLargeLabel},
   147  			Body:   "This issue is of size extra large",
   148  		},
   149  	}
   150  	if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
   151  		t.Error("Failed: TestAddSizeToNewIssue -> Test if the size command with 'extra' is valid.")
   152  	}
   153  
   154  	// Test if the size command is valid
   155  	c.GithubIssues = &plugin.MockIssueService{
   156  		Labels: []string{string(constants.Task)},
   157  		Body:   "This issue is of size large",
   158  	}
   159  	ie = github.IssuesEvent{
   160  		Action: github.String("opened"),
   161  		Issue: &github.Issue{
   162  			Labels: []*github.Label{{Name: github.String(string(constants.Task))}},
   163  			Body:   github.String("This issue is of size large"),
   164  		},
   165  	}
   166  
   167  	handleIssue(*hp, ie)
   168  
   169  	ct = &plugin.MockGithubClient{
   170  		GithubIssues: &plugin.MockIssueService{
   171  			Labels: []string{string(constants.Task), constants.LargeLabel},
   172  			Body:   "This issue is of size large",
   173  		},
   174  	}
   175  	if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
   176  		t.Error("Failed: TestAddSizeToNewIssue -> // Test if the size command is valid.")
   177  	}
   178  }
   179  
   180  func TestAddLabel(t *testing.T) {
   181  	// Test if an issue has an existing parent label
   182  	c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Epic)}}
   183  	ie := github.IssuesEvent{
   184  		Action: github.String("labeled"),
   185  		Label:  &github.Label{Name: github.String(string(constants.Feature))},
   186  		Issue:  &github.Issue{Labels: []*github.Label{{Name: github.String(string(constants.Epic))}}},
   187  	}
   188  
   189  	handleIssue(*hp, ie)
   190  
   191  	ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Epic)}}}
   192  	if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
   193  		t.Error("Failed: TestAddLabel -> Test if an issue has an existing parent label.")
   194  	}
   195  
   196  	// Test if the added label is a parent label and no parent label currently exists
   197  	// This action should clear an existing size label
   198  	c.GithubIssues = &plugin.MockIssueService{Labels: []string{constants.SmallLabel}}
   199  	ie = github.IssuesEvent{
   200  		Action: github.String("labeled"),
   201  		Label:  &github.Label{Name: github.String(string(constants.Feature))},
   202  		Issue:  &github.Issue{Labels: []*github.Label{{Name: github.String(constants.SmallLabel)}}},
   203  	}
   204  
   205  	handleIssue(*hp, ie)
   206  
   207  	ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{}}}
   208  	if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
   209  		t.Error("Failed: TestAddLabel -> Test if the added label is a parent label and no parent label currently exists.")
   210  	}
   211  
   212  	// Test if the new label is an invalid size label
   213  	c.GithubIssues = &plugin.MockIssueService{Labels: []string{}}
   214  	ie = github.IssuesEvent{
   215  		Action: github.String("labeled"),
   216  		Label:  &github.Label{Name: github.String("size/1234")},
   217  		Issue:  &github.Issue{Labels: []*github.Label{}},
   218  	}
   219  
   220  	handleIssue(*hp, ie)
   221  
   222  	ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{}}}
   223  	if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
   224  		t.Error("Failed: TestAddLabel -> Test if the new label is not a size label.")
   225  	}
   226  
   227  	// Test if a valid size label is added but a parent label exists
   228  	c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Epic)}}
   229  	ie = github.IssuesEvent{
   230  		Action: github.String("labeled"),
   231  		Label:  &github.Label{Name: github.String(constants.SmallLabel)},
   232  		Issue:  &github.Issue{Labels: []*github.Label{{Name: github.String(string(constants.Epic))}}},
   233  	}
   234  
   235  	handleIssue(*hp, ie)
   236  
   237  	ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Epic)}}}
   238  	if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
   239  		t.Error("Failed: TestAddLabel -> Test if a valid size label is added but a parent label exists.")
   240  	}
   241  
   242  	// Test adding a valid size label (normal behavior)
   243  	c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Task)}}
   244  	ie = github.IssuesEvent{
   245  		Action: github.String("labeled"),
   246  		Label:  &github.Label{Name: github.String(constants.SmallLabel)},
   247  		Issue:  &github.Issue{Labels: []*github.Label{{Name: github.String(string(constants.Task))}}},
   248  	}
   249  
   250  	handleIssue(*hp, ie)
   251  
   252  	ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Task), constants.SmallLabel}}}
   253  	if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
   254  		t.Error("Failed: TestAddLabel -> Test adding a valid size label (normal behavior).")
   255  	}
   256  }
   257  
   258  func TestRemoveLabel(t *testing.T) {
   259  	// Test if issue has a parent label
   260  	c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Feature), constants.MediumLabel}}
   261  	ie := github.IssuesEvent{
   262  		Action: github.String("unlabeled"),
   263  		Label:  &github.Label{Name: github.String(constants.MediumLabel)},
   264  		Issue:  &github.Issue{Labels: []*github.Label{{Name: github.String(string(constants.Feature))}, {Name: github.String(constants.MediumLabel)}}},
   265  	}
   266  
   267  	handleIssue(*hp, ie)
   268  
   269  	ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Feature), constants.MediumLabel}}}
   270  	if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
   271  		t.Error("Failed: TestRemoveLabel -> Test if issue has a parent label.")
   272  	}
   273  
   274  	// Test if an epic label is being removed
   275  	c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Feature)}}
   276  	ie = github.IssuesEvent{
   277  		Action: github.String("unlabeled"),
   278  		Label:  &github.Label{Name: github.String(string(constants.Feature))},
   279  		Issue:  &github.Issue{Labels: []*github.Label{}},
   280  	}
   281  
   282  	handleIssue(*hp, ie)
   283  
   284  	ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{constants.TriageSizeLabel}}}
   285  	if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
   286  		t.Error("Failed: TestRemoveLabel -> // Test if an epic label is being removed.")
   287  	}
   288  
   289  	// Test if the label is not a valid size
   290  	c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Task), constants.MediumLabel}}
   291  	ie = github.IssuesEvent{
   292  		Action: github.String("unlabeled"),
   293  		Label:  &github.Label{Name: github.String(string(constants.Task))},
   294  		Issue:  &github.Issue{Labels: []*github.Label{{Name: github.String(string(constants.Task))}, {Name: github.String(constants.MediumLabel)}}},
   295  	}
   296  
   297  	handleIssue(*hp, ie)
   298  
   299  	ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Task), constants.MediumLabel}}}
   300  	if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
   301  		t.Error("Failed: TestRemoveLabel -> Test if the label is not a valid size.")
   302  	}
   303  
   304  	// Test removal of valid size label
   305  	c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Task), constants.MediumLabel}}
   306  	ie = github.IssuesEvent{
   307  		Action: github.String("unlabeled"),
   308  		Label:  &github.Label{Name: github.String(constants.MediumLabel)},
   309  		Issue:  &github.Issue{Labels: []*github.Label{{Name: github.String(string(constants.Task))}}},
   310  	}
   311  
   312  	handleIssue(*hp, ie)
   313  
   314  	ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Task), constants.TriageSizeLabel}}}
   315  	if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
   316  		t.Error("Failed: TestRemoveLabel -> Test removal of valid size label.")
   317  	}
   318  }
   319  
   320  // Check Issue Comment events for handleIssueComment
   321  func TestCommentEvents(t *testing.T) {
   322  	// Test if no size commands exist in the comment
   323  	c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Feature)}}
   324  	ce := github.IssueCommentEvent{
   325  		Action:  github.String("created"),
   326  		Comment: &github.IssueComment{Body: github.String("No valid comment")},
   327  		Issue:   &github.Issue{Labels: []*github.Label{{Name: github.String(string(constants.Feature))}}},
   328  	}
   329  
   330  	handleIssueComment(*hp, ce)
   331  
   332  	ct := &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Feature)}}}
   333  	if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
   334  		t.Error("Failed: TestCommentEvents -> Test if no size commands exist in the comment.")
   335  	}
   336  
   337  	// Test if the size command applies to an epic
   338  	// A warning comment should be added to the issue
   339  	c.GithubIssues = &plugin.MockIssueService{
   340  		Labels:   []string{string(constants.Feature)},
   341  		Comments: 0,
   342  		ID:       1,
   343  	}
   344  	ce = github.IssueCommentEvent{
   345  		Action:  github.String("created"),
   346  		Comment: &github.IssueComment{Body: github.String("size small")},
   347  		Issue: &github.Issue{
   348  			Labels: []*github.Label{{Name: github.String(string(constants.Feature))}},
   349  			Number: github.Int(1),
   350  			ID:     github.Int64(1),
   351  		},
   352  		Repo: &github.Repository{
   353  			Name:  github.String(hp.Repo),
   354  			Owner: &github.User{Login: github.String(hp.Org)},
   355  		},
   356  	}
   357  
   358  	handleIssueComment(*hp, ce)
   359  
   360  	ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{
   361  		Labels:   []string{string(constants.Feature)},
   362  		Comments: 1,
   363  		ID:       1,
   364  	}}
   365  	if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
   366  		t.Error("Failed: TestCommentEvents -> Test if the size command applies to an epic.")
   367  	}
   368  
   369  	// Test if the command is an invalid extra size command
   370  	c.GithubIssues = &plugin.MockIssueService{Labels: []string{}}
   371  	ce = github.IssueCommentEvent{
   372  		Action:  github.String("created"),
   373  		Comment: &github.IssueComment{Body: github.String("size extra medismell")},
   374  		Issue:   &github.Issue{Labels: []*github.Label{}},
   375  	}
   376  
   377  	handleIssueComment(*hp, ce)
   378  
   379  	ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{}}}
   380  	if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
   381  		t.Error("Failed: TestCommentEvents -> Test if the command is an invalid extra size command.")
   382  	}
   383  
   384  	// Test if the command in the comment already exists
   385  	c.GithubIssues = &plugin.MockIssueService{Labels: []string{constants.ExtraSmallLabel}}
   386  	ce = github.IssueCommentEvent{
   387  		Action:  github.String("created"),
   388  		Comment: &github.IssueComment{Body: github.String("size extra small")},
   389  		Issue:   &github.Issue{Labels: []*github.Label{{Name: github.String(constants.ExtraSmallLabel)}}},
   390  	}
   391  
   392  	handleIssueComment(*hp, ce)
   393  
   394  	ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{constants.ExtraSmallLabel}}}
   395  	if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
   396  		t.Error("Failed: TestCommentEvents -> Test if the command in the comment already exists.")
   397  	}
   398  
   399  	// Test if the size command is added as a label (normal behavior)
   400  	c.GithubIssues = &plugin.MockIssueService{Labels: []string{string(constants.Task), constants.ExtraSmallLabel}}
   401  	ce = github.IssueCommentEvent{
   402  		Action:  github.String("created"),
   403  		Comment: &github.IssueComment{Body: github.String("size small")},
   404  		Issue:   &github.Issue{Labels: []*github.Label{{Name: github.String(string(constants.Task))}, {Name: github.String(constants.ExtraSmallLabel)}}},
   405  	}
   406  
   407  	handleIssueComment(*hp, ce)
   408  
   409  	ct = &plugin.MockGithubClient{GithubIssues: &plugin.MockIssueService{Labels: []string{string(constants.Task), constants.SmallLabel}}}
   410  	if !reflect.DeepEqual(ct.GithubIssues, c.GithubIssues) {
   411  		t.Error("Failed: TestCommentEvents -> Test if the size command is added as a label (normal behavior).")
   412  	}
   413  }
   414  

View as plain text