-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathprotocol-boot-hydration-scoped.test.ts
More file actions
176 lines (158 loc) · 6.88 KB
/
Copy pathprotocol-boot-hydration-scoped.test.ts
File metadata and controls
176 lines (158 loc) · 6.88 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
// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license.
/**
* #4624 — boot hydration (`loadMetaFromDb`) grafts each overlay row's
* protection envelope from ITS OWN package (ADR-0048 / #1828).
*
* Pre-fix, the non-object branch of `loadMetaFromDb` kept a third inline
* copy of the overlay→SchemaRegistry rule and looked the artifact up
* UNSCOPED (`lookupArtifactItem(type, name)` without the row's
* `package_id`) — the exact pre-#1828 shape: with two installed packages
* shipping the same `type`/`name`, a name-colliding overlay row grafted
* the FIRST-registered package's `_lock`/`_packageId`/`_provenance` onto
* another package's row at boot (composite-scan first-match by Map
* iteration order).
*
* Post-fix the branch delegates to the ONE shared
* `hydrateOverlayIntoRegistry` (#4521), so the ADR-0048 package-scoped
* lookup applies at boot exactly as it does on the read-side hydration
* and the write-through.
*/
import { describe, it, expect } from 'vitest';
import { ObjectStackProtocolImplementation } from '@objectstack/metadata-protocol';
import { SchemaRegistry } from './registry.js';
const PKG_A = 'com.acme.a';
const PKG_B = 'com.acme.b';
function artifactPage(pkg: string, label: string) {
return {
name: 'home',
label,
_packageId: pkg,
_packageVersion: '1.0.0',
_provenance: 'package',
_lock: 'full',
_lockReason: `Locked by ${pkg}`,
};
}
interface Row {
id: string;
type: string;
name: string;
organization_id: string | null;
package_id: string | null;
state: string;
metadata: string;
}
function makeEngine(registry: SchemaRegistry, rows: Row[]) {
const matches = (r: Row, where: Record<string, unknown>): boolean => {
for (const [k, v] of Object.entries(where)) {
if (v === undefined) continue;
if ((r as any)[k] !== v) return false;
}
return true;
};
const engine: any = {
registry,
async find(_t: string, opts: { where: Record<string, unknown> }) {
return rows.filter((r) => matches(r, opts.where));
},
async findOne(_t: string, opts: { where: Record<string, unknown> }) {
return rows.find((r) => matches(r, opts.where)) ?? null;
},
async insert() { return { id: 'x' }; },
async update() { return { id: 'x' }; },
async delete() { return { deleted: 0 }; },
};
return engine;
}
function overlayRow(partial: Partial<Row> & { name: string; metadata: unknown }): Row {
return {
id: `r_${partial.name}_${partial.package_id ?? 'global'}`,
type: 'page',
organization_id: null,
package_id: null,
state: 'active',
...partial,
metadata: typeof partial.metadata === 'string'
? partial.metadata
: JSON.stringify(partial.metadata),
} as Row;
}
describe('loadMetaFromDb — ADR-0048 package-scoped protection graft at boot (#4624)', () => {
it('grafts the envelope from the row\'s OWN package, not the first-registered one', async () => {
const registry = new SchemaRegistry({ multiTenant: false });
registry.logLevel = 'silent';
// Package A registers FIRST — pre-fix, the unscoped composite scan
// returned A for every same-named row, whatever package owned it.
registry.registerItem('page', artifactPage(PKG_A, 'A Home'), 'name', PKG_A);
registry.registerItem('page', artifactPage(PKG_B, 'B Home'), 'name', PKG_B);
const rows = [
overlayRow({
name: 'home',
package_id: PKG_B,
metadata: { name: 'home', label: 'B Home (customized)' },
}),
];
const engine = makeEngine(registry, rows);
const protocol = new ObjectStackProtocolImplementation(engine);
const res = await protocol.loadMetaFromDb();
expect(res.loaded).toBe(1);
expect(res.errors).toBe(0);
// The hydrated plain-key entry carries package B's envelope —
// pre-fix it carried PKG_A's (`_packageId: 'com.acme.a'`,
// `_lockReason: 'Locked by com.acme.a'`).
const direct: any = registry.getItem('page', 'home');
expect(direct.label).toBe('B Home (customized)'); // overlay content wins
expect(direct._packageId).toBe(PKG_B);
expect(direct._lock).toBe('full');
expect(direct._lockReason).toBe(`Locked by ${PKG_B}`);
expect(direct._provenance).toBe('package');
});
it('registers the row unchanged when artifacts have not loaded yet (boot-order no-op)', async () => {
// Empty registry at hydration time — the scoped lookup finds
// nothing, exactly like the unscoped one did, and the row
// registers without a grafted envelope. Artifact-after-hydration
// boot orders are unaffected by the scoping.
const registry = new SchemaRegistry({ multiTenant: false });
registry.logLevel = 'silent';
const rows = [
overlayRow({
name: 'home',
package_id: PKG_B,
metadata: { name: 'home', label: 'B Home (customized)' },
}),
];
const engine = makeEngine(registry, rows);
const protocol = new ObjectStackProtocolImplementation(engine);
const res = await protocol.loadMetaFromDb();
expect(res.loaded).toBe(1);
const direct: any = registry.getItem('page', 'home');
expect(direct.label).toBe('B Home (customized)');
expect(direct._lock).toBeUndefined();
expect(direct._packageId).toBeUndefined();
expect(direct._provenance).toBeUndefined();
});
it('keeps the legacy best-effort graft for package-less (global) rows', async () => {
// A row with no package binding keeps the pre-existing unscoped
// first-match semantics — identical to the read-side hydration.
const registry = new SchemaRegistry({ multiTenant: false });
registry.logLevel = 'silent';
registry.registerItem('page', artifactPage(PKG_A, 'A Home'), 'name', PKG_A);
registry.registerItem('page', artifactPage(PKG_B, 'B Home'), 'name', PKG_B);
const rows = [
overlayRow({
name: 'home',
package_id: null,
metadata: { name: 'home', label: 'Global overlay' },
}),
];
const engine = makeEngine(registry, rows);
const protocol = new ObjectStackProtocolImplementation(engine);
await protocol.loadMetaFromDb();
const direct: any = registry.getItem('page', 'home');
expect(direct.label).toBe('Global overlay');
// Best-effort first-match: SOME package's envelope is grafted
// (legacy behaviour, unchanged by #4624 — do not over-pin which).
expect([PKG_A, PKG_B]).toContain(direct._packageId);
expect(direct._lock).toBe('full');
});
});