Repository: cli/cli
File: pkg/cmd/gist/edit/edit_test.go (Go)

Code before:
			wantErr: "gist ID or URL required when not running interactively",
		},
		{
			name: "edit no-file gist (#10626)",
			opts: &EditOptions{
				Selector: "1234",
			},
			mockGist: &shared.Gist{
				ID:    "1234",
				Files: map[string]*shared.GistFile{},
				Owner: &shared.GistOwner{Login: "octocat"},
			},
			wantErr: "No file found in this gist.",
		},
	}

	for _, tt := range tests {
		reg := &httpmock.Registry{}
		pm := prompter.NewMockPrompter(t)

		if tt.opts == nil {
			tt.opts = &EditOptions{}
		}

		if tt.opts.Selector != "" {
			// Only register the HTTP stubs for a direct gist lookup if a selector is provided.
			if tt.mockGist == nil {
				// If no gist is provided, we expect a 404.
				reg.Register(httpmock.REST("GET", fmt.Sprintf("gists/%s", tt.opts.Selector)),
					httpmock.StatusStringResponse(404, "Not Found"))
			} else {
				// If a gist is provided, we expect the gist to be fetched.
				reg.Register(httpmock.REST("GET", fmt.Sprintf("gists/%s", tt.opts.Selector)),
					httpmock.JSONResponse(tt.mockGist))
				reg.Register(httpmock.GraphQL(`query UserCurrent\b`),
					httpmock.StringResponse(`{"data":{"viewer":{"login":"octocat"}}}`))
			}
		}

		if tt.mockGistList {
			sixHours, _ := time.ParseDuration("6h")
			sixHoursAgo := time.Now().Add(-sixHours)
			reg.Register(httpmock.GraphQL(`query GistList\b`),
				httpmock.StringResponse(
					fmt.Sprintf(`{ "data": { "viewer": { "gists": { "nodes": [
							{
								"description": "whatever",
								"files": [{ "name": "cicada.txt" }, { "name": "unix.md"	}],
								"isPublic": true,
								"name": "1234",
								"updatedAt": "%s"

Review comment (suggestion):
To future-proof this, we also need another test where the `Files` map is `nil` (not empty).

```suggestion
		{
			name: "edit no-file gist (#10626)",
			opts: &EditOptions{
				Selector: "1234",
			},
			mockGist: &shared.Gist{
				ID:    "1234",
				Files: map[string]*shared.GistFile{},
				Owner: &shared.GistOwner{Login: "octocat"},
			},
			wantErr: "No file found in this gist.",
		},
		{
			name: "edit no-file gist, nil map (#10626)",
			opts: &EditOptions{
				Selector: "1234",
			},
			mockGist: &shared.Gist{
				ID:    "1234",
				Files: nil,
				Owner: &shared.GistOwner{Login: "octocat"},
			},
			wantErr: "No file found in this gist.",
		},
```

Code after:
			wantErr: "gist ID or URL required when not running interactively",
		},
		{
			name: "edit no-file gist (#10626)",
			opts: &EditOptions{
				Selector: "1234",
			},
			mockGist: &shared.Gist{
				ID:    "1234",
				Files: map[string]*shared.GistFile{},
				Owner: &shared.GistOwner{Login: "octocat"},
			},
			wantErr: "no file in the gist",
		},
		{
			name: "edit no-file gist, nil map (#10626)",
			opts: &EditOptions{
				Selector: "1234",
			},
			mockGist: &shared.Gist{
				ID:    "1234",
				Files: nil,
				Owner: &shared.GistOwner{Login: "octocat"},
			},
			wantErr: "no file in the gist",
		},
	}

	for _, tt := range tests {
		reg := &httpmock.Registry{}
		pm := prompter.NewMockPrompter(t)

		if tt.opts == nil {
			tt.opts = &EditOptions{}
		}

		if tt.opts.Selector != "" {
			// Only register the HTTP stubs for a direct gist lookup if a selector is provided.
			if tt.mockGist == nil {
				// If no gist is provided, we expect a 404.
				reg.Register(httpmock.REST("GET", fmt.Sprintf("gists/%s", tt.opts.Selector)),
					httpmock.StatusStringResponse(404, "Not Found"))
			} else {
				// If a gist is provided, we expect the gist to be fetched.
				reg.Register(httpmock.REST("GET", fmt.Sprintf("gists/%s", tt.opts.Selector)),
					httpmock.JSONResponse(tt.mockGist))
				reg.Register(httpmock.GraphQL(`query UserCurrent\b`),
					httpmock.StringResponse(`{"data":{"viewer":{"login":"octocat"}}}`))
			}
		}