-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-turso.ts
More file actions
165 lines (141 loc) Β· 4.6 KB
/
test-turso.ts
File metadata and controls
165 lines (141 loc) Β· 4.6 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env npx tsx
/**
* Test script for Turso database implementation
*
* Usage: npx tsx test-turso.ts
*/
import { TursoDatabase } from './src/lib/turso-graph';
import type { CognitiveSpace } from './src/lib/turso-graph';
async function runTests() {
console.log('π§ͺ Testing Turso Database Implementation\n');
const db = new TursoDatabase({
url: 'file:test-modeler.db' // Use local file for testing
});
try {
// Test 1: Create a space
console.log('Test 1: Creating a new space...');
const testSpace: CognitiveSpace = {
metadata: {
id: 'test-space-' + Date.now(),
title: 'Test Cognitive Space',
description: 'A test space to verify Turso integration',
createdAt: Date.now()
},
nodes: {
'TestNode1': {
id: 'TestNode1',
meanings: [
{ content: 'First test node', confidence: 0.9, timestamp: Date.now() }
],
values: { importance: 0.8, complexity: 0.5 },
relationships: [],
history: ['Node created']
},
'TestNode2': {
id: 'TestNode2',
meanings: [
{ content: 'Second test node', confidence: 0.85, timestamp: Date.now() }
],
values: { importance: 0.7, complexity: 0.3 },
relationships: [],
history: ['Node created', 'Connected to TestNode1']
}
},
globalHistory: [
'Space initialized',
'TestNode1 created',
'TestNode2 created'
]
};
await db.insertSpace(testSpace);
console.log('β
Space created successfully\n');
// Test 2: Retrieve the space
console.log('Test 2: Retrieving the space...');
const retrieved = await db.getSpace(testSpace.metadata.id);
if (!retrieved) {
throw new Error('Failed to retrieve space');
}
console.log('β
Space retrieved:', {
id: retrieved.metadata.id,
title: retrieved.metadata.title,
nodeCount: Object.keys(retrieved.nodes).length,
historyCount: retrieved.globalHistory.length
});
console.log('');
// Test 3: Verify data integrity
console.log('Test 3: Verifying data integrity...');
if (retrieved.metadata.title !== testSpace.metadata.title) {
throw new Error('Title mismatch');
}
if (Object.keys(retrieved.nodes).length !== Object.keys(testSpace.nodes).length) {
throw new Error('Node count mismatch');
}
if (retrieved.globalHistory.length !== testSpace.globalHistory.length) {
throw new Error('History count mismatch');
}
console.log('β
Data integrity verified\n');
// Test 4: List spaces
console.log('Test 4: Listing all spaces...');
const spaces = await db.listSpaces();
console.log(`β
Found ${spaces.length} space(s):`);
for (const space of spaces) {
console.log(` - ${space.title} (${space.nodeCount} nodes)`);
}
console.log('');
// Test 5: Update space
console.log('Test 5: Updating space...');
const updatedSpace = {
...retrieved,
metadata: {
...retrieved.metadata,
title: 'Updated Test Space'
},
nodes: {
...retrieved.nodes,
'TestNode3': {
id: 'TestNode3',
meanings: [{ content: 'Third node', confidence: 0.95, timestamp: Date.now() }],
values: {},
relationships: [],
history: ['Added in update']
}
},
globalHistory: [
...retrieved.globalHistory,
'TestNode3 added'
]
};
await db.insertSpace(updatedSpace);
const afterUpdate = await db.getSpace(testSpace.metadata.id);
if (!afterUpdate) {
throw new Error('Failed to retrieve updated space');
}
console.log('β
Space updated:', {
newTitle: afterUpdate.metadata.title,
nodeCount: Object.keys(afterUpdate.nodes).length,
historyCount: afterUpdate.globalHistory.length
});
console.log('');
// Test 6: Delete space
console.log('Test 6: Deleting space...');
const deleted = await db.deleteSpace(testSpace.metadata.id);
if (!deleted) {
throw new Error('Failed to delete space');
}
console.log('β
Space deleted\n');
// Test 7: Verify deletion
console.log('Test 7: Verifying deletion...');
const afterDelete = await db.getSpace(testSpace.metadata.id);
if (afterDelete !== null) {
throw new Error('Space still exists after deletion');
}
console.log('β
Deletion verified\n');
console.log('π All tests passed!\n');
} catch (error) {
console.error('β Test failed:', error);
process.exit(1);
} finally {
await db.close();
}
}
runTests();