|
1 | 1 | // Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
2 | 2 |
|
3 | 3 | import { describe, it, expect, vi } from 'vitest'; |
| 4 | +import { LiteKernel, ObjectKernel } from '@objectstack/core'; |
| 5 | +import type { Plugin, PluginContext } from '@objectstack/core'; |
| 6 | +import { KnowledgeServicePlugin } from '../knowledge-service-plugin.js'; |
4 | 7 | import { |
5 | 8 | DOC_HASH_VERSION, |
6 | 9 | KnowledgeService, |
@@ -1373,3 +1376,93 @@ describe('KnowledgeService — search hands the adapter the caller\'s organizati |
1373 | 1376 | expect(logger.error).toHaveBeenCalledWith(expect.stringContaining('tenancy posture could not be read')); |
1374 | 1377 | }); |
1375 | 1378 | }); |
| 1379 | + |
| 1380 | +// The plugin's `resolveTenancyPosture` wiring is the only production reader of |
| 1381 | +// the posture (ADR-0161 K8, item 9): broken, every deployment reads as |
| 1382 | +// `single`, no document is stamped and no filter is handed, silently. So the |
| 1383 | +// wiring is driven through real kernels with a `tenancy` service: `LiteKernel` |
| 1384 | +// (no async accessor: the sync leg) and `ObjectKernel` (`getServiceAsync`). |
| 1385 | +describe('KnowledgeServicePlugin reads the tenancy posture from the kernel (ADR-0161 K8, item 9)', () => { |
| 1386 | + const PERSON = { |
| 1387 | + userId: 'u1', tenantId: 'org_a', accessible_org_ids: ['org_a'], positions: [], permissions: [], systemPermissions: [], isSystem: false, |
| 1388 | + } as never; |
| 1389 | + |
| 1390 | + class FakeHostPlugin implements Plugin { |
| 1391 | + name = 'test.fake.host'; |
| 1392 | + version = '1.0.0'; |
| 1393 | + type = 'standard' as const; |
| 1394 | + constructor(private readonly tenancy: unknown) {} |
| 1395 | + async init(ctx: PluginContext): Promise<void> { |
| 1396 | + ctx.registerService('objectql', { find: vi.fn(async () => []) }); |
| 1397 | + if (this.tenancy !== undefined) ctx.registerService('tenancy', this.tenancy); |
| 1398 | + } |
| 1399 | + } |
| 1400 | + |
| 1401 | + async function boot(Kernel: typeof LiteKernel | typeof ObjectKernel, tenancy: unknown) { |
| 1402 | + // `ObjectKernel` refuses to start without a `data` service; this harness composes none. |
| 1403 | + const kernel = Kernel === ObjectKernel |
| 1404 | + ? new ObjectKernel({ skipSystemValidation: true, logger: { level: 'silent' }, gracefulShutdown: false }) |
| 1405 | + : new LiteKernel(); |
| 1406 | + await kernel.use(new FakeHostPlugin(tenancy)); |
| 1407 | + await kernel.use(new KnowledgeServicePlugin({ enableEventSync: false, sources: [objectSource('s1', 'task')] })); |
| 1408 | + await kernel.bootstrap(); |
| 1409 | + const svc = kernel instanceof ObjectKernel |
| 1410 | + ? await kernel.getServiceAsync<KnowledgeService>('knowledge') |
| 1411 | + : kernel.getService<KnowledgeService>('knowledge'); |
| 1412 | + const adapter = makeAdapter('memory'); |
| 1413 | + svc.registerAdapter('memory', adapter); |
| 1414 | + return { kernel, svc, adapter }; |
| 1415 | + } |
| 1416 | + |
| 1417 | + it.each([ |
| 1418 | + ['LiteKernel', LiteKernel], |
| 1419 | + ['ObjectKernel', ObjectKernel], |
| 1420 | + ] as const)('%s: a walled posture hands the adapter the caller\'s organizations', async (_name, Kernel) => { |
| 1421 | + const { kernel, svc, adapter } = await boot(Kernel, { posture: 'isolated' }); |
| 1422 | + await svc.search('q', { executionContext: PERSON }); |
| 1423 | + expect(adapter.searchSpy).toHaveBeenCalledOnce(); |
| 1424 | + expect(adapter.searchSpy.mock.calls[0][1]).toMatchObject({ organizationIds: ['org_a'] }); |
| 1425 | + await kernel.shutdown(); |
| 1426 | + }); |
| 1427 | + |
| 1428 | + it('no `tenancy` service registered: no wall exists, and no filter is handed', async () => { |
| 1429 | + const { kernel, svc, adapter } = await boot(LiteKernel, undefined); |
| 1430 | + await svc.search('q', { executionContext: PERSON }); |
| 1431 | + expect(adapter.searchSpy).toHaveBeenCalledOnce(); |
| 1432 | + expect(adapter.searchSpy.mock.calls[0][1]).not.toHaveProperty('organizationIds'); |
| 1433 | + await kernel.shutdown(); |
| 1434 | + }); |
| 1435 | + |
| 1436 | + it('a `tenancy` service that is registered but cannot be read: no object source is searched', async () => { |
| 1437 | + const unreadable = Object.defineProperty({}, 'posture', { |
| 1438 | + get: () => { |
| 1439 | + throw new Error('tenancy store down'); |
| 1440 | + }, |
| 1441 | + }); |
| 1442 | + const { kernel, svc, adapter } = await boot(LiteKernel, unreadable); |
| 1443 | + expect(await svc.search('q', { executionContext: PERSON })).toEqual([]); |
| 1444 | + expect(adapter.searchSpy).not.toHaveBeenCalled(); |
| 1445 | + await kernel.shutdown(); |
| 1446 | + }); |
| 1447 | +}); |
| 1448 | + |
| 1449 | +describe('KnowledgeService — an event sync whose tenancy posture cannot be read (ADR-0161 K8, item 9)', () => { |
| 1450 | + it('logs at warn and leaves the stored document as it was: no upsert, no delete', async () => { |
| 1451 | + const adapter = makeAdapter('memory'); |
| 1452 | + const row = { id: 'r1', title: 'A', notes: 'B', status: 'open', organization_id: 'org_a' }; |
| 1453 | + const warn = vi.fn(); |
| 1454 | + const svc = new KnowledgeService({ |
| 1455 | + dataEngine: { find: vi.fn(async () => [row]) } as unknown as IDataEngine, |
| 1456 | + logger: { warn }, |
| 1457 | + resolveTenancyPosture: async () => { |
| 1458 | + throw new Error('tenancy store down'); |
| 1459 | + }, |
| 1460 | + }); |
| 1461 | + svc.registerAdapter('memory', adapter); |
| 1462 | + svc.registerSource(objectSource('s1', 'task')); |
| 1463 | + await svc.handleRecordUpsert('task', row); |
| 1464 | + expect(adapter.upsertSpy).not.toHaveBeenCalled(); |
| 1465 | + expect(adapter.deleteSpy).not.toHaveBeenCalled(); |
| 1466 | + expect(warn).toHaveBeenCalledWith(expect.stringContaining('tenancy store down')); |
| 1467 | + }); |
| 1468 | +}); |
0 commit comments