forked from onlook-dev/onlook
-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathindex.test.ts
More file actions
91 lines (74 loc) · 2.37 KB
/
index.test.ts
File metadata and controls
91 lines (74 loc) · 2.37 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
import { LongTermMemory, Rule } from './index';
import fs from 'fs-extra';
import path from 'path';
describe('LongTermMemory', () => {
let memory: LongTermMemory;
const testRulesDir = path.join(process.cwd(), 'test-rules');
beforeEach(async () => {
await fs.remove(testRulesDir);
memory = new LongTermMemory(testRulesDir);
await memory.init();
});
afterEach(async () => {
await fs.remove(testRulesDir);
});
it('should initialize with empty rules', () => {
expect(memory.getAllRules()).toEqual([]);
});
it('should add a new rule', async () => {
const rule: Omit<Rule, 'id' | 'createdAt' | 'updatedAt'> = {
content: 'Test rule content',
tags: ['test']
};
const addedRule = await memory.addRule(rule);
expect(addedRule).toMatchObject({
content: rule.content,
tags: rule.tags
});
expect(addedRule.id).toBeDefined();
expect(addedRule.createdAt).toBeInstanceOf(Date);
expect(addedRule.updatedAt).toBeInstanceOf(Date);
const filePath = path.join(testRulesDir, `${addedRule.id}.json`);
expect(await fs.pathExists(filePath)).toBe(true);
});
it('should update an existing rule', async () => {
const rule = await memory.addRule({
content: 'Original content',
tags: ['test']
});
const updatedRule = await memory.updateRule(rule.id, {
content: 'Updated content'
});
expect(updatedRule).not.toBeNull();
expect(updatedRule?.content).toBe('Updated content');
expect(updatedRule?.tags).toEqual(['test']);
});
it('should delete a rule', async () => {
const rule = await memory.addRule({
content: 'Test rule',
tags: ['test']
});
const deleted = await memory.deleteRule(rule.id);
expect(deleted).toBe(true);
expect(memory.getRule(rule.id)).toBeNull();
const filePath = path.join(testRulesDir, `${rule.id}.json`);
expect(await fs.pathExists(filePath)).toBe(false);
});
it('should get rules by tag', async () => {
await memory.addRule({
content: 'Rule 1',
tags: ['test']
});
await memory.addRule({
content: 'Rule 2',
tags: ['test']
});
await memory.addRule({
content: 'Rule 3',
tags: ['other']
});
const testRules = memory.getRulesByTag('test');
expect(testRules).toHaveLength(2);
expect(testRules.every(rule => rule.tags?.includes('test'))).toBe(true);
});
});