-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathplugin.test.ts
More file actions
280 lines (248 loc) · 12.9 KB
/
Copy pathplugin.test.ts
File metadata and controls
280 lines (248 loc) · 12.9 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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect, vi } from 'vitest';
import { MetadataPlugin } from './plugin';
import { NodeMetadataManager } from './node-metadata-manager';
vi.mock('@objectstack/core', async (orig) => {
const real = (await orig()) as any;
return {
...real,
createLogger: () => ({
info: vi.fn(),
warn: vi.fn(),
error: vi.fn(),
debug: vi.fn(),
}),
};
});
describe('MetadataPlugin — bootstrap × watch coupling (D2)', () => {
it('attaches a filesystem watcher in eager mode when watch=true', () => {
const plugin = new MetadataPlugin({
watch: true,
config: { bootstrap: 'eager' },
});
const mgr = (plugin as any).manager as NodeMetadataManager;
expect((mgr as any).watcher).toBeDefined();
// Cleanup
return mgr.stopWatching();
});
it('attaches a filesystem watcher in lazy mode when watch=true', () => {
const plugin = new MetadataPlugin({
watch: true,
config: { bootstrap: 'lazy' },
});
const mgr = (plugin as any).manager as NodeMetadataManager;
expect((mgr as any).watcher).toBeDefined();
return mgr.stopWatching();
});
it('NEVER attaches a filesystem watcher in artifact-only mode', () => {
const plugin = new MetadataPlugin({
watch: true, // explicitly requested — must be ignored
config: { bootstrap: 'artifact-only' },
});
const mgr = (plugin as any).manager as NodeMetadataManager;
expect((mgr as any).watcher).toBeUndefined();
});
it('honors watch=false in eager mode', () => {
const plugin = new MetadataPlugin({
watch: false,
config: { bootstrap: 'eager' },
});
const mgr = (plugin as any).manager as NodeMetadataManager;
expect((mgr as any).watcher).toBeUndefined();
});
});
// ─────────────────────────────────────────────────────────────────────────
// PR-10e regression: artifact view items have no top-level `name`. Their
// identity is the target object (encoded in `list.data.object` /
// `form.data.object`). When `_parseAndRegisterArtifact` consumes a
// compiled artifact it must derive the view name from the inner data
// source — otherwise views are silently SKIPPED and reads through
// `metadataService.get('view', <object>)` return undefined, falling
// back to the boot-time SchemaRegistry copy and breaking HMR data
// reload.
// ─────────────────────────────────────────────────────────────────────────
describe('MetadataPlugin._parseAndRegisterArtifact — view name resolution (PR-10e)', () => {
it('registers view items by their target object even when top-level `name` is absent', async () => {
const plugin = new MetadataPlugin({
watch: false,
config: { bootstrap: 'eager' },
environmentId: 'proj_test',
});
const mgr = (plugin as any).manager as NodeMetadataManager;
const fakeCtx = {
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
} as any;
const artifact = {
id: 'com.example.test',
name: 'test',
version: '0.0.0',
type: 'app',
scope: 'app',
namespace: 'test',
defaultDatasource: 'memory',
views: [
{
// intentionally NO top-level name — mirrors compiled artifact shape
list: { name: 'all_case', label: 'All Cases', type: 'grid',
data: { provider: 'object', object: 'case' }, columns: [] },
listViews: {
case_workflow: { name: 'case_workflow', label: 'Service Workflow', type: 'kanban',
data: { provider: 'object', object: 'case' }, columns: [] },
},
},
],
};
await (plugin as any)._parseAndRegisterArtifact(fakeCtx, artifact, 'test-artifact');
const registered = await mgr.get('view', 'case');
expect(registered).toBeDefined();
const label = (registered as any)?.listViews?.case_workflow?.label;
expect(label).toBe('Service Workflow');
});
});
// ─────────────────────────────────────────────────────────────────────────
// ADR-0046 regression: a compiled artifact carries package docs in a
// top-level `docs: DocSchema[]` array. The artifact loader registers only
// the metadata fields enumerated in ARTIFACT_FIELD_TO_TYPE; `docs` was
// omitted, so the bundle's docs were silently dropped and GET /meta/doc
// returned an empty list even though the package shipped docs. The field
// must map to the `doc` type so docs register like any other item.
// ─────────────────────────────────────────────────────────────────────────
describe('MetadataPlugin._parseAndRegisterArtifact — package docs (ADR-0046)', () => {
it('registers `doc` items from the artifact `docs` array', async () => {
const plugin = new MetadataPlugin({
watch: false,
config: { bootstrap: 'eager' },
environmentId: 'proj_test',
});
const mgr = (plugin as any).manager as NodeMetadataManager;
const fakeCtx = {
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
} as any;
const artifact = {
id: 'com.example.docs',
name: 'test',
version: '0.0.0',
type: 'app',
scope: 'app',
namespace: 'test',
defaultDatasource: 'memory',
docs: [
{ name: 'test_index', label: 'Overview', content: '# Overview\n' },
{ name: 'test_guide', content: '# Guide\n' },
],
};
await (plugin as any)._parseAndRegisterArtifact(fakeCtx, artifact, 'test-artifact');
const index = await mgr.get('doc', 'test_index');
expect(index).toBeDefined();
expect((index as any)?.content).toContain('# Overview');
const guide = await mgr.get('doc', 'test_guide');
expect(guide).toBeDefined();
});
});
// ─────────────────────────────────────────────────────────────────────────
// Filesystem-scanner provenance: when the host declares its package id
// (options.packageId — the project's `defineStack` manifest id), scanned
// source-file metadata must be stamped `_packageId`/`_provenance` exactly
// like the artifact path, so GET /meta consumers (objectui
// NavigationSyncEffect) can tell code-defined items from user-authored
// rows. Without the option, items must stay unstamped — `_packageId`
// feeds isArtifactBacked() write authorization.
// ─────────────────────────────────────────────────────────────────────────
describe('MetadataPlugin._loadFromFileSystem — package provenance stamping', () => {
const fakeCtx = {
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
} as any;
it('stamps _packageId/_provenance on scanned items when options.packageId is set', async () => {
const plugin = new MetadataPlugin({
watch: false,
config: { bootstrap: 'eager' },
packageId: 'com.example.proj',
});
const mgr = (plugin as any).manager as NodeMetadataManager;
vi.spyOn(mgr, 'loadMany').mockImplementation(async (type: string) =>
type === 'page' ? [{ name: 'home_page', label: 'Home' }] : []);
await (plugin as any)._loadFromFileSystem(fakeCtx);
const item = await mgr.get('page', 'home_page') as any;
expect(item).toBeDefined();
expect(item._packageId).toBe('com.example.proj');
expect(item._provenance).toBe('package');
});
it('does not overwrite an item\'s pre-existing _packageId', async () => {
const plugin = new MetadataPlugin({
watch: false,
config: { bootstrap: 'eager' },
packageId: 'com.example.proj',
});
const mgr = (plugin as any).manager as NodeMetadataManager;
vi.spyOn(mgr, 'loadMany').mockImplementation(async (type: string) =>
type === 'page' ? [{ name: 'vendor_page', _packageId: 'com.vendor.pkg' }] : []);
await (plugin as any)._loadFromFileSystem(fakeCtx);
const item = await mgr.get('page', 'vendor_page') as any;
expect(item._packageId).toBe('com.vendor.pkg');
expect(item._provenance).toBe('package');
});
it('leaves scanned items unstamped when options.packageId is not configured', async () => {
const plugin = new MetadataPlugin({
watch: false,
config: { bootstrap: 'eager' },
});
const mgr = (plugin as any).manager as NodeMetadataManager;
vi.spyOn(mgr, 'loadMany').mockImplementation(async (type: string) =>
type === 'page' ? [{ name: 'plain_page', label: 'Plain' }] : []);
await (plugin as any)._loadFromFileSystem(fakeCtx);
const item = await mgr.get('page', 'plain_page') as any;
expect(item).toBeDefined();
expect(item._packageId).toBeUndefined();
expect(item._provenance).toBeUndefined();
});
});
// ─────────────────────────────────────────────────────────────────────────
// ADR-0067 regression: the package-scoped commit log `sys_metadata_commit`
// must be registered among the queryable system objects so per-project
// (cloud) env kernels provision the table at boot. Every publish/apply
// writes a commit row via `publishPackageDrafts`; the object was omitted
// from `queryableMetadataObjects` (only the standalone ObjectQLPlugin
// `environmentId === undefined` path had it), so env builds logged
// `no such table: sys_metadata_commit` and the commit timeline recorded
// nothing. init() must register it via the manifest — next to its
// `sys_metadata_history` sibling — whenever registerSystemObjects is on.
// ─────────────────────────────────────────────────────────────────────────
describe('MetadataPlugin — system object provisioning (ADR-0067 commit log)', () => {
function fakeCtxWithManifest() {
const registered: any[] = [];
const manifest = { register: vi.fn((m: any) => registered.push(m)) };
const ctx = {
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
registerService: vi.fn(),
getService: vi.fn((name: string) => (name === 'manifest' ? manifest : undefined)),
} as any;
return { ctx, registered };
}
it('registers sys_metadata_commit alongside its history sibling (env kernel)', async () => {
const plugin = new MetadataPlugin({
watch: false,
config: { bootstrap: 'lazy' },
environmentId: 'proj_test',
// registerSystemObjects defaults to true → env-kernel provisioning path
});
const { ctx, registered } = fakeCtxWithManifest();
await plugin.init(ctx);
const names = registered.flatMap((m) => m.objects ?? []).map((o: any) => o.name);
// The bug: history present, commit absent → the table is never provisioned.
expect(names).toContain('sys_metadata_history');
expect(names).toContain('sys_metadata_commit');
});
it('registers NOTHING when registerSystemObjects=false (control-plane kernel)', async () => {
const plugin = new MetadataPlugin({
watch: false,
config: { bootstrap: 'lazy' },
environmentId: 'proj_test',
registerSystemObjects: false,
});
const { ctx, registered } = fakeCtxWithManifest();
await plugin.init(ctx);
// Control-plane owns these tables; per-ADR they must NOT leak into a
// kernel that opted out of system-object registration.
expect(registered).toHaveLength(0);
});
});