Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.
Python agent-orchestrationai-agentai-coding-assistantclaude-codecoding-agentcomputer-usedesktop-applocal-firstmcppersonal-jarvisvoice-assistantvoice-coding
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { robustCopy, robustPaste, robustPasteFiles, saveOrDownload } from "./clipboard";
const originalClipboard = Object.getOwnPropertyDescriptor(navigator, "clipboard");
const originalExecCommand = Object.getOwnPropertyDescriptor(document, "execCommand");
function restoreProperty(
owner: object,
name: string,
descriptor: PropertyDescriptor | undefined,
) {
if (descriptor) Object.defineProperty(owner, name, descriptor);
else delete (owner as Record<string, unknown>)[name];
}
function stubClipboard(writeText: (text: string) => Promise<void>) {
Object.defineProperty(navigator, "clipboard", {
configurable: true,
value: { writeText },
});
}
function stubExecCommand(copy: () => boolean) {
Object.defineProperty(document, "execCommand", {
configurable: true,
value: copy,
});
}
/** jsdom does not define URL.createObjectURL at all, so assign the methods
* directly (spyOn would fail) + stub the anchor click so the browser-download
* path (downloadAs/downloadBlob) runs without throwing. */
function stubBrowserDownload() {
const u = URL as unknown as Record<string, unknown>;
u.createObjectURL = vi.fn(() => "blob:stub");
u.revokeObjectURL = vi.fn();
const clickSpy = vi
.spyOn(HTMLAnchorElement.prototype, "click")
.mockImplementation(() => {});
return { clickSpy };
}
describe("saveOrDownload", () => {
beforeEach(() => {
vi.useRealTimers();
});
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
restoreProperty(navigator, "clipboard", originalClipboard);
restoreProperty(document, "execCommand", originalExecCommand);
});
describe("robustCopy", () => {
it("keeps browser success when independent fallbacks are unavailable", async () => {
const writeText = vi.fn(async () => undefined);
const fetchMock = vi.fn(async () => ({ ok: false }));
stubClipboard(writeText);
stubExecCommand(() => false);
vi.stubGlobal("fetch", fetchMock);
await expect(robustCopy("short text")).resolves.toBe(true);
expect(writeText).toHaveBeenCalledWith("short text");
expect(fetchMock).toHaveBeenCalledTimes(1);
});
it("also writes short commands through the desktop fallback", async () => {
stubClipboard(vi.fn(async () => undefined));
stubExecCommand(() => false);
const fetchMock = vi.fn(async () => ({ ok: true }));
vi.stubGlobal("fetch", fetchMock);
await expect(robustCopy("jarvis setup telephony")).resolves.toBe(true);
expect(fetchMock).toHaveBeenCalledWith(
"/api/clipboard/text",
expect.objectContaining({
method: "POST",
body: JSON.stringify({ text: "jarvis setup telephony" }),
}),
);
});
it("falls back to the desktop backend when WKWebView blocks DOM copy", async () => {
stubClipboard(vi.fn(async () => Promise.reject(new Error("blocked"))));
stubExecCommand(() => false);
const fetchMock = vi.fn(async () => ({
ok: true,
json: async () => ({ copied: true }),
}));
vi.stubGlobal("fetch", fetchMock);
await expect(robustCopy("first line\nsecond line")).resolves.toBe(true);
expect(fetchMock).toHaveBeenCalledWith(
"/api/clipboard/text",
expect.objectContaining({
method: "POST",
body: JSON.stringify({ text: "first line\nsecond line" }),
}),
);
});
it("uses the backend when WebView2 cannot confirm a multiline overwrite", async () => {
stubClipboard(vi.fn(async () => undefined));
stubExecCommand(() => false);
const fetchMock = vi.fn(async () => ({
ok: true,
json: async () => ({ copied: true }),
}));
vi.stubGlobal("fetch", fetchMock);
await expect(robustCopy("one\ntwo")).resolves.toBe(true);
expect(fetchMock).toHaveBeenCalledTimes(1);
});
it("reports failure when browser and native clipboard paths fail", async () => {
stubClipboard(vi.fn(async () => Promise.reject(new Error("blocked"))));
stubExecCommand(() => false);
vi.stubGlobal("fetch", vi.fn(async () => ({ ok: false })));
await expect(robustCopy("copy me")).resolves.toBe(false);
});
});
it("posts base64 to the backend on desktop and returns the saved path", async () => {
const fetchMock = vi.fn(async () => ({
ok: true,
json: async () => ({ saved_path: "/home/u/Downloads/note.txt" }),
}));
vi.stubGlobal("fetch", fetchMock);
const saved = await saveOrDownload({
filename: "note.txt",
text: "hi",
native: true,
});
expect(saved).toBe("/home/u/Downloads/note.txt");
expect(fetchMock).toHaveBeenCalledTimes(1);
const [url, init] = fetchMock.mock.calls[0] as unknown as [
string,
RequestInit,
];
expect(url).toBe("/api/downloads/save");
const body = JSON.parse(init.body as string) as {
filename: string;
content_b64: string;
};
expect(body.filename).toBe("note.txt");
expect(body.content_b64.length).toBeGreaterThan(0);
});
it("uses the browser download (no backend call) when not native", async () => {
const fetchMock = vi.fn();
vi.stubGlobal("fetch", fetchMock);
const { clickSpy } = stubBrowserDownload();
const saved = await saveOrDownload({
filename: "note.txt",
text: "hi",
native: false,
});
expect(saved).toBeNull();
expect(fetchMock).not.toHaveBeenCalled();
expect(clickSpy).toHaveBeenCalledTimes(1);
});
it("falls back to the browser download when the backend save fails", async () => {
const fetchMock = vi.fn(async () => ({ ok: false, status: 500 }));
vi.stubGlobal("fetch", fetchMock);
const { clickSpy } = stubBrowserDownload();
const saved = await saveOrDownload({
filename: "note.txt",
text: "hi",
native: true,
});
expect(saved).toBeNull();
expect(fetchMock).toHaveBeenCalledTimes(1);
expect(clickSpy).toHaveBeenCalledTimes(1);
});
});
describe("robustPaste", () => {
afterEach(() => {
vi.useRealTimers();
vi.restoreAllMocks();
vi.unstubAllGlobals();
restoreProperty(navigator, "clipboard", originalClipboard);
});
function stubReadText(readText: () => Promise<string>) {
Object.defineProperty(navigator, "clipboard", {
configurable: true,
value: { readText },
});
}
function stubRest(response: { ok: boolean; body?: unknown }) {
const fetchMock = vi.fn(async () => ({
ok: response.ok,
status: response.ok ? 200 : 404,
json: async () => response.body,
}));
vi.stubGlobal("fetch", fetchMock);
return fetchMock;
}
it("reads through the desktop route first", async () => {
const readText = vi.fn(async () => "from the browser");
stubReadText(readText);
stubRest({ ok: true, body: { text: "from the desktop" } });
await expect(robustPaste()).resolves.toBe("from the desktop");
expect(readText).not.toHaveBeenCalled();
});
it("is not blocked by a clipboard permission prompt that never settles", async () => {
// The regression this ordering exists for: inside the desktop WebView,
// navigator.clipboard.readText() stays pending forever — neither resolved
// nor rejected — so awaiting it first made right-click Paste look dead.
stubReadText(() => new Promise<string>(() => {}));
stubRest({ ok: true, body: { text: "pasted anyway" } });
await expect(robustPaste()).resolves.toBe("pasted anyway");
});
it("falls back to the browser when the desktop route is absent", async () => {
stubReadText(async () => "from the browser");
stubRest({ ok: false });
await expect(robustPaste()).resolves.toBe("from the browser");
});
it("reports a genuinely empty clipboard as empty, not as a failure", async () => {
stubReadText(async () => "should not be consulted");
stubRest({ ok: true, body: { text: "" } });
await expect(robustPaste()).resolves.toBe("");
});
it("gives up on a browser read that never settles", async () => {
vi.useFakeTimers();
stubReadText(() => new Promise<string>(() => {}));
stubRest({ ok: false });
const pending = robustPaste();
await vi.advanceTimersByTimeAsync(5000);
await expect(pending).resolves.toBeNull();
});
it("returns null when no path can read the clipboard", async () => {
stubReadText(async () => {
throw new Error("NotAllowedError");
});
stubRest({ ok: false });
await expect(robustPaste()).resolves.toBeNull();
});
});
describe("robustPasteFiles", () => {
afterEach(() => {
vi.restoreAllMocks();
vi.unstubAllGlobals();
});
it("reads copied Explorer paths through the desktop route", async () => {
const fetchMock = vi.fn(async () => ({
ok: true,
json: async () => ({ paths: ["C:/pictures/one.png", "C:/documents/notes.txt"] }),
}));
vi.stubGlobal("fetch", fetchMock);
await expect(robustPasteFiles()).resolves.toEqual([
"C:/pictures/one.png",
"C:/documents/notes.txt",
]);
expect(fetchMock).toHaveBeenCalledWith("/api/clipboard/files", {
method: "GET",
credentials: "same-origin",
});
});
it("rejects malformed path entries without inventing a file", async () => {
vi.stubGlobal("fetch", vi.fn(async () => ({
ok: true,
json: async () => ({ paths: ["C:/pictures/one.png", 42, ""] }),
})));
await expect(robustPasteFiles()).resolves.toEqual(["C:/pictures/one.png"]);
});
it("returns null when the desktop route is unavailable", async () => {
vi.stubGlobal("fetch", vi.fn(async () => ({ ok: false })));
await expect(robustPasteFiles()).resolves.toBeNull();
});
});