Open-source, local-first AI agent for real work.
TypeScript agent-toolsai-agentbyokcoding-agentcross-platformdesktop-appdeveloper-toolselectronharnessknowledge-basellmwikilocal-firstmcpplugin-systemprivacy-firstskillstypescriptvettaworkflow-automation
// @vitest-environment jsdom
import { createStore } from "jotai";
import { beforeEach, describe, expect, it, vi } from "vitest";
/** jsdom 未提供 localStorage,这里补一个内存实现,顺便让测试能预置历史记录。 */
function installStorage(seed?: Record<string, string>): Map<string, string> {
const store = new Map<string, string>(Object.entries(seed ?? {}));
vi.stubGlobal("localStorage", {
clear: () => store.clear(),
getItem: (key: string) => store.get(key) ?? null,
removeItem: (key: string) => void store.delete(key),
setItem: (key: string, value: string) => void store.set(key, value),
});
return store;
}
function setWindowWidth(width: number): void {
Object.defineProperty(window, "innerWidth", { configurable: true, value: width, writable: true });
}
/**
* 模块加载时就会读取持久化的宽度,所以每个用例都重新导入一次,
* 以便在导入前预置 localStorage 与窗口宽度。
*/
async function loadAtoms() {
vi.resetModules();
return await import("./activity-atoms");
}
/** 该窗口宽度下面板能达到的最大宽度(= 窗口宽 - 聊天区最小宽 384)。 */
function maxAt(windowWidth: number): number {
return windowWidth - 384;
}
const CANVAS = "plugin:vetta-ui-design:canvas";
/** 准备一个注册了画布(默认拉满)的面板,选中卡由参数决定。 */
async function panel(initialTab = "todo") {
const atoms = await loadAtoms();
const store = createStore();
store.set(atoms.setActivityTabDefaultWidthAtom, { tab: CANVAS, width: "max" });
store.set(atoms.setActivityPanelWidthTabAtom, initialTab);
const show = (tab: string) => store.set(atoms.setActivityPanelWidthTabAtom, tab);
const width = () => store.get(atoms.activityPanelWidthAtom);
/** 用户拖动分隔条到 px 并松手。 */
const drag = (px: number) => {
store.set(atoms.setTransientActivityPanelWidthAtom, px);
store.set(atoms.activityPanelWidthAtom, px);
};
return { atoms, store, show, width, drag };
}
describe("宽度跟着标签卡走(场景表)", () => {
beforeEach(() => {
installStorage();
setWindowWidth(1200);
});
it("1. 画布拉满之后,别处打开待办仍是默认 360", async () => {
const { show, width } = await panel(CANVAS);
expect(width()).toBe(maxAt(1200));
show("todo");
expect(width()).toBe(360);
});
it("2/3. 同一面板里画布与待办来回切,各用各的宽度", async () => {
const { show, width } = await panel("todo");
show(CANVAS);
expect(width()).toBe(maxAt(1200));
show("todo");
expect(width()).toBe(360);
show(CANVAS);
expect(width()).toBe(maxAt(1200));
});
it("4. 把画布拖窄只记在画布上,普通卡不受影响", async () => {
const { show, width, drag } = await panel(CANVAS);
drag(700);
show("todo");
expect(width()).toBe(360);
show(CANVAS);
expect(width()).toBe(700);
});
it("5. 内置浏览器默认拉满,切回别的卡恢复各自宽度", async () => {
const { show, width } = await panel("todo");
show("browser");
expect(width()).toBe(maxAt(1200));
show("todo");
expect(width()).toBe(360);
});
it("6. 自己把普通卡拖宽算偏好:所有普通卡共用、重新加载后仍在", async () => {
const first = await panel("todo");
first.drag(800);
first.show("plan");
expect(first.width()).toBe(800);
first.show(CANVAS);
expect(first.width()).toBe(maxAt(1200));
const reloaded = await panel("todo");
expect(reloaded.width()).toBe(800);
});
it("7/8. 双击分隔条:当前这张卡回到它的默认宽度,别的卡不变", async () => {
const { atoms, store, show, width, drag } = await panel("todo");
drag(800);
show(CANVAS);
drag(600);
store.set(atoms.resetActivityPanelWidthAtom);
expect(width()).toBe(maxAt(1200));
show("todo");
expect(width()).toBe(800);
store.set(atoms.resetActivityPanelWidthAtom);
expect(width()).toBe(360);
});
it("9. 重新加载后每张卡保留用户拖出的宽度,没拖过的用默认", async () => {
const first = await panel(CANVAS);
first.drag(640);
const reloaded = await panel("todo");
expect(reloaded.width()).toBe(360);
reloaded.show(CANVAS);
expect(reloaded.width()).toBe(640);
});
it("14. 插件给的默认宽度不改用户宽度;用户拖过就以用户为准", async () => {
const { atoms, store, show, width, drag } = await panel(CANVAS);
drag(700);
// 插件每次打开都重申默认宽度(activate 重放、工具调用):不该冲掉用户的 700。
store.set(atoms.setActivityTabDefaultWidthAtom, { tab: CANVAS, width: "max" });
expect(width()).toBe(700);
show("todo");
expect(width()).toBe(360);
});
});
describe("临时宽度(标签卡内部切换模式)", () => {
beforeEach(() => {
installStorage();
setWindowWidth(1200);
});
it("只作用于当前卡,切走即失效,不落盘、不改用户宽度", async () => {
const { atoms, store, show, width } = await panel("git");
store.set(atoms.setActivityPanelWidthAtom, "max");
expect(width()).toBe(maxAt(1200));
show("todo");
expect(width()).toBe(360);
show("git");
expect(width()).toBe(360);
expect(localStorage.getItem(atoms.ACTIVITY_PANEL_WIDTHS_STORAGE_KEY)).toBeNull();
});
it("可以先给还没选中的卡定好,切过去才生效(文件预览从聊天附件打开)", async () => {
const { atoms, store, show, width } = await panel(CANVAS);
store.set(atoms.setActivityTabTransientWidthAtom, { tab: "file", width: "max" });
show("file");
expect(width()).toBe(maxAt(1200));
store.set(atoms.setActivityTabTransientWidthAtom, { tab: "file", width: null });
expect(width()).toBe(360);
});
it("用户拖动压过当前卡的临时宽度", async () => {
const { atoms, store, width, drag } = await panel("git");
store.set(atoms.setActivityPanelWidthAtom, "max");
drag(500);
expect(width()).toBe(500);
expect(store.get(atoms.activityPanelWidthModeAtom)).toEqual({ kind: "fixed", px: 500 });
});
it("tab 拖拽期间挂起,结束时只应用最后一次请求", async () => {
const { atoms, store, width } = await panel("git");
store.set(atoms.setActivityPanelTabDraggingAtom, true);
store.set(atoms.setActivityPanelWidthAtom, 600);
store.set(atoms.setActivityPanelWidthAtom, "max");
expect(width()).toBe(360);
store.set(atoms.setActivityPanelTabDraggingAtom, false);
expect(store.get(atoms.activityPanelWidthModeAtom)).toEqual({ kind: "max" });
});
});
describe("窗口尺寸与阈值", () => {
beforeEach(() => {
installStorage();
setWindowWidth(1200);
});
it("拉满的卡跟着窗口涨落", async () => {
const { atoms, store, width } = await panel(CANVAS);
setWindowWidth(1600);
store.set(atoms.syncActivityPanelWidthToWindowAtom, 1600);
expect(width()).toBe(maxAt(1600));
setWindowWidth(900);
store.set(atoms.syncActivityPanelWidthToWindowAtom, 900);
expect(width()).toBe(maxAt(900));
});
it("固定宽度在窗口变窄时夹紧、变宽时回到用户指定的宽度", async () => {
setWindowWidth(1600);
const { atoms, store, width, drag } = await panel("todo");
drag(900);
setWindowWidth(1000);
store.set(atoms.syncActivityPanelWidthToWindowAtom, 1000);
expect(width()).toBe(maxAt(1000));
setWindowWidth(1600);
store.set(atoms.syncActivityPanelWidthToWindowAtom, 1600);
expect(width()).toBe(900);
});
it("拖拽中的瞬时宽度不落盘,松手才记下", async () => {
const { atoms, store, width } = await panel("todo");
store.set(atoms.setTransientActivityPanelWidthAtom, 540);
expect(width()).toBe(540);
expect(localStorage.getItem(atoms.ACTIVITY_PANEL_WIDTHS_STORAGE_KEY)).toBeNull();
store.set(atoms.persistActivityPanelWidthAtom);
expect(JSON.parse(localStorage.getItem(atoms.ACTIVITY_PANEL_WIDTHS_STORAGE_KEY) ?? "{}")).toEqual({
shared: 540,
});
});
it("预览可用状态只在跨过宽度阈值时通知内容消费者", async () => {
const atoms = await loadAtoms();
const store = createStore();
const changes: boolean[] = [];
const unsubscribe = store.sub(atoms.activityPanelPreviewAvailableAtom, () => {
changes.push(store.get(atoms.activityPanelPreviewAvailableAtom));
});
store.set(atoms.syncActivityPanelPreviewAvailabilityAtom, 400);
store.set(atoms.syncActivityPanelPreviewAvailabilityAtom, 519);
expect(changes).toEqual([]);
store.set(atoms.syncActivityPanelPreviewAvailabilityAtom, 520);
store.set(atoms.syncActivityPanelPreviewAvailabilityAtom, 700);
store.set(atoms.syncActivityPanelPreviewAvailabilityAtom, 521);
expect(changes).toEqual([true]);
store.set(atoms.syncActivityPanelPreviewAvailabilityAtom, 519);
expect(changes).toEqual([true, false]);
unsubscribe();
});
});
describe("改版前的记录", () => {
it("旧版存的像素宽度迁移成普通卡的用户宽度", async () => {
installStorage({ "vetta-activity-panel-width": "820" });
setWindowWidth(1600);
const { show, width } = await panel("todo");
expect(width()).toBe(820);
show(CANVAS);
expect(width()).toBe(maxAt(1600));
});
// 旧版的「拉满」几乎都是画布、浏览器替用户设的,带过来正是这次要修的问题。
it('旧版的 "max" 不迁移,普通卡回到默认宽度', async () => {
installStorage({ "vetta-activity-panel-width": "max" });
setWindowWidth(1600);
const { width } = await panel("todo");
expect(width()).toBe(360);
});
it("新记录已存在时不再看旧记录", async () => {
installStorage({
"vetta-activity-panel-width": "820",
"vetta-activity-panel-widths.v1": JSON.stringify({ shared: 500 }),
});
const { width } = await panel("todo");
expect(width()).toBe(500);
});
it("新记录损坏时按默认宽度开始", async () => {
installStorage({ "vetta-activity-panel-widths.v1": "{oops" });
const { width } = await panel("todo");
expect(width()).toBe(360);
});
});