-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtest-event-persistence.mjs
More file actions
141 lines (118 loc) · 3.75 KB
/
Copy pathtest-event-persistence.mjs
File metadata and controls
141 lines (118 loc) · 3.75 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/**
* Test script for event persistence
*/
import { JsStore } from '@animalabs/chronicle';
import { Membrane, AnthropicAdapter } from '@animalabs/membrane';
import { AgentFramework, ApiModule } from './dist/index.js';
import { rm } from 'fs/promises';
const TEST_STORE_PATH = './test-event-store';
// Clean up any previous test data
try {
await rm(TEST_STORE_PATH, { recursive: true });
} catch {
// Ignore if doesn't exist
}
console.log('=== Test 1: Create framework and push events ===\n');
// Create framework
const adapter = new AnthropicAdapter({});
const membrane = new Membrane(adapter);
let framework = await AgentFramework.create({
storePath: TEST_STORE_PATH,
membrane,
agents: [
{
name: 'test-agent',
model: 'claude-sonnet-4-20250514',
systemPrompt: 'You are a test agent.',
},
],
modules: [new ApiModule()],
});
// Listen for events
framework.on((event) => {
if (event.type === 'event:persisted') {
console.log(` Event persisted: ${event.eventId} (seq: ${event.sequence})`);
}
});
framework.start();
// Push some events
console.log('Pushing events...');
const eventId1 = framework.pushEvent({
type: 'api:message',
participant: 'user',
content: 'Hello, this is message 1',
});
console.log(` Pushed event 1: ${eventId1}`);
const eventId2 = framework.pushEvent({
type: 'api:message',
participant: 'user',
content: 'Hello, this is message 2',
});
console.log(` Pushed event 2: ${eventId2}`);
const eventId3 = framework.pushEvent({
type: 'api:inference-request',
agentName: 'test-agent',
reason: 'test',
});
console.log(` Pushed event 3: ${eventId3}`);
// Query events
console.log('\nQuerying events...');
const allEvents = framework.queryEvents();
console.log(` Total events: ${allEvents.length}`);
allEvents.forEach((e, i) => {
console.log(` [${i}] ${e.type} (id: ${e.id}, seq: ${e.sequence})`);
});
// Query with filter
const messageEvents = framework.queryEvents({ types: ['api:message'] });
console.log(`\n api:message events: ${messageEvents.length}`);
const inferenceEvents = framework.queryEvents({ types: ['api:inference-request'] });
console.log(` api:inference-request events: ${inferenceEvents.length}`);
// Query with glob pattern
const apiEvents = framework.queryEvents({ types: ['api:*'] });
console.log(` api:* events: ${apiEvents.length}`);
// Stop framework
await framework.stop();
console.log('\nFramework stopped.\n');
console.log('=== Test 2: Restart framework and verify persistence ===\n');
// Create a new framework with the same store
const membrane2 = new Membrane(adapter);
framework = await AgentFramework.create({
storePath: TEST_STORE_PATH,
membrane: membrane2,
agents: [
{
name: 'test-agent',
model: 'claude-sonnet-4-20250514',
systemPrompt: 'You are a test agent.',
},
],
modules: [new ApiModule()],
});
// Query events - they should still be there!
console.log('Querying events after restart...');
const persistedEvents = framework.queryEvents();
console.log(` Total events: ${persistedEvents.length}`);
persistedEvents.forEach((e, i) => {
console.log(` [${i}] ${e.type} (id: ${e.id}, seq: ${e.sequence})`);
});
// Verify event content
const firstEvent = persistedEvents.find(e => e.id === eventId1);
if (firstEvent) {
console.log('\n First event payload:', JSON.stringify(firstEvent.payload, null, 2));
}
// Stop and cleanup
await framework.stop();
// Verify counts match
const expectedCount = 3;
if (persistedEvents.length === expectedCount) {
console.log(`\n✅ SUCCESS: All ${expectedCount} events persisted and recovered!`);
} else {
console.log(`\n❌ FAIL: Expected ${expectedCount} events, got ${persistedEvents.length}`);
process.exit(1);
}
// Cleanup test data
try {
await rm(TEST_STORE_PATH, { recursive: true });
} catch {
// Ignore
}