Files
coder/codersdk/workspaces_internal_test.go
T
Spike Curtis 9a29aab2ee feat: add include_related option to client.Workspace in codersdk (#29540)
Part of the lite `codersdk.Workspace` work
([GRU-82](https://linear.app/codercom/issue/GRU-82),
[RFC](https://app.notion.com/p/coderhq/Lite-Workspace-API-Requests-3aad579be5928060b5e7cc65c539717f)).
Stacked on #29539, which wired `include_related` into `GET
/api/v2/workspaces/{workspace}`.

Adds `include_related` support to the `codersdk` client, expressed with
the typed selection tree so only supported selections are representable:

- Moves the `wsrelated` package from `coderd/` to `codersdk/` so both
the server and the client can share `wsrelated.Config`. `coderd` already
depends on `codersdk`, so this only changes import paths on the server
side.
- Adds `Config.QueryParam`, which encodes a selection into the
`include_related` value: the dotted path of each selected node that has
no selected descendants. Selecting a node implies its ancestors, so
`Parse` round-trips the result back into an equal `Config` (covered by a
round-trip test).
- `WorkspaceOptions` gains `IncludeRelated *wsrelated.Config`. A nil
value omits the parameter (server returns everything, unchanged); a
non-nil value is encoded via `QueryParam`, and a zero `Config` loads
only the workspace.
- `Client.Workspace` now accepts optional `WorkspaceOptions`, backward
compatible with existing `client.Workspace(ctx, id)` calls.

### Testing

- `codersdk` internal test asserts `WorkspaceOptions.asRequestOption`
sets `include_related` from the selection, following the existing
`WorkspaceFilter` test pattern rather than spinning up a server.
- `wsrelated` tests cover `QueryParam` output and a `Parse`/`QueryParam`
round trip.

`make gen` regenerated the `WorkspaceOptions` TypeScript type. Because
the wire representation of `include_related` is a comma-separated
string, `scripts/apitypings` maps `wsrelated.Config` to `string` in
TypeScript, so `include_related?: string` rather than a nested object
the frontend cannot serialize. `api.getWorkspace` already forwards
`WorkspaceOptions` as query params, and a frontend test in `api.test.ts`
confirms `include_related` is sent through.

---

*Generated by Coder Agents on behalf of @spikecurtis.*
2026-09-21 11:53:11 +02:00

105 lines
2.6 KiB
Go

package codersdk
import (
"context"
"net/http"
"testing"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/codersdk/wsrelated"
)
func TestWorkspaceFilterAsRequestOption(t *testing.T) {
t.Parallel()
// applyFilter applies the filter's request option to a blank request and
// returns the resulting "q" query parameter.
applyFilter := func(f WorkspaceFilter) string {
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "http://example.com", nil)
require.NoError(t, err)
f.asRequestOption()(req)
return req.URL.Query().Get("q")
}
tests := []struct {
name string
filter WorkspaceFilter
contains []string
empty bool
}{
{
name: "Empty",
filter: WorkspaceFilter{},
empty: true,
},
{
name: "Owner",
filter: WorkspaceFilter{Owner: "alice"},
contains: []string{`owner:"alice"`},
},
{
name: "Name",
filter: WorkspaceFilter{Name: "my-workspace"},
contains: []string{`name:"my-workspace"`},
},
{
name: "Template",
filter: WorkspaceFilter{Template: "base"},
contains: []string{`template:"base"`},
},
{
name: "Status",
filter: WorkspaceFilter{Status: "running"},
contains: []string{`status:"running"`},
},
{
name: "Organization",
filter: WorkspaceFilter{Organization: "acme"},
contains: []string{`organization:"acme"`},
},
{
name: "OrganizationByUUID",
filter: WorkspaceFilter{Organization: "550e8400-e29b-41d4-a716-446655440000"},
contains: []string{`organization:"550e8400-e29b-41d4-a716-446655440000"`},
},
{
name: "MultipleFields",
filter: WorkspaceFilter{Owner: "alice", Organization: "acme", Status: "running"},
contains: []string{
`owner:"alice"`,
`organization:"acme"`,
`status:"running"`,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
q := applyFilter(tt.filter)
if tt.empty {
require.Empty(t, q)
return
}
for _, s := range tt.contains {
require.Contains(t, q, s)
}
})
}
}
// TestWorkspaceOptionsAsRequestOption verifies that WorkspaceOptions sets the
// include_related query parameter from its IncludeRelated selection.
func TestWorkspaceOptionsAsRequestOption(t *testing.T) {
t.Parallel()
req, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "http://example.com", nil)
require.NoError(t, err)
config := wsrelated.Config{Template: true}
WorkspaceOptions{IncludeRelated: &config}.asRequestOption()(req)
require.Equal(t, config.QueryParam(), req.URL.Query().Get("include_related"))
}