Files
openidea/src/features/agents/sendToAgent.test.ts
T
LU XIANGRUIandwildcatDownstairs 846794450a fix(agents): let Send to Agent reference files of non-primary workspace projects (#4459)
Send to Agent resolved paths against the primary project only, so files of other workspace projects were dropped silently. It now goes through referenceOf with the target composer's roots (the task's cwd/project, or the project picked on the New Task page), keeps folders as folder references, maps only the task's own project into a worktree, and shows the existing notice for paths outside every project. Closes #4438

Co-authored-by: wildcatDownstairs <26499420+wildcatDownstairs@users.noreply.github.com>
2026-11-19 17:03:44 +08:00

139 lines
5.2 KiB
TypeScript

import { EditorSelection, EditorState } from "@codemirror/state";
import { beforeEach, describe, expect, it, vi } from "vitest";
import type { AgentTask, ProjectInfo } from "@/bindings";
import { useProject } from "@/platform/project";
import { sendPaths, sendSelection } from "./references";
import { HOME, composerDraft, useAgents } from "./store";
const mocks = vi.hoisted(() => ({ notify: vi.fn(), editor: null as unknown }));
vi.mock("@/editor/api", async (original) => ({
...(await original<typeof import("@/editor/api")>()),
activeEditor: () => mocks.editor,
}));
vi.mock("@/workbench/contributions", async (original) => ({
...(await original<typeof import("@/workbench/contributions")>()),
editorService: () => ({ openInput: vi.fn(), openTabs: () => [], activePath: () => null }),
notifications: () => ({ notify: mocks.notify }),
}));
const BACK = "/w/backend";
const FRONT = "/w/frontend";
const NOTICE = "Only files of this window's projects can be referenced";
const task = (cwd: string, projectRoot: string, isolation: AgentTask["isolation"]) =>
({ id: "t", cwd, projectRoot, isolation, title: "t", status: "ready" }) as AgentTask;
function setup(opts: { members?: string[]; homeRoot?: string; task?: AgentTask }) {
useProject.setState({
project: {
root: BACK,
trusted: true,
workspace: opts.members ? { projects: opts.members } : null,
} as unknown as ProjectInfo,
});
useAgents.setState({
tasks: new Map(opts.task ? [["t", opts.task]] : []),
composers: new Map(),
lastComposer: opts.task ? "t" : HOME,
sessionLocation: "editor",
draft: { provider: "codex", config: {}, isolation: "local", base: null, projectRoot: opts.homeRoot ?? null },
});
}
const sent = (key = HOME) => {
const { text, files } = composerDraft(key);
return { text: text.trim(), files: files.map((f) => [f.rel, f.path]) };
};
beforeEach(() => {
mocks.notify.mockReset();
mocks.editor = null;
});
describe("Send to Agent from the Project view", () => {
it("single project: files and folders as before", () => {
setup({});
expect(sendPaths([`${BACK}/pom.xml`, `${BACK}/src/`])).toBe(true);
expect(sent()).toEqual({
text: "@pom.xml @src/",
files: [
["pom.xml", `${BACK}/pom.xml`],
["src/", `${BACK}/src`],
],
});
expect(mocks.notify).not.toHaveBeenCalled();
});
it("a file and folder of a non-primary project, named by that project (#4438)", () => {
setup({ members: [BACK, FRONT], homeRoot: BACK });
expect(sendPaths([`${FRONT}/package.json`, `${FRONT}/src/`])).toBe(true);
expect(sent()).toEqual({
text: "@frontend/package.json @frontend/src/",
files: [
["frontend/package.json", `${FRONT}/package.json`],
["frontend/src/", `${FRONT}/src`],
],
});
});
it("a selection across both projects keeps every file", () => {
setup({ members: [BACK, FRONT], homeRoot: BACK });
sendPaths([`${BACK}/pom.xml`, `${FRONT}/package.json`]);
expect(sent().text).toBe("@pom.xml @frontend/package.json");
});
it("the New Task page's picked project is the one files are relative to", () => {
setup({ members: [BACK, FRONT], homeRoot: FRONT });
sendPaths([`${FRONT}/package.json`, `${BACK}/pom.xml`]);
expect(sent().text).toBe("@package.json @backend/pom.xml");
});
it("a worktree task maps its own project into the worktree, not another project", () => {
setup({ members: [BACK, FRONT], task: task("/wt/t", BACK, "worktree") });
sendPaths([`${BACK}/pom.xml`, `${FRONT}/package.json`]);
expect(sent("t").files).toEqual([
["pom.xml", "/wt/t/pom.xml"],
["frontend/package.json", `${FRONT}/package.json`],
]);
});
it("a path outside every project shows the notice instead of doing nothing", () => {
setup({ members: [BACK, FRONT], homeRoot: BACK });
expect(sendPaths(["/etc/hosts"])).toBe(false);
expect(sent().files).toEqual([]);
expect(mocks.notify).toHaveBeenCalledWith({ level: "info", title: NOTICE });
mocks.notify.mockReset();
expect(sendPaths(["/etc/hosts", `${FRONT}/package.json`])).toBe(true);
expect(sent().text).toBe("@frontend/package.json");
expect(mocks.notify).toHaveBeenCalledTimes(1);
});
});
describe("Send to Agent from the editor", () => {
const openEditor = (path: string, from = 0, to = 0) => {
const state = EditorState.create({ doc: "a\nb\nc\n", selection: EditorSelection.single(from, to) });
mocks.editor = { ctx: { path }, view: { state } };
};
it("a non-primary project's file, with the selected lines", () => {
setup({ members: [BACK, FRONT], homeRoot: BACK });
openEditor(`${FRONT}/src/App.vue`, 2, 6);
expect(sendSelection()).toBe(true);
expect(sent()).toEqual({
text: "@frontend/src/App.vue:2-3",
files: [["frontend/src/App.vue", `${FRONT}/src/App.vue`]],
});
});
it("the whole file without a selection; a file outside the projects shows the notice", () => {
setup({ members: [BACK, FRONT], homeRoot: BACK });
openEditor(`${BACK}/pom.xml`);
sendSelection();
expect(sent().text).toBe("@pom.xml");
openEditor("/tmp/scratch.txt");
expect(sendSelection()).toBe(false);
expect(mocks.notify).toHaveBeenCalledWith({ level: "info", title: NOTICE });
});
});