mirror of
https://github.com/Youzini-afk/ST-Bionic-Memory-Ecology.git
synced 2026-05-15 22:30:38 +08:00
153 lines
4.4 KiB
JavaScript
153 lines
4.4 KiB
JavaScript
import assert from "node:assert/strict";
|
|
|
|
import {
|
|
applyHideSettings,
|
|
getHideStateSnapshot,
|
|
resetHideState,
|
|
runIncrementalHideCheck,
|
|
unhideAll,
|
|
} from "../hide-engine.js";
|
|
|
|
function createRuntime(chat, chatId = "chat-a") {
|
|
const commands = [];
|
|
const domWrites = [];
|
|
return {
|
|
chat,
|
|
chatId,
|
|
commands,
|
|
domWrites,
|
|
async executeSlashCommands(command) {
|
|
commands.push(command);
|
|
return "";
|
|
},
|
|
$(selector) {
|
|
return {
|
|
attr(name, value) {
|
|
domWrites.push({ selector, name, value });
|
|
},
|
|
};
|
|
},
|
|
getContext() {
|
|
return {
|
|
chat: this.chat,
|
|
chatId: this.chatId,
|
|
executeSlashCommands: this.executeSlashCommands.bind(this),
|
|
};
|
|
},
|
|
};
|
|
}
|
|
|
|
async function testApplyUsesNativeHide() {
|
|
resetHideState();
|
|
const chat = [
|
|
{ mes: "user-1", is_user: true, is_system: false },
|
|
{ mes: "assistant-1", is_user: false, is_system: false },
|
|
{ mes: "user-2", is_user: true, is_system: false },
|
|
{ mes: "assistant-2", is_user: false, is_system: false },
|
|
{ mes: "user-3", is_user: true, is_system: false },
|
|
];
|
|
const runtime = createRuntime(chat);
|
|
|
|
const result = await applyHideSettings(
|
|
{ enabled: true, hide_last_n: 2 },
|
|
runtime,
|
|
);
|
|
|
|
assert.equal(result.active, true);
|
|
assert.equal(result.hiddenCount, 3);
|
|
assert.deepEqual(runtime.commands, ["/hide 0-2"]);
|
|
assert.equal(chat[0].is_system, true);
|
|
assert.equal(chat[1].is_system, true);
|
|
assert.equal(chat[2].is_system, true);
|
|
assert.equal(chat[3].is_system, false);
|
|
assert.deepEqual(getHideStateSnapshot(), {
|
|
hasManagedChat: true,
|
|
managedHiddenCount: 3,
|
|
lastProcessedLength: 5,
|
|
scheduled: false,
|
|
});
|
|
}
|
|
|
|
async function testDisableUnhidesManagedRange() {
|
|
resetHideState();
|
|
const chat = [
|
|
{ mes: "system", is_user: false, is_system: true },
|
|
{ mes: "assistant-1", is_user: false, is_system: false },
|
|
{ mes: "user-2", is_user: true, is_system: false },
|
|
{ mes: "assistant-2", is_user: false, is_system: false },
|
|
];
|
|
const runtime = createRuntime(chat);
|
|
|
|
await applyHideSettings({ enabled: true, hide_last_n: 1 }, runtime);
|
|
runtime.commands.length = 0;
|
|
|
|
const result = await unhideAll(runtime);
|
|
assert.equal(result.active, false);
|
|
assert.equal(result.shownCount, 4);
|
|
assert.deepEqual(runtime.commands, ["/unhide 0-3"]);
|
|
assert.equal(chat[0].is_system, true);
|
|
assert.equal(chat[1].is_system, false);
|
|
assert.equal(chat[2].is_system, false);
|
|
assert.equal(getHideStateSnapshot().managedHiddenCount, 0);
|
|
}
|
|
|
|
async function testIncrementalOnlyHidesOverflowDelta() {
|
|
resetHideState();
|
|
const chat = [
|
|
{ mes: "user-1", is_user: true, is_system: false },
|
|
{ mes: "assistant-1", is_user: false, is_system: false },
|
|
{ mes: "user-2", is_user: true, is_system: false },
|
|
];
|
|
const runtime = createRuntime(chat);
|
|
|
|
await applyHideSettings({ enabled: true, hide_last_n: 2 }, runtime);
|
|
runtime.commands.length = 0;
|
|
|
|
chat.push({ mes: "assistant-2", is_user: false, is_system: false });
|
|
const result = await runIncrementalHideCheck(
|
|
{ enabled: true, hide_last_n: 2 },
|
|
runtime,
|
|
);
|
|
|
|
assert.equal(result.incremental, true);
|
|
assert.equal(result.hiddenCount, 2);
|
|
assert.deepEqual(runtime.commands, ["/hide 1-1"]);
|
|
assert.equal(chat[0].is_system, true);
|
|
assert.equal(chat[1].is_system, true);
|
|
assert.equal(chat[2].is_system, false);
|
|
assert.equal(chat[3].is_system, false);
|
|
assert.equal(getHideStateSnapshot().managedHiddenCount, 2);
|
|
}
|
|
|
|
async function testResetClearsStateWithoutIssuingCommands() {
|
|
resetHideState();
|
|
const chat = [
|
|
{ mes: "user-1", is_user: true, is_system: false },
|
|
{ mes: "assistant-1", is_user: false, is_system: false },
|
|
{ mes: "user-2", is_user: true, is_system: false },
|
|
];
|
|
const runtime = createRuntime(chat);
|
|
|
|
await applyHideSettings({ enabled: true, hide_last_n: 1 }, runtime);
|
|
runtime.commands.length = 0;
|
|
|
|
resetHideState(runtime);
|
|
|
|
assert.deepEqual(runtime.commands, []);
|
|
assert.equal(chat[0].is_system, false);
|
|
assert.equal(chat[1].is_system, false);
|
|
assert.deepEqual(getHideStateSnapshot(), {
|
|
hasManagedChat: false,
|
|
managedHiddenCount: 0,
|
|
lastProcessedLength: 0,
|
|
scheduled: false,
|
|
});
|
|
}
|
|
|
|
await testApplyUsesNativeHide();
|
|
await testDisableUnhidesManagedRange();
|
|
await testIncrementalOnlyHidesOverflowDelta();
|
|
await testResetClearsStateWithoutIssuingCommands();
|
|
|
|
console.log("hide-engine tests passed");
|