fix: 降低BangDream首次目录加载内存峰值

This commit is contained in:
sunlei 2026-06-16 06:59:59 +08:00
parent 52403c08a8
commit 4e5ed876c3
2 changed files with 70 additions and 2 deletions

View File

@ -111,9 +111,8 @@ let initialLoadPromise: Promise<void> | undefined;
function ensureCatalogInitialLoad() {
if (!initialLoadPromise) {
logger('catalog', 'initializing...');
initialLoadPromise = loadCatalogData(true).then(async () => {
initialLoadPromise = loadCatalogData(true).then(() => {
logger('catalog', 'initializing done');
await loadCatalogData();
});
}
return initialLoadPromise;

View File

@ -0,0 +1,69 @@
const mockGetJson = jest.fn<
Promise<Record<string, { path: string }>>,
[string, { cacheTime?: number }?]
>(async (path) => ({
1: { path },
}));
const mockReadJson = jest.fn(async () => ({}));
const mockReadExcelRows = jest.fn(async () => []);
const mockLog = jest.fn();
jest.mock(
'@/modules/qqbot/plugins/bangdream/src/infrastructure/integration/bestdori.provider',
() => ({
bangdreamBestdoriProvider: {
getJson: mockGetJson,
},
}),
);
jest.mock(
'@/modules/qqbot/plugins/bangdream/src/infrastructure/storage/static-patch.provider',
() => ({
bangdreamStaticPatchProvider: {
readExcelRows: mockReadExcelRows,
readJson: mockReadJson,
},
}),
);
jest.mock(
'@/modules/qqbot/plugins/bangdream/src/application/bangdream-logger',
() => ({
logger: mockLog,
}),
);
jest.mock(
'@/modules/qqbot/plugins/bangdream/src/infrastructure/integration/runtime-io',
() => ({
readBangDreamRuntimeConfig: jest.fn(() => undefined),
sleepBangDreamRuntime: jest.fn(() => new Promise(() => undefined)),
}),
);
import { waitForBangDreamCatalogReady } from '@/modules/qqbot/plugins/bangdream/src/application/catalog/bangdream-catalog-cache';
import { BANGDREAM_BESTDORI_API_PATHS } from '@/modules/qqbot/plugins/bangdream/src/domain/common/bangdream-protocol';
describe('BangDream catalog cache', () => {
beforeEach(() => {
mockGetJson.mockClear();
mockReadJson.mockClear();
mockReadExcelRows.mockClear();
mockLog.mockClear();
});
it('loads each Bestdori catalog path only once during the initial ready wait', async () => {
await waitForBangDreamCatalogReady!();
const catalogPaths = Object.values(BANGDREAM_BESTDORI_API_PATHS);
expect(mockGetJson).toHaveBeenCalledTimes(catalogPaths.length);
expect(mockGetJson.mock.calls.map(([path]) => path).sort()).toEqual(
[...catalogPaths].sort(),
);
expect(
mockGetJson.mock.calls.every(
([, options]) => options?.cacheTime === 1 / 0,
),
).toBe(true);
expect(mockReadJson).toHaveBeenCalledTimes(3);
expect(mockReadExcelRows).toHaveBeenCalledTimes(1);
});
});