Skip to content

test: reduce flakiness of workspace update test #19294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
tryin
  • Loading branch information
aslilac committed Aug 11, 2025
commit df06d534edd093c4ed6e4d21ad18cb7ecbe5b5c6
12 changes: 2 additions & 10 deletions site/src/api/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import type dayjs from "dayjs";
import userAgentParser from "ua-parser-js";
import { OneWayWebSocket } from "../utils/OneWayWebSocket";
import { delay } from "../utils/delay";
import { type FieldError, isApiError } from "./errors";
import { ParameterValidationError, isApiError } from "./errors";
import type {
DynamicParametersRequest,
PostWorkspaceUsageRequest,
Expand Down Expand Up @@ -398,15 +398,6 @@ export class MissingBuildParameters extends Error {
}
}

export class ParameterValidationError extends Error {
constructor(
public readonly versionId: string,
public readonly validations: FieldError[],
) {
super("Parameters are not valid for new template version");
}
}

export type GetProvisionerJobsParams = {
status?: string;
limit?: number;
Expand Down Expand Up @@ -2256,6 +2247,7 @@ class ApiMethods {
error.response.data.validations &&
error.response.data.validations.length > 0
) {
console.log(error.response.data.validations);
throw new ParameterValidationError(
templateVersionId,
error.response.data.validations,
Expand Down
9 changes: 9 additions & 0 deletions site/src/api/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,3 +149,12 @@ export class DetailedError extends Error {
super(message);
}
}

export class ParameterValidationError extends Error {
constructor(
public readonly versionId: string,
public readonly validations: FieldError[],
) {
super("Parameters are not valid for new template version");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { MissingBuildParameters, ParameterValidationError } from "api/api";
import { isApiError } from "api/errors";
import { MissingBuildParameters } from "api/api";
import { ParameterValidationError, isApiError } from "api/errors";
import { type ApiError, getErrorMessage } from "api/errors";
import {
changeVersion,
Expand Down
3 changes: 2 additions & 1 deletion site/src/modules/workspaces/WorkspaceUpdateDialogs.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MissingBuildParameters, ParameterValidationError } from "api/api";
import { MissingBuildParameters } from "api/api";
import { ParameterValidationError } from "api/errors";
import { updateWorkspace } from "api/queries/workspaces";
import type {
TemplateVersion,
Expand Down
69 changes: 16 additions & 53 deletions site/src/pages/WorkspacePage/WorkspacePage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { screen, waitFor, within } from "@testing-library/react";
import userEvent from "@testing-library/user-event";
import * as apiModule from "api/api";
import { ParameterValidationError } from "api/errors";
import type { TemplateVersionParameter, Workspace } from "api/typesGenerated";
import MockServerSocket from "jest-websocket-mock";
import {
Expand Down Expand Up @@ -282,11 +283,9 @@ describe("WorkspacePage", () => {
});

it("requests an update when the user presses Update", async () => {
// Mocks
jest
.spyOn(API, "getWorkspaceByOwnerAndName")
.mockResolvedValueOnce(MockOutdatedWorkspace);

const updateWorkspaceMock = jest
.spyOn(API, "updateWorkspace")
.mockResolvedValueOnce(MockWorkspaceBuild);
Expand All @@ -306,73 +305,37 @@ describe("WorkspacePage", () => {
});
});

it("updates the parameters when they are missing during update", async () => {
// Mocks
it("requires invalid parameters to be updated", async () => {
jest
.spyOn(API, "getWorkspaceByOwnerAndName")
.mockResolvedValueOnce(MockOutdatedWorkspace);
const updateWorkspaceSpy = jest
const updateWorkspaceMock = jest
.spyOn(API, "updateWorkspace")
.mockRejectedValueOnce(
new MissingBuildParameters(
[MockTemplateVersionParameter1, MockTemplateVersionParameter2],
new ParameterValidationError(
MockOutdatedWorkspace.template_active_version_id,
[
{
field: MockTemplateVersionParameter1.name,
detail:
"Required parameter not provided; parameter value is null",
},
],
),
);

// Render
await renderWorkspacePage(MockWorkspace);

// Actions
// Start workspace update
const user = userEvent.setup();
await user.click(screen.getByTestId("workspace-update-button"));
const confirmButton = await screen.findByTestId("confirm-button");
await user.click(confirmButton);

// The update was called
await waitFor(() => {
expect(API.updateWorkspace).toBeCalled();
updateWorkspaceSpy.mockClear();
});

// After trying to update, a new dialog asking for missed parameters should
// be displayed and filled
const dialog = await waitFor(() => screen.findByTestId("dialog"), {
timeout: 2000,
});
const firstParameterInput = within(dialog).getByLabelText(
MockTemplateVersionParameter1.name,
{ exact: false },
);
await user.clear(firstParameterInput);
await user.type(firstParameterInput, "some-value");
const secondParameterInput = within(dialog).getByLabelText(
MockTemplateVersionParameter2.name,
{ exact: false },
);
await user.clear(secondParameterInput);
await user.type(secondParameterInput, "2");
await user.click(
within(dialog).getByRole("button", { name: /update parameters/i }),
);

// Check if the update was called using the values from the form
await waitFor(() => {
expect(API.updateWorkspace).toHaveBeenCalledWith(
MockOutdatedWorkspace,
[
{
name: MockTemplateVersionParameter1.name,
value: "some-value",
},
{
name: MockTemplateVersionParameter2.name,
value: "2",
},
],
false,
);
});
// Dialog should warn the parameters need to be updated
const dialog = await screen.findByTestId("dialog");
await within(dialog).findByText("Update workspace parameters");
await screen.findByText(/go to the workspace parameters page to review/);
});

it("restart the workspace with one time parameters when having the confirmation dialog", async () => {
Expand Down
Loading