Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

5.3 KB
Raw Blame
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
import {
    collapseAVCellSelectionToAnchor,
    findAVItemPointIndex,
    getAVCellSelection,
    getAVItemSelection,
    getAVSelectionRoot,
    reconcileAVSelectedItemIDs,
    resolveAVSelectedCell,
    setAVCellSelection,
    setAVItemAnchorState,
    setAVItemSelectionState,
} from "./selectionState";

describe("database range selection helpers", () => {
    it("resolves the selection root from an item instead of requiring a block ancestor", () => {
        const root = {} as HTMLElement;
        const item = {
            closest: (selector: string) => {
                assert.equal(selector, "[data-av-id][data-av-type]");
                return root;
            },
        } as unknown as HTMLElement;

        assert.equal(getAVSelectionRoot(item), root);
        assert.equal(getAVSelectionRoot(), undefined);
    });

    it("locates duplicate items by both group and item ID", () => {
        const items = [
            {groupID: "group-a", itemID: "item-1"},
            {groupID: "group-b", itemID: "item-1"},
            {groupID: "group-b", itemID: "item-2"},
        ];

        assert.equal(findAVItemPointIndex(items, "item-1", "group-b"), 1);
        assert.equal(findAVItemPointIndex(items, "item-1", "group-a"), 0);
        assert.equal(findAVItemPointIndex(items, "item-1", "group-c"), -1);
    });

    it("drops selected items that are absent from the current view", () => {
        const selectedIDs = reconcileAVSelectedItemIDs(
            ["item-1", "item-3"], ["item-1", "item-2", "item-3"]);

        assert.deepEqual(Array.from(selectedIDs), ["item-1", "item-3"]);
    });

    it("collapses a cell range to its anchor after columns are sorted", () => {
        const blockElement = {} as HTMLElement;
        const anchor = {groupID: "group-a", rowID: "row-1", colID: "col-1"};
        const anchorCell = {
            ...anchor,
            rowIndex: 0,
            colIndex: 0,
            cell: {} as IAVCell,
            column: {} as IAVColumn,
        };
        const focusCell = {
            groupID: "group-a",
            rowID: "row-2",
            colID: "col-2",
            rowIndex: 1,
            colIndex: 1,
            cell: {} as IAVCell,
            column: {} as IAVColumn,
        };
        setAVCellSelection(blockElement, {
            anchor,
            focus: {
                groupID: focusCell.groupID,
                rowID: focusCell.rowID,
                colID: focusCell.colID,
            },
            cells: [anchorCell, focusCell],
        });

        collapseAVCellSelectionToAnchor(blockElement);

        assert.deepEqual(getAVCellSelection(blockElement), {
            anchor,
            focus: anchor,
            cells: [anchorCell],
        });
    });

    it("clears the cell selection when an item selection starts", () => {
        const blockElement = {} as HTMLElement;
        const point = {groupID: "group-a", rowID: "row-1", colID: "col-1"};
        setAVCellSelection(blockElement, {
            anchor: point,
            focus: point,
            cells: [],
        });

        setAVItemAnchorState(blockElement, "row-2", "group-a");

        assert.equal(getAVCellSelection(blockElement), undefined);
        assert.deepEqual(getAVItemSelection(blockElement), {
            anchorID: "row-2",
            anchorGroupID: "group-a",
            focusID: "row-2",
            focusGroupID: "group-a",
        });
    });

    it("clears the item selection when a cell selection starts", () => {
        const blockElement = {} as HTMLElement;
        setAVItemSelectionState(blockElement, {
            anchorID: "row-1",
            anchorGroupID: "group-a",
            focusID: "row-2",
            focusGroupID: "group-a",
        });
        const point = {groupID: "group-a", rowID: "row-1", colID: "col-1"};

        setAVCellSelection(blockElement, {
            anchor: point,
            focus: point,
            cells: [],
        });

        assert.equal(getAVItemSelection(blockElement), undefined);
        assert.deepEqual(getAVCellSelection(blockElement), {
            anchor: point,
            focus: point,
            cells: [],
        });
    });

    it("resolves a captured cell identity against the current database data", () => {
        const previousCell = {id: "old-cell"} as IAVCell;
        const currentCell = {id: "current-cell"} as IAVCell;
        const column = {id: "asset-column", type: "mAsset", hidden: false} as IAVColumn;
        const capturedCell = {
            groupID: "",
            rowID: "row-1",
            colID: "asset-column",
            rowIndex: 0,
            colIndex: 0,
            cell: previousCell,
            column,
        };
        const data = {
            view: {
                id: "view",
                groups: [],
                columns: [column],
                rows: [{id: "row-1", cells: [currentCell]}],
            },
        } as unknown as IAV;

        const resolved = resolveAVSelectedCell(data, capturedCell);

        assert.equal(resolved?.cell, currentCell);
        assert.notEqual(resolved?.cell, previousCell);
        assert.equal(resolveAVSelectedCell(data, {
            groupID: "",
            rowID: "missing-row",
            colID: "asset-column",
        }), undefined);
    });
});