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 {describe, it} from "node:test";
import * as assert from "node:assert/strict";
import {
endsWithMultiCharHintPrefix,
getBlockHintTriggerOffset,
getBlockRefStaticText,
isBlockHintQueryAtCaret,
shouldAutoCloseRefBrackets,
shouldCaptureHintUndoFocus,
shouldIgnoreHintTrigger,
stripBlockHintCloseKey,
} from "./blockHintRange";
describe("getBlockHintTriggerOffset", () => {
it("uses the latest overlapping trigger inside existing closing markers", () => {
assert.equal(getBlockHintTriggerOffset("[[[", "]]", "[[", "]]"), 1);
assert.equal(getBlockHintTriggerOffset("【【【query", "】】", "【【", "】】"), 1);
});
it("replaces exactly the latest trigger in the reported scenario", () => {
const text = "[[[]]";
const caretOffset = 3;
const triggerOffset = getBlockHintTriggerOffset(text.substring(0, caretOffset),
text.substring(caretOffset), "[[", "]]");
assert.equal(text.substring(0, triggerOffset) + "reference" +
text.substring(caretOffset), "[reference]]");
});
it("keeps the first trigger when the query starts with an opening marker", () => {
const text = "[[[电影自习室] 全集";
const triggerOffset = getBlockHintTriggerOffset(text, "", "[[", "]]");
assert.equal(triggerOffset, 0);
assert.equal(text.substring(triggerOffset + 2), "[电影自习室] 全集");
assert.equal(getBlockHintTriggerOffset("[[[", "]", "[[", "]]"), 0);
assert.equal(getBlockHintTriggerOffset("【【【", "】", "【【", "】】"), 0);
});
it("extracts the query after the latest overlapping trigger", () => {
const text = "[[[query";
const triggerOffset = getBlockHintTriggerOffset(text, "]]", "[[", "]]");
assert.equal(text.substring(triggerOffset + 2), "query");
});
it("preserves text after the caret when replacing a block hint", () => {
const text = "[[query]]tail";
const caretOffset = 7;
const triggerOffset = getBlockHintTriggerOffset(text.substring(0, caretOffset),
text.substring(caretOffset), "[[", "]]");
assert.equal(text.substring(0, triggerOffset) + "reference" + text.substring(caretOffset),
"reference]]tail");
});
it("includes manually entered closing markers in the query", () => {
const text = "[[foo]]";
const triggerOffset = getBlockHintTriggerOffset(text, "", "[[", "]]");
assert.equal(text.substring(triggerOffset + 2), "foo]]");
});
});
describe("isBlockHintQueryAtCaret", () => {
it("recognizes an unfinished query around the caret", () => {
assert.equal(isBlockHintQueryAtCaret("prefix [[que", "ry", "[[", "]]", 512), true);
assert.equal(isBlockHintQueryAtCaret("prefix ((query", "", "((", "))", 512), true);
});
it("keeps a query editable before its closing marker", () => {
assert.equal(isBlockHintQueryAtCaret("[[que", "ry]]", "[[", "]]", 512), true);
});
it("rejects text outside a valid query", () => {
assert.equal(isBlockHintQueryAtCaret("plain text", "", "[[", "]]", 512), false);
assert.equal(isBlockHintQueryAtCaret("[[ query", "", "[[", "]]", 512), false);
assert.equal(isBlockHintQueryAtCaret("[[12345", "", "[[", "]]", 5), false);
});
});
describe("getBlockRefStaticText", () => {
it("preserves the complete toolbar selection", () => {
assert.equal(getBlockRefStaticText("旧的开始", "((", false), "旧的开始");
assert.equal(getBlockRefStaticText("((literal", "((", false), "((literal");
});
it("removes the trigger from an inline block hint", () => {
assert.equal(getBlockRefStaticText("[[旧的开始", "[[", true), "旧的开始");
assert.equal(getBlockRefStaticText("((query", "((", true), "query");
});
it("removes a manually typed closing marker from an inline block hint", () => {
assert.equal(getBlockRefStaticText("[[Title]]", "[[", true, "]]"), "Title");
assert.equal(getBlockRefStaticText("【【标题】】", "【【", true, "】】"), "标题");
assert.equal(getBlockRefStaticText("[[Title]]", "[[", false, "]]"), "[[Title]]");
});
});
describe("stripBlockHintCloseKey", () => {
it("removes only a complete trailing closing marker", () => {
assert.equal(stripBlockHintCloseKey("Title]]", "]]"), "Title");
assert.equal(stripBlockHintCloseKey("query))", "))"), "query");
assert.equal(stripBlockHintCloseKey("Title]", "]]"), "Title]");
assert.equal(stripBlockHintCloseKey("[draft] notes", "]]"), "[draft] notes");
assert.equal(stripBlockHintCloseKey("]]", "]]"), "");
assert.equal(stripBlockHintCloseKey("Title]]", undefined), "Title]]");
});
});
describe("shouldIgnoreHintTrigger", () => {
const blockHintKeys = ["((", "[[", "((", "【【"];
it("keeps block reference queries intact when slash hints appear inside them", () => {
assert.equal(shouldIgnoreHintTrigger("[[", "、", blockHintKeys), true);
assert.equal(shouldIgnoreHintTrigger("((", "/", blockHintKeys), true);
assert.equal(shouldIgnoreHintTrigger("[[", "#", blockHintKeys), true);
assert.equal(shouldIgnoreHintTrigger("[[", ":", blockHintKeys), true);
});
it("does not block unrelated hint contexts", () => {
assert.equal(shouldIgnoreHintTrigger("", "、", blockHintKeys), false);
assert.equal(shouldIgnoreHintTrigger("#", "、", blockHintKeys), true);
assert.equal(shouldIgnoreHintTrigger("、", "[[", blockHintKeys), false);
});
});
describe("shouldCaptureHintUndoFocus", () => {
const blockHintKeys = ["((", "[[", "((", "【【"];
it("captures block hint focus in all editors", () => {
assert.equal(shouldCaptureHintUndoFocus("[[", blockHintKeys), true);
});
it("captures slash hint focus in all editors", () => {
assert.equal(shouldCaptureHintUndoFocus("/", blockHintKeys), true);
assert.equal(shouldCaptureHintUndoFocus("、", blockHintKeys), true);
});
it("captures slash emoji focus in regular editors", () => {
assert.equal(shouldCaptureHintUndoFocus("/", blockHintKeys, "emoji"), true);
assert.equal(shouldCaptureHintUndoFocus("、", blockHintKeys, "emoji"), true);
});
it("does not change unrelated hint focus handling", () => {
assert.equal(shouldCaptureHintUndoFocus("#", blockHintKeys), false);
});
});
describe("endsWithMultiCharHintPrefix", () => {
const hintKeys = ["((", "【【", "[[", "{{", "#", "/", "、", ":"];
it("ends the current hint when another multi-character hint starts", () => {
assert.equal(endsWithMultiCharHintPrefix("2【", hintKeys), true);
assert.equal(endsWithMultiCharHintPrefix("query[", hintKeys), true);
});
it("keeps the current hint active for ordinary query text", () => {
assert.equal(endsWithMultiCharHintPrefix("2级", hintKeys), false);
assert.equal(endsWithMultiCharHintPrefix("2", hintKeys), false);
});
});
describe("auto-closing reference brackets", () => {
it("inserts closing brackets only right after a new [[", () => {
assert.equal(shouldAutoCloseRefBrackets("note [[", ""), true);
assert.equal(shouldAutoCloseRefBrackets("[[", " tail"), true);
assert.equal(shouldAutoCloseRefBrackets("[[", "]]"), false);
assert.equal(shouldAutoCloseRefBrackets("[", ""), false);
assert.equal(shouldAutoCloseRefBrackets("[[Title", ""), false);
assert.equal(shouldAutoCloseRefBrackets("note 【【", "", "【【"), true);
assert.equal(shouldAutoCloseRefBrackets("【【", "】】", "【【"), false);
assert.equal(shouldAutoCloseRefBrackets("【【", "】", "【【"), false);
assert.equal(shouldAutoCloseRefBrackets("【", "", "【【"), false);
assert.equal(shouldAutoCloseRefBrackets("【【标题", "", "【【"), false);
});
});