83 lines
3.0 KiB
TypeScript
83 lines
3.0 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
import { pasteSizeBytes, selectionSize, utf8ByteLength } from "./measure";
|
|
|
|
describe("utf8ByteLength", () => {
|
|
it("matches the platform encoder for ASCII, Cyrillic, emoji, and lone surrogates", () => {
|
|
for (const sample of ["", "abc", "привет, мир", "a😀b", "\ud800", "x\udc00y", "日本語"]) {
|
|
expect(utf8ByteLength(sample)).toBe(new TextEncoder().encode(sample).length);
|
|
}
|
|
});
|
|
});
|
|
|
|
describe("pasteSizeBytes", () => {
|
|
it("returns only a number for plain text and adds file sizes", () => {
|
|
const source = {
|
|
types: ["text/plain", "text/html", "Files"],
|
|
files: [{ size: 100 }, { size: 20 }],
|
|
getData: vi.fn((format: string) => (format === "text/plain" ? "привет" : "<b>x</b>")),
|
|
};
|
|
const size = pasteSizeBytes(source);
|
|
expect(size).toBe(12 + 120);
|
|
expect(typeof size).toBe("number");
|
|
expect(source.getData).toHaveBeenCalledTimes(1);
|
|
expect(source.getData).toHaveBeenCalledWith("text/plain");
|
|
});
|
|
|
|
it("does not read any data when the clipboard has no plain text", () => {
|
|
const getData = vi.fn(() => "secret");
|
|
expect(pasteSizeBytes({ types: ["text/html"], files: [], getData })).toBe(0);
|
|
expect(getData).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("treats a missing source as an empty paste", () => {
|
|
expect(pasteSizeBytes(null)).toBe(0);
|
|
expect(pasteSizeBytes(undefined)).toBe(0);
|
|
});
|
|
|
|
it("stays within the contract range", () => {
|
|
const huge = { types: [], files: [{ size: 5_000_000_000 }], getData: () => "" };
|
|
expect(pasteSizeBytes(huge)).toBe(2_147_483_647);
|
|
});
|
|
});
|
|
|
|
describe("selectionSize", () => {
|
|
it("uses the offsets of a text control without reading its value", () => {
|
|
const control = {
|
|
selectionStart: 2,
|
|
selectionEnd: 9,
|
|
get value(): string {
|
|
throw new Error("the typed value must not be read");
|
|
},
|
|
};
|
|
expect(selectionSize({ activeElement: control })).toBe(7);
|
|
});
|
|
|
|
it("never falls back to the page selection while a form field has focus", () => {
|
|
const getSelection = vi.fn(() => ({ rangeCount: 1, toString: () => "typed secret" }));
|
|
expect(
|
|
selectionSize({ activeElement: { selectionStart: null, selectionEnd: null }, getSelection }),
|
|
).toBe(0);
|
|
expect(
|
|
selectionSize({ activeElement: { selectionStart: 3, selectionEnd: 3 }, getSelection }),
|
|
).toBe(0);
|
|
const throwing = {
|
|
get selectionStart(): number {
|
|
throw new DOMException("not supported");
|
|
},
|
|
selectionEnd: 0,
|
|
};
|
|
expect(selectionSize({ activeElement: throwing, getSelection })).toBe(0);
|
|
expect(getSelection).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("uses the length of the page selection when no form field has focus", () => {
|
|
const doc = {
|
|
activeElement: {},
|
|
getSelection: () => ({ rangeCount: 1, toString: () => "условие" }),
|
|
};
|
|
expect(selectionSize(doc)).toBe(7);
|
|
expect(selectionSize({ getSelection: () => ({ rangeCount: 0, toString: () => "x" }) })).toBe(0);
|
|
expect(selectionSize({})).toBe(0);
|
|
});
|
|
});
|