bunsane 0.1.4 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.claude/settings.local.json +47 -0
- package/.claude/skills/update-memory.md +74 -0
- package/.prettierrc +4 -0
- package/.serena/memories/architectural-decision-no-dependency-injection.md +76 -0
- package/.serena/memories/architecture.md +154 -0
- package/.serena/memories/cache-interface-refactoring-2026-01-24.md +165 -0
- package/.serena/memories/code_style_and_conventions.md +76 -0
- package/.serena/memories/project_overview.md +43 -0
- package/.serena/memories/schema-dsl-plan.md +107 -0
- package/.serena/memories/suggested_commands.md +80 -0
- package/.serena/memories/typescript-compilation-status.md +54 -0
- package/.serena/project.yml +114 -0
- package/TODO.md +1 -7
- package/bun.lock +150 -4
- package/bunfig.toml +10 -0
- package/config/cache.config.ts +77 -0
- package/config/upload.config.ts +4 -5
- package/core/App.ts +870 -123
- package/core/ArcheType.ts +2268 -377
- package/core/BatchLoader.ts +181 -71
- package/core/Config.ts +153 -0
- package/core/Decorators.ts +4 -1
- package/core/Entity.ts +621 -92
- package/core/EntityHookManager.ts +1 -1
- package/core/EntityInterface.ts +3 -1
- package/core/EntityManager.ts +1 -13
- package/core/ErrorHandler.ts +8 -2
- package/core/Logger.ts +9 -0
- package/core/Middleware.ts +34 -0
- package/core/RequestContext.ts +5 -1
- package/core/RequestLoaders.ts +227 -93
- package/core/SchedulerManager.ts +193 -52
- package/core/cache/CacheAnalytics.ts +399 -0
- package/core/cache/CacheFactory.ts +145 -0
- package/core/cache/CacheManager.ts +520 -0
- package/core/cache/CacheProvider.ts +34 -0
- package/core/cache/CacheWarmer.ts +157 -0
- package/core/cache/CompressionUtils.ts +110 -0
- package/core/cache/MemoryCache.ts +251 -0
- package/core/cache/MultiLevelCache.ts +180 -0
- package/core/cache/NoOpCache.ts +53 -0
- package/core/cache/RedisCache.ts +464 -0
- package/core/cache/TTLStrategy.ts +254 -0
- package/core/cache/index.ts +6 -0
- package/core/components/BaseComponent.ts +120 -0
- package/core/{ComponentRegistry.ts → components/ComponentRegistry.ts} +148 -54
- package/core/components/Decorators.ts +88 -0
- package/core/components/Interfaces.ts +7 -0
- package/core/components/index.ts +5 -0
- package/core/decorators/EntityHooks.ts +0 -3
- package/core/decorators/IndexedField.ts +26 -0
- package/core/decorators/ScheduledTask.ts +0 -47
- package/core/events/EntityLifecycleEvents.ts +1 -1
- package/core/health.ts +112 -0
- package/core/metadata/definitions/ArcheType.ts +14 -0
- package/core/metadata/definitions/Component.ts +9 -0
- package/core/metadata/definitions/gqlObject.ts +1 -1
- package/core/metadata/index.ts +42 -1
- package/core/metadata/metadata-storage.ts +28 -2
- package/core/middleware/AccessLog.ts +59 -0
- package/core/middleware/RequestId.ts +38 -0
- package/core/middleware/SecurityHeaders.ts +62 -0
- package/core/middleware/index.ts +3 -0
- package/core/scheduler/DistributedLock.ts +266 -0
- package/core/scheduler/index.ts +15 -0
- package/core/validateEnv.ts +92 -0
- package/database/DatabaseHelper.ts +416 -40
- package/database/IndexingStrategy.ts +342 -0
- package/database/PreparedStatementCache.ts +226 -0
- package/database/index.ts +32 -7
- package/database/sqlHelpers.ts +14 -2
- package/endpoints/archetypes.ts +362 -0
- package/endpoints/components.ts +58 -0
- package/endpoints/entity.ts +80 -0
- package/endpoints/index.ts +27 -0
- package/endpoints/query.ts +93 -0
- package/endpoints/stats.ts +76 -0
- package/endpoints/tables.ts +212 -0
- package/endpoints/types.ts +155 -0
- package/gql/ArchetypeOperations.ts +32 -86
- package/gql/Generator.ts +27 -315
- package/gql/GeneratorV2.ts +37 -0
- package/gql/builders/InputTypeBuilder.ts +99 -0
- package/gql/builders/ResolverBuilder.ts +234 -0
- package/gql/builders/TypeDefBuilder.ts +105 -0
- package/gql/builders/index.ts +3 -0
- package/gql/decorators/Upload.ts +1 -1
- package/gql/depthLimit.ts +85 -0
- package/gql/graph/GraphNode.ts +224 -0
- package/gql/graph/SchemaGraph.ts +278 -0
- package/gql/helpers.ts +8 -2
- package/gql/index.ts +56 -4
- package/gql/middleware.ts +79 -0
- package/gql/orchestration/GraphQLSchemaOrchestrator.ts +241 -0
- package/gql/orchestration/index.ts +1 -0
- package/gql/scanner/ServiceScanner.ts +347 -0
- package/gql/schema/index.ts +458 -0
- package/gql/strategies/TypeGenerationStrategy.ts +329 -0
- package/gql/types.ts +1 -0
- package/gql/utils/TypeSignature.ts +220 -0
- package/gql/utils/index.ts +1 -0
- package/gql/visitors/ArchetypePreprocessorVisitor.ts +80 -0
- package/gql/visitors/DeduplicationVisitor.ts +82 -0
- package/gql/visitors/GraphVisitor.ts +78 -0
- package/gql/visitors/ResolverGeneratorVisitor.ts +122 -0
- package/gql/visitors/SchemaGeneratorVisitor.ts +851 -0
- package/gql/visitors/TypeCollectorVisitor.ts +79 -0
- package/gql/visitors/VisitorComposer.ts +96 -0
- package/gql/visitors/index.ts +7 -0
- package/package.json +59 -37
- package/plugins/index.ts +2 -2
- package/query/CTENode.ts +97 -0
- package/query/ComponentInclusionNode.ts +689 -0
- package/query/FilterBuilder.ts +127 -0
- package/query/FilterBuilderRegistry.ts +202 -0
- package/query/OrNode.ts +517 -0
- package/query/OrQuery.ts +42 -0
- package/query/Query.ts +1022 -0
- package/query/QueryContext.ts +170 -0
- package/query/QueryDAG.ts +122 -0
- package/query/QueryNode.ts +65 -0
- package/query/SourceNode.ts +53 -0
- package/query/builders/FullTextSearchBuilder.ts +236 -0
- package/query/index.ts +21 -0
- package/scheduler/index.ts +40 -8
- package/service/Service.ts +2 -1
- package/service/ServiceRegistry.ts +6 -5
- package/{core/storage → storage}/LocalStorageProvider.ts +2 -2
- package/storage/S3StorageProvider.ts +316 -0
- package/{core/storage → storage}/StorageProvider.ts +7 -3
- package/studio/bun.lock +482 -0
- package/studio/index.html +13 -0
- package/studio/package.json +39 -0
- package/studio/postcss.config.js +6 -0
- package/studio/src/components/DataTable.tsx +211 -0
- package/studio/src/components/Layout.tsx +13 -0
- package/studio/src/components/PageContainer.tsx +9 -0
- package/studio/src/components/PageHeader.tsx +13 -0
- package/studio/src/components/SearchBar.tsx +57 -0
- package/studio/src/components/Sidebar.tsx +294 -0
- package/studio/src/components/ui/button.tsx +56 -0
- package/studio/src/components/ui/checkbox.tsx +26 -0
- package/studio/src/components/ui/input.tsx +25 -0
- package/studio/src/hooks/useDataTable.ts +131 -0
- package/studio/src/index.css +36 -0
- package/studio/src/lib/api.ts +186 -0
- package/studio/src/lib/utils.ts +13 -0
- package/studio/src/main.tsx +17 -0
- package/studio/src/pages/ArcheType.tsx +239 -0
- package/studio/src/pages/Components.tsx +124 -0
- package/studio/src/pages/EntityInspector.tsx +302 -0
- package/studio/src/pages/QueryRunner.tsx +246 -0
- package/studio/src/pages/Table.tsx +94 -0
- package/studio/src/pages/Welcome.tsx +241 -0
- package/studio/src/routes.tsx +45 -0
- package/studio/src/store/archeTypeSettings.ts +30 -0
- package/studio/src/store/studio.ts +65 -0
- package/studio/src/utils/columnHelpers.tsx +114 -0
- package/studio/studio-instructions.md +81 -0
- package/studio/tailwind.config.js +77 -0
- package/studio/tsconfig.json +24 -0
- package/studio/utils.ts +54 -0
- package/studio/vite.config.js +19 -0
- package/swagger/generator.ts +1 -1
- package/tests/e2e/http.test.ts +126 -0
- package/tests/fixtures/archetypes/TestUserArchetype.ts +21 -0
- package/tests/fixtures/components/TestOrder.ts +23 -0
- package/tests/fixtures/components/TestProduct.ts +23 -0
- package/tests/fixtures/components/TestUser.ts +20 -0
- package/tests/fixtures/components/index.ts +6 -0
- package/tests/graphql/SchemaGeneration.test.ts +90 -0
- package/tests/graphql/builders/ResolverBuilder.test.ts +223 -0
- package/tests/graphql/builders/TypeDefBuilder.test.ts +153 -0
- package/tests/integration/archetype/ArcheType.persistence.test.ts +241 -0
- package/tests/integration/cache/CacheInvalidation.test.ts +259 -0
- package/tests/integration/entity/Entity.persistence.test.ts +333 -0
- package/tests/integration/query/Query.exec.test.ts +523 -0
- package/tests/pglite-setup.ts +61 -0
- package/tests/setup.ts +164 -0
- package/tests/stress/BenchmarkRunner.ts +203 -0
- package/tests/stress/DataSeeder.ts +190 -0
- package/tests/stress/StressTestReporter.ts +229 -0
- package/tests/stress/cursor-perf-test.ts +171 -0
- package/tests/stress/fixtures/StressTestComponents.ts +58 -0
- package/tests/stress/index.ts +7 -0
- package/tests/stress/scenarios/query-benchmarks.test.ts +285 -0
- package/tests/unit/BatchLoader.test.ts +82 -0
- package/tests/unit/archetype/ArcheType.test.ts +107 -0
- package/tests/unit/cache/CacheManager.test.ts +347 -0
- package/tests/unit/cache/MemoryCache.test.ts +260 -0
- package/tests/unit/cache/RedisCache.test.ts +411 -0
- package/tests/unit/entity/Entity.components.test.ts +244 -0
- package/tests/unit/entity/Entity.test.ts +345 -0
- package/tests/unit/gql/depthLimit.test.ts +203 -0
- package/tests/unit/gql/operationMiddleware.test.ts +293 -0
- package/tests/unit/health/Health.test.ts +129 -0
- package/tests/unit/middleware/AccessLog.test.ts +37 -0
- package/tests/unit/middleware/Middleware.test.ts +98 -0
- package/tests/unit/middleware/RequestId.test.ts +54 -0
- package/tests/unit/middleware/SecurityHeaders.test.ts +66 -0
- package/tests/unit/query/FilterBuilder.test.ts +111 -0
- package/tests/unit/query/Query.test.ts +308 -0
- package/tests/unit/scheduler/DistributedLock.test.ts +274 -0
- package/tests/unit/schema/schema-integration.test.ts +426 -0
- package/tests/unit/schema/schema.test.ts +580 -0
- package/tests/unit/storage/S3StorageProvider.test.ts +571 -0
- package/tests/unit/upload/RestUpload.test.ts +267 -0
- package/tests/unit/validateEnv.test.ts +82 -0
- package/tests/utils/entity-tracker.ts +57 -0
- package/tests/utils/index.ts +13 -0
- package/tests/utils/test-context.ts +149 -0
- package/tsconfig.json +5 -1
- package/types/archetype.types.ts +6 -0
- package/types/hooks.types.ts +1 -1
- package/types/query.types.ts +110 -0
- package/types/scheduler.types.ts +68 -7
- package/types/upload.types.ts +1 -0
- package/{core → upload}/FileValidator.ts +10 -1
- package/upload/RestUpload.ts +130 -0
- package/{core/components → upload}/UploadComponent.ts +11 -11
- package/{core → upload}/UploadManager.ts +3 -3
- package/upload/index.ts +23 -7
- package/utils/UploadHelper.ts +27 -6
- package/utils/cronParser.ts +16 -6
- package/.github/workflows/deploy-docs.yml +0 -57
- package/core/Components.ts +0 -202
- package/core/EntityCache.ts +0 -15
- package/core/Query.ts +0 -880
- package/docs/README.md +0 -149
- package/docs/_coverpage.md +0 -36
- package/docs/_sidebar.md +0 -23
- package/docs/api/core.md +0 -568
- package/docs/api/hooks.md +0 -554
- package/docs/api/index.md +0 -222
- package/docs/api/query.md +0 -678
- package/docs/api/service.md +0 -744
- package/docs/core-concepts/archetypes.md +0 -512
- package/docs/core-concepts/components.md +0 -498
- package/docs/core-concepts/entity.md +0 -314
- package/docs/core-concepts/hooks.md +0 -683
- package/docs/core-concepts/query.md +0 -588
- package/docs/core-concepts/services.md +0 -647
- package/docs/examples/code-examples.md +0 -425
- package/docs/getting-started.md +0 -337
- package/docs/index.html +0 -97
- package/tests/bench/insert.bench.ts +0 -60
- package/tests/bench/relations.bench.ts +0 -270
- package/tests/bench/sorting.bench.ts +0 -416
- package/tests/component-hooks-simple.test.ts +0 -117
- package/tests/component-hooks.test.ts +0 -1461
- package/tests/component.test.ts +0 -339
- package/tests/errorHandling.test.ts +0 -155
- package/tests/hooks.test.ts +0 -667
- package/tests/query-sorting.test.ts +0 -101
- package/tests/query.test.ts +0 -81
- package/tests/relations.test.ts +0 -170
- package/tests/scheduler.test.ts +0 -724
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for RedisCache
|
|
3
|
+
* Tests Redis cache provider functionality with a real Redis server
|
|
4
|
+
*
|
|
5
|
+
* PREREQUISITE: Redis server must be running on localhost:6379
|
|
6
|
+
*/
|
|
7
|
+
import { describe, test, expect, beforeAll, afterAll, beforeEach } from 'bun:test';
|
|
8
|
+
import { RedisCache } from '../../../core/cache/RedisCache';
|
|
9
|
+
|
|
10
|
+
describe('RedisCache', () => {
|
|
11
|
+
let cache: RedisCache;
|
|
12
|
+
const TEST_PREFIX = 'bunsane:test:';
|
|
13
|
+
|
|
14
|
+
beforeAll(async () => {
|
|
15
|
+
cache = new RedisCache({
|
|
16
|
+
host: 'localhost',
|
|
17
|
+
port: 6379,
|
|
18
|
+
keyPrefix: TEST_PREFIX,
|
|
19
|
+
lazyConnect: false,
|
|
20
|
+
enableReadyCheck: true,
|
|
21
|
+
maxRetriesPerRequest: 3
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
// Wait for connection
|
|
25
|
+
await new Promise(resolve => setTimeout(resolve, 100));
|
|
26
|
+
|
|
27
|
+
// Verify connection
|
|
28
|
+
const isConnected = await cache.ping();
|
|
29
|
+
if (!isConnected) {
|
|
30
|
+
throw new Error('Failed to connect to Redis. Make sure Redis server is running on localhost:6379');
|
|
31
|
+
}
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
afterAll(async () => {
|
|
35
|
+
// Clean up test keys before disconnecting
|
|
36
|
+
await cache.invalidatePattern('*');
|
|
37
|
+
await cache.disconnect();
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
beforeEach(async () => {
|
|
41
|
+
// Clear test keys before each test
|
|
42
|
+
await cache.invalidatePattern('*');
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
describe('ping()', () => {
|
|
46
|
+
test('returns true when connected', async () => {
|
|
47
|
+
const result = await cache.ping();
|
|
48
|
+
expect(result).toBe(true);
|
|
49
|
+
});
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
describe('healthCheck()', () => {
|
|
53
|
+
test('returns health status with connection info', async () => {
|
|
54
|
+
const health = await cache.healthCheck();
|
|
55
|
+
|
|
56
|
+
expect(health.connected).toBe(true);
|
|
57
|
+
expect(typeof health.latency).toBe('number');
|
|
58
|
+
expect(health.latency).toBeGreaterThanOrEqual(0);
|
|
59
|
+
expect(health.version).toBeDefined();
|
|
60
|
+
});
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
describe('get() and set()', () => {
|
|
64
|
+
test('returns null for non-existent key', async () => {
|
|
65
|
+
const result = await cache.get('non-existent-key');
|
|
66
|
+
expect(result).toBeNull();
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
test('sets and retrieves string value', async () => {
|
|
70
|
+
await cache.set('string-key', 'hello world', 3600000);
|
|
71
|
+
const result = await cache.get<string>('string-key');
|
|
72
|
+
expect(result).toBe('hello world');
|
|
73
|
+
});
|
|
74
|
+
|
|
75
|
+
test('sets and retrieves number value', async () => {
|
|
76
|
+
await cache.set('number-key', 42, 3600000);
|
|
77
|
+
const result = await cache.get<number>('number-key');
|
|
78
|
+
expect(result).toBe(42);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
test('sets and retrieves object', async () => {
|
|
82
|
+
const obj = { name: 'test', value: 123, nested: { a: 1, b: 2 } };
|
|
83
|
+
await cache.set('obj-key', obj, 3600000);
|
|
84
|
+
const result = await cache.get<typeof obj>('obj-key');
|
|
85
|
+
expect(result).toEqual(obj);
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
test('sets and retrieves array', async () => {
|
|
89
|
+
const arr = [1, 2, 3, 'four', { five: 5 }];
|
|
90
|
+
await cache.set('arr-key', arr, 3600000);
|
|
91
|
+
const result = await cache.get<typeof arr>('arr-key');
|
|
92
|
+
expect(result).toEqual(arr);
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
test('sets and retrieves boolean values', async () => {
|
|
96
|
+
await cache.set('bool-true', true, 3600000);
|
|
97
|
+
await cache.set('bool-false', false, 3600000);
|
|
98
|
+
|
|
99
|
+
expect(await cache.get<boolean>('bool-true')).toBe(true);
|
|
100
|
+
expect(await cache.get<boolean>('bool-false')).toBe(false);
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test('sets and retrieves null value', async () => {
|
|
104
|
+
await cache.set('null-key', null, 3600000);
|
|
105
|
+
const result = await cache.get('null-key');
|
|
106
|
+
expect(result).toBeNull();
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
test('expires after TTL', async () => {
|
|
110
|
+
// Redis TTL is in seconds (minimum 1 second), our API uses milliseconds
|
|
111
|
+
await cache.set('expire-key', 'value', 1000); // 1 second TTL
|
|
112
|
+
|
|
113
|
+
const immediate = await cache.get('expire-key');
|
|
114
|
+
expect(immediate).toBe('value');
|
|
115
|
+
|
|
116
|
+
await new Promise(resolve => setTimeout(resolve, 1500));
|
|
117
|
+
|
|
118
|
+
const expired = await cache.get('expire-key');
|
|
119
|
+
expect(expired).toBeNull();
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
test('overwrites existing key', async () => {
|
|
123
|
+
await cache.set('overwrite-key', 'original', 3600000);
|
|
124
|
+
await cache.set('overwrite-key', 'updated', 3600000);
|
|
125
|
+
|
|
126
|
+
const result = await cache.get('overwrite-key');
|
|
127
|
+
expect(result).toBe('updated');
|
|
128
|
+
});
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
describe('delete()', () => {
|
|
132
|
+
test('removes single key', async () => {
|
|
133
|
+
await cache.set('delete-key', 'value', 3600000);
|
|
134
|
+
await cache.delete('delete-key');
|
|
135
|
+
|
|
136
|
+
const result = await cache.get('delete-key');
|
|
137
|
+
expect(result).toBeNull();
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
test('removes multiple keys via array', async () => {
|
|
141
|
+
await cache.set('del-key1', 'value1', 3600000);
|
|
142
|
+
await cache.set('del-key2', 'value2', 3600000);
|
|
143
|
+
await cache.delete(['del-key1', 'del-key2']);
|
|
144
|
+
|
|
145
|
+
expect(await cache.get('del-key1')).toBeNull();
|
|
146
|
+
expect(await cache.get('del-key2')).toBeNull();
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
test('handles non-existent key gracefully', async () => {
|
|
150
|
+
// Should not throw and key should remain absent
|
|
151
|
+
await cache.delete('definitely-not-exists');
|
|
152
|
+
const result = await cache.get('definitely-not-exists');
|
|
153
|
+
expect(result).toBeNull();
|
|
154
|
+
});
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
describe('deleteMany()', () => {
|
|
158
|
+
test('removes multiple keys', async () => {
|
|
159
|
+
await cache.set('dm-a', 1, 3600000);
|
|
160
|
+
await cache.set('dm-b', 2, 3600000);
|
|
161
|
+
await cache.set('dm-c', 3, 3600000);
|
|
162
|
+
|
|
163
|
+
await cache.deleteMany(['dm-a', 'dm-b']);
|
|
164
|
+
|
|
165
|
+
expect(await cache.get('dm-a')).toBeNull();
|
|
166
|
+
expect(await cache.get('dm-b')).toBeNull();
|
|
167
|
+
expect(await cache.get<number>('dm-c')).toBe(3);
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
test('handles empty array', async () => {
|
|
171
|
+
// Set a key first, then deleteMany([]) should not affect it
|
|
172
|
+
await cache.set('survive-key', 'value', 3600000);
|
|
173
|
+
await cache.deleteMany([]);
|
|
174
|
+
const result = await cache.get<string>('survive-key');
|
|
175
|
+
expect(result).toBe('value');
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
describe('getMany()', () => {
|
|
180
|
+
test('returns array of values in order', async () => {
|
|
181
|
+
await cache.set('gm-k1', 'v1', 3600000);
|
|
182
|
+
await cache.set('gm-k2', 'v2', 3600000);
|
|
183
|
+
await cache.set('gm-k3', 'v3', 3600000);
|
|
184
|
+
|
|
185
|
+
const results = await cache.getMany(['gm-k1', 'gm-k2', 'gm-k3']);
|
|
186
|
+
|
|
187
|
+
expect(results).toEqual(['v1', 'v2', 'v3']);
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
test('returns null for missing keys', async () => {
|
|
191
|
+
await cache.set('gm-exists', 'value', 3600000);
|
|
192
|
+
|
|
193
|
+
const results = await cache.getMany(['gm-exists', 'gm-missing', 'gm-also-missing']);
|
|
194
|
+
|
|
195
|
+
expect(results[0]).toBe('value');
|
|
196
|
+
expect(results[1]).toBeNull();
|
|
197
|
+
expect(results[2]).toBeNull();
|
|
198
|
+
});
|
|
199
|
+
|
|
200
|
+
test('handles empty array', async () => {
|
|
201
|
+
const results = await cache.getMany([]);
|
|
202
|
+
expect(results).toEqual([]);
|
|
203
|
+
});
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
describe('setMany()', () => {
|
|
207
|
+
test('sets multiple entries', async () => {
|
|
208
|
+
await cache.setMany([
|
|
209
|
+
{ key: 'sm-a', value: 1, ttl: 3600000 },
|
|
210
|
+
{ key: 'sm-b', value: 2, ttl: 3600000 },
|
|
211
|
+
{ key: 'sm-c', value: 3, ttl: 3600000 }
|
|
212
|
+
]);
|
|
213
|
+
|
|
214
|
+
expect(await cache.get<number>('sm-a')).toBe(1);
|
|
215
|
+
expect(await cache.get<number>('sm-b')).toBe(2);
|
|
216
|
+
expect(await cache.get<number>('sm-c')).toBe(3);
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
test('sets entries with different TTLs', async () => {
|
|
220
|
+
// Redis TTL minimum is 1 second
|
|
221
|
+
await cache.setMany([
|
|
222
|
+
{ key: 'sm-short', value: 'short-lived', ttl: 1000 }, // 1 second
|
|
223
|
+
{ key: 'sm-long', value: 'long-lived', ttl: 3600000 }
|
|
224
|
+
]);
|
|
225
|
+
|
|
226
|
+
// Both should exist initially
|
|
227
|
+
expect(await cache.get<string>('sm-short')).toBe('short-lived');
|
|
228
|
+
expect(await cache.get<string>('sm-long')).toBe('long-lived');
|
|
229
|
+
|
|
230
|
+
// Wait for short TTL to expire
|
|
231
|
+
await new Promise(resolve => setTimeout(resolve, 1500));
|
|
232
|
+
|
|
233
|
+
expect(await cache.get('sm-short')).toBeNull();
|
|
234
|
+
expect(await cache.get<string>('sm-long')).toBe('long-lived');
|
|
235
|
+
});
|
|
236
|
+
|
|
237
|
+
test('handles empty array', async () => {
|
|
238
|
+
// setMany([]) should be a no-op, existing keys unaffected
|
|
239
|
+
await cache.set('survive-set', 'value', 3600000);
|
|
240
|
+
await cache.setMany([]);
|
|
241
|
+
const result = await cache.get<string>('survive-set');
|
|
242
|
+
expect(result).toBe('value');
|
|
243
|
+
});
|
|
244
|
+
});
|
|
245
|
+
|
|
246
|
+
describe('clear()', () => {
|
|
247
|
+
test('removes all entries with prefix', async () => {
|
|
248
|
+
await cache.set('clear-key1', 'value1', 3600000);
|
|
249
|
+
await cache.set('clear-key2', 'value2', 3600000);
|
|
250
|
+
|
|
251
|
+
await cache.clear();
|
|
252
|
+
|
|
253
|
+
expect(await cache.get('clear-key1')).toBeNull();
|
|
254
|
+
expect(await cache.get('clear-key2')).toBeNull();
|
|
255
|
+
});
|
|
256
|
+
});
|
|
257
|
+
|
|
258
|
+
describe('invalidatePattern()', () => {
|
|
259
|
+
test('removes keys matching simple prefix pattern', async () => {
|
|
260
|
+
await cache.set('prefix:1', 'v1', 3600000);
|
|
261
|
+
await cache.set('prefix:2', 'v2', 3600000);
|
|
262
|
+
await cache.set('other:1', 'o1', 3600000);
|
|
263
|
+
|
|
264
|
+
await cache.invalidatePattern('prefix:*');
|
|
265
|
+
|
|
266
|
+
expect(await cache.get('prefix:1')).toBeNull();
|
|
267
|
+
expect(await cache.get('prefix:2')).toBeNull();
|
|
268
|
+
expect(await cache.get<string>('other:1')).toBe('o1');
|
|
269
|
+
});
|
|
270
|
+
|
|
271
|
+
test('handles complex patterns', async () => {
|
|
272
|
+
await cache.set('component:entity1:type1', 'c1', 3600000);
|
|
273
|
+
await cache.set('component:entity1:type2', 'c2', 3600000);
|
|
274
|
+
await cache.set('component:entity2:type1', 'c3', 3600000);
|
|
275
|
+
|
|
276
|
+
await cache.invalidatePattern('component:entity1:*');
|
|
277
|
+
|
|
278
|
+
expect(await cache.get('component:entity1:type1')).toBeNull();
|
|
279
|
+
expect(await cache.get('component:entity1:type2')).toBeNull();
|
|
280
|
+
expect(await cache.get<string>('component:entity2:type1')).toBe('c3');
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
test('handles pattern with no matches', async () => {
|
|
284
|
+
await cache.set('keep-this', 'value', 3600000);
|
|
285
|
+
|
|
286
|
+
await cache.invalidatePattern('nomatch:*');
|
|
287
|
+
|
|
288
|
+
expect(await cache.get<string>('keep-this')).toBe('value');
|
|
289
|
+
});
|
|
290
|
+
});
|
|
291
|
+
|
|
292
|
+
describe('getStats()', () => {
|
|
293
|
+
test('returns statistics object', async () => {
|
|
294
|
+
// Perform some operations to generate stats
|
|
295
|
+
await cache.set('stats-key', 'value', 3600000);
|
|
296
|
+
await cache.get('stats-key'); // Hit
|
|
297
|
+
await cache.get('stats-missing'); // Miss
|
|
298
|
+
|
|
299
|
+
const stats = await cache.getStats();
|
|
300
|
+
|
|
301
|
+
expect(typeof stats.hits).toBe('number');
|
|
302
|
+
expect(typeof stats.misses).toBe('number');
|
|
303
|
+
expect(typeof stats.hitRate).toBe('number');
|
|
304
|
+
expect(typeof stats.size).toBe('number');
|
|
305
|
+
expect(stats.hits).toBeGreaterThanOrEqual(0);
|
|
306
|
+
expect(stats.misses).toBeGreaterThanOrEqual(0);
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
test('tracks hit rate correctly', async () => {
|
|
310
|
+
// Fresh stats after clear
|
|
311
|
+
await cache.set('hr-key', 'value', 3600000);
|
|
312
|
+
|
|
313
|
+
// Create predictable hits and misses
|
|
314
|
+
await cache.get('hr-key'); // Hit
|
|
315
|
+
await cache.get('hr-missing'); // Miss
|
|
316
|
+
|
|
317
|
+
const stats = await cache.getStats();
|
|
318
|
+
|
|
319
|
+
// Hit rate should be between 0 and 1
|
|
320
|
+
expect(stats.hitRate).toBeGreaterThanOrEqual(0);
|
|
321
|
+
expect(stats.hitRate).toBeLessThanOrEqual(1);
|
|
322
|
+
});
|
|
323
|
+
});
|
|
324
|
+
|
|
325
|
+
describe('complex data types', () => {
|
|
326
|
+
test('handles deeply nested objects', async () => {
|
|
327
|
+
const complex = {
|
|
328
|
+
level1: {
|
|
329
|
+
level2: {
|
|
330
|
+
level3: {
|
|
331
|
+
value: 'deep',
|
|
332
|
+
array: [1, 2, { nested: true }]
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
}
|
|
336
|
+
};
|
|
337
|
+
|
|
338
|
+
await cache.set('complex-obj', complex, 3600000);
|
|
339
|
+
const result = await cache.get<typeof complex>('complex-obj');
|
|
340
|
+
|
|
341
|
+
expect(result).toEqual(complex);
|
|
342
|
+
});
|
|
343
|
+
|
|
344
|
+
test('handles Date objects (serialized as string)', async () => {
|
|
345
|
+
const date = new Date('2024-01-15T10:30:00Z');
|
|
346
|
+
await cache.set('date-key', date, 3600000);
|
|
347
|
+
|
|
348
|
+
const result = await cache.get<string>('date-key');
|
|
349
|
+
// Date gets serialized to ISO string
|
|
350
|
+
expect(result).toBe(date.toISOString());
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
test('handles large arrays', async () => {
|
|
354
|
+
const largeArray = Array.from({ length: 1000 }, (_, i) => ({ id: i, value: `item-${i}` }));
|
|
355
|
+
|
|
356
|
+
await cache.set('large-array', largeArray, 3600000);
|
|
357
|
+
const result = await cache.get<typeof largeArray>('large-array');
|
|
358
|
+
|
|
359
|
+
expect(result).toEqual(largeArray);
|
|
360
|
+
expect(result?.length).toBe(1000);
|
|
361
|
+
});
|
|
362
|
+
});
|
|
363
|
+
|
|
364
|
+
describe('concurrent operations', () => {
|
|
365
|
+
test('handles concurrent sets', async () => {
|
|
366
|
+
const promises = Array.from({ length: 10 }, (_, i) =>
|
|
367
|
+
cache.set(`concurrent-${i}`, i, 3600000)
|
|
368
|
+
);
|
|
369
|
+
|
|
370
|
+
await Promise.all(promises);
|
|
371
|
+
|
|
372
|
+
for (let i = 0; i < 10; i++) {
|
|
373
|
+
const result = await cache.get<number>(`concurrent-${i}`);
|
|
374
|
+
expect(result).toBe(i);
|
|
375
|
+
}
|
|
376
|
+
});
|
|
377
|
+
|
|
378
|
+
test('handles concurrent gets', async () => {
|
|
379
|
+
await cache.set('concurrent-read', 'value', 3600000);
|
|
380
|
+
|
|
381
|
+
const promises = Array.from({ length: 10 }, () =>
|
|
382
|
+
cache.get<string>('concurrent-read')
|
|
383
|
+
);
|
|
384
|
+
|
|
385
|
+
const results = await Promise.all(promises);
|
|
386
|
+
|
|
387
|
+
results.forEach(result => {
|
|
388
|
+
expect(result).toBe('value');
|
|
389
|
+
});
|
|
390
|
+
});
|
|
391
|
+
|
|
392
|
+
test('handles mixed concurrent operations', async () => {
|
|
393
|
+
const operations = [
|
|
394
|
+
cache.set('mixed-1', 'a', 3600000),
|
|
395
|
+
cache.set('mixed-2', 'b', 3600000),
|
|
396
|
+
cache.set('mixed-3', 'c', 3600000),
|
|
397
|
+
];
|
|
398
|
+
|
|
399
|
+
await Promise.all(operations);
|
|
400
|
+
|
|
401
|
+
const reads = [
|
|
402
|
+
cache.get('mixed-1'),
|
|
403
|
+
cache.get('mixed-2'),
|
|
404
|
+
cache.get('mixed-3'),
|
|
405
|
+
];
|
|
406
|
+
|
|
407
|
+
const results = await Promise.all(reads);
|
|
408
|
+
expect(results).toEqual(['a', 'b', 'c']);
|
|
409
|
+
});
|
|
410
|
+
});
|
|
411
|
+
});
|
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unit tests for Entity component management
|
|
3
|
+
* Tests component add, remove, and data handling
|
|
4
|
+
*/
|
|
5
|
+
import { describe, test, expect, beforeAll } from 'bun:test';
|
|
6
|
+
import { Entity } from '../../../core/Entity';
|
|
7
|
+
import { TestUser, TestProduct, TestOrder } from '../../fixtures/components';
|
|
8
|
+
import { ensureComponentsRegistered } from '../../utils';
|
|
9
|
+
|
|
10
|
+
describe('Entity Component Management', () => {
|
|
11
|
+
beforeAll(async () => {
|
|
12
|
+
await ensureComponentsRegistered(TestUser, TestProduct, TestOrder);
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
describe('component data handling', () => {
|
|
16
|
+
test('component data() returns correct properties', () => {
|
|
17
|
+
const entity = new Entity();
|
|
18
|
+
entity.add(TestUser, { name: 'John', email: 'john@test.com', age: 30, bio: 'A bio' });
|
|
19
|
+
|
|
20
|
+
const component = entity.getInMemory(TestUser);
|
|
21
|
+
const data = component?.data();
|
|
22
|
+
|
|
23
|
+
expect(data?.name).toBe('John');
|
|
24
|
+
expect(data?.email).toBe('john@test.com');
|
|
25
|
+
expect(data?.age).toBe(30);
|
|
26
|
+
expect(data?.bio).toBe('A bio');
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
test('component handles nullable fields', () => {
|
|
30
|
+
const entity = new Entity();
|
|
31
|
+
entity.add(TestUser, { name: 'John', email: 'john@test.com', age: 30 });
|
|
32
|
+
|
|
33
|
+
const component = entity.getInMemory(TestUser);
|
|
34
|
+
const data = component?.data();
|
|
35
|
+
|
|
36
|
+
expect(data?.bio).toBeUndefined();
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
test('component handles boolean fields', () => {
|
|
40
|
+
const entity = new Entity();
|
|
41
|
+
entity.add(TestProduct, {
|
|
42
|
+
sku: 'SKU123',
|
|
43
|
+
name: 'Test Product',
|
|
44
|
+
price: 99.99,
|
|
45
|
+
inStock: true
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const component = entity.getInMemory(TestProduct);
|
|
49
|
+
expect(component?.inStock).toBe(true);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
test('component handles false boolean correctly', () => {
|
|
53
|
+
const entity = new Entity();
|
|
54
|
+
entity.add(TestProduct, {
|
|
55
|
+
sku: 'SKU123',
|
|
56
|
+
name: 'Test Product',
|
|
57
|
+
price: 99.99,
|
|
58
|
+
inStock: false
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
const component = entity.getInMemory(TestProduct);
|
|
62
|
+
expect(component?.inStock).toBe(false);
|
|
63
|
+
});
|
|
64
|
+
|
|
65
|
+
test('component handles number fields', () => {
|
|
66
|
+
const entity = new Entity();
|
|
67
|
+
entity.add(TestProduct, {
|
|
68
|
+
sku: 'SKU123',
|
|
69
|
+
name: 'Test Product',
|
|
70
|
+
price: 123.45,
|
|
71
|
+
inStock: true
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const component = entity.getInMemory(TestProduct);
|
|
75
|
+
expect(component?.price).toBe(123.45);
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
test('component handles zero value correctly', () => {
|
|
79
|
+
const entity = new Entity();
|
|
80
|
+
entity.add(TestUser, { name: 'John', email: 'john@test.com', age: 0 });
|
|
81
|
+
|
|
82
|
+
const component = entity.getInMemory(TestUser);
|
|
83
|
+
expect(component?.age).toBe(0);
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
test('component handles Date fields', () => {
|
|
87
|
+
const date = new Date('2024-01-15T10:30:00Z');
|
|
88
|
+
const entity = new Entity();
|
|
89
|
+
entity.add(TestOrder, {
|
|
90
|
+
orderNumber: 'ORD-001',
|
|
91
|
+
total: 150.00,
|
|
92
|
+
status: 'pending',
|
|
93
|
+
createdAt: date
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
const component = entity.getInMemory(TestOrder);
|
|
97
|
+
expect(component?.createdAt).toEqual(date);
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
describe('multiple components', () => {
|
|
102
|
+
test('handles multiple components of different types', () => {
|
|
103
|
+
const entity = new Entity();
|
|
104
|
+
entity.add(TestUser, { name: 'John', email: 'john@test.com', age: 30 });
|
|
105
|
+
entity.add(TestProduct, { sku: 'SKU1', name: 'Product 1', price: 50, inStock: true });
|
|
106
|
+
entity.add(TestOrder, {
|
|
107
|
+
orderNumber: 'ORD-001',
|
|
108
|
+
total: 150,
|
|
109
|
+
status: 'completed',
|
|
110
|
+
createdAt: new Date()
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
expect(entity.componentList().length).toBe(3);
|
|
114
|
+
expect(entity.hasInMemory(TestUser)).toBe(true);
|
|
115
|
+
expect(entity.hasInMemory(TestProduct)).toBe(true);
|
|
116
|
+
expect(entity.hasInMemory(TestOrder)).toBe(true);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
test('each component maintains its own data', () => {
|
|
120
|
+
const entity = new Entity();
|
|
121
|
+
entity.add(TestUser, { name: 'User Name', email: 'user@test.com', age: 25 });
|
|
122
|
+
entity.add(TestProduct, { sku: 'PROD1', name: 'Product Name', price: 100, inStock: true });
|
|
123
|
+
|
|
124
|
+
const user = entity.getInMemory(TestUser);
|
|
125
|
+
const product = entity.getInMemory(TestProduct);
|
|
126
|
+
|
|
127
|
+
expect(user?.name).toBe('User Name');
|
|
128
|
+
expect(product?.name).toBe('Product Name');
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
test('removing one component does not affect others', () => {
|
|
132
|
+
const entity = new Entity();
|
|
133
|
+
entity.add(TestUser, { name: 'John', email: 'john@test.com', age: 30 });
|
|
134
|
+
entity.add(TestProduct, { sku: 'SKU1', name: 'Product', price: 50, inStock: true });
|
|
135
|
+
|
|
136
|
+
entity.remove(TestUser);
|
|
137
|
+
|
|
138
|
+
expect(entity.hasInMemory(TestUser)).toBe(false);
|
|
139
|
+
expect(entity.hasInMemory(TestProduct)).toBe(true);
|
|
140
|
+
expect(entity.getInMemory(TestProduct)?.name).toBe('Product');
|
|
141
|
+
});
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
describe('component replacement', () => {
|
|
145
|
+
test('adding same component type replaces existing', () => {
|
|
146
|
+
const entity = new Entity();
|
|
147
|
+
entity.add(TestUser, { name: 'Original', email: 'original@test.com', age: 30 });
|
|
148
|
+
entity.add(TestUser, { name: 'Replaced', email: 'replaced@test.com', age: 25 });
|
|
149
|
+
|
|
150
|
+
const components = entity.componentList();
|
|
151
|
+
expect(components.length).toBe(1);
|
|
152
|
+
|
|
153
|
+
const user = entity.getInMemory(TestUser);
|
|
154
|
+
expect(user?.name).toBe('Replaced');
|
|
155
|
+
expect(user?.email).toBe('replaced@test.com');
|
|
156
|
+
});
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
describe('component serialization', () => {
|
|
160
|
+
test('serializableData returns proper format', () => {
|
|
161
|
+
const entity = new Entity();
|
|
162
|
+
entity.add(TestUser, { name: 'John', email: 'john@test.com', age: 30 });
|
|
163
|
+
|
|
164
|
+
const component = entity.getInMemory(TestUser);
|
|
165
|
+
const data = component?.serializableData();
|
|
166
|
+
|
|
167
|
+
expect(data).toEqual({
|
|
168
|
+
name: 'John',
|
|
169
|
+
email: 'john@test.com',
|
|
170
|
+
age: 30,
|
|
171
|
+
bio: undefined
|
|
172
|
+
});
|
|
173
|
+
});
|
|
174
|
+
|
|
175
|
+
test('serializableData handles Date as ISO string', () => {
|
|
176
|
+
const date = new Date('2024-01-15T10:30:00.000Z');
|
|
177
|
+
const entity = new Entity();
|
|
178
|
+
entity.add(TestOrder, {
|
|
179
|
+
orderNumber: 'ORD-001',
|
|
180
|
+
total: 100,
|
|
181
|
+
status: 'pending',
|
|
182
|
+
createdAt: date
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
const component = entity.getInMemory(TestOrder);
|
|
186
|
+
const data = component?.serializableData();
|
|
187
|
+
|
|
188
|
+
expect(data?.createdAt).toBe('2024-01-15T10:30:00.000Z');
|
|
189
|
+
});
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
describe('component state', () => {
|
|
193
|
+
test('new component is not persisted', () => {
|
|
194
|
+
const entity = new Entity();
|
|
195
|
+
entity.add(TestUser, { name: 'John', email: 'john@test.com', age: 30 });
|
|
196
|
+
|
|
197
|
+
const component = entity.getInMemory(TestUser);
|
|
198
|
+
expect((component as any)._persisted).toBe(false);
|
|
199
|
+
});
|
|
200
|
+
|
|
201
|
+
test('new component is dirty', () => {
|
|
202
|
+
const entity = new Entity();
|
|
203
|
+
entity.add(TestUser, { name: 'John', email: 'john@test.com', age: 30 });
|
|
204
|
+
|
|
205
|
+
const component = entity.getInMemory(TestUser);
|
|
206
|
+
expect((component as any)._dirty).toBe(false); // Initial state from constructor
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
test('component has type ID', () => {
|
|
210
|
+
const entity = new Entity();
|
|
211
|
+
entity.add(TestUser, { name: 'John', email: 'john@test.com', age: 30 });
|
|
212
|
+
|
|
213
|
+
const component = entity.getInMemory(TestUser);
|
|
214
|
+
expect(component?.getTypeID()).toBeDefined();
|
|
215
|
+
expect(component?.getTypeID().length).toBeGreaterThan(0);
|
|
216
|
+
});
|
|
217
|
+
|
|
218
|
+
test('component properties returns correct list', () => {
|
|
219
|
+
const entity = new Entity();
|
|
220
|
+
entity.add(TestUser, { name: 'John', email: 'john@test.com', age: 30 });
|
|
221
|
+
|
|
222
|
+
const component = entity.getInMemory(TestUser);
|
|
223
|
+
const props = component?.properties();
|
|
224
|
+
|
|
225
|
+
expect(props).toContain('name');
|
|
226
|
+
expect(props).toContain('email');
|
|
227
|
+
expect(props).toContain('age');
|
|
228
|
+
expect(props).toContain('bio');
|
|
229
|
+
});
|
|
230
|
+
|
|
231
|
+
test('indexedProperties returns only indexed fields', () => {
|
|
232
|
+
const entity = new Entity();
|
|
233
|
+
entity.add(TestUser, { name: 'John', email: 'john@test.com', age: 30 });
|
|
234
|
+
|
|
235
|
+
const component = entity.getInMemory(TestUser);
|
|
236
|
+
const indexed = component?.indexedProperties();
|
|
237
|
+
|
|
238
|
+
expect(indexed).toContain('name');
|
|
239
|
+
expect(indexed).toContain('email');
|
|
240
|
+
expect(indexed).not.toContain('age');
|
|
241
|
+
expect(indexed).not.toContain('bio');
|
|
242
|
+
});
|
|
243
|
+
});
|
|
244
|
+
});
|