This commit is contained in:
DrSmoothl committed 2026-10-12 15:43:34 +08:00
1 parent effeb66554
commit 7c0519f75f
977 files changed
+198426 -158324

No files matched your search

+41 -41
View File
@@ -8,53 +8,53 @@ import { cleanupInvalidPerUinConfigs } from '../src/config';
// config/onebot_<garbage>.json. Startup cleanup must delete exactly those and
// never touch legitimate per-UIN configs or the global onebot.json.
describe('cleanupInvalidPerUinConfigs', () => {
let tempDir: string;
let prevCwd: string;
let tempDir: string;
let prevCwd: string;
beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'snowluma-config-cleanup-'));
prevCwd = process.cwd();
process.chdir(tempDir);
fs.mkdirSync('config', { recursive: true });
});
beforeEach(() => {
tempDir = fs.mkdtempSync(path.join(os.tmpdir(), 'snowluma-config-cleanup-'));
prevCwd = process.cwd();
process.chdir(tempDir);
fs.mkdirSync('config', { recursive: true });
});
afterEach(() => {
process.chdir(prevCwd);
fs.rmSync(tempDir, { recursive: true, force: true });
});
afterEach(() => {
process.chdir(prevCwd);
fs.rmSync(tempDir, { recursive: true, force: true });
});
const write = (name: string) => fs.writeFileSync(path.join('config', name), '{}');
const exists = (name: string) => fs.existsSync(path.join('config', name));
const write = (name: string) => fs.writeFileSync(path.join('config', name), '{}');
const exists = (name: string) => fs.existsSync(path.join('config', name));
it('deletes only per-UIN files whose UIN fails validation', () => {
write('onebot.json'); // global — must survive
write('onebot_123456789.json'); // legit 9-digit — must survive
write('onebot_4294967295.json'); // legit 10-digit — must survive
write('onebot_1701414379536.json'); // 13-digit phantom — must go
write('onebot_12345678901.json'); // 11-digit phantom — must go
write('snowluma.json'); // unrelated global — must survive
it('deletes only per-UIN files whose UIN fails validation', () => {
write('onebot.json'); // global — must survive
write('onebot_123456789.json'); // legit 9-digit — must survive
write('onebot_4294967295.json'); // legit 10-digit — must survive
write('onebot_1701414379536.json'); // 13-digit phantom — must go
write('onebot_12345678901.json'); // 11-digit phantom — must go
write('snowluma.json'); // unrelated global — must survive
const removed = cleanupInvalidPerUinConfigs().sort();
const removed = cleanupInvalidPerUinConfigs().sort();
expect(removed).toEqual(['onebot_12345678901.json', 'onebot_1701414379536.json']);
expect(exists('onebot.json')).toBe(true);
expect(exists('onebot_123456789.json')).toBe(true);
expect(exists('onebot_4294967295.json')).toBe(true);
expect(exists('snowluma.json')).toBe(true);
expect(exists('onebot_1701414379536.json')).toBe(false);
expect(exists('onebot_12345678901.json')).toBe(false);
});
expect(removed).toEqual(['onebot_12345678901.json', 'onebot_1701414379536.json']);
expect(exists('onebot.json')).toBe(true);
expect(exists('onebot_123456789.json')).toBe(true);
expect(exists('onebot_4294967295.json')).toBe(true);
expect(exists('snowluma.json')).toBe(true);
expect(exists('onebot_1701414379536.json')).toBe(false);
expect(exists('onebot_12345678901.json')).toBe(false);
});
it('is a no-op when there are no phantom files', () => {
write('onebot.json');
write('onebot_123456789.json');
expect(cleanupInvalidPerUinConfigs()).toEqual([]);
expect(exists('onebot_123456789.json')).toBe(true);
});
it('is a no-op when there are no phantom files', () => {
write('onebot.json');
write('onebot_123456789.json');
expect(cleanupInvalidPerUinConfigs()).toEqual([]);
expect(exists('onebot_123456789.json')).toBe(true);
});
it('does not throw when the config dir is absent', () => {
fs.rmSync('config', { recursive: true, force: true });
expect(() => cleanupInvalidPerUinConfigs()).not.toThrow();
expect(cleanupInvalidPerUinConfigs()).toEqual([]);
});
it('does not throw when the config dir is absent', () => {
fs.rmSync('config', { recursive: true, force: true });
expect(() => cleanupInvalidPerUinConfigs()).not.toThrow();
expect(cleanupInvalidPerUinConfigs()).toEqual([]);
});
});