-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtenancy-service.test.ts
More file actions
266 lines (241 loc) · 10.4 KB
/
Copy pathtenancy-service.test.ts
File metadata and controls
266 lines (241 loc) · 10.4 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
// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license.
import { describe, it, expect, vi } from 'vitest';
import { createTenancyService, resolveDefaultOrgId } from './tenancy-service.js';
function makeEngine(orgs: Array<{ id: string; slug?: string }>) {
return {
find: vi.fn(async (object: string, query: any) => {
if (object !== 'sys_organization') return [];
const where = query?.where ?? {};
let rows = orgs;
if (where.slug !== undefined) rows = rows.filter((o) => o.slug === where.slug);
return rows.slice(0, query?.limit ?? rows.length);
}),
};
}
describe('createTenancyService', () => {
it('single posture: no wall requested, isolation off', () => {
const t = createTenancyService({ requested: 'single', probeIsolation: () => false });
expect(t.posture).toBe('single');
expect(t.requestedPosture).toBe('single');
expect(t.isolationActive).toBe(false);
expect(t.requested).toBe(false);
expect(t.degraded).toBe(false);
});
it('isolated posture: requested and isolation active', () => {
const t = createTenancyService({ requested: 'isolated', probeIsolation: () => true });
expect(t.posture).toBe('isolated');
expect(t.isolationActive).toBe(true);
expect(t.degraded).toBe(false);
});
it('degraded: isolated requested but isolation NOT active', () => {
const t = createTenancyService({ requested: 'isolated', probeIsolation: () => false });
expect(t.posture).toBe('single'); // behaves single-org-like — nothing isolates
expect(t.requestedPosture).toBe('isolated');
expect(t.isolationActive).toBe(false);
expect(t.requested).toBe(true);
expect(t.degraded).toBe(true);
});
it('a throwing probe is treated as isolation off (fail-closed to single)', () => {
const t = createTenancyService({
requested: 'isolated',
probeIsolation: () => {
throw new Error('registry exploded');
},
});
expect(t.isolationActive).toBe(false);
expect(t.degraded).toBe(true);
});
it('re-reads the probe each access (org-scoping may register after construction)', () => {
let active = false;
const t = createTenancyService({ requested: 'isolated', probeIsolation: () => active });
expect(t.posture).toBe('single');
active = true; // org-scoping registers later
expect(t.posture).toBe('isolated');
expect(t.degraded).toBe(false);
});
// [ADR-0105 D1 / ADR-0081 D2] Multi-organization operation is an ENTITLEMENT.
// The wall's code is open, but activating either walled posture requires the
// enterprise org-scoping runtime — otherwise `group` would be a free multi-org
// back door around the `isolated` gate. The iron rule (cloud ADR-0016) is
// satisfied by refusing to run an unwalled multi-org deployment, not by giving
// the posture away: an unenforceable request resolves to `single` + degraded,
// and the CLI fails fast on that (ADR-0093 D5).
describe('group posture (entitled, like isolated)', () => {
it('is ACTIVE when the enterprise org-scoping service is present', () => {
const t = createTenancyService({ requested: 'group', probeIsolation: () => true });
expect(t.posture).toBe('group');
expect(t.isolationActive).toBe(true);
expect(t.degraded).toBe(false);
});
it('DEGRADES without the enterprise package — never a silent free multi-org', () => {
const probe = vi.fn(() => false);
const t = createTenancyService({ requested: 'group', probeIsolation: probe });
expect(t.requestedPosture).toBe('group');
expect(t.posture).toBe('single'); // behaves single-org — nothing walls it
// The probe is lazy — org-scoping registers after plugin-auth — so it
// fires on the first read of a derived fact, not at construction.
expect(probe).toHaveBeenCalled();
expect(t.isolationActive).toBe(false);
expect(t.requested).toBe(true);
expect(t.degraded).toBe(true);
});
it('never guesses a default org while active — membership is explicit', async () => {
const engine = makeEngine([{ id: 'org_default', slug: 'default' }]);
const t = createTenancyService({
requested: 'group',
probeIsolation: () => true,
getEngine: () => engine,
});
expect(await t.defaultOrgId()).toBeNull();
});
});
// The legacy boolean shape stays accepted so an embedding that passes
// `resolveMultiOrgEnabled()` keeps working: true ⇒ isolated, false ⇒ single.
describe('legacy boolean `requested`', () => {
it('true maps to the isolated posture', () => {
const t = createTenancyService({ requested: true, probeIsolation: () => true });
expect(t.requestedPosture).toBe('isolated');
expect(t.posture).toBe('isolated');
});
it('false maps to the single posture', () => {
const t = createTenancyService({ requested: false, probeIsolation: () => true });
expect(t.requestedPosture).toBe('single');
expect(t.posture).toBe('single');
expect(t.isolationActive).toBe(false);
});
});
describe('defaultOrgId', () => {
it('isolated posture never guesses — returns null', async () => {
const engine = makeEngine([{ id: 'org_a' }, { id: 'org_b' }]);
const t = createTenancyService({
requested: true,
probeIsolation: () => true,
getEngine: () => engine,
});
expect(await t.defaultOrgId()).toBeNull();
expect(engine.find).not.toHaveBeenCalled(); // short-circuits before any query
});
it('single mode prefers the slug=default bootstrap org', async () => {
const engine = makeEngine([{ id: 'org_x' }, { id: 'org_default', slug: 'default' }]);
const t = createTenancyService({
requested: false,
probeIsolation: () => false,
getEngine: () => engine,
});
expect(await t.defaultOrgId()).toBe('org_default');
});
it('single mode falls back to the sole org when no default slug', async () => {
const engine = makeEngine([{ id: 'org_only' }]);
const t = createTenancyService({
requested: false,
probeIsolation: () => false,
getEngine: () => engine,
});
expect(await t.defaultOrgId()).toBe('org_only');
});
it('single mode returns null when the org is ambiguous (≥2, no default)', async () => {
const engine = makeEngine([{ id: 'org_a' }, { id: 'org_b' }]);
const t = createTenancyService({
requested: false,
probeIsolation: () => false,
getEngine: () => engine,
});
expect(await t.defaultOrgId()).toBeNull();
});
it('memoizes a positive resolution but re-resolves a null', async () => {
const orgs: Array<{ id: string; slug?: string }> = [];
const engine = makeEngine(orgs);
const t = createTenancyService({
requested: false,
probeIsolation: () => false,
getEngine: () => engine,
});
expect(await t.defaultOrgId()).toBeNull(); // not bootstrapped yet
orgs.push({ id: 'org_default', slug: 'default' }); // bootstrap runs
expect(await t.defaultOrgId()).toBe('org_default'); // re-resolved
const callsAfterResolve = engine.find.mock.calls.length;
await t.defaultOrgId(); // memoized — no new query
expect(engine.find.mock.calls.length).toBe(callsAfterResolve);
});
});
});
describe('resolveDefaultOrgId', () => {
it('returns null for a missing/invalid engine', async () => {
expect(await resolveDefaultOrgId(undefined)).toBeNull();
expect(await resolveDefaultOrgId({})).toBeNull();
});
});
// ---------------------------------------------------------------------------
// [ADR-0105 D12] Posture ENTITLEMENT is declared by the commercial runtime.
//
// Presence-of-package answers "may this deployment run multi-org at all", not
// "which shapes of it". Whether `group` and `isolated` are one tier or two is a
// packaging decision the enterprise runtime owns, so the open core asks instead
// of assuming — and fails closed on anything not entitled.
// ---------------------------------------------------------------------------
describe('posture entitlement declared by the org-scoping runtime', () => {
const installed = () => true;
it('entitles every walled posture when the runtime declares nothing (back-compat)', () => {
for (const posture of ['group', 'isolated'] as const) {
const t = createTenancyService({
requested: posture,
probeIsolation: installed,
probeEntitledPostures: () => undefined,
});
expect(t.posture, posture).toBe(posture);
expect(t.degraded).toBe(false);
}
});
it('activates a posture the runtime DOES entitle', () => {
const t = createTenancyService({
requested: 'group',
probeIsolation: installed,
probeEntitledPostures: () => ['group'],
});
expect(t.posture).toBe('group');
expect(t.isolationActive).toBe(true);
expect(t.degraded).toBe(false);
});
it('DEGRADES a posture the runtime does not entitle, even though it is installed', () => {
// e.g. a licence covering legal-entity isolation but not the group shape.
const t = createTenancyService({
requested: 'group',
probeIsolation: installed,
probeEntitledPostures: () => ['isolated'],
});
expect(t.requestedPosture).toBe('group');
expect(t.posture).toBe('single');
expect(t.isolationActive).toBe(false);
expect(t.degraded).toBe(true); // → the CLI refuses to boot (ADR-0093 D5)
});
it('fails closed on an EMPTY entitlement set', () => {
const t = createTenancyService({
requested: 'isolated',
probeIsolation: installed,
probeEntitledPostures: () => [],
});
expect(t.isolationActive).toBe(false);
expect(t.degraded).toBe(true);
});
it('fails closed when the entitlement probe throws', () => {
const t = createTenancyService({
requested: 'group',
probeIsolation: installed,
probeEntitledPostures: () => {
throw new Error('licence service unreachable');
},
});
expect(t.isolationActive).toBe(false);
expect(t.degraded).toBe(true);
});
it('never consults entitlement when the runtime is absent (already degraded)', () => {
const entitle = vi.fn(() => ['group'] as const);
const t = createTenancyService({
requested: 'group',
probeIsolation: () => false,
probeEntitledPostures: entitle,
});
expect(t.degraded).toBe(true);
expect(entitle).not.toHaveBeenCalled();
});
});