File size: 11,099 Bytes
fd2c364 | 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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 | import { afterEach, describe, expect, it, vi } from "vitest";
import {
clearEmptyContent,
clearFileInput,
clearTextContent,
ensureCursorVisible,
focusContentEditableAtEnd,
getClipboardFiles,
getTextContent,
isContentEmpty,
isPastedClipboardImage,
normalizePastedFile,
partitionImagesForUpload,
} from "#/components/features/chat/utils/chat-input.utils";
afterEach(() => {
vi.restoreAllMocks();
});
function createFileList(files: File[]): FileList {
const list = {
length: files.length,
item: (index: number) => files[index] ?? null,
} as FileList & Record<number, File>;
for (let i = 0; i < files.length; i += 1) {
list[i] = files[i];
}
return list;
}
function createMockDataTransfer({
files = [],
items = [],
}: {
files?: File[];
items?: Array<{ kind: string; type: string; file: File | null }>;
}): DataTransfer {
const fileItems = items.map((entry) => ({
kind: entry.kind,
type: entry.type,
getAsFile: () => entry.file,
}));
return {
files: createFileList(files),
items: fileItems as unknown as DataTransferItemList,
getData: () => "",
} as unknown as DataTransfer;
}
describe("normalizePastedFile", () => {
it("returns the file unchanged when it already has a name", () => {
const file = new File(["x"], "photo.png", { type: "image/png" });
expect(normalizePastedFile(file)).toBe(file);
});
it("assigns a generated name for unnamed clipboard images", () => {
const file = new File(["x"], "", { type: "image/png" });
const normalized = normalizePastedFile(file);
expect(normalized.name).toMatch(/^pasted-image-\d+\.png$/);
expect(normalized.type).toBe("image/png");
expect(normalized.size).toBe(file.size);
});
it.each([
["image/jpeg", "jpg"],
["image/jpg", "jpg"],
["image/gif", "gif"],
["image/webp", "webp"],
["image/bmp", "bmp"],
["image/avif", "png"],
["application/octet-stream", "bin"],
])("uses the stable extension for %s", (type, extension) => {
vi.spyOn(Date, "now").mockReturnValue(1234);
const normalized = normalizePastedFile(
new File(["x"], " ", { type, lastModified: 99 }),
);
expect(normalized.name).toBe(`pasted-image-1234.${extension}`);
expect(normalized.lastModified).toBe(99);
});
});
describe("isPastedClipboardImage", () => {
it("returns true for normalized clipboard screenshot names", () => {
const file = new File(["x"], "pasted-image-1710000000000.png", {
type: "image/png",
});
expect(isPastedClipboardImage(file)).toBe(true);
});
it("returns false for images picked from the file dialog", () => {
const file = new File(["x"], "photo.png", { type: "image/png" });
expect(isPastedClipboardImage(file)).toBe(false);
});
it("matches normalized names case-insensitively", () => {
expect(
isPastedClipboardImage(
new File(["x"], "PASTED-IMAGE-1710000000000.WEBP"),
),
).toBe(true);
});
});
describe("partitionImagesForUpload", () => {
it("splits marked images into the file-upload bucket", () => {
const embed = new File(["a"], "embed.png", { type: "image/png" });
const upload = new File(["b"], "upload.png", { type: "image/png" });
const result = partitionImagesForUpload([embed, upload], ["upload.png"]);
expect(result.imagesToEmbed).toEqual([embed]);
expect(result.imagesAsFiles).toEqual([upload]);
});
it("handles empty input and duplicate mark names", () => {
expect(partitionImagesForUpload([], ["missing.png"])).toEqual({
imagesToEmbed: [],
imagesAsFiles: [],
});
const image = new File(["a"], "same.png", { type: "image/png" });
expect(partitionImagesForUpload([image], ["same.png", "same.png"])).toEqual(
{
imagesToEmbed: [],
imagesAsFiles: [image],
},
);
});
});
describe("getClipboardFiles", () => {
it("reads from clipboardData.files when present", () => {
const file = new File(["x"], "doc.txt", { type: "text/plain" });
const clipboard = createMockDataTransfer({ files: [file] });
expect(getClipboardFiles(clipboard)).toEqual([file]);
});
it("normalizes unnamed files from clipboardData.files", () => {
const file = new File(["x"], "", { type: "application/pdf" });
const clipboard = createMockDataTransfer({ files: [file] });
expect(getClipboardFiles(clipboard)[0].name).toMatch(
/^pasted-image-\d+\.bin$/,
);
});
it("falls back to clipboard items for screenshot-style image paste", () => {
const image = new File(["pixels"], "", { type: "image/png" });
const clipboard = createMockDataTransfer({
items: [{ kind: "file", type: "image/png", file: image }],
});
const result = getClipboardFiles(clipboard);
expect(result).toHaveLength(1);
expect(result[0].type).toBe("image/png");
expect(result[0].name).toMatch(/^pasted-image-\d+\.png$/);
});
it("ignores non-file clipboard items", () => {
const clipboard = createMockDataTransfer({
items: [
{
kind: "string",
type: "text/plain",
file: new File(["not a clipboard file"], "ignored.txt"),
},
],
});
expect(getClipboardFiles(clipboard)).toEqual([]);
});
it("ignores file items that do not expose a File", () => {
const clipboard = createMockDataTransfer({
items: [{ kind: "file", type: "image/png", file: null }],
});
expect(getClipboardFiles(clipboard)).toEqual([]);
});
});
describe("module-initialized clipboard contracts", () => {
it("preserves file extensions, normalized names, and editable text", async () => {
vi.resetModules();
const fresh =
await import("#/components/features/chat/utils/chat-input.utils");
vi.spyOn(Date, "now").mockReturnValue(1234);
for (const [type, extension] of [
["image/png", "png"],
["image/jpeg", "jpg"],
["image/jpg", "jpg"],
["image/gif", "gif"],
["image/webp", "webp"],
["image/bmp", "bmp"],
]) {
expect(
fresh.normalizePastedFile(new File(["x"], "", { type })).name,
).toBe(`pasted-image-1234.${extension}`);
}
const named = (name: string) =>
fresh.isPastedClipboardImage(new File(["x"], name));
expect(named("pasted-image-1710000000000.png")).toBe(true);
expect(named("prefix-pasted-image-1710000000000.png")).toBe(false);
expect(named("pasted-image-1710000000000.png.extra")).toBe(false);
const editable = document.createElement("div");
editable.innerText = "message";
expect(fresh.getTextContent(editable)).toBe("message");
});
});
describe("content-editable helpers", () => {
it("detects empty elements across null, innerText, and textContent", () => {
expect(isContentEmpty(null)).toBe(true);
const element = document.createElement("div");
element.innerText = "visible";
element.textContent = "fallback";
expect(isContentEmpty(element)).toBe(false);
element.innerText = "";
element.textContent = " fallback ";
expect(isContentEmpty(element)).toBe(false);
element.textContent = " \n ";
expect(isContentEmpty(element)).toBe(true);
element.innerText = "";
element.textContent = "";
expect(isContentEmpty(element)).toBe(true);
});
it("clears only empty editable content", () => {
clearEmptyContent(null);
const nonempty = document.createElement("div");
nonempty.innerText = "keep";
nonempty.innerHTML = "<b>keep</b>";
clearEmptyContent(nonempty);
expect(nonempty.innerHTML).toBe("<b>keep</b>");
const empty = document.createElement("div");
empty.innerText = " ";
empty.innerHTML = "<br>";
clearEmptyContent(empty);
expect(empty.innerHTML).toBe("");
expect(empty.textContent).toBe("");
});
it("gets and clears editable text and file inputs safely", () => {
expect(getTextContent(null)).toBe("");
const editable = document.createElement("div");
editable.innerText = "message";
expect(getTextContent(editable)).toBe("message");
editable.textContent = "message";
clearTextContent(editable);
expect(editable.textContent).toBe("");
clearTextContent(null);
const input = document.createElement("input");
input.value = "C:\\fakepath\\file.txt";
clearFileInput(input);
expect(input.value).toBe("");
clearFileInput(null);
});
it("scrolls only when the cursor falls below the visible input", () => {
const element = document.createElement("div");
Object.defineProperties(element, {
scrollHeight: { configurable: true, value: 200 },
clientHeight: { configurable: true, value: 80 },
});
vi.spyOn(element, "getBoundingClientRect").mockReturnValue({
bottom: 100,
} as DOMRect);
const range = {
getBoundingClientRect: vi.fn(() => ({ bottom: 120 }) as DOMRect),
} as unknown as Range;
vi.spyOn(window, "getSelection").mockReturnValue({
rangeCount: 1,
getRangeAt: () => range,
} as unknown as Selection);
ensureCursorVisible(element);
expect(element.scrollTop).toBe(120);
range.getBoundingClientRect = vi.fn(() => ({ bottom: 90 }) as DOMRect);
element.scrollTop = 0;
ensureCursorVisible(element);
expect(element.scrollTop).toBe(0);
range.getBoundingClientRect = vi.fn(() => ({ bottom: 100 }) as DOMRect);
ensureCursorVisible(element);
expect(element.scrollTop).toBe(0);
});
it("returns safely when cursor geometry is unavailable", () => {
const element = document.createElement("div");
const selectionSpy = vi.spyOn(window, "getSelection");
selectionSpy.mockReturnValue(null);
ensureCursorVisible(element);
selectionSpy.mockReturnValue({ rangeCount: 0 } as Selection);
ensureCursorVisible(element);
selectionSpy.mockReturnValue({
rangeCount: 1,
getRangeAt: () => ({}) as Range,
} as unknown as Selection);
ensureCursorVisible(element);
selectionSpy.mockReturnValue({
rangeCount: 1,
getRangeAt: () =>
({ getBoundingClientRect: () => ({ bottom: 1 }) }) as Range,
} as unknown as Selection);
expect(() => ensureCursorVisible(null)).not.toThrow();
Object.defineProperty(element, "getBoundingClientRect", {
configurable: true,
value: undefined,
});
ensureCursorVisible(element);
});
it("focuses content-editable text at the end", () => {
focusContentEditableAtEnd(null);
const element = document.createElement("div");
element.textContent = "hello";
document.body.appendChild(element);
const focus = vi.spyOn(element, "focus");
const selection = window.getSelection()!;
focusContentEditableAtEnd(element);
expect(focus).toHaveBeenCalledOnce();
expect(selection.rangeCount).toBe(1);
const caret = selection.getRangeAt(0);
expect(caret.collapsed).toBe(true);
expect(caret.startOffset).toBe(element.childNodes.length);
vi.spyOn(window, "getSelection").mockReturnValue(null);
focusContentEditableAtEnd(element);
expect(focus).toHaveBeenCalledTimes(2);
});
});
|