Cron+Slack: fix cooldown omission and cache cap enforcement

This commit is contained in:
Onur
2026-03-01 18:35:18 +01:00
committed by Onur Solmaz
parent 8292401719
commit 18033d3962
4 changed files with 75 additions and 5 deletions

View File

@@ -55,4 +55,13 @@ describe("slack sent-thread-cache", () => {
vi.spyOn(Date, "now").mockReturnValue(Date.now() + 25 * 60 * 60 * 1000);
expect(hasSlackThreadParticipation("A1", "C123", "1700000000.000001")).toBe(false);
});
it("enforces maximum entries by evicting oldest fresh entries", () => {
for (let i = 0; i < 5001; i += 1) {
recordSlackThreadParticipation("A1", "C123", `1700000000.${String(i).padStart(6, "0")}`);
}
expect(hasSlackThreadParticipation("A1", "C123", "1700000000.000000")).toBe(false);
expect(hasSlackThreadParticipation("A1", "C123", "1700000000.005000")).toBe(true);
});
});

View File

@@ -22,6 +22,13 @@ function evictExpired(): void {
}
}
function evictOldest(): void {
const oldest = threadParticipation.keys().next().value;
if (oldest) {
threadParticipation.delete(oldest);
}
}
export function recordSlackThreadParticipation(
accountId: string,
channelId: string,
@@ -33,6 +40,9 @@ export function recordSlackThreadParticipation(
if (threadParticipation.size >= MAX_ENTRIES) {
evictExpired();
}
if (threadParticipation.size >= MAX_ENTRIES) {
evictOldest();
}
threadParticipation.set(makeKey(accountId, channelId, threadTs), Date.now());
}