Skip to content

Local-first AI coding agent desktop: Electron + Rust host core + pi Agent Harness + user-installable plugins

test(composer): isolate native clipboard fixtures from the host

Linuxdazhaob2c11b5f1400
diff --git a/apps/desktop/electron/main/composer-clipboard-directories.ts b/apps/desktop/electron/main/composer-clipboard-directories.ts
index b1eb01632..1e3a45517 100644
--- a/apps/desktop/electron/main/composer-clipboard-directories.ts
+++ b/apps/desktop/electron/main/composer-clipboard-directories.ts
@@ -1,5 +1,5 @@
import { execFileSync } from "node:child_process";
-import { basename, isAbsolute, join, win32 } from "node:path";
+import { basename, join, posix, win32 } from "node:path";
import { realpath, stat } from "node:fs/promises";
import { fileURLToPath } from "node:url";
@@ -44,11 +44,11 @@ export function nativeClipboardPaths(
paths = value;
} else {
const uris = read("text/uri-list").toString("utf8") || read("x-special/gnome-copied-files").toString("utf8") || read("public.file-url").toString("utf8");
- paths = uris.split(/[\r\n\0]+/).filter(line => line.startsWith("file:")).map(uri => fileURLToPath(uri));
+ paths = uris.split(/[\r\n\0]+/).filter(line => line.startsWith("file:")).map(uri => fileURLToPath(uri, { windows: false }));
}
paths = [...new Set(paths)];
if (paths.length > MAX_ITEMS) throw new Error(`too many pasted items (maximum ${MAX_ITEMS})`);
- const absolute = platform === "win32" ? win32.isAbsolute : isAbsolute;
+ const absolute = platform === "win32" ? win32.isAbsolute : posix.isAbsolute;
if (paths.some(path => !absolute(path) || path.includes("\0"))) throw new Error("invalid clipboard file path");
return paths;
}
@@ -57,12 +57,13 @@ export function nativeClipboardPaths(
export async function readComposerClipboardDirectories(
clipboard: NativeClipboard,
rememberFiles: (paths: string[]) => string,
+ capturePaths: () => string[] = () => nativeClipboardPaths(clipboard),
): Promise<{
items: Array<{ kind: "directory"; path: string; name: string } | { kind: "file"; name: string }>;
token: string | null;
} | null> {
// Capture before yielding so later clipboard changes cannot replace this selection.
- const paths = nativeClipboardPaths(clipboard);
+ const paths = capturePaths();
const entries = await Promise.all(paths.map(async path => {
const canonical = await realpath(path);
const metadata = await stat(canonical);
diff --git a/apps/desktop/test/composer-clipboard-directories.test.mjs b/apps/desktop/test/composer-clipboard-directories.test.mjs
index 72d4ef7a2..ea4d6a503 100644
--- a/apps/desktop/test/composer-clipboard-directories.test.mjs
+++ b/apps/desktop/test/composer-clipboard-directories.test.mjs
@@ -2,7 +2,7 @@ import assert from "node:assert/strict";
import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
-import { pathToFileURL, fileURLToPath } from "node:url";
+import { fileURLToPath } from "node:url";
import { createRequire } from "node:module";
import test from "node:test";
import { buildSync } from "esbuild";
@@ -22,14 +22,14 @@ test("native URI file lists preserve order and decode spaces/unicode without rea
assert.deepEqual(nativeClipboardPaths(clipboard({
"text/uri-list": "# comment\r\nfile:///tmp/one%20folder\r\nfile:///tmp/%E4%B8%AD%E6%96%87\r\nhttps://example.com/ignored",
"text/plain": "unrelated clipboard text",
- })), ["/tmp/one folder", "/tmp/中文"]);
- assert.deepEqual(nativeClipboardPaths(clipboard({ "x-special/gnome-copied-files": "copy\nfile:///tmp/a\nfile:///tmp/a" })), ["/tmp/a"]);
+ }), "linux"), ["/tmp/one folder", "/tmp/中文"]);
+ assert.deepEqual(nativeClipboardPaths(clipboard({ "x-special/gnome-copied-files": "copy\nfile:///tmp/a\nfile:///tmp/a" }), "linux"), ["/tmp/a"]);
});
test("native file lists enforce metadata and selection bounds", () => {
- assert.throws(() => nativeClipboardPaths(clipboard({ "text/uri-list": "x".repeat(1024 * 1024 + 1) })), /too large/);
- assert.throws(() => nativeClipboardPaths(clipboard({ "text/uri-list": Array.from({ length: 21 }, (_, i) => `file:///tmp/${i}`).join("\n") })), /too many/);
- assert.throws(() => nativeClipboardPaths(clipboard({ "text/uri-list": "file:///tmp/a%00b" })), /null|invalid|encoded/i);
+ assert.throws(() => nativeClipboardPaths(clipboard({ "text/uri-list": "x".repeat(1024 * 1024 + 1) }), "linux"), /too large/);
+ assert.throws(() => nativeClipboardPaths(clipboard({ "text/uri-list": Array.from({ length: 21 }, (_, i) => `file:///tmp/${i}`).join("\n") }), "linux"), /too many/);
+ assert.throws(() => nativeClipboardPaths(clipboard({ "text/uri-list": "file:///tmp/a%00b" }), "linux"), /null|invalid|encoded/i);
});
test("Windows uses the complete native FileDropList instead of custom CF_HDROP or one FileNameW", () => {
@@ -41,12 +41,12 @@ test("Windows uses the complete native FileDropList instead of custom CF_HDROP o
test("normalized MIME enumeration does not hide raw Finder file URLs", () => {
const native = clipboard({ "public.file-url": "file:///tmp/synthetic-folder" });
native.availableFormats = () => ["text/uri-list"];
- assert.deepEqual(nativeClipboardPaths(native), ["/tmp/synthetic-folder"]);
+ assert.deepEqual(nativeClipboardPaths(native, "darwin"), ["/tmp/synthetic-folder"]);
});
test("macOS Finder file-list metadata is decoded in full", { skip: process.platform !== "darwin" }, () => {
const xml = '<?xml version="1.0"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"><array><string>/tmp/first folder</string><string>/tmp/second</string></array></plist>';
- assert.deepEqual(nativeClipboardPaths(clipboard({ NSFilenamesPboardType: xml })), ["/tmp/first folder", "/tmp/second"]);
+ assert.deepEqual(nativeClipboardPaths(clipboard({ NSFilenamesPboardType: xml }), "darwin"), ["/tmp/first folder", "/tmp/second"]);
});
test("directory metadata never copies a tree and mixed files use the existing import capability", async t => {
@@ -57,14 +57,14 @@ test("directory metadata never copies a tree and mixed files use the existing im
await writeFile(join(folder, "keep.txt"), "original");
await writeFile(file, "notes");
let captured;
- const result = await readComposerClipboardDirectories(clipboard({ "text/uri-list": [folder, file].map(path => pathToFileURL(path).href).join("\n") }), paths => { captured = paths; return "selection"; });
+ const result = await readComposerClipboardDirectories(clipboard({}), paths => { captured = paths; return "selection"; }, () => [folder, file]);
assert.deepEqual(result.items.map(item => item.kind), ["directory", "file"]);
assert.equal(result.items[0].name, "folder & 中文");
assert.equal(result.token, "selection");
assert.equal(captured.length, 1);
assert.equal(result.items[1].path, undefined, "regular source paths stay in main");
assert.equal(await readFile(join(folder, "keep.txt"), "utf8"), "original");
- assert.equal(await readComposerClipboardDirectories(clipboard({ "public.file-url": pathToFileURL(file).href }), () => assert.fail("regular-only paste keeps its current flow")), null);
+ assert.equal(await readComposerClipboardDirectories(clipboard({}), () => assert.fail("regular-only paste keeps its current flow"), () => [file]), null);
});
test("both send serializers include one directory instruction and escaped path markers", () => {