Skip to content
TypeScript

fix(web): create tasks on a permitted connected environment (#23244)

Victor Vogelde386ada9ec3
diff --git a/apps/web/src/components/settings/ScheduledTasksSettings.browser.test.tsx b/apps/web/src/components/settings/ScheduledTasksSettings.browser.test.tsx
index e42242f0109..124a27c7b11 100644
--- a/apps/web/src/components/settings/ScheduledTasksSettings.browser.test.tsx
+++ b/apps/web/src/components/settings/ScheduledTasksSettings.browser.test.tsx
@@ -1,3 +1,5 @@
+import { RegistryContext } from "@effect/atom-react";
+import { Atom, AtomRegistry } from "effect/reactivity";
import {
DEFAULT_UNIFIED_SETTINGS,
EnvironmentId,
@@ -10,7 +12,7 @@ import { describe, expect, it, vi } from "vite-plus/test";
import { page, userEvent } from "vite-plus/test/browser";
import "~/index.css";
-import { render } from "../../../test/render";
+import { act, render } from "../../../test/render";
import { ScheduledTasksSettings } from "./ScheduledTasksSettings";
const environmentId = EnvironmentId.make("laptop");
@@ -38,7 +40,23 @@ const task: ScheduledTask = {
runCount: 0,
};
-const state = vi.hoisted(() => ({ connected: true, tasksLoaded: true }));
+const state = vi.hoisted(() => ({ connected: true, tasksLoaded: true, secondary: false }));
+const secondaryId = EnvironmentId.make("secondary");
+const permissions = new Map<string, Atom.Writable<boolean>>();
+
+function setup() {
+ state.connected = true;
+ state.tasksLoaded = true;
+ state.secondary = false;
+ permissions.clear();
+ permissions.set(environmentId, Atom.make(true));
+ permissions.set(secondaryId, Atom.make(true));
+ permissions.set("", Atom.make(false));
+}
+
+function secondaryEnvironment() {
+ return { ...environment(), environmentId: secondaryId, label: "Secondary" };
+}
function environment() {
return {
@@ -54,8 +72,12 @@ vi.mock(import("./SettingsScopeContext"), async (original) => {
const entry = environment();
return {
scope: { kind: "all", label: "All", members: [], environmentIds: [environmentId] },
- environments: [entry],
- connectedEnvironments: state.connected ? [entry] : [],
+ environments: state.secondary ? [entry, secondaryEnvironment()] : [entry],
+ connectedEnvironments: state.connected
+ ? state.secondary
+ ? [entry, secondaryEnvironment()]
+ : [entry]
+ : [],
environment: entry,
target: null,
targets: [],
@@ -68,15 +90,14 @@ vi.mock(import("./SettingsScopeContext"), async (original) => {
});
vi.mock(import("../../state/server"), async (original) => {
const actual = await original();
- const { Atom } = await import("effect/reactivity");
- const allowed = Atom.make(true);
+
return {
...actual,
serverEnvironment: {
...actual.serverEnvironment,
upsertScheduledTask: {
...actual.serverEnvironment.upsertScheduledTask,
- permissionAtom: () => allowed,
+ permissionAtom: (id: string | null) => permissions.get(id ?? "")!,
},
},
};
@@ -93,7 +114,8 @@ vi.mock(import("../../state/query"), async (original) => ({
}));
vi.mock(import("../../state/environments"), async (original) => ({
...(await original()),
- useEnvironment: (() => environment()) as never,
+ useEnvironment: ((id: EnvironmentId) =>
+ id === secondaryId ? secondaryEnvironment() : environment()) as never,
}));
vi.mock(import("../../state/entities"), async (original) => ({
...(await original()),
@@ -105,9 +127,44 @@ vi.mock(import("../../hooks/useSettings"), async (original) => ({
}));
describe("ScheduledTasksSettings", () => {
+ it("creates on a permitted secondary and reacts to permission changes", async () => {
+ setup();
+ state.secondary = true;
+ const registry = AtomRegistry.make();
+ using _registry = { [Symbol.dispose]: () => registry.dispose() };
+ registry.set(permissions.get(environmentId)!, false);
+ await using screen = await render(
+ <RegistryContext.Provider value={registry}>
+ <ScheduledTasksSettings />
+ </RegistryContext.Provider>,
+ );
+ const create = screen.getByRole("button", { name: "New task" });
+ await expect.element(create).toBeEnabled();
+ await act(() => registry.set(permissions.get(secondaryId)!, false));
+ await expect.element(create).toBeDisabled();
+ await act(() => registry.set(permissions.get(secondaryId)!, true));
+ await expect.element(create).toBeEnabled();
+ await create.click();
+ await expect.element(page.getByLabelText("Runs on")).toHaveTextContent("Secondary");
+ });
+
+ it("keeps the permitted representative as the creation target", async () => {
+ setup();
+ state.secondary = true;
+ await using screen = await render(<ScheduledTasksSettings />);
+ await screen.getByRole("button", { name: "New task" }).click();
+ await expect.element(page.getByLabelText("Runs on")).toHaveTextContent("Laptop");
+ });
+
+ it("disables creation without a connected target", async () => {
+ setup();
+ state.connected = false;
+ await using screen = await render(<ScheduledTasksSettings />);
+ await expect.element(screen.getByRole("button", { name: "New task" })).toBeDisabled();
+ });
+
it("keeps a linked task's unsaved edits across a disconnect and reconnect", async () => {
- state.connected = true;
- state.tasksLoaded = true;
+ setup();
await using screen = await render(<ScheduledTasksSettings taskId={task.id} />);
const name = page.getByRole("dialog").getByRole("textbox", { name: "Name" });
await expect.element(name).toHaveValue("Review issues");
@@ -124,7 +181,7 @@ describe("ScheduledTasksSettings", () => {
});
it("keeps an editor the user opened when the linked task loads later", async () => {
- state.connected = true;
+ setup();
state.tasksLoaded = false;
await using screen = await render(<ScheduledTasksSettings taskId={task.id} />);
await userEvent.click(page.getByRole("button", { name: "New task" }));
diff --git a/apps/web/src/components/settings/ScheduledTasksSettings.tsx b/apps/web/src/components/settings/ScheduledTasksSettings.tsx
index 1f64fe501af..e027438a6ab 100644
--- a/apps/web/src/components/settings/ScheduledTasksSettings.tsx
+++ b/apps/web/src/components/settings/ScheduledTasksSettings.tsx
@@ -396,9 +396,18 @@ export function ScheduledTasksSettings(target: {
replace: true,
});
};
- const canCreate = useAtomValue(
- serverEnvironment.upsertScheduledTask.permissionAtom(defaultEnvironment?.environmentId ?? null),
+ const createPermissions = useAtomValue(
+ scheduledTaskCreatePermissionsAtom(
+ environmentIdsKey(connectedEnvironments.map((entry) => entry.environmentId)),
+ ),
);
+ const permittedEnvironments = connectedEnvironments.filter(
+ (_entry, index) => createPermissions[index],
+ );
+ const creationEnvironment =
+ permittedEnvironments.find(
+ (entry) => entry.environmentId === defaultEnvironment?.environmentId,
+ ) ?? permittedEnvironments[0];
return (
<SettingsPageContainer>
<SettingsSection
@@ -408,10 +417,10 @@ export function ScheduledTasksSettings(target: {
<Button
size="xs"
variant="ghost-muted"
- disabled={!defaultEnvironment || !canCreate}
+ disabled={!creationEnvironment}
onClick={() =>
- defaultEnvironment &&
- setEditor({ environmentId: defaultEnvironment.environmentId, task: null })
+ creationEnvironment &&
+ setEditor({ environmentId: creationEnvironment.environmentId, task: null })
}
>
<PlusIcon className="size-3" />
@@ -464,6 +473,11 @@ export function ScheduledTasksSettings(target: {
);
}
+const scheduledTaskCreatePermissionsAtom = environmentAtomList(
+ "web-scheduled-task-create-permissions",
+ serverEnvironment.upsertScheduledTask.permissionAtom,
+);
+
const scheduledTasksListAtom = environmentAtomList("web-scheduled-tasks-list", (environmentId) =>
serverEnvironment.scheduledTasksLive({ environmentId, input: {} }),
);