File size: 4,804 Bytes
3201ca6 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | import { describe, expect, it, vi, beforeEach } from "vitest";
import { RemoteWorkspace } from "@openhands/typescript-client/workspace/remote-workspace";
import ConversationService from "#/api/conversation-service/conversation-service.api";
import { clearAgentServerHomeDirCache } from "#/api/agent-server-home";
const fileUploadMock = vi.fn();
const getHomeMock = vi.fn();
vi.mock("@openhands/typescript-client/workspace/remote-workspace", () => ({
RemoteWorkspace: vi.fn(function RemoteWorkspaceMock() {
return { fileUpload: fileUploadMock };
}),
}));
vi.mock("@openhands/typescript-client/clients", () => ({
FileClient: vi.fn(function FileClientMock() {
return { getHome: getHomeMock };
}),
}));
function makeFile(name: string) {
return new File(["content"], name, { type: "text/plain" });
}
describe("ConversationService", () => {
beforeEach(() => {
vi.clearAllMocks();
ConversationService.setCurrentConversation(null);
clearAgentServerHomeDirCache();
getHomeMock.mockResolvedValue({ home: "/Users/agent" });
});
describe("uploadFiles", () => {
// @spec WUP-001 — The default fallback working dir is relative
// (`workspace/project`); the upload path is resolved against the
// agent-server's home directory via /api/file/home.
it("uploads files through RemoteWorkspace and reports successes", async () => {
fileUploadMock.mockResolvedValue(undefined);
const result = await ConversationService.uploadFiles("conv-1", [
makeFile("a.txt"),
makeFile("b.txt"),
]);
expect(RemoteWorkspace).toHaveBeenCalledWith(
expect.objectContaining({
host: expect.any(String),
workingDir: expect.any(String),
}),
);
expect(fileUploadMock).toHaveBeenCalledWith(
expect.objectContaining({ name: "a.txt" }),
"/Users/agent/workspace/project/a.txt",
);
expect(fileUploadMock).toHaveBeenCalledWith(
expect.objectContaining({ name: "b.txt" }),
"/Users/agent/workspace/project/b.txt",
);
expect(result).toEqual({
uploaded_files: ["a.txt", "b.txt"],
skipped_files: [],
});
});
it("uploads using only the basename of user-provided file names", async () => {
fileUploadMock.mockResolvedValue(undefined);
const result = await ConversationService.uploadFiles("conv-1", [
makeFile("../../evil.txt"),
]);
expect(fileUploadMock).toHaveBeenCalledWith(
expect.objectContaining({ name: "../../evil.txt" }),
"/Users/agent/workspace/project/evil.txt",
);
expect(result).toEqual({
uploaded_files: ["evil.txt"],
skipped_files: [],
});
});
it("uploads into the active conversation workspace when set", async () => {
ConversationService.setCurrentConversation({
id: "conv-1",
workspace: { working_dir: "/workspace/project/my-app" },
} as never);
fileUploadMock.mockResolvedValue(undefined);
await ConversationService.uploadFiles("conv-1", [makeFile("doc.txt")]);
expect(fileUploadMock).toHaveBeenCalledWith(
expect.objectContaining({ name: "doc.txt" }),
"/workspace/project/my-app/doc.txt",
);
});
it("uses the current conversation session key and reports per-file failures", async () => {
ConversationService.setCurrentConversation({
id: "conv-1",
session_api_key: "session-key",
} as never);
fileUploadMock
.mockResolvedValueOnce(undefined)
.mockRejectedValueOnce(new Error("too large"));
const result = await ConversationService.uploadFiles("conv-1", [
makeFile("ok.txt"),
makeFile("bad.txt"),
]);
expect(RemoteWorkspace).toHaveBeenCalledWith(
expect.objectContaining({ apiKey: "session-key" }),
);
expect(result).toEqual({
uploaded_files: ["ok.txt"],
skipped_files: [{ name: "bad.txt", reason: "too large" }],
});
});
it("uploads files in bounded batches", async () => {
let activeUploads = 0;
let maxActiveUploads = 0;
fileUploadMock.mockImplementation(async () => {
activeUploads += 1;
maxActiveUploads = Math.max(maxActiveUploads, activeUploads);
await Promise.resolve();
activeUploads -= 1;
});
const files = Array.from({ length: 7 }, (_, index) =>
makeFile(`file-${String(index)}.txt`),
);
const result = await ConversationService.uploadFiles("conv-1", files);
expect(fileUploadMock).toHaveBeenCalledTimes(7);
expect(maxActiveUploads).toBe(5);
expect(result.uploaded_files).toEqual(files.map((file) => file.name));
expect(result.skipped_files).toEqual([]);
});
});
});
|