|
1 | 1 | // Recent-session preservation tests cover the operator-configured retention shield. |
2 | 2 | import path from "node:path"; |
3 | | -import { describe, expect, it } from "vitest"; |
| 3 | +import { describe, expect, it, vi } from "vitest"; |
| 4 | +import { resetPluginRuntimeStateForTest, setActivePluginRegistry } from "../../plugins/runtime.js"; |
4 | 5 | import { withTestDir } from "../../test-helpers/temp-dir.js"; |
| 6 | +import { createTestRegistry } from "../../test-utils/channel-plugins.js"; |
5 | 7 | import { enforceSessionDiskBudget } from "./disk-budget.js"; |
6 | 8 | import { |
7 | 9 | capEntryCount, |
8 | 10 | pruneStaleEntries, |
9 | 11 | resolveMaintenanceConfigFromInput, |
| 12 | + shouldPreserveMaintenanceEntry, |
10 | 13 | } from "./store-maintenance.js"; |
11 | 14 | import type { SessionEntry } from "./types.js"; |
12 | 15 |
|
13 | 16 | const DAY_MS = 24 * 60 * 60 * 1000; |
14 | 17 |
|
15 | 18 | describe("recent session maintenance preservation", () => { |
| 19 | + it.each(["classification", "pruning", "capping"] as const)( |
| 20 | + "preserves external conversations during %s without invoking channel plugins", |
| 21 | + (boundary) => { |
| 22 | + const resolveSessionConversation = vi.fn(() => { |
| 23 | + throw new Error("channel resolver must not run during session maintenance"); |
| 24 | + }); |
| 25 | + setActivePluginRegistry( |
| 26 | + createTestRegistry([ |
| 27 | + { |
| 28 | + pluginId: "broken", |
| 29 | + source: "test", |
| 30 | + plugin: { |
| 31 | + id: "broken", |
| 32 | + meta: { label: "Broken" }, |
| 33 | + messaging: { resolveSessionConversation }, |
| 34 | + }, |
| 35 | + }, |
| 36 | + ]), |
| 37 | + ); |
| 38 | + |
| 39 | + const updatedAt = Date.now() - 31 * DAY_MS; |
| 40 | + const protectedKeys = [ |
| 41 | + "agent:main:broken:group:room:thread:reply", |
| 42 | + "agent:main:broken:channel:room:with:colon", |
| 43 | + "agent:main:telegram:dm:user:topic:77", |
| 44 | + "agent:main:opaque:thread:reply", |
| 45 | + ]; |
| 46 | + const removableKeys = [ |
| 47 | + "agent:main:old", |
| 48 | + "agent:main:subagent:worker", |
| 49 | + "agent:main:opaque:topic:unrelated", |
| 50 | + ]; |
| 51 | + const store: Record<string, SessionEntry> = Object.fromEntries( |
| 52 | + [...protectedKeys, ...removableKeys].map((key) => [key, { sessionId: key, updatedAt }]), |
| 53 | + ); |
| 54 | + |
| 55 | + try { |
| 56 | + if (boundary === "classification") { |
| 57 | + for (const key of protectedKeys) { |
| 58 | + expect(shouldPreserveMaintenanceEntry({ key, entry: store[key] })).toBe(true); |
| 59 | + } |
| 60 | + for (const key of removableKeys) { |
| 61 | + expect(shouldPreserveMaintenanceEntry({ key, entry: store[key] })).toBe(false); |
| 62 | + } |
| 63 | + } else if (boundary === "pruning") { |
| 64 | + expect(pruneStaleEntries(store, 30 * DAY_MS, { log: false })).toBe(removableKeys.length); |
| 65 | + } else { |
| 66 | + expect(capEntryCount(store, protectedKeys.length, { log: false })).toBe( |
| 67 | + removableKeys.length, |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + for (const key of protectedKeys) { |
| 72 | + expect(store).toHaveProperty(key); |
| 73 | + } |
| 74 | + expect(resolveSessionConversation).not.toHaveBeenCalled(); |
| 75 | + } finally { |
| 76 | + resetPluginRuntimeStateForTest(); |
| 77 | + } |
| 78 | + }, |
| 79 | + ); |
| 80 | + |
16 | 81 | it("is opt-in and keeps recent interactive sessions through prune and cap pressure", () => { |
17 | 82 | const now = Date.now(); |
18 | 83 | const recentKey = "agent:main:dashboard:recent"; |
|
0 commit comments