fix: 修复Bilibili卡片绑定缓存

This commit is contained in:
sunlei 2026-06-20 01:49:14 +08:00
parent 9fc11975c1
commit 832eeeeec7
2 changed files with 28 additions and 15 deletions

View File

@ -11,10 +11,6 @@ import { parseBilibiliVideoReference } from '../domain/bilibili-url-parser';
import { BilibiliVideoClient } from '../infrastructure/integration/bilibili-video-client';
export class BilibiliCardApplication {
private readonly boundCache = new Map<
string,
{ expiresAt: number; value: boolean }
>();
private readonly dedupe = new Map<string, { expiresAt: number }>();
private readonly videoClient: BilibiliVideoClient;
@ -117,20 +113,10 @@ export class BilibiliCardApplication {
const normalizedSelfId = `${selfId || ''}`.trim();
if (!normalizedSelfId) return false;
const current = this.now();
const cached = this.boundCache.get(normalizedSelfId);
if (cached && cached.expiresAt > current) return cached.value;
const config = readBilibiliCardRuntimeConfig(this.host);
try {
const value = (
return (
await this.host.getBoundEventPluginKeys(normalizedSelfId)
).includes(this.manifest.pluginKey);
this.boundCache.set(normalizedSelfId, {
expiresAt: current + Math.min(config.dedupeTtlMs, 60000),
value,
});
return value;
} catch (error) {
this.warn(`Bilibili 事件绑定查询失败: ${normalizeError(error)}`);
return false;

View File

@ -150,6 +150,33 @@ describe('Bilibili card application', () => {
expect(host.sendText).toHaveBeenCalledTimes(1);
});
it('rechecks binding after an unbound message so newly bound accounts work immediately', async () => {
let current = 1000;
const host = createHost({
getBoundEventPluginKeys: jest
.fn()
.mockResolvedValueOnce([])
.mockResolvedValueOnce(['bilibili-card']),
requestJson: jest.fn().mockResolvedValue(createBilibiliViewResponse()),
});
const application = new BilibiliCardApplication(
host,
createManifest(),
() => current,
);
const message = createMessage({
messageText: 'https://www.bilibili.com/video/BV1xx411c7mD',
});
await expect(application.handleMessage(message)).resolves.toBe(false);
current += 1000;
await expect(application.handleMessage(message)).resolves.toBe(true);
expect(host.getBoundEventPluginKeys).toHaveBeenCalledTimes(2);
expect(host.requestJson).toHaveBeenCalledTimes(1);
expect(host.sendText).toHaveBeenCalledTimes(1);
});
it('routes generic worker message events to package handler', async () => {
const host = createHost({
getBoundEventPluginKeys: jest.fn().mockResolvedValue(['bilibili-card']),