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 * as assert from "node:assert/strict";
import test from "node:test";
import {
detectCodeTabSpaces,
getCodeBlockOutdentRange,
getCodeBlockLineRange,
getCodeTabSpace,
getCodeTabSpacesLabel,
resolveCodeTabSpaces,
updateCodeBlockLines,
} from "./codeBlockUtil";
test("code block Tab spaces inherit the global value unless explicitly overridden", () => {
assert.equal(resolveCodeTabSpaces(null, 4), 4);
assert.equal(resolveCodeTabSpaces("", 4), 4);
assert.equal(resolveCodeTabSpaces("3", 4), 4);
assert.equal(resolveCodeTabSpaces("0", 4), 0);
assert.equal(resolveCodeTabSpaces("2", 4), 2);
assert.equal(resolveCodeTabSpaces("8", 4), 8);
assert.equal(getCodeTabSpace(0), "\t");
assert.equal(getCodeTabSpace(4), " ");
});
test("code block Tab spaces labels share one wording across settings and menus", () => {
const descriptor = Object.getOwnPropertyDescriptor(globalThis, "window");
Object.defineProperty(globalThis, "window", {configurable: true, value: {siyuan: {languages: {
codeTabSpacesCount: "${x} spaces", codeTabSpacesKeepTab: "Keep tab"}}}});
try {
assert.equal(getCodeTabSpacesLabel(0), "Keep tab");
for (const spaces of [2, 4, 6, 8]) {
assert.equal(getCodeTabSpacesLabel(spaces), `${spaces} spaces`);
}
} finally {
if (descriptor) {
Object.defineProperty(globalThis, "window", descriptor);
} else {
Reflect.deleteProperty(globalThis, "window");
}
}
});
test("code block indentation detection recognizes consistent tabs and supported space widths", () => {
for (const spaces of [2, 4, 6, 8]) {
const indent = " ".repeat(spaces);
assert.equal(detectCodeTabSpaces(`root\n${indent}child\n${indent}${indent}nested\nend`), spaces);
assert.equal(detectCodeTabSpaces(`${indent}child\n${indent}${indent}nested`), spaces);
}
assert.equal(detectCodeTabSpaces("root\n\tchild\n\t\tnested\nend"), 0);
assert.equal(detectCodeTabSpaces("root\n \t \n child\n\t\n nested"), 2);
});
test("code block indentation detection declines mixed, inconsistent and insufficient samples", () => {
for (const text of ["", "\n \t\n", "root\nend", " child\n", "\tchild",
"root\n \tchild", "root\n\t child", "root\n child\n\tnested", "root\n\tchild\n nested",
"root\n child\n aligned", "root\n child\n nested", "root\n child"]) {
assert.equal(detectCodeTabSpaces(text), null, JSON.stringify(text));
}
});
test("detected indentation preserves pasted code levels and whitespace during explicit operations", () => {
const text = "if ready:\n if enabled:\n run( value)\n";
const tabSpace = getCodeTabSpace(detectCodeTabSpaces(text));
const selected = " if enabled:\n run( value)";
assert.equal(updateCodeBlockLines(selected, tabSpace), " if enabled:\n run( value)");
assert.equal(updateCodeBlockLines(selected, tabSpace, true), "if enabled:\n run( value)");
});
test("code block selections start at the first touched line and exclude a trailing line start", () => {
const text = "first\nsecond\nthird\n";
assert.deepEqual(getCodeBlockLineRange(text, 2, 10), {start: 0, end: 10});
assert.deepEqual(getCodeBlockLineRange(text, 2, 13), {start: 0, end: 12});
assert.deepEqual(getCodeBlockLineRange(text, 6, 12), {start: 6, end: 12});
assert.deepEqual(getCodeBlockLineRange(text, 0, text.length), {start: 0, end: text.length - 1});
assert.deepEqual(getCodeBlockLineRange("\nfirst", 0, 1), {start: 0, end: 0});
});
test("code block line indentation handles spaces, tabs, partial indentation and blank lines", () => {
assert.equal(updateCodeBlockLines("one\ntwo", " "), " one\n two");
assert.equal(updateCodeBlockLines("one\n\ntwo", "\t"), "\tone\n\t\n\ttwo");
assert.equal(updateCodeBlockLines(" one\n two\n\tthree\nfour", " ", true),
"one\ntwo\nthree\nfour");
assert.equal(updateCodeBlockLines(" one\n\ttwo", "\t", true), "one\ntwo");
});
test("tab mode outdents pasted spaces by up to four columns without changing code whitespace", () => {
const text = " if ready:\n run()\n partial\n\t\tnested\n \t mixed\n \n\nvalue tail\t";
assert.equal(updateCodeBlockLines(text, "\t", true),
"if ready:\n run()\npartial\n\tnested\n\t mixed\n\n\nvalue tail\t");
assert.equal(updateCodeBlockLines(updateCodeBlockLines(text, "\t"), "\t", true), text);
});
const outdentAtCaret = (text: string, caret: number, tabSpace = " ") => {
const range = getCodeBlockOutdentRange(text, caret, tabSpace);
return {text: text.substring(0, range.start) + text.substring(range.end), caret: range.caret};
};
test("collapsed code block outdent removes leading indentation from any caret position", () => {
for (const tabSpace of [" ", "\t"]) {
for (const [caret, expected] of [[0, 0], [2, 0], [4, 0], [6, 2], [9, 5]]) {
assert.deepEqual(outdentAtCaret(" value", caret, tabSpace), {text: "value", caret: expected});
}
assert.deepEqual(outdentAtCaret(" value", 5, tabSpace), {text: " value", caret: 1});
}
assert.deepEqual(outdentAtCaret(" value", 7), {text: "value", caret: 5});
assert.deepEqual(outdentAtCaret(" value", 9, " "), {text: " value", caret: 7});
});
test("collapsed code block outdent uses the configured unit and preserves other whitespace", () => {
assert.deepEqual(outdentAtCaret("\tvalue", 6), {text: "value", caret: 5});
assert.deepEqual(outdentAtCaret("\t\tvalue", 7, "\t"), {text: "\tvalue", caret: 6});
assert.deepEqual(outdentAtCaret(" value", 7, "\t"), {text: "value", caret: 5});
assert.deepEqual(outdentAtCaret(" \tvalue", 8), {text: "\tvalue", caret: 6});
assert.deepEqual(outdentAtCaret("value ", 9), {text: "value ", caret: 9});
assert.deepEqual(outdentAtCaret(" value tail\t", 16), {text: "value tail\t", caret: 12});
});
test("collapsed code block outdent changes only the current line, including blank lines", () => {
assert.deepEqual(outdentAtCaret(" first\n value\n last", 10),
{text: " first\nvalue\n last", caret: 10});
assert.deepEqual(outdentAtCaret(" first\n value\n last", 19),
{text: " first\nvalue\n last", caret: 15});
assert.deepEqual(outdentAtCaret("value\n \nnext", 7), {text: "value\n\nnext", caret: 6});
assert.deepEqual(outdentAtCaret("value\n\n next", 6), {text: "value\n\n next", caret: 6});
assert.deepEqual(outdentAtCaret("value\n", 6), {text: "value\n", caret: 6});
assert.deepEqual(outdentAtCaret("", 0), {text: "", caret: 0});
});