An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
import {after, before, beforeEach, describe, it} from "node:test";
import * as assert from "node:assert/strict";
import {
cancelFileTreeCollapse,
collapseFileTree,
expandFileTree,
isFileTreeCollapsing,
setFileTreeVisibility,
toggleFileTree
} from "./fileTreeAnimation";
let reduceMotion = false;
const originalWindow = Object.getOwnPropertyDescriptor(globalThis, "window");
before(() => {
Object.defineProperty(globalThis, "window", {
configurable: true,
value: {
matchMedia: () => ({matches: reduceMotion}),
},
});
});
beforeEach(() => {
reduceMotion = false;
});
after(() => {
if (originalWindow) {
Object.defineProperty(globalThis, "window", originalWindow);
} else {
Reflect.deleteProperty(globalThis, "window");
}
});
const createAnimation = () => {
let resolve!: () => void;
let reject!: (reason?: unknown) => void;
let canceled = false;
const finished = new Promise<void>((resolvePromise, rejectPromise) => {
resolve = resolvePromise;
reject = rejectPromise;
});
const animation = {
finished,
cancel() {
canceled = true;
reject(new Error("Animation canceled"));
},
} as unknown as Animation;
return {
animation,
finish: resolve,
fail: reject,
isCanceled: () => canceled,
};
};
const createTreeElements = (expanded = true) => {
const animation = createAnimation();
let frames!: Keyframe[] | PropertyIndexedKeyframes;
let options!: number | KeyframeAnimationOptions;
let arrowClosed = false;
let arrowOpen = expanded;
const style = {
overflow: "",
removeProperty(property: string) {
if (property === "overflow") {
this.overflow = "";
}
return "";
},
} as unknown as CSSStyleDeclaration;
const leafElement = {
tagName: "UL",
scrollHeight: 96,
style,
isConnected: true,
animate(animationFrames: Keyframe[] | PropertyIndexedKeyframes, animationOptions?: number | KeyframeAnimationOptions) {
frames = animationFrames;
options = animationOptions;
return animation.animation;
},
remove() {
this.isConnected = false;
},
} as unknown as HTMLElement;
const arrowElement = {
classList: {
remove() {
arrowClosed = true;
arrowOpen = false;
},
},
};
const liElement = {
nextElementSibling: leafElement,
querySelector(selector: string) {
if (selector === ".b3-list-item__arrow--open" && !arrowOpen) {
return null;
}
return arrowElement;
},
} as unknown as Element;
return {
animation,
leafElement,
liElement,
getFrames: () => frames,
getOptions: () => options as KeyframeAnimationOptions,
isArrowClosed: () => arrowClosed,
};
};
describe("fileTreeAnimation", () => {
it("retains pinned lists and cancels stale collapse when expanding again", async () => {
const animations = [createAnimation(), createAnimation()];
let hidden = false;
let index = 0;
const frames: Keyframe[][] = [];
const element = {
style: {overflow: "", removeProperty() { this.overflow = ""; }},
classList: {remove() { hidden = false; }, toggle(_name: string, value: boolean) { hidden = value; }},
getBoundingClientRect: () => ({height: 80}),
animate(value: Keyframe[]) { frames.push(value); return animations[index++].animation; },
} as unknown as HTMLElement;
setFileTreeVisibility(element, false, true);
assert.equal(hidden, false);
setFileTreeVisibility(element, true, true);
assert.equal(animations[0].isCanceled(), true);
await Promise.resolve();
assert.equal(hidden, false);
animations[1].finish();
await Promise.resolve();
assert.equal(hidden, false);
assert.equal(element.style.overflow, "");
assert.deepEqual(frames[0], [{height: "80px", maxHeight: "80px"}, {height: "0px", maxHeight: "0px"}]);
setFileTreeVisibility(element, false);
assert.equal(hidden, true);
});
it("expands to the actual content height and restores animation styles", async () => {
const tree = createTreeElements();
let finished = false;
expandFileTree(tree.leafElement, () => {
finished = true;
});
assert.deepEqual(tree.getFrames(), [{height: "0"}, {height: "96px"}]);
assert.equal(tree.getOptions().duration, 200);
assert.equal(tree.leafElement.style.overflow, "clip");
tree.animation.finish();
await tree.animation.animation.finished;
assert.equal(tree.animation.isCanceled(), true);
assert.equal(tree.leafElement.style.overflow, "");
assert.equal(finished, true);
});
it("restores animation styles if expansion fails", async () => {
const tree = createTreeElements();
let finished = false;
expandFileTree(tree.leafElement, () => {
finished = true;
});
tree.animation.fail(new Error("Animation failed"));
await assert.rejects(tree.animation.animation.finished);
assert.equal(tree.leafElement.style.overflow, "");
assert.equal(finished, false);
});
it("uses no duration when reduced motion is requested", async () => {
reduceMotion = true;
const tree = createTreeElements();
expandFileTree(tree.leafElement);
assert.equal(tree.getOptions().duration, 0);
tree.animation.finish();
await tree.animation.animation.finished;
});
it("collapses once and removes the child list after the animation", async () => {
const tree = createTreeElements();
let finishCount = 0;
collapseFileTree(tree.liElement, () => finishCount++);
collapseFileTree(tree.liElement, () => finishCount++);
assert.equal(tree.isArrowClosed(), true);
assert.equal(isFileTreeCollapsing(tree.liElement), true);
assert.deepEqual(tree.getFrames(), [{height: "96px"}, {height: "0"}]);
assert.equal(tree.leafElement.style.overflow, "clip");
tree.animation.finish();
await tree.animation.animation.finished;
assert.equal(tree.leafElement.isConnected, false);
assert.equal(isFileTreeCollapsing(tree.liElement), false);
assert.equal(finishCount, 1);
});
it("cancels and removes an in-progress collapse", async () => {
const tree = createTreeElements();
let finished = false;
collapseFileTree(tree.liElement, () => {
finished = true;
});
assert.equal(cancelFileTreeCollapse(tree.liElement), true);
await assert.rejects(tree.animation.animation.finished);
assert.equal(tree.animation.isCanceled(), true);
assert.equal(tree.leafElement.isConnected, false);
assert.equal(isFileTreeCollapsing(tree.liElement), false);
assert.equal(finished, false);
});
it("finishes a collapse if the animation fails unexpectedly", async () => {
const tree = createTreeElements();
let finished = false;
collapseFileTree(tree.liElement, () => {
finished = true;
});
tree.animation.fail(new Error("Animation failed"));
await assert.rejects(tree.animation.animation.finished);
assert.equal(tree.leafElement.isConnected, false);
assert.equal(isFileTreeCollapsing(tree.liElement), false);
assert.equal(finished, true);
});
it("routes an expanded item through the collapse animation", async () => {
const tree = createTreeElements();
let collapseCount = 0;
let expandCount = 0;
toggleFileTree(tree.liElement, () => collapseCount++, () => expandCount++);
assert.equal(isFileTreeCollapsing(tree.liElement), true);
assert.equal(expandCount, 0);
tree.animation.finish();
await tree.animation.animation.finished;
assert.equal(collapseCount, 1);
});
it("ignores a collapse request until expansion finishes", async () => {
const tree = createTreeElements();
let collapseCount = 0;
let expandCount = 0;
expandFileTree(tree.leafElement);
toggleFileTree(tree.liElement, () => collapseCount++, () => expandCount++);
assert.equal(tree.isArrowClosed(), false);
assert.equal(isFileTreeCollapsing(tree.liElement), false);
assert.equal(collapseCount, 0);
assert.equal(expandCount, 0);
tree.animation.finish();
await tree.animation.animation.finished;
});
it("routes a collapsed item through expansion", () => {
const tree = createTreeElements(false);
let collapseCount = 0;
let expandCount = 0;
toggleFileTree(tree.liElement, () => collapseCount++, () => expandCount++);
assert.equal(collapseCount, 0);
assert.equal(expandCount, 1);
});
it("ignores repeated toggles while an item is collapsing", async () => {
const tree = createTreeElements();
let collapseCount = 0;
let expandCount = 0;
toggleFileTree(tree.liElement, () => collapseCount++, () => expandCount++);
toggleFileTree(tree.liElement, () => collapseCount++, () => expandCount++);
assert.equal(collapseCount, 0);
assert.equal(expandCount, 0);
tree.animation.finish();
await tree.animation.animation.finished;
assert.equal(collapseCount, 1);
});
});