Files
kandev/apps/web/components/task-create-dialog-effects-executor.test.ts
T
Carlos Florêncio bc218dfbbe feat: add workspace sources from files panel (#1900)
* feat: add workspace sources from files panel

* fix: preserve worktree identity during recovery

* feat: unify workspace source actions

* fix: validate recovered workspace repositories

* fix: harden workspace source filesystem boundaries

* test: split workspace repository chip coverage

* fix: restrict repository rescan recovery paths

* fix: stabilize workspace source CI checks

* test: canonicalize workspace source paths

* test: align workspace source container paths

* fix: populate direct workspace repository names

* test: preserve git fixture rewrites across restarts

* test: isolate backend git fixture config

* fix: resume remote workspace executions before creation

* fix: preserve ssh agent identity during resume

* fix: preserve SSH workspace across backend restart

* fix: simplify workspace actions trigger

* fix: refine add workspace repositories UX

* docs: narrow workspace source UX updates

* fix: avoid phantom turn on session resume

* test(web): normalize workspace source request headers

* feat(web): streamline workspace source picker

* docs: remove trailing spaces from workspace source plan

* fix: prevent resume status from creating phantom turns

* fix(web): stabilize workspace source attachment

* refactor(web): extract workspace source store contract

* fix: avoid phantom turns on idle agent resume

* fix: address backend static analysis findings

* fix: preserve agentctl readiness status value

* fix: preserve resume messages without active turns

* fix: resume agents from promoted workspace root

* chore: merge origin/main into feature/add-new-repo-folder-d97

* feat(web): explain workspace source consequences

* test(websocket): count session activity subscription
2026-07-25 21:29:53 +01:00

305 lines
11 KiB
TypeScript

import { describe, it, expect, vi } from "vitest";
import { renderHook, waitFor } from "@testing-library/react";
import { useDefaultSelectionsEffect } from "./task-create-dialog-effects";
import type { DialogFormState, StoreSelections } from "@/components/task-create-dialog-types";
const PROFILE_DOCKER = "profile-docker";
const PROFILE_LOCAL = "profile-local";
const PROFILE_WORKTREE = "profile-worktree";
const PROFILE_WORKTREE_B = "profile-worktree-b";
const EXECUTOR_DOCKER = "exec-docker";
const REMOTE_URL_A = "github.com/acme/a";
const REMOTE_URL_B = "github.com/acme/b";
type DefaultSelFake = Pick<
DialogFormState,
| "agentProfileId"
| "workflowAgentProfileId"
| "selectedWorkflowId"
| "executorId"
| "executorProfileId"
| "setAgentProfileId"
| "setExecutorId"
| "setExecutorProfileId"
| "noRepository"
| "repositories"
| "remoteRepos"
| "useRemote"
>;
function makeDefaultSelFs(overrides: Partial<DefaultSelFake> = {}): DialogFormState {
return {
agentProfileId: "",
workflowAgentProfileId: "",
selectedWorkflowId: null,
executorId: "exec-1",
executorProfileId: "profile-1",
setAgentProfileId: vi.fn(),
setExecutorId: vi.fn(),
setExecutorProfileId: vi.fn(),
noRepository: false,
repositories: [],
remoteRepos: [],
useRemote: false,
...overrides,
} as unknown as DialogFormState;
}
function makeSel(overrides: Partial<StoreSelections> = {}): StoreSelections {
return {
agentProfiles: [],
compatibleAgentProfiles: [],
authLoaded: true,
executors: [],
workspaceDefaults: null,
...overrides,
};
}
function localExecutor(): StoreSelections["executors"][number] {
return {
id: "exec-local",
type: "local",
profiles: [{ id: PROFILE_LOCAL, executor_type: "local" }],
} as unknown as StoreSelections["executors"][number];
}
function dockerExecutor(): StoreSelections["executors"][number] {
return {
id: EXECUTOR_DOCKER,
type: "local_docker",
profiles: [{ id: PROFILE_DOCKER, executor_type: "local_docker" }],
} as unknown as StoreSelections["executors"][number];
}
function worktreeExecutor(): StoreSelections["executors"][number] {
return {
id: "exec-worktree",
type: "worktree",
profiles: [
{ id: PROFILE_WORKTREE, executor_type: "worktree" },
{ id: PROFILE_WORKTREE_B, executor_type: "worktree" },
],
} as unknown as StoreSelections["executors"][number];
}
describe("useDefaultSelectionsEffect - executor profile defaults", () => {
it("defaults repo-backed tasks to the worktree profile when no profile was saved", async () => {
const fs = makeDefaultSelFs({ executorId: "", executorProfileId: "" });
const local = localExecutor();
const worktree = worktreeExecutor();
const sel = makeSel({ executors: [local, worktree] });
renderHook(() => useDefaultSelectionsEffect(fs, true, sel, []));
await waitFor(() => expect(fs.setExecutorId).toHaveBeenCalledWith(worktree.id));
await waitFor(() => expect(fs.setExecutorProfileId).toHaveBeenCalledWith(PROFILE_WORKTREE));
});
it("defaults repo-less tasks to a local profile because worktree needs a repo", async () => {
const fs = makeDefaultSelFs({ executorId: "", executorProfileId: "", noRepository: true });
const worktree = worktreeExecutor();
const local = localExecutor();
const sel = makeSel({ executors: [worktree, local] });
renderHook(() => useDefaultSelectionsEffect(fs, true, sel, []));
await waitFor(() => expect(fs.setExecutorId).toHaveBeenCalledWith(local.id));
await waitFor(() => expect(fs.setExecutorProfileId).toHaveBeenCalledWith(PROFILE_LOCAL));
});
it("defaults explicit local-path tasks to a local profile when no profile was saved", async () => {
const fs = makeDefaultSelFs({
executorId: "",
executorProfileId: "",
repositories: [{ key: "row-0", localPath: "/workspace/custom", branch: "" }],
});
const worktree = worktreeExecutor();
const local = localExecutor();
const sel = makeSel({ executors: [worktree, local] });
renderHook(() => useDefaultSelectionsEffect(fs, true, sel, []));
await waitFor(() => expect(fs.setExecutorId).toHaveBeenCalledWith(local.id));
await waitFor(() => expect(fs.setExecutorProfileId).toHaveBeenCalledWith(PROFILE_LOCAL));
});
it("ignores a workspace-default worktree executor for explicit local-path tasks", async () => {
const fs = makeDefaultSelFs({
executorId: "",
executorProfileId: "",
repositories: [{ key: "row-0", localPath: "/workspace/custom", branch: "" }],
});
const worktree = worktreeExecutor();
const local = localExecutor();
const sel = makeSel({
executors: [worktree, local],
workspaceDefaults: {
default_executor_id: worktree.id,
} as StoreSelections["workspaceDefaults"],
});
renderHook(() => useDefaultSelectionsEffect(fs, true, sel, []));
await waitFor(() => expect(fs.setExecutorId).toHaveBeenCalledWith(local.id));
expect(fs.setExecutorId).not.toHaveBeenCalledWith(worktree.id);
await waitFor(() => expect(fs.setExecutorProfileId).toHaveBeenCalledWith(PROFILE_LOCAL));
});
it("does not fall back to a worktree profile for explicit local-path tasks", async () => {
const fs = makeDefaultSelFs({
executorId: "",
executorProfileId: "",
repositories: [{ key: "row-0", localPath: "/workspace/custom", branch: "" }],
});
const localWithoutProfiles = { ...localExecutor(), profiles: [] };
const worktree = worktreeExecutor();
const docker = dockerExecutor();
const sel = makeSel({ executors: [worktree, localWithoutProfiles, docker] });
renderHook(() => useDefaultSelectionsEffect(fs, true, sel, []));
await waitFor(() => expect(fs.setExecutorProfileId).toHaveBeenCalledWith(PROFILE_DOCKER));
expect(fs.setExecutorProfileId).not.toHaveBeenCalledWith(PROFILE_WORKTREE);
});
});
describe("useDefaultSelectionsEffect - executor profile restoration", () => {
it("defers executor profile fallback until user settings have loaded or settled", async () => {
const fs = makeDefaultSelFs({ executorProfileId: "", executorId: "" });
const worktree = worktreeExecutor();
const selBefore = makeSel({
executors: [worktree],
userSettingsLoaded: false,
});
const { rerender } = renderHook(({ sel }) => useDefaultSelectionsEffect(fs, true, sel, []), {
initialProps: { sel: selBefore },
});
await new Promise((resolve) => setTimeout(resolve, 10));
expect(fs.setExecutorId).not.toHaveBeenCalled();
expect(fs.setExecutorProfileId).not.toHaveBeenCalled();
const selAfter = makeSel({
executors: [worktree],
userSettingsLoaded: true,
});
rerender({ sel: selAfter });
await waitFor(() => expect(fs.setExecutorProfileId).toHaveBeenCalledWith(PROFILE_WORKTREE));
});
});
describe("useDefaultSelectionsEffect - executor profile settings restoration", () => {
it("restores executor profile from backend settings", async () => {
const fs = makeDefaultSelFs({ executorProfileId: "", executorId: "" });
const worktree = worktreeExecutor();
const sel = makeSel({
executors: [worktree],
lastUsedExecutorProfileId: PROFILE_WORKTREE_B,
userSettingsLoaded: true,
});
renderHook(() => useDefaultSelectionsEffect(fs, true, sel, []));
await waitFor(() => expect(fs.setExecutorProfileId).toHaveBeenCalledWith(PROFILE_WORKTREE_B));
});
it("does not pick a fallback executor id while a valid last-used profile is restoring", async () => {
const local = localExecutor();
const worktree = worktreeExecutor();
const sel = makeSel({
executors: [local, worktree],
workspaceDefaults: { default_executor_id: local.id } as StoreSelections["workspaceDefaults"],
lastUsedExecutorProfileId: PROFILE_WORKTREE_B,
userSettingsLoaded: true,
});
const setExecutorId = vi.fn();
const setExecutorProfileId = vi.fn();
const { rerender } = renderHook(
({ formState }) => useDefaultSelectionsEffect(formState, true, sel, []),
{
initialProps: {
formState: makeDefaultSelFs({
executorProfileId: "",
executorId: "",
setExecutorId,
setExecutorProfileId,
}),
},
},
);
await waitFor(() => expect(setExecutorProfileId).toHaveBeenCalledWith(PROFILE_WORKTREE_B));
rerender({
formState: makeDefaultSelFs({
executorProfileId: PROFILE_WORKTREE_B,
executorId: "",
setExecutorId,
setExecutorProfileId,
}),
});
await waitFor(() => expect(setExecutorId).toHaveBeenCalledWith(worktree.id));
expect(setExecutorId).not.toHaveBeenCalledWith(local.id);
});
});
describe("useDefaultSelectionsEffect - multi-repo executor selection", () => {
it("keeps Local Docker selected when 2+ Remote URL rows are set", async () => {
const fs = makeDefaultSelFs({
executorId: EXECUTOR_DOCKER,
executorProfileId: PROFILE_DOCKER,
useRemote: true,
remoteRepos: [
{ key: "remote-0", url: REMOTE_URL_A, branch: "", source: "paste" },
{ key: "remote-1", url: REMOTE_URL_B, branch: "", source: "paste" },
] as DialogFormState["remoteRepos"],
});
const sel = makeSel({ executors: [dockerExecutor(), worktreeExecutor()] });
renderHook(() => useDefaultSelectionsEffect(fs, true, sel, []));
await new Promise((resolve) => setTimeout(resolve, 10));
expect(fs.setExecutorProfileId).not.toHaveBeenCalled();
});
it("leaves a worktree profile alone when 2+ Remote rows are set", async () => {
const fs = makeDefaultSelFs({
executorId: "exec-worktree",
executorProfileId: PROFILE_WORKTREE,
useRemote: true,
remoteRepos: [
{ key: "remote-0", url: REMOTE_URL_A, branch: "", source: "paste" },
{ key: "remote-1", url: REMOTE_URL_B, branch: "", source: "paste" },
] as DialogFormState["remoteRepos"],
});
const sel = makeSel({ executors: [worktreeExecutor()] });
renderHook(() => useDefaultSelectionsEffect(fs, true, sel, []));
await new Promise((resolve) => setTimeout(resolve, 10));
expect(fs.setExecutorProfileId).not.toHaveBeenCalled();
});
it("does not swap when only a single Remote row is filled", async () => {
const fs = makeDefaultSelFs({
executorId: EXECUTOR_DOCKER,
executorProfileId: PROFILE_DOCKER,
useRemote: true,
remoteRepos: [
{ key: "remote-0", url: REMOTE_URL_A, branch: "", source: "paste" },
{ key: "remote-1", url: "", branch: "", source: "paste" },
] as DialogFormState["remoteRepos"],
});
const sel = makeSel({ executors: [dockerExecutor(), worktreeExecutor()] });
renderHook(() => useDefaultSelectionsEffect(fs, true, sel, []));
await new Promise((resolve) => setTimeout(resolve, 10));
expect(fs.setExecutorProfileId).not.toHaveBeenCalled();
});
});