github.com/ungtb10d/cli/v2@v2.0.0-20221110210412-98537dd9d6a1/pkg/cmd/run/shared/shared_test.go (about)

     1  package shared
     2  
     3  import (
     4  	"encoding/json"
     5  	"net/http"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/MakeNowJust/heredoc"
    10  	"github.com/ungtb10d/cli/v2/api"
    11  	"github.com/ungtb10d/cli/v2/internal/ghrepo"
    12  	"github.com/ungtb10d/cli/v2/pkg/httpmock"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  func TestPreciseAgo(t *testing.T) {
    17  	const form = "2006-Jan-02 15:04:05"
    18  	now, _ := time.Parse(form, "2021-Apr-12 14:00:00")
    19  
    20  	cases := map[string]string{
    21  		"2021-Apr-12 14:00:00": "0s ago",
    22  		"2021-Apr-12 13:59:30": "30s ago",
    23  		"2021-Apr-12 13:59:00": "1m0s ago",
    24  		"2021-Apr-12 13:30:15": "29m45s ago",
    25  		"2021-Apr-12 13:00:00": "1h0m0s ago",
    26  		"2021-Apr-12 02:30:45": "11h29m15s ago",
    27  		"2021-Apr-11 14:00:00": "24h0m0s ago",
    28  		"2021-Apr-01 14:00:00": "264h0m0s ago",
    29  		"2021-Mar-12 14:00:00": "Mar 12, 2021",
    30  	}
    31  
    32  	for createdAt, expected := range cases {
    33  		d, _ := time.Parse(form, createdAt)
    34  		got := preciseAgo(now, d)
    35  		if got != expected {
    36  			t.Errorf("expected %s but got %s for %s", expected, got, createdAt)
    37  		}
    38  	}
    39  }
    40  
    41  func TestGetAnnotations404(t *testing.T) {
    42  	reg := &httpmock.Registry{}
    43  	defer reg.Verify(t)
    44  
    45  	reg.Register(
    46  		httpmock.REST("GET", "repos/OWNER/REPO/check-runs/123456/annotations"),
    47  		httpmock.StatusStringResponse(404, "not found"))
    48  
    49  	httpClient := &http.Client{Transport: reg}
    50  	apiClient := api.NewClientFromHTTP(httpClient)
    51  	repo := ghrepo.New("OWNER", "REPO")
    52  
    53  	result, err := GetAnnotations(apiClient, repo, Job{ID: 123456, Name: "a job"})
    54  	assert.NoError(t, err)
    55  	assert.Equal(t, result, []Annotation{})
    56  }
    57  
    58  func TestRun_Duration(t *testing.T) {
    59  	now, _ := time.Parse(time.RFC3339, "2022-07-20T11:22:58Z")
    60  
    61  	tests := []struct {
    62  		name  string
    63  		json  string
    64  		wants string
    65  	}{
    66  		{
    67  			name: "no run_started_at",
    68  			json: heredoc.Doc(`
    69  				{
    70  					"created_at": "2022-07-20T11:20:13Z",
    71  					"updated_at": "2022-07-20T11:21:16Z",
    72  					"status": "completed"
    73  				}`),
    74  			wants: "1m3s",
    75  		},
    76  		{
    77  			name: "with run_started_at",
    78  			json: heredoc.Doc(`
    79  				{
    80  					"created_at": "2022-07-20T11:20:13Z",
    81  					"run_started_at": "2022-07-20T11:20:55Z",
    82  					"updated_at": "2022-07-20T11:21:16Z",
    83  					"status": "completed"
    84  				}`),
    85  			wants: "21s",
    86  		},
    87  		{
    88  			name: "in_progress",
    89  			json: heredoc.Doc(`
    90  				{
    91  					"created_at": "2022-07-20T11:20:13Z",
    92  					"run_started_at": "2022-07-20T11:20:55Z",
    93  					"updated_at": "2022-07-20T11:21:16Z",
    94  					"status": "in_progress"
    95  				}`),
    96  			wants: "2m3s",
    97  		},
    98  	}
    99  	for _, tt := range tests {
   100  		t.Run(tt.name, func(t *testing.T) {
   101  			var r Run
   102  			assert.NoError(t, json.Unmarshal([]byte(tt.json), &r))
   103  			assert.Equal(t, tt.wants, r.Duration(now).String())
   104  		})
   105  	}
   106  }