mirror of
https://github.com/Youzini-afk/ST-Bionic-Memory-Ecology.git
synced 2026-05-15 22:30:38 +08:00
Add native hide support for old-message hiding
This commit is contained in:
@@ -8,13 +8,17 @@ import {
|
||||
unhideAll,
|
||||
} from "../hide-engine.js";
|
||||
|
||||
function createRuntime(chat) {
|
||||
function createRuntime(chat, chatId = "chat-a") {
|
||||
const commands = [];
|
||||
const domWrites = [];
|
||||
return {
|
||||
chat,
|
||||
chatId,
|
||||
commands,
|
||||
domWrites,
|
||||
getContext() {
|
||||
return { chat: this.chat };
|
||||
async executeSlashCommands(command) {
|
||||
commands.push(command);
|
||||
return "";
|
||||
},
|
||||
$(selector) {
|
||||
return {
|
||||
@@ -23,62 +27,115 @@ function createRuntime(chat) {
|
||||
},
|
||||
};
|
||||
},
|
||||
getContext() {
|
||||
return {
|
||||
chat: this.chat,
|
||||
chatId: this.chatId,
|
||||
executeSlashCommands: this.executeSlashCommands.bind(this),
|
||||
};
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
function testApplyAndUnhidePreservesOriginalSystemMessages() {
|
||||
async function testApplyUsesNativeHide() {
|
||||
resetHideState();
|
||||
const chat = [
|
||||
{ mes: "原系统", is_system: true },
|
||||
{ mes: "用户1", is_user: true, is_system: false },
|
||||
{ mes: "助手1", is_user: false, is_system: false },
|
||||
{ mes: "用户2", is_user: true, is_system: false },
|
||||
{ mes: "助手2", is_user: false, is_system: false },
|
||||
{ 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 applyResult = applyHideSettings(
|
||||
const result = await applyHideSettings(
|
||||
{ enabled: true, hide_last_n: 2 },
|
||||
runtime,
|
||||
);
|
||||
assert.equal(applyResult.active, true);
|
||||
|
||||
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.equal(chat[4].is_system, false);
|
||||
assert.equal(applyResult.managedCount, 2);
|
||||
|
||||
const unhideResult = unhideAll(runtime);
|
||||
assert.equal(unhideResult.active, false);
|
||||
assert.equal(chat[0].is_system, true, "原系统消息不应被恢复");
|
||||
assert.equal(chat[1].is_system, false);
|
||||
assert.equal(chat[2].is_system, false);
|
||||
assert.deepEqual(getHideStateSnapshot(), {
|
||||
hasManagedChat: true,
|
||||
managedHiddenCount: 3,
|
||||
lastProcessedLength: 5,
|
||||
scheduled: false,
|
||||
});
|
||||
}
|
||||
|
||||
function testResetRestoresPreviousManagedChat() {
|
||||
const oldChat = [
|
||||
{ mes: "用户1", is_user: true, is_system: false },
|
||||
{ mes: "助手1", is_user: false, is_system: false },
|
||||
{ mes: "用户2", is_user: true, is_system: false },
|
||||
{ mes: "助手2", is_user: false, is_system: 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 newChat = [
|
||||
{ mes: "新用户", is_user: true, is_system: false },
|
||||
{ mes: "新助手", 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, 3);
|
||||
assert.deepEqual(runtime.commands, ["/unhide 0-2"]);
|
||||
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(oldChat);
|
||||
const runtime = createRuntime(chat);
|
||||
|
||||
applyHideSettings({ enabled: true, hide_last_n: 1 }, runtime);
|
||||
assert.equal(oldChat[0].is_system, true);
|
||||
assert.equal(oldChat[1].is_system, true);
|
||||
assert.equal(oldChat[2].is_system, true);
|
||||
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;
|
||||
|
||||
runtime.chat = newChat;
|
||||
resetHideState(runtime);
|
||||
|
||||
assert.equal(oldChat[0].is_system, false);
|
||||
assert.equal(oldChat[1].is_system, false);
|
||||
assert.equal(oldChat[2].is_system, false);
|
||||
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,
|
||||
@@ -87,33 +144,9 @@ function testResetRestoresPreviousManagedChat() {
|
||||
});
|
||||
}
|
||||
|
||||
function testIncrementalHideOnlyHidesNewOverflowMessages() {
|
||||
const chat = [
|
||||
{ mes: "用户1", is_user: true, is_system: false },
|
||||
{ mes: "助手1", is_user: false, is_system: false },
|
||||
{ mes: "用户2", is_user: true, is_system: false },
|
||||
];
|
||||
const runtime = createRuntime(chat);
|
||||
|
||||
applyHideSettings({ enabled: true, hide_last_n: 2 }, runtime);
|
||||
assert.equal(chat[0].is_system, true);
|
||||
assert.equal(chat[1].is_system, false);
|
||||
assert.equal(chat[2].is_system, false);
|
||||
|
||||
chat.push({ mes: "助手2", is_user: false, is_system: false });
|
||||
const result = runIncrementalHideCheck(
|
||||
{ enabled: true, hide_last_n: 2 },
|
||||
runtime,
|
||||
);
|
||||
assert.equal(result.incremental, true);
|
||||
assert.equal(result.hiddenCount, 1);
|
||||
assert.equal(chat[1].is_system, true);
|
||||
assert.equal(chat[2].is_system, false);
|
||||
assert.equal(chat[3].is_system, false);
|
||||
}
|
||||
|
||||
testApplyAndUnhidePreservesOriginalSystemMessages();
|
||||
testResetRestoresPreviousManagedChat();
|
||||
testIncrementalHideOnlyHidesNewOverflowMessages();
|
||||
await testApplyUsesNativeHide();
|
||||
await testDisableUnhidesManagedRange();
|
||||
await testIncrementalOnlyHidesOverflowDelta();
|
||||
await testResetClearsStateWithoutIssuingCommands();
|
||||
|
||||
console.log("hide-engine tests passed");
|
||||
|
||||
Reference in New Issue
Block a user