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.*
105 lines
2.5 KiB
Go
105 lines
2.5 KiB
Go
package wsrelated_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/codersdk/wsrelated"
|
|
)
|
|
|
|
func TestConfigQueryParam(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cases := []struct {
|
|
name string
|
|
config wsrelated.Config
|
|
want string
|
|
}{
|
|
{
|
|
name: "Empty",
|
|
config: wsrelated.Config{},
|
|
want: "",
|
|
},
|
|
{
|
|
name: "Template",
|
|
config: wsrelated.Config{Template: true},
|
|
want: "template",
|
|
},
|
|
{
|
|
name: "LatestBuildOnly",
|
|
config: wsrelated.Config{LatestBuild: &wsrelated.LatestBuild{}},
|
|
want: "latest_build",
|
|
},
|
|
{
|
|
name: "TemplateVersion",
|
|
config: wsrelated.Config{LatestBuild: &wsrelated.LatestBuild{TemplateVersion: true}},
|
|
want: "latest_build.template_version",
|
|
},
|
|
{
|
|
name: "JobWithoutQueuePosition",
|
|
config: wsrelated.Config{LatestBuild: &wsrelated.LatestBuild{Job: &wsrelated.Job{}}},
|
|
want: "latest_build.job",
|
|
},
|
|
{
|
|
name: "JobQueuePosition",
|
|
config: wsrelated.Config{LatestBuild: &wsrelated.LatestBuild{Job: &wsrelated.Job{QueuePosition: true}}},
|
|
want: "latest_build.job.queue_position",
|
|
},
|
|
{
|
|
name: "ResourcesOnly",
|
|
config: wsrelated.Config{LatestBuild: &wsrelated.LatestBuild{Resources: &wsrelated.Resources{}}},
|
|
want: "latest_build.resources",
|
|
},
|
|
{
|
|
name: "AgentsOnly",
|
|
config: wsrelated.Config{LatestBuild: &wsrelated.LatestBuild{Resources: &wsrelated.Resources{Agents: &wsrelated.Agents{}}}},
|
|
want: "latest_build.resources.agents",
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
require.Equal(t, tc.want, tc.config.QueryParam())
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestConfigQueryParamRoundTrip asserts that encoding a Config and parsing the
|
|
// result yields an equal Config.
|
|
func TestConfigQueryParamRoundTrip(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cases := []struct {
|
|
name string
|
|
config wsrelated.Config
|
|
}{
|
|
{name: "Empty", config: wsrelated.Config{}},
|
|
{name: "All", config: wsrelated.All()},
|
|
{name: "Template", config: wsrelated.Config{Template: true}},
|
|
{
|
|
name: "Mixed",
|
|
config: wsrelated.Config{
|
|
Template: true,
|
|
LatestBuild: &wsrelated.LatestBuild{
|
|
Job: &wsrelated.Job{},
|
|
Resources: &wsrelated.Resources{
|
|
Metadata: true,
|
|
Agents: &wsrelated.Agents{Scripts: true},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
got, err := wsrelated.Parse(tc.config.QueryParam())
|
|
require.NoError(t, err)
|
|
require.Equal(t, tc.config, got)
|
|
})
|
|
}
|
|
}
|