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.*
176 lines
4.5 KiB
Go
176 lines
4.5 KiB
Go
package wsrelated_test
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"github.com/coder/coder/v2/codersdk/wsrelated"
|
|
)
|
|
|
|
func TestParse(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cases := []struct {
|
|
name string
|
|
input string
|
|
want wsrelated.Config
|
|
}{
|
|
{
|
|
name: "Empty",
|
|
input: "",
|
|
want: wsrelated.Config{},
|
|
},
|
|
{
|
|
name: "Template",
|
|
input: "template",
|
|
want: wsrelated.Config{Template: true},
|
|
},
|
|
{
|
|
name: "LatestBuild",
|
|
input: "latest_build",
|
|
want: wsrelated.Config{LatestBuild: &wsrelated.LatestBuild{}},
|
|
},
|
|
{
|
|
name: "JobSelectsAncestor",
|
|
input: "latest_build.job",
|
|
want: wsrelated.Config{LatestBuild: &wsrelated.LatestBuild{Job: &wsrelated.Job{}}},
|
|
},
|
|
{
|
|
name: "QueuePositionSelectsAncestors",
|
|
input: "latest_build.job.queue_position",
|
|
want: wsrelated.Config{LatestBuild: &wsrelated.LatestBuild{Job: &wsrelated.Job{QueuePosition: true}}},
|
|
},
|
|
{
|
|
name: "TemplateVersion",
|
|
input: "latest_build.template_version",
|
|
want: wsrelated.Config{LatestBuild: &wsrelated.LatestBuild{TemplateVersion: true}},
|
|
},
|
|
{
|
|
name: "Metadata",
|
|
input: "latest_build.resources.metadata",
|
|
want: wsrelated.Config{LatestBuild: &wsrelated.LatestBuild{
|
|
Resources: &wsrelated.Resources{Metadata: true},
|
|
}},
|
|
},
|
|
{
|
|
name: "AppStatusesDeep",
|
|
input: "latest_build.resources.agents.apps.statuses",
|
|
want: wsrelated.Config{LatestBuild: &wsrelated.LatestBuild{
|
|
Resources: &wsrelated.Resources{Agents: &wsrelated.Agents{Apps: &wsrelated.Apps{Statuses: true}}},
|
|
}},
|
|
},
|
|
{
|
|
name: "WildcardJob",
|
|
input: "latest_build.job.*",
|
|
want: wsrelated.Config{LatestBuild: &wsrelated.LatestBuild{Job: &wsrelated.Job{QueuePosition: true}}},
|
|
},
|
|
{
|
|
name: "WildcardAgents",
|
|
input: "latest_build.resources.agents.*",
|
|
want: wsrelated.Config{LatestBuild: &wsrelated.LatestBuild{
|
|
Resources: &wsrelated.Resources{Agents: &wsrelated.Agents{
|
|
Apps: &wsrelated.Apps{Statuses: true},
|
|
Scripts: true,
|
|
LogSources: true,
|
|
}},
|
|
}},
|
|
},
|
|
{
|
|
name: "WildcardLatestBuild",
|
|
input: "latest_build.*",
|
|
want: wsrelated.Config{LatestBuild: new(wsrelated.AllLatestBuild())},
|
|
},
|
|
{
|
|
name: "WildcardOnLeaf",
|
|
input: "template.*",
|
|
want: wsrelated.Config{Template: true},
|
|
},
|
|
{
|
|
name: "RootWildcard",
|
|
input: "*",
|
|
want: wsrelated.All(),
|
|
},
|
|
{
|
|
name: "MultiplePaths",
|
|
input: "template,latest_build.template_version",
|
|
want: wsrelated.Config{
|
|
Template: true,
|
|
LatestBuild: &wsrelated.LatestBuild{TemplateVersion: true},
|
|
},
|
|
},
|
|
{
|
|
name: "MergedSiblings",
|
|
input: "latest_build.job,latest_build.resources.metadata",
|
|
want: wsrelated.Config{LatestBuild: &wsrelated.LatestBuild{
|
|
Job: &wsrelated.Job{},
|
|
Resources: &wsrelated.Resources{Metadata: true},
|
|
}},
|
|
},
|
|
{
|
|
name: "OverlappingAncestorAndChild",
|
|
input: "latest_build,latest_build.job",
|
|
want: wsrelated.Config{LatestBuild: &wsrelated.LatestBuild{Job: &wsrelated.Job{}}},
|
|
},
|
|
{
|
|
name: "WhitespaceAndTrailingComma",
|
|
input: " template , latest_build , ",
|
|
want: wsrelated.Config{
|
|
Template: true,
|
|
LatestBuild: &wsrelated.LatestBuild{},
|
|
},
|
|
},
|
|
{
|
|
name: "WildcardSubtreeCoversExplicitChild",
|
|
input: "latest_build.resources.*,latest_build.resources.agents.scripts",
|
|
want: wsrelated.Config{LatestBuild: &wsrelated.LatestBuild{
|
|
Resources: &wsrelated.Resources{
|
|
Metadata: true,
|
|
Agents: &wsrelated.Agents{
|
|
Apps: &wsrelated.Apps{Statuses: true},
|
|
Scripts: true,
|
|
LogSources: true,
|
|
},
|
|
},
|
|
}},
|
|
},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
got, err := wsrelated.Parse(tc.input)
|
|
require.NoError(t, err)
|
|
require.Equal(t, tc.want, got)
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestParseErrors(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
cases := []struct {
|
|
name string
|
|
input string
|
|
}{
|
|
{name: "UnknownTopLevel", input: "bogus"},
|
|
{name: "DoubleComma", input: "template,,template"},
|
|
{name: "LeadingComma", input: ",template"},
|
|
{name: "WhitespaceOnlyEntries", input: ", ,"},
|
|
{name: "UnknownChild", input: "latest_build.bogus"},
|
|
{name: "PathBeyondLeaf", input: "latest_build.job.queue_position.bogus"},
|
|
{name: "MidWildcard", input: "latest_build.*.agents"},
|
|
{name: "EmptySegment", input: "latest_build..job"},
|
|
{name: "LeadingWildcardSegment", input: "*.template"},
|
|
{name: "UnknownWithValidSibling", input: "template,bogus"},
|
|
}
|
|
|
|
for _, tc := range cases {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
t.Parallel()
|
|
_, err := wsrelated.Parse(tc.input)
|
|
require.Error(t, err)
|
|
})
|
|
}
|
|
}
|