Skip to content

Commit 4b791f8

Browse files
Copilothotlong
andcommitted
Fix tests: update runtime mock, fix metadata access, remove obsolete test files
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent e54d6fc commit 4b791f8

File tree

6 files changed

+121
-833
lines changed

6 files changed

+121
-833
lines changed

packages/foundation/core/src/app.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
*/
88

99
import {
10-
MetadataRegistry,
10+
MetadataRegistry,
11+
MetadataItem,
1112
Driver,
1213
ObjectConfig,
1314
ObjectQLContext,
@@ -210,13 +211,15 @@ export class ObjectQL implements IObjectQL {
210211
}
211212

212213
getObject(name: string): ObjectConfig | undefined {
213-
return this.kernel.metadata.get<ObjectConfig>('object', name);
214+
const item = this.kernel.metadata.get<MetadataItem>('object', name);
215+
return item?.content as ObjectConfig | undefined;
214216
}
215217

216218
getConfigs(): Record<string, ObjectConfig> {
217219
const result: Record<string, ObjectConfig> = {};
218-
const objects = this.kernel.metadata.list<ObjectConfig>('object');
219-
for (const obj of objects) {
220+
const items = this.kernel.metadata.list<MetadataItem>('object');
221+
for (const item of items) {
222+
const obj = item.content as ObjectConfig;
220223
result[obj.name] = obj;
221224
}
222225
return result;

packages/foundation/core/test/__mocks__/@objectstack/runtime.ts

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,124 @@
77
* during the migration phase.
88
*/
99

10+
// Simple mock implementations of runtime managers
11+
class MockMetadataRegistry {
12+
private store = new Map<string, Map<string, any>>();
13+
14+
register(type: string, item: any): void {
15+
if (!this.store.has(type)) {
16+
this.store.set(type, new Map());
17+
}
18+
const typeMap = this.store.get(type)!;
19+
typeMap.set(item.id || item.name, item);
20+
}
21+
22+
get<T = any>(type: string, id: string): T | undefined {
23+
const typeMap = this.store.get(type);
24+
return typeMap?.get(id) as T | undefined;
25+
}
26+
27+
list<T = any>(type: string): T[] {
28+
const typeMap = this.store.get(type);
29+
if (!typeMap) return [];
30+
return Array.from(typeMap.values()) as T[];
31+
}
32+
33+
unregister(type: string, id: string): boolean {
34+
const typeMap = this.store.get(type);
35+
if (!typeMap) return false;
36+
return typeMap.delete(id);
37+
}
38+
39+
getTypes(): string[] {
40+
return Array.from(this.store.keys());
41+
}
42+
43+
unregisterPackage(packageName: string): void {
44+
// Simple implementation - in real runtime this would filter by package
45+
for (const [type, typeMap] of this.store.entries()) {
46+
const toDelete: string[] = [];
47+
for (const [id, item] of typeMap.entries()) {
48+
if (item.packageName === packageName || item.package === packageName) {
49+
toDelete.push(id);
50+
}
51+
}
52+
toDelete.forEach(id => typeMap.delete(id));
53+
}
54+
}
55+
}
56+
57+
class MockHookManager {
58+
private hooks = new Map<string, any[]>();
59+
60+
register(hookName: string, handler: any, packageName?: string): void {
61+
if (!this.hooks.has(hookName)) {
62+
this.hooks.set(hookName, []);
63+
}
64+
this.hooks.get(hookName)!.push({ handler, packageName });
65+
}
66+
67+
async trigger(hookName: string, context: any): Promise<void> {
68+
const handlers = this.hooks.get(hookName) || [];
69+
for (const { handler } of handlers) {
70+
await handler(context);
71+
}
72+
}
73+
74+
unregisterPackage(packageName: string): void {
75+
for (const [hookName, handlers] of this.hooks.entries()) {
76+
this.hooks.set(hookName, handlers.filter(h => h.packageName !== packageName));
77+
}
78+
}
79+
}
80+
81+
class MockActionManager {
82+
private actions = new Map<string, any>();
83+
84+
register(objectName: string, actionName: string, handler: any, packageName?: string): void {
85+
const key = `${objectName}.${actionName}`;
86+
this.actions.set(key, { handler, packageName });
87+
}
88+
89+
async execute(objectName: string, actionName: string, context: any): Promise<any> {
90+
const key = `${objectName}.${actionName}`;
91+
const action = this.actions.get(key);
92+
if (!action) {
93+
throw new Error(`Action ${actionName} not found for object ${objectName}`);
94+
}
95+
return await action.handler(context);
96+
}
97+
98+
get(objectName: string, actionName: string): any {
99+
const key = `${objectName}.${actionName}`;
100+
return this.actions.get(key)?.handler;
101+
}
102+
103+
unregisterPackage(packageName: string): void {
104+
const toDelete: string[] = [];
105+
for (const [key, action] of this.actions.entries()) {
106+
if (action.packageName === packageName) {
107+
toDelete.push(key);
108+
}
109+
}
110+
toDelete.forEach(key => this.actions.delete(key));
111+
}
112+
}
113+
10114
export class ObjectStackKernel {
11115
public ql: unknown = null;
116+
public metadata: MockMetadataRegistry;
117+
public hooks: MockHookManager;
118+
public actions: MockActionManager;
119+
12120
private plugins: any[] = [];
13121
private driver: any = null; // Will be set by the ObjectQL app
14122

15123
constructor(plugins: any[] = []) {
16124
this.plugins = plugins;
125+
this.metadata = new MockMetadataRegistry();
126+
this.hooks = new MockHookManager();
127+
this.actions = new MockActionManager();
17128
}
18129

19130
// Method to set the driver for delegation during migration

0 commit comments

Comments
 (0)