Open-source Grok Bot alternative with a virtual machine that bots can use
TypeScript ai-agentsclaude-codecodexcomputer-usedockerelectrongrokgrok-botgrok-bot-alternativeself-hostedtypescript
import { describe, expect, it, vi } from "vitest";
import { TURN_IMAGE_MAX_BYTES as SERVER_TURN_IMAGE_MAX_BYTES, TURN_IMAGE_MAX_COUNT as SERVER_TURN_IMAGE_MAX_COUNT } from "../../server/turn-images";
import { FILE_MAX_BYTES, fitImagesToSlots, IMAGE_MAX_BYTES, imageBytesLeft, imageSlotsLeft, imagesThatFit, intakeFiles, TURN_IMAGE_MAX_BYTES, TURN_IMAGE_MAX_COUNT, type Attachment } from "./composer-attachments";
type Fake = { name: string; size: number; type: string; text: () => Promise<string> };
const file = (name: string, type: string, size = 10): Fake => ({
name,
size,
type,
text: async () => "contents",
});
const upload = async (f: Fake): Promise<Attachment> => ({
kind: "image",
id: `id-${f.name}`,
name: f.name,
path: `/api/attachments/${f.name}`,
size: f.size,
mime: f.type,
});
const onDisk = (f: Fake) => `/Users/me/${f.name}`;
describe("intakeFiles", () => {
it("uploads a browser audio drop instead of requiring a Finder path", async () => {
const fetch = vi.spyOn(globalThis, "fetch").mockResolvedValue(new Response(JSON.stringify({
path: "/private/attachments/note.ogg", name: "Voice note.ogg", bytes: 4,
}), { status: 201, headers: { "content-type": "application/json" } }));
const getPath = vi.fn(() => "");
try {
const out = await intakeFiles([new File(["OggS"], "Voice note.opus", { type: "audio/ogg" })], {
allowImages: true, getPath, uploadImage: async () => null,
});
expect(out.attachments).toEqual([expect.objectContaining({ kind: "file", path: "/private/attachments/note.ogg" })]);
expect(out.problems).toEqual([]);
expect(getPath).not.toHaveBeenCalled();
} finally {
fetch.mockRestore();
}
});
it("uploads a desktop video into the private store instead of attaching its disk path", async () => {
const fetch = vi.spyOn(globalThis, "fetch").mockImplementation(async (_url, init) => {
const type = new Headers(init?.headers).get("content-type");
const name = type === "video/quicktime" ? "Screen.mov" : "clip.mp4";
return new Response(JSON.stringify({ path: `/private/attachments/${name}`, name, bytes: 3 }), {
status: 201, headers: { "content-type": "application/json" },
});
});
const getPath = vi.fn(onDisk);
try {
const out = await intakeFiles([
new File(["mp4"], "clip.mp4", { type: "video/mp4" }),
new File(["mov"], "Screen.MOV", { type: "" }),
], { allowImages: true, getPath, uploadImage: async () => null });
expect(fetch.mock.calls.map(([, init]) => new Headers(init?.headers).get("content-type")).sort()).toEqual(["video/mp4", "video/quicktime"]);
expect(out.attachments).toEqual([
expect.objectContaining({ kind: "file", path: "/private/attachments/clip.mp4" }),
expect.objectContaining({ kind: "file", path: "/private/attachments/Screen.mov" }),
]);
expect(out.problems).toEqual([]);
expect(getPath).not.toHaveBeenCalled();
} finally {
fetch.mockRestore();
}
});
it("keeps a desktop video over the upload limit as its disk path", async () => {
const fetch = vi.spyOn(globalThis, "fetch");
try {
const big = { ...file("film.mp4", "video/mp4", FILE_MAX_BYTES + 1), arrayBuffer: async () => new ArrayBuffer(0) };
const out = await intakeFiles([big], { allowImages: true, getPath: onDisk, uploadImage: async () => null });
expect(out.attachments).toEqual([expect.objectContaining({ kind: "file", path: "/Users/me/film.mp4" })]);
expect(out.problems).toEqual([]);
expect(fetch).not.toHaveBeenCalled();
} finally {
fetch.mockRestore();
}
});
it("uploads a mind map as an XMind workbook though the browser gives it no type", async () => {
const fetch = vi.spyOn(globalThis, "fetch").mockResolvedValue(new Response(JSON.stringify({
path: "/private/attachments/id.md", name: "Plan.xmind.md", bytes: 9,
}), { status: 201, headers: { "content-type": "application/json" } }));
try {
const out = await intakeFiles([new File(["PK"], "Plan.xmind", { type: "" })], {
allowImages: true, getPath: onDisk, uploadImage: async () => null,
});
expect(fetch.mock.calls[0]![1]).toMatchObject({ headers: { "content-type": "application/vnd.xmind.workbook" } });
expect(out.attachments).toEqual([expect.objectContaining({ kind: "file", name: "Plan.xmind.md", path: "/private/attachments/id.md" })]);
expect(out.problems).toEqual([]);
} finally {
fetch.mockRestore();
}
});
it("uploads images and keeps ordinary files as paths", async () => {
const out = await intakeFiles([file("shot.png", "image/png"), file("notes.txt", "text/plain")], {
allowImages: true,
getPath: onDisk,
uploadImage: upload,
});
expect(out.attachments.map((a) => [a.kind, "name" in a ? a.name : ""])).toEqual([
["image", "shot.png"],
["file", "notes.txt"],
]);
expect(out.problems).toEqual([]);
});
it("uses a private uploaded path for supported documents", async () => {
const out = await intakeFiles([file("notes.pdf", "application/pdf")], {
allowImages: true,
getPath: onDisk,
uploadImage: upload,
uploadFile: async (value) => ({
kind: "file",
id: "private-file",
name: value.name,
path: "/private/attachments/id.pdf",
size: value.size,
}),
});
expect(out.attachments).toEqual([expect.objectContaining({
kind: "file",
name: "notes.pdf",
path: "/private/attachments/id.pdf",
})]);
expect(out.problems).toEqual([]);
});
it("does not fall back to an arbitrary disk path when a private upload fails", async () => {
const out = await intakeFiles([file("notes.pdf", "application/pdf")], {
allowImages: true,
getPath: onDisk,
uploadImage: upload,
uploadFile: async () => { throw new Error("private store is full"); },
});
expect(out.attachments).toEqual([]);
expect(out.problems).toEqual([{ kind: "upload-failed", name: "notes.pdf", reason: "private store is full" }]);
});
it("treats an image as an ordinary file when the engine cannot read one", async () => {
const out = await intakeFiles([file("shot.png", "image/png")], {
allowImages: false,
getPath: onDisk,
uploadImage: async () => {
throw new Error("must not upload");
},
});
expect(out.attachments).toHaveLength(1);
expect(out.attachments[0].kind).toBe("file");
});
it("names the files it could not take, rather than dropping them in silence", async () => {
const out = await intakeFiles([{ ...file("ghost.bin", "application/octet-stream", 999_999_999) }], {
allowImages: true,
getPath: () => "",
uploadImage: upload,
});
expect(out.attachments).toHaveLength(0);
expect(out.problems).toEqual([{ kind: "unsupported", names: ["ghost.bin"] }]);
});
it("reports an upload that failed without losing the files that worked", async () => {
const out = await intakeFiles([file("ok.png", "image/png"), file("bad.png", "image/png")], {
allowImages: true,
getPath: onDisk,
uploadImage: async (f) => {
if (f.name === "bad.png") throw new Error("too large");
return upload(f);
},
});
expect(out.attachments).toHaveLength(1);
expect(out.problems).toEqual([{ kind: "upload-failed", name: "bad.png", reason: "too large" }]);
});
it("stops at the images a message can still take and names the rest", async () => {
const uploadImage = vi.fn(upload);
const out = await intakeFiles(
[file("1.png", "image/png"), file("notes.txt", "text/plain"), file("2.png", "image/png"), file("3.png", "image/png")],
{ allowImages: true, getPath: onDisk, uploadImage, imageSlots: 1 },
);
expect(out.attachments.map((a) => "name" in a ? a.name : "")).toEqual(["1.png", "notes.txt"]);
expect(uploadImage).toHaveBeenCalledTimes(1);
expect(out.notice).toBe(`A message takes up to ${TURN_IMAGE_MAX_COUNT} images, 40 MB in all. 2.png, 3.png weren't attached.`);
});
it("lets an oversized image fail on its own without taking the last slot", async () => {
const out = await intakeFiles([file("huge.png", "image/png", IMAGE_MAX_BYTES + 1), file("ok.png", "image/png")], {
allowImages: true,
getPath: onDisk,
uploadImage: async (f) => {
if (f.size > IMAGE_MAX_BYTES) throw new Error(`${f.name} exceeds 10 MB`);
return upload(f);
},
imageSlots: 1,
});
expect(out.attachments.map((a) => "name" in a ? a.name : "")).toEqual(["ok.png"]);
expect(out.notice).toBe("huge.png: huge.png exceeds 10 MB");
});
it("splits pasted images by the slots left, passing oversized ones through", () => {
const images = [{ size: 1 }, { size: IMAGE_MAX_BYTES + 1 }, { size: 2 }, { size: 3 }];
expect(fitImagesToSlots(images, 2)).toEqual({ kept: [images[0], images[1], images[2]], leftOut: [images[3]] });
expect(fitImagesToSlots(images, 0)).toEqual({ kept: [images[1]], leftOut: [images[0], images[2], images[3]] });
});
it("leaves images uncounted when the engine takes them as plain files", async () => {
const out = await intakeFiles([file("1.png", "image/png"), file("2.png", "image/png")], {
allowImages: false, getPath: onDisk, uploadImage: upload, uploadFile: async () => null, imageSlots: 0,
});
expect(out.attachments).toHaveLength(2);
expect(out.notice).toBeNull();
});
it("names a file it can't take as unsupported", async () => {
const out = await intakeFiles([file("run.exe", "application/octet-stream")], {
allowImages: true, getPath: () => "", uploadImage: upload, uploadFile: async () => null,
});
expect(out.attachments).toEqual([]);
expect(out.notice).toBe("Unable to attach run.exe. Choose a supported image, document, or audio file.");
});
it("uploads a browser file of any other type as a plain download", async () => {
const fetch = vi.spyOn(globalThis, "fetch").mockResolvedValue(new Response(JSON.stringify({
path: "/private/attachments/id.bin", name: "Setup.exe", bytes: 2,
}), { status: 201, headers: { "content-type": "application/json" } }));
try {
const out = await intakeFiles([new File(["MZ"], "Setup.exe", { type: "application/x-msdownload" })], {
allowImages: true, getPath: () => "", uploadImage: async () => null,
});
expect(fetch.mock.calls[0]![1]).toMatchObject({ headers: { "content-type": "application/x-msdownload" } });
expect(out.attachments).toEqual([expect.objectContaining({ kind: "file", name: "Setup.exe", path: "/private/attachments/id.bin" })]);
expect(out.problems).toEqual([]);
expect(out.notice).toBeNull();
} finally {
fetch.mockRestore();
}
});
it("counts the images already in the draft, and names pasted images past the slots left", () => {
const draft = [{ kind: "image" }, { kind: "file" }, { kind: "image" }] as unknown as Attachment[];
expect(imageSlotsLeft(draft)).toBe(TURN_IMAGE_MAX_COUNT - 2);
expect(imageSlotsLeft(Array.from({ length: TURN_IMAGE_MAX_COUNT + 1 }, () => ({ kind: "image" })) as unknown as Attachment[])).toBe(0);
const notices: string[] = [];
const pasted = [file("a.png", "image/png"), file("b.png", "image/png"), file("c.png", "image/png")];
expect(imagesThatFit(pasted, 2, (notice) => notices.push(notice)).map((image) => image.name)).toEqual(["a.png", "b.png"]);
expect(notices).toEqual([`A message takes up to ${TURN_IMAGE_MAX_COUNT} images, 40 MB in all. c.png wasn't attached.`]);
expect(imagesThatFit(pasted, 3, (notice) => notices.push(notice))).toHaveLength(3);
expect(notices).toHaveLength(1);
});
it("keeps the image caps in step with the server", () => {
expect(TURN_IMAGE_MAX_COUNT).toBe(SERVER_TURN_IMAGE_MAX_COUNT);
expect(TURN_IMAGE_MAX_BYTES).toBe(SERVER_TURN_IMAGE_MAX_BYTES);
});
it("stops at the image bytes a message can still take, so large images are never refused at send", async () => {
const big = IMAGE_MAX_BYTES - 1;
const draft = [{ kind: "image", size: big }, { kind: "file", size: big }] as unknown as Attachment[];
expect(imageBytesLeft(draft)).toBe(TURN_IMAGE_MAX_BYTES - big);
const out = await intakeFiles(Array.from({ length: 5 }, (_, index) => file(`${index + 1}.png`, "image/png", big)), {
allowImages: true, getPath: onDisk, uploadImage: upload, imageSlots: imageSlotsLeft(draft), imageBytes: imageBytesLeft(draft),
});
expect(out.attachments.map((a) => "name" in a ? a.name : "")).toEqual(["1.png", "2.png", "3.png"]);
expect(out.notice).toBe("A message takes up to 10 images, 40 MB in all. 4.png, 5.png weren't attached.");
const notices: string[] = [];
expect(imagesThatFit([file("a.png", "image/png", big)], 9, (notice) => notices.push(notice), big - 1)).toEqual([]);
expect(notices).toEqual(["A message takes up to 10 images, 40 MB in all. a.png wasn't attached."]);
});
it("lets an empty draft take ten images and names the eleventh", () => {
expect(imageSlotsLeft([])).toBe(10);
const notices: string[] = [];
const pasted = Array.from({ length: 11 }, (_, index) => file(`${index + 1}.png`, "image/png"));
expect(imagesThatFit(pasted, imageSlotsLeft([]), (notice) => notices.push(notice))).toHaveLength(10);
expect(notices).toEqual(["A message takes up to 10 images, 40 MB in all. 11.png wasn't attached."]);
});
});
describe("intakeFiles on the desktop app's own server: files over the upload limits go by their place", () => {
const big = (name: string, type: string, size: number) => ({ ...file(name, type, size), arrayBuffer: async () => new ArrayBuffer(0) });
it("attaches an image over the image limit by its place, keeping its name, without uploading it", async () => {
const uploadImage = vi.fn(upload);
const out = await intakeFiles([big("Original photo.png", "image/png", IMAGE_MAX_BYTES + 1), file("small.png", "image/png")], {
allowImages: true, getPath: onDisk, localPath: onDisk, uploadImage,
});
expect(out.attachments).toEqual([
expect.objectContaining({ kind: "file", name: "Original photo.png", path: "/Users/me/Original photo.png", size: IMAGE_MAX_BYTES + 1 }),
expect.objectContaining({ kind: "image", name: "small.png" }),
]);
expect(uploadImage.mock.calls.map(([f]) => f.name)).toEqual(["small.png"]);
expect(out.problems).toEqual([]);
});
it("attaches the images past the message's count and bytes by their place instead of leaving them out", async () => {
const uploadImage = vi.fn(upload);
const out = await intakeFiles(Array.from({ length: 12 }, (_, index) => file(`${index + 1}.jpg`, "image/jpeg")), {
allowImages: true, getPath: onDisk, localPath: onDisk, uploadImage, imageSlots: TURN_IMAGE_MAX_COUNT,
});
expect(uploadImage).toHaveBeenCalledTimes(TURN_IMAGE_MAX_COUNT);
expect(out.attachments.filter((a) => a.kind === "file").map((a) => "path" in a ? a.path : "")).toEqual(["/Users/me/11.jpg", "/Users/me/12.jpg"]);
expect(out.attachments).toHaveLength(12);
expect(out.notice).toBeNull();
});
it("attaches a document over the file limit by its place", async () => {
const uploadFile = vi.fn(async () => null);
const out = await intakeFiles([big("Scans.pdf", "application/pdf", FILE_MAX_BYTES + 1)], {
allowImages: true, getPath: onDisk, localPath: onDisk, uploadImage: upload, uploadFile,
});
expect(out.attachments).toEqual([expect.objectContaining({ kind: "file", name: "Scans.pdf", path: "/Users/me/Scans.pdf" })]);
expect(uploadFile).not.toHaveBeenCalled();
expect(out.problems).toEqual([]);
});
it("keeps today's limits where the file has no place on the bot's computer (phone, browser, a paired window)", async () => {
const out = await intakeFiles([big("huge.png", "image/png", IMAGE_MAX_BYTES + 1), file("1.png", "image/png"), file("2.png", "image/png")], {
allowImages: true, getPath: onDisk, localPath: () => "", imageSlots: 1,
uploadImage: async (f) => {
if (f.size > IMAGE_MAX_BYTES) throw new Error(`${f.name} exceeds 10 MB`);
return upload(f);
},
});
expect(out.attachments.map((a) => "name" in a ? a.name : "")).toEqual(["1.png"]);
expect(out.notice).toBe("A message takes up to 10 images, 40 MB in all. 2.png wasn't attached. huge.png: huge.png exceeds 10 MB");
});
it("still uploads files within the limits", async () => {
const localPath = vi.fn(onDisk);
const out = await intakeFiles([file("shot.png", "image/png")], { allowImages: true, getPath: onDisk, localPath, uploadImage: upload });
expect(out.attachments).toEqual([expect.objectContaining({ kind: "image", path: "/api/attachments/shot.png" })]);
expect(localPath).not.toHaveBeenCalled();
});
});