* fix: give non-ASCII chat names distinct backup keys and throttles Backup filenames replace all non-alphanumeric characters with underscores, so names such as equal-length CJK ones collapsed to the same key and shared a single backup quota: busy chats evicted the backups of unrelated quiet ones. The backup key now appends a short hash of the raw name whenever the name contains non-ASCII characters, keeping keys distinct and stable while ASCII names keep their exact current keys and retention. The backup throttle was also keyed per user only, so rapid saves in one chat deferred or swallowed the backup of another chat saved in the same window. It is now keyed per user and chat. Fixes #5780 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * fix: replace stray NUL byte in backup throttle key separator A literal 0x00 byte slipped into the template string separator, which made Git treat the whole file as binary. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> * chore: reunite backupChat docblock with its function Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> --------- Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
79 lines
3.2 KiB
JavaScript
79 lines
3.2 KiB
JavaScript
import { beforeAll, afterAll, describe, test, expect } from '@jest/globals';
|
|
import fs from 'node:fs';
|
|
import os from 'node:os';
|
|
import path from 'node:path';
|
|
|
|
// chats.js reads these config values at module load. Environment variables take
|
|
// precedence over config.yaml, so setting them here lets the module import
|
|
// without a config file present. The short throttle interval keeps lodash
|
|
// timers from delaying the Jest process while still being long enough that a
|
|
// per-user (rather than per-chat) throttle would defer the second chat's backup.
|
|
process.env.SILLYTAVERN_BACKUPS_CHAT_ENABLED = 'true';
|
|
process.env.SILLYTAVERN_BACKUPS_CHAT_MAXTOTALBACKUPS = '-1';
|
|
process.env.SILLYTAVERN_BACKUPS_CHAT_THROTTLEINTERVAL = '200';
|
|
process.env.SILLYTAVERN_BACKUPS_CHAT_CHECKINTEGRITY = 'false';
|
|
process.env.SILLYTAVERN_BACKUPS_COMMON_NUMBEROFBACKUPS = '1';
|
|
|
|
/** @type {import('../src/endpoints/chats.js')} */
|
|
let chats;
|
|
let workDir;
|
|
|
|
beforeAll(async () => {
|
|
chats = await import('../src/endpoints/chats.js');
|
|
workDir = fs.mkdtempSync(path.join(os.tmpdir(), 'st-backups-'));
|
|
});
|
|
|
|
afterAll(() => {
|
|
fs.rmSync(workDir, { recursive: true, force: true });
|
|
});
|
|
|
|
/**
|
|
* Builds a minimal chat array like the client sends it.
|
|
* @param {string} message Message text
|
|
* @returns {object[]} Chat array
|
|
*/
|
|
function makeChat(message) {
|
|
return [
|
|
{ user_name: 'User', character_name: 'Char', chat_metadata: {} },
|
|
{ name: 'Char', is_user: false, mes: message },
|
|
];
|
|
}
|
|
|
|
describe('getBackupKey', () => {
|
|
test('keeps plain ASCII names unchanged', () => {
|
|
expect(chats.getBackupKey('Seraphina')).toBe('seraphina');
|
|
expect(chats.getBackupKey('Some Test')).toBe('some_test');
|
|
});
|
|
|
|
test('produces distinct stable keys for names that sanitize identically', () => {
|
|
const keyA = chats.getBackupKey('雷电将军');
|
|
const keyB = chats.getBackupKey('测试角色');
|
|
|
|
expect(keyA).not.toBe(keyB);
|
|
expect(chats.getBackupKey('雷电将军')).toBe(keyA);
|
|
});
|
|
});
|
|
|
|
describe('chat backups', () => {
|
|
test('chats with equal-length CJK names get independent backups and quotas', async () => {
|
|
const backupsDir = path.join(workDir, 'backups');
|
|
const chatsDir = path.join(workDir, 'chats');
|
|
fs.mkdirSync(backupsDir, { recursive: true });
|
|
fs.mkdirSync(chatsDir, { recursive: true });
|
|
|
|
// numberOfBackups is 1: with a shared backup key, the second chat's
|
|
// backup would evict the first; with a shared throttle, it would not be
|
|
// written at the leading edge at all
|
|
await chats.trySaveChat(makeChat('first'), path.join(chatsDir, 'a.jsonl'), true, 'test-user', '雷电将军', backupsDir);
|
|
await chats.trySaveChat(makeChat('second'), path.join(chatsDir, 'b.jsonl'), true, 'test-user', '测试角色', backupsDir);
|
|
|
|
const backupFiles = fs.readdirSync(backupsDir);
|
|
expect(backupFiles).toHaveLength(2);
|
|
|
|
const keyA = chats.getBackupKey('雷电将军');
|
|
const keyB = chats.getBackupKey('测试角色');
|
|
expect(backupFiles.some(f => f.startsWith(`chat_${keyA}_`))).toBe(true);
|
|
expect(backupFiles.some(f => f.startsWith(`chat_${keyB}_`))).toBe(true);
|
|
});
|
|
});
|