6adf98fd10
- Added OSC5522 parsing and a payload controller that prefers image MIME and reassembles chunks. - Added bracketed image-path detection and `onPasteImagePath` callback wiring in the editor. - Added local image-path paste insertion with fallback to text paste on image-size errors. - Added `Editor.pasteText()` and OSC5522 disable sequencing on terminal cleanup.
110 lines
4.3 KiB
TypeScript
110 lines
4.3 KiB
TypeScript
import { describe, expect, it } from "bun:test";
|
|
import { EnhancedPasteController } from "../../src/utils/enhanced-paste";
|
|
|
|
const ST = "\x1b\\";
|
|
const OSC = "\x1b]5522;";
|
|
|
|
function packet(metadata: string, payload?: string): string {
|
|
return `${OSC}${metadata}${payload === undefined ? "" : `;${payload}`}${ST}`;
|
|
}
|
|
|
|
describe("EnhancedPasteController", () => {
|
|
it("requests image data from an OSC 5522 paste event and preserves chunk boundaries", () => {
|
|
const writes: string[] = [];
|
|
const pastedImages: Array<{ data: string; mimeType: string }> = [];
|
|
const statuses: string[] = [];
|
|
const controller = new EnhancedPasteController({
|
|
write: data => writes.push(data),
|
|
pasteText: () => statuses.push("unexpected text paste"),
|
|
pasteImage: image => {
|
|
pastedImages.push({ data: image.data, mimeType: image.mimeType });
|
|
},
|
|
showStatus: message => statuses.push(message),
|
|
});
|
|
|
|
controller.enable();
|
|
expect(writes).toEqual(["\x1b[?5522h"]);
|
|
|
|
const imageMime = Buffer.from("image/png", "utf8").toString("base64");
|
|
const textMime = Buffer.from("text/plain", "utf8").toString("base64");
|
|
const password = Buffer.from("secret123", "utf8").toString("base64");
|
|
controller.handleInput(packet(`type=read:status=OK:pw=${password}`));
|
|
controller.handleInput(packet(`type=read:status=DATA:mime=${textMime}`));
|
|
controller.handleInput(packet(`type=read:status=DATA:mime=${imageMime}`));
|
|
controller.handleInput(packet("type=read:status=DONE"));
|
|
|
|
const pasteEventName = Buffer.from("Paste event", "utf8").toString("base64");
|
|
expect(writes.at(-1)).toBe(`${OSC}type=read:mime=${imageMime}:pw=${password}:name=${pasteEventName}${ST}`);
|
|
|
|
controller.handleInput(packet("type=read:status=OK"));
|
|
controller.handleInput(
|
|
packet(`type=read:status=DATA:mime=${imageMime}`, Buffer.from("image-", "utf8").toString("base64")),
|
|
);
|
|
controller.handleInput(
|
|
packet(`type=read:status=DATA:mime=${imageMime}`, Buffer.from("bytes", "utf8").toString("base64")),
|
|
);
|
|
controller.handleInput(packet("type=read:status=DONE"));
|
|
|
|
expect(pastedImages).toEqual([
|
|
{
|
|
data: Buffer.from("image-bytes", "utf8").toString("base64"),
|
|
mimeType: "image/png",
|
|
},
|
|
]);
|
|
expect(statuses).toEqual([]);
|
|
});
|
|
|
|
it("falls back to text/plain and carries primary-selection location into the read request", () => {
|
|
const writes: string[] = [];
|
|
const pastedText: string[] = [];
|
|
const controller = new EnhancedPasteController({
|
|
write: data => writes.push(data),
|
|
pasteText: text => pastedText.push(text),
|
|
pasteImage: () => {
|
|
throw new Error("unexpected image paste");
|
|
},
|
|
showStatus: message => pastedText.push(`status:${message}`),
|
|
});
|
|
|
|
const textMime = Buffer.from("text/plain", "utf8").toString("base64");
|
|
const password = Buffer.from("secret456", "utf8").toString("base64");
|
|
expect(controller.handleInput("plain text")).toBe(false);
|
|
controller.handleInput(packet(`type=read:status=OK:loc=primary:pw=${password}`));
|
|
controller.handleInput(packet(`type=read:status=DATA:mime=${textMime}`));
|
|
controller.handleInput(packet("type=read:status=DONE"));
|
|
|
|
expect(writes).toHaveLength(1);
|
|
expect(writes[0]).toContain(`mime=${textMime}`);
|
|
expect(writes[0]).toContain("loc=primary");
|
|
expect(writes[0]).toContain(`pw=${password}`);
|
|
|
|
controller.handleInput(packet("type=read:status=OK"));
|
|
controller.handleInput(
|
|
packet(`type=read:status=DATA:mime=${textMime}`, Buffer.from("hello ", "utf8").toString("base64")),
|
|
);
|
|
controller.handleInput(
|
|
packet(`type=read:status=DATA:mime=${textMime}`, Buffer.from("world", "utf8").toString("base64")),
|
|
);
|
|
controller.handleInput(packet("type=read:status=DONE"));
|
|
|
|
expect(pastedText).toEqual(["hello world"]);
|
|
});
|
|
|
|
it("reports unsupported paste events instead of leaking OSC packets to the editor", () => {
|
|
const statuses: string[] = [];
|
|
const controller = new EnhancedPasteController({
|
|
write: () => {},
|
|
pasteText: () => {},
|
|
pasteImage: () => {},
|
|
showStatus: message => statuses.push(message),
|
|
});
|
|
|
|
const htmlMime = Buffer.from("text/html", "utf8").toString("base64");
|
|
expect(controller.handleInput(packet("type=read:status=OK"))).toBe(true);
|
|
expect(controller.handleInput(packet(`type=read:status=DATA:mime=${htmlMime}`))).toBe(true);
|
|
expect(controller.handleInput(packet("type=read:status=DONE"))).toBe(true);
|
|
|
|
expect(statuses).toEqual(["Clipboard paste has no supported text or image data"]);
|
|
});
|
|
});
|