forked from Lilly-Protocol/agentlily-runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory-store-capacity.test.ts
More file actions
104 lines (92 loc) · 2.91 KB
/
Copy pathmemory-store-capacity.test.ts
File metadata and controls
104 lines (92 loc) · 2.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { describe, expect, it } from "vitest";
import {
InMemoryMemoryStore,
DEFAULT_MAX_MEMORY_ENTRIES
} from "../../src/memory/memory-store.js";
describe("InMemoryMemoryStore Capacity & Eviction Invariants", () => {
it("initializes with default maximum capacity of 10,000", () => {
const store = new InMemoryMemoryStore();
expect(store.maxEntries).toBe(DEFAULT_MAX_MEMORY_ENTRIES);
expect(store.size).toBe(0);
});
it("supports explicit numeric and object capacity configuration", () => {
const storeObj = new InMemoryMemoryStore({ maxEntries: 25 });
expect(storeObj.maxEntries).toBe(25);
const storeNum = new InMemoryMemoryStore(50);
expect(storeNum.maxEntries).toBe(50);
});
it("throws RangeError on non-positive or invalid capacity", () => {
expect(() => new InMemoryMemoryStore(0)).toThrow(RangeError);
expect(() => new InMemoryMemoryStore(-10)).toThrow(RangeError);
expect(() => new InMemoryMemoryStore(NaN)).toThrow(RangeError);
});
it("evicts oldest entries in FIFO order when maxEntries is exceeded", async () => {
const store = new InMemoryMemoryStore({ maxEntries: 3 });
for (let i = 1; i <= 5; i++) {
await store.append({
agentId: "agent-1",
taskId: `task-${i}`,
input: `input-${i}`,
output: { step: i },
recordedAt: new Date().toISOString()
});
}
expect(store.size).toBe(3);
const records = await store.listByAgent("agent-1");
expect(records.map((r) => r.taskId)).toEqual([
"task-3",
"task-4",
"task-5"
]);
});
it("maintains agent isolation across eviction cycles", async () => {
const store = new InMemoryMemoryStore({ maxEntries: 3 });
await store.append({
agentId: "agent-A",
taskId: "t1",
input: "i1",
output: 1,
recordedAt: ""
});
await store.append({
agentId: "agent-B",
taskId: "t2",
input: "i2",
output: 2,
recordedAt: ""
});
await store.append({
agentId: "agent-A",
taskId: "t3",
input: "i3",
output: 3,
recordedAt: ""
});
// Capacity reached (3). Appending 4th evicts t1
await store.append({
agentId: "agent-B",
taskId: "t4",
input: "i4",
output: 4,
recordedAt: ""
});
const aEntries = await store.listByAgent("agent-A");
const bEntries = await store.listByAgent("agent-B");
expect(aEntries.map((e) => e.taskId)).toEqual(["t3"]);
expect(bEntries.map((e) => e.taskId)).toEqual(["t2", "t4"]);
});
it("clears all stored memory entries", async () => {
const store = new InMemoryMemoryStore({ maxEntries: 10 });
await store.append({
agentId: "agent-1",
taskId: "t1",
input: "i1",
output: 1,
recordedAt: ""
});
expect(store.size).toBe(1);
await store.clear();
expect(store.size).toBe(0);
expect(await store.listByAgent("agent-1")).toEqual([]);
});
});