snapshot of https://github.com/excalidraw/excalidraw.git (master, 2026-09-16; blobs > 131072B omitted)
Some checks failed
Tests / test (push) Has been cancelled
Some checks failed
Tests / test (push) Has been cancelled
This commit is contained in:
109
packages/common/tests/binary-heap.test.ts
Normal file
109
packages/common/tests/binary-heap.test.ts
Normal file
@@ -0,0 +1,109 @@
|
||||
import { BinaryHeap } from "../src/binary-heap";
|
||||
|
||||
describe("BinaryHeap", () => {
|
||||
const numberHeap = () => new BinaryHeap<number>((n) => n);
|
||||
|
||||
const drain = (heap: BinaryHeap<number>) => {
|
||||
const out: number[] = [];
|
||||
while (heap.size() > 0) {
|
||||
out.push(heap.pop()!);
|
||||
}
|
||||
return out;
|
||||
};
|
||||
|
||||
describe("empty heap", () => {
|
||||
it("has size 0", () => {
|
||||
expect(numberHeap().size()).toBe(0);
|
||||
});
|
||||
|
||||
it("pop() returns null", () => {
|
||||
expect(numberHeap().pop()).toBe(null);
|
||||
});
|
||||
|
||||
it("remove() is a no-op and does not throw", () => {
|
||||
const heap = numberHeap();
|
||||
expect(() => heap.remove(1)).not.toThrow();
|
||||
expect(heap.size()).toBe(0);
|
||||
});
|
||||
});
|
||||
|
||||
describe("push / pop", () => {
|
||||
it("tracks size as items are added and removed", () => {
|
||||
const heap = numberHeap();
|
||||
[3, 1, 2].forEach((n) => heap.push(n));
|
||||
expect(heap.size()).toBe(3);
|
||||
|
||||
heap.pop();
|
||||
expect(heap.size()).toBe(2);
|
||||
});
|
||||
|
||||
it("pops a single pushed element back out", () => {
|
||||
const heap = numberHeap();
|
||||
heap.push(42);
|
||||
expect(heap.pop()).toBe(42);
|
||||
expect(heap.pop()).toBe(null);
|
||||
});
|
||||
|
||||
it("always pops the smallest score first", () => {
|
||||
const heap = numberHeap();
|
||||
[5, 3, 8, 1, 9, 2, 7].forEach((n) => heap.push(n));
|
||||
expect(drain(heap)).toEqual([1, 2, 3, 5, 7, 8, 9]);
|
||||
});
|
||||
|
||||
it("handles duplicate scores", () => {
|
||||
const heap = numberHeap();
|
||||
[4, 1, 4, 1, 2].forEach((n) => heap.push(n));
|
||||
expect(drain(heap)).toEqual([1, 1, 2, 4, 4]);
|
||||
});
|
||||
|
||||
it("maintains the heap invariant for a large adversarial (reverse-sorted) input", () => {
|
||||
const heap = numberHeap();
|
||||
// pushing in descending order forces a sift-up on every insert
|
||||
const input = Array.from({ length: 1000 }, (_, i) => 1000 - i);
|
||||
input.forEach((n) => heap.push(n));
|
||||
expect(drain(heap)).toEqual([...input].sort((a, b) => a - b));
|
||||
});
|
||||
});
|
||||
|
||||
describe("remove", () => {
|
||||
it("removes an interior element and keeps the rest ordered", () => {
|
||||
const heap = numberHeap();
|
||||
[5, 3, 8, 1, 9].forEach((n) => heap.push(n));
|
||||
|
||||
heap.remove(8);
|
||||
|
||||
expect(heap.size()).toBe(4);
|
||||
expect(drain(heap)).toEqual([1, 3, 5, 9]);
|
||||
});
|
||||
|
||||
it("can remove the current minimum", () => {
|
||||
const heap = numberHeap();
|
||||
[5, 3, 8, 1, 9].forEach((n) => heap.push(n));
|
||||
|
||||
heap.remove(1);
|
||||
|
||||
expect(heap.size()).toBe(4);
|
||||
expect(heap.pop()).toBe(3);
|
||||
});
|
||||
});
|
||||
|
||||
describe("rescoreElement", () => {
|
||||
type Node = { id: string; f: number };
|
||||
|
||||
it("re-sorts a node after its score is lowered", () => {
|
||||
const heap = new BinaryHeap<Node>((node) => node.f);
|
||||
|
||||
const a = { id: "a", f: 10 };
|
||||
const b = { id: "b", f: 20 };
|
||||
const c = { id: "c", f: 30 };
|
||||
[a, b, c].forEach((node) => heap.push(node));
|
||||
|
||||
c.f = 5;
|
||||
heap.rescoreElement(c);
|
||||
|
||||
expect(heap.pop()).toBe(c);
|
||||
expect(heap.pop()).toBe(a);
|
||||
expect(heap.pop()).toBe(b);
|
||||
});
|
||||
});
|
||||
});
|
||||
271
packages/common/tests/keys.test.ts
Normal file
271
packages/common/tests/keys.test.ts
Normal file
@@ -0,0 +1,271 @@
|
||||
import { KEYS, matchKey } from "../src/keys";
|
||||
|
||||
describe("key matcher", async () => {
|
||||
it("should not match unexpected key", async () => {
|
||||
expect(
|
||||
matchKey(new KeyboardEvent("keydown", { key: "N" }), KEYS.Y),
|
||||
).toBeFalsy();
|
||||
expect(
|
||||
matchKey(new KeyboardEvent("keydown", { key: "Unidentified" }), KEYS.Z),
|
||||
).toBeFalsy();
|
||||
|
||||
expect(
|
||||
matchKey(new KeyboardEvent("keydown", { key: "z" }), KEYS.Y),
|
||||
).toBeFalsy();
|
||||
expect(
|
||||
matchKey(new KeyboardEvent("keydown", { key: "y" }), KEYS.Z),
|
||||
).toBeFalsy();
|
||||
|
||||
expect(
|
||||
matchKey(new KeyboardEvent("keydown", { key: "Z" }), KEYS.Y),
|
||||
).toBeFalsy();
|
||||
expect(
|
||||
matchKey(new KeyboardEvent("keydown", { key: "Y" }), KEYS.Z),
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it("should match key (case insensitive) when key is latin", async () => {
|
||||
expect(
|
||||
matchKey(new KeyboardEvent("keydown", { key: "z" }), KEYS.Z),
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
matchKey(new KeyboardEvent("keydown", { key: "y" }), KEYS.Y),
|
||||
).toBeTruthy();
|
||||
|
||||
expect(
|
||||
matchKey(new KeyboardEvent("keydown", { key: "Z" }), KEYS.Z),
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
matchKey(new KeyboardEvent("keydown", { key: "Y" }), KEYS.Y),
|
||||
).toBeTruthy();
|
||||
});
|
||||
|
||||
it("should match key on QWERTY, QWERTZ, AZERTY", async () => {
|
||||
// QWERTY
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "z", code: "KeyZ" }),
|
||||
KEYS.Z,
|
||||
),
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "y", code: "KeyY" }),
|
||||
KEYS.Y,
|
||||
),
|
||||
).toBeTruthy();
|
||||
|
||||
// QWERTZ
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "z", code: "KeyY" }),
|
||||
KEYS.Z,
|
||||
),
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "y", code: "KeyZ" }),
|
||||
KEYS.Y,
|
||||
),
|
||||
).toBeTruthy();
|
||||
|
||||
// AZERTY
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "z", code: "KeyW" }),
|
||||
KEYS.Z,
|
||||
),
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "y", code: "KeyY" }),
|
||||
KEYS.Y,
|
||||
),
|
||||
).toBeTruthy();
|
||||
});
|
||||
|
||||
it("should match key on DVORAK, COLEMAK", async () => {
|
||||
// DVORAK
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "z", code: "KeySemicolon" }),
|
||||
KEYS.Z,
|
||||
),
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "y", code: "KeyF" }),
|
||||
KEYS.Y,
|
||||
),
|
||||
).toBeTruthy();
|
||||
|
||||
// COLEMAK
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "z", code: "KeyZ" }),
|
||||
KEYS.Z,
|
||||
),
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "y", code: "KeyJ" }),
|
||||
KEYS.Y,
|
||||
),
|
||||
).toBeTruthy();
|
||||
});
|
||||
|
||||
it("should match key on Turkish-Q", async () => {
|
||||
// Turkish-Q
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "z", code: "KeyN" }),
|
||||
KEYS.Z,
|
||||
),
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "Y", code: "KeyY" }),
|
||||
KEYS.Y,
|
||||
),
|
||||
).toBeTruthy();
|
||||
});
|
||||
|
||||
it("should not fallback when code is not defined", async () => {
|
||||
expect(
|
||||
matchKey(new KeyboardEvent("keydown", { key: "я" }), KEYS.Z),
|
||||
).toBeFalsy();
|
||||
|
||||
expect(
|
||||
matchKey(new KeyboardEvent("keydown", { key: "卜" }), KEYS.Y),
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it("should not fallback when code is incorrect", async () => {
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "z", code: "KeyY" }),
|
||||
KEYS.Y,
|
||||
),
|
||||
).toBeFalsy();
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "Y", code: "KeyZ" }),
|
||||
KEYS.Z,
|
||||
),
|
||||
).toBeFalsy();
|
||||
});
|
||||
|
||||
it("should fallback to code when key is non-latin", async () => {
|
||||
// Macedonian
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "з", code: "KeyZ" }),
|
||||
KEYS.Z,
|
||||
),
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "ѕ", code: "KeyY" }),
|
||||
KEYS.Y,
|
||||
),
|
||||
).toBeTruthy();
|
||||
|
||||
// Russian
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "я", code: "KeyZ" }),
|
||||
KEYS.Z,
|
||||
),
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "н", code: "KeyY" }),
|
||||
KEYS.Y,
|
||||
),
|
||||
).toBeTruthy();
|
||||
|
||||
// Serbian
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "ѕ", code: "KeyZ" }),
|
||||
KEYS.Z,
|
||||
),
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "з", code: "KeyY" }),
|
||||
KEYS.Y,
|
||||
),
|
||||
).toBeTruthy();
|
||||
|
||||
// Greek
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "ζ", code: "KeyZ" }),
|
||||
KEYS.Z,
|
||||
),
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "υ", code: "KeyY" }),
|
||||
KEYS.Y,
|
||||
),
|
||||
).toBeTruthy();
|
||||
|
||||
// Hebrew
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "ז", code: "KeyZ" }),
|
||||
KEYS.Z,
|
||||
),
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "ט", code: "KeyY" }),
|
||||
KEYS.Y,
|
||||
),
|
||||
).toBeTruthy();
|
||||
|
||||
// Cangjie - Traditional
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "重", code: "KeyZ" }),
|
||||
KEYS.Z,
|
||||
),
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "卜", code: "KeyY" }),
|
||||
KEYS.Y,
|
||||
),
|
||||
).toBeTruthy();
|
||||
|
||||
// Japanese
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "つ", code: "KeyZ" }),
|
||||
KEYS.Z,
|
||||
),
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "ん", code: "KeyY" }),
|
||||
KEYS.Y,
|
||||
),
|
||||
).toBeTruthy();
|
||||
|
||||
// 2-Set Korean
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "ㅋ", code: "KeyZ" }),
|
||||
KEYS.Z,
|
||||
),
|
||||
).toBeTruthy();
|
||||
expect(
|
||||
matchKey(
|
||||
new KeyboardEvent("keydown", { key: "ㅛ", code: "KeyY" }),
|
||||
KEYS.Y,
|
||||
),
|
||||
).toBeTruthy();
|
||||
});
|
||||
});
|
||||
62
packages/common/tests/queue.test.ts
Normal file
62
packages/common/tests/queue.test.ts
Normal file
@@ -0,0 +1,62 @@
|
||||
import { Queue } from "../src/queue";
|
||||
|
||||
describe("Queue", () => {
|
||||
const calls: any[] = [];
|
||||
|
||||
const createJobFactory =
|
||||
<T>(
|
||||
// for purpose of this test, Error object will become a rejection value
|
||||
resolutionOrRejectionValue: T,
|
||||
ms = 1,
|
||||
) =>
|
||||
() => {
|
||||
return new Promise<T>((resolve, reject) => {
|
||||
setTimeout(() => {
|
||||
if (resolutionOrRejectionValue instanceof Error) {
|
||||
reject(resolutionOrRejectionValue);
|
||||
} else {
|
||||
resolve(resolutionOrRejectionValue);
|
||||
}
|
||||
}, ms);
|
||||
}).then((x) => {
|
||||
calls.push(x);
|
||||
return x;
|
||||
});
|
||||
};
|
||||
|
||||
beforeEach(() => {
|
||||
calls.length = 0;
|
||||
});
|
||||
|
||||
it("should await and resolve values in order of enqueueing", async () => {
|
||||
const queue = new Queue();
|
||||
|
||||
const p1 = queue.push(createJobFactory("A", 50));
|
||||
const p2 = queue.push(createJobFactory("B"));
|
||||
const p3 = queue.push(createJobFactory("C"));
|
||||
|
||||
expect(await p3).toBe("C");
|
||||
expect(await p2).toBe("B");
|
||||
expect(await p1).toBe("A");
|
||||
|
||||
expect(calls).toEqual(["A", "B", "C"]);
|
||||
});
|
||||
|
||||
it("should reject a job if it throws, and not affect other jobs", async () => {
|
||||
const queue = new Queue();
|
||||
|
||||
const err = new Error("B");
|
||||
|
||||
queue.push(createJobFactory("A", 50));
|
||||
const p2 = queue.push(createJobFactory(err));
|
||||
const p3 = queue.push(createJobFactory("C"));
|
||||
|
||||
const p2err = p2.catch((err) => err);
|
||||
|
||||
await p3;
|
||||
|
||||
expect(await p2err).toBe(err);
|
||||
|
||||
expect(calls).toEqual(["A", "C"]);
|
||||
});
|
||||
});
|
||||
31
packages/common/tests/url.test.tsx
Normal file
31
packages/common/tests/url.test.tsx
Normal file
@@ -0,0 +1,31 @@
|
||||
import { normalizeLink } from "../src/url";
|
||||
|
||||
describe("normalizeLink", () => {
|
||||
// NOTE not an extensive XSS test suite, just to check if we're not
|
||||
// regressing in sanitization
|
||||
it("should sanitize links", () => {
|
||||
expect(
|
||||
// eslint-disable-next-line no-script-url
|
||||
normalizeLink(`javascript://%0aalert(document.domain)`).startsWith(
|
||||
// eslint-disable-next-line no-script-url
|
||||
`javascript:`,
|
||||
),
|
||||
).toBe(false);
|
||||
expect(normalizeLink("ola")).toBe("ola");
|
||||
expect(normalizeLink(" ola")).toBe("ola");
|
||||
|
||||
expect(normalizeLink("https://www.excalidraw.com")).toBe(
|
||||
"https://www.excalidraw.com",
|
||||
);
|
||||
expect(normalizeLink("www.excalidraw.com")).toBe("www.excalidraw.com");
|
||||
expect(normalizeLink("/ola")).toBe("/ola");
|
||||
expect(normalizeLink("http://test")).toBe("http://test");
|
||||
expect(normalizeLink("ftp://test")).toBe("ftp://test");
|
||||
expect(normalizeLink("file://")).toBe("file://");
|
||||
expect(normalizeLink("file://")).toBe("file://");
|
||||
expect(normalizeLink("[test](https://test)")).toBe("[test](https://test)");
|
||||
expect(normalizeLink("[[test]]")).toBe("[[test]]");
|
||||
expect(normalizeLink("<test>")).toBe("<test>");
|
||||
expect(normalizeLink("test&")).toBe("test&");
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user