|
26 | 26 | import '@objectstack/metadata'; |
27 | 27 | import { describe, it, expect, vi } from 'vitest'; |
28 | 28 | import { ObjectKernel, ServiceLifecycle } from '@objectstack/core'; |
| 29 | +import type { Plugin, PluginContext } from '@objectstack/core'; |
| 30 | +// The platform's knowledge service, for the doors' wording of its own verdict. |
| 31 | +import { KnowledgeServicePlugin, type KnowledgeService } from '@objectstack/service-knowledge'; |
29 | 32 | import { HttpDispatcher } from '../http-dispatcher.js'; |
30 | 33 |
|
31 | 34 | const ctx = (executionContext: any): any => ({ request: {}, environmentId: 'platform', executionContext }); |
@@ -415,3 +418,123 @@ describe('/packages — what the gate leaves alone', () => { |
415 | 418 | expect(s.registry.installPackage).toHaveBeenCalled(); |
416 | 419 | }); |
417 | 420 | }); |
| 421 | + |
| 422 | +/** |
| 423 | + * The verdict the platform's own knowledge service answers, through both doors |
| 424 | + * that ask it: the package duplicate and the `/meta` save. A real kernel runs |
| 425 | + * `KnowledgeServicePlugin` over host doubles (the engine, the security |
| 426 | + * service, the metadata protocol), so the sentence each door returns is the |
| 427 | + * one `admitAuthoredSource` composes for that door, not a double's. |
| 428 | + * |
| 429 | + * The duplicate's caller copies a package: never told to save a source, and |
| 430 | + * told once, by the door's prefix, that nothing was copied. The `/meta` |
| 431 | + * save's sentences are the control, pinned byte for byte. |
| 432 | + */ |
| 433 | +describe('/packages duplicate and /meta save on a real kernel running KnowledgeServicePlugin: each door words the service\'s own verdict', () => { |
| 434 | + const ARTICLE = { |
| 435 | + name: 'article', |
| 436 | + fields: { id: { type: 'text' }, title: { type: 'text' }, body: { type: 'textarea' }, salary: { type: 'textarea' } }, |
| 437 | + }; |
| 438 | + const FIELDS = Object.keys(ARTICLE.fields); |
| 439 | + type Arm = 'transient' | 'content' | 'where' | 'title'; |
| 440 | + const sourceFor = (arm: Arm) => ({ |
| 441 | + name: 'faq', label: 'FAQ', adapter: 'memory', |
| 442 | + source: { |
| 443 | + kind: 'object', object: 'article', content: arm === 'content' ? ['body', 'salary'] : ['body'], |
| 444 | + ...(arm === 'where' ? { where: { salary: { $ne: null } } } : {}), |
| 445 | + }, |
| 446 | + }); |
| 447 | + /** What the save door answers today, byte for byte. */ |
| 448 | + const SAVE_VERDICT: Record<Arm, { status: number; code: string; message: string }> = { |
| 449 | + transient: { |
| 450 | + status: 503, code: 'SERVICE_UNAVAILABLE', |
| 451 | + message: 'The knowledge source "faq" was not saved: whether you may search object \'article\' could not be judged ' |
| 452 | + + '(registry unavailable), and an author must be allowed to search what their source indexes. Nothing was written; try again.', |
| 453 | + }, |
| 454 | + content: { |
| 455 | + status: 403, code: 'PERMISSION_DENIED', |
| 456 | + message: 'The knowledge source "faq" was not saved: you may not query field \'salary\' of object \'article\' (it is hidden ' |
| 457 | + + 'from you, or served to you masked), and the index would hold its text unmasked. Remove the field from `content` ' |
| 458 | + + 'and `metadataFields`, or have someone who may query it save the source.', |
| 459 | + }, |
| 460 | + where: { |
| 461 | + status: 403, code: 'PERMISSION_DENIED', |
| 462 | + message: 'The knowledge source "faq" was not saved: its `where` reads field \'salary\' of object \'article\', which you may ' |
| 463 | + + 'not query (it is hidden from you, or served to you masked), and the source would select its records by that ' |
| 464 | + + 'field. Remove the field from `where`, or have someone who may query it save the source.', |
| 465 | + }, |
| 466 | + title: { |
| 467 | + status: 403, code: 'PERMISSION_DENIED', |
| 468 | + message: 'The knowledge source "faq" was not saved: you may not query field \'title\' of object \'article\' (it is hidden ' |
| 469 | + + 'from you, or served to you masked), and the index would hold its text unmasked. The source does not list it: ' |
| 470 | + + "it is the object's display field, which every document carries as its title. Have someone who may query it save the source.", |
| 471 | + }, |
| 472 | + }; |
| 473 | + |
| 474 | + async function bootPlatformKnowledge(arm: Arm) { |
| 475 | + const s = make({ items: [sourceFor(arm)] }); |
| 476 | + const saveMetaItem = vi.fn(async ({ name }: any) => ({ success: true, name })); |
| 477 | + Object.assign(s.protocol, { saveMetaItem }); |
| 478 | + // The engine the knowledge service reads schemas through; the package registry rides on it. |
| 479 | + Object.assign(s.kernelServices.objectql, { |
| 480 | + getSchema: (name: string) => { |
| 481 | + if (arm === 'transient') throw new Error('registry unavailable'); |
| 482 | + return name === 'article' ? ARTICLE : undefined; |
| 483 | + }, |
| 484 | + find: vi.fn(async () => []), |
| 485 | + }); |
| 486 | + const hidden = arm === 'title' ? 'title' : arm === 'transient' ? undefined : 'salary'; |
| 487 | + const security = { |
| 488 | + getQueryableFields: vi.fn(async () => FIELDS.filter((f) => f !== hidden)), |
| 489 | + getReadableFields: vi.fn(async () => FIELDS), |
| 490 | + }; |
| 491 | + const doubles: Record<string, unknown> = { ...s.kernelServices, security }; |
| 492 | + const host: Plugin = { |
| 493 | + name: 'test.duplicate-door.host', |
| 494 | + version: '1.0.0', |
| 495 | + type: 'standard', |
| 496 | + async init(ctx: PluginContext) { |
| 497 | + for (const [name, service] of Object.entries(doubles)) ctx.registerService(name, service); |
| 498 | + }, |
| 499 | + } as Plugin; |
| 500 | + const kernel = new ObjectKernel({ gracefulShutdown: false, skipSystemValidation: true }); |
| 501 | + kernel.use(host); |
| 502 | + kernel.use(new KnowledgeServicePlugin({ enableEventSync: false })); |
| 503 | + await kernel.bootstrap(); |
| 504 | + const knowledge = kernel.getService<KnowledgeService>('knowledge'); |
| 505 | + knowledge.registerAdapter('memory', { id: 'memory', upsert: async () => undefined, delete: async () => undefined, search: async () => [] }); |
| 506 | + const dispatcher = new HttpDispatcher(kernel as any); |
| 507 | + return { |
| 508 | + ...s, kernel, saveMetaItem, |
| 509 | + duplicate: (caller: any) => dispatcher.handlePackages('/pkg-a/duplicate', 'POST', { targetPackageId: 'pkg-b' }, {}, caller), |
| 510 | + save: (caller: any) => dispatcher.handleMetadata('/knowledge_source/faq', caller, 'PUT', JSON.parse(JSON.stringify(sourceFor(arm)))), |
| 511 | + }; |
| 512 | + } |
| 513 | + |
| 514 | + for (const arm of ['transient', 'content', 'where', 'title'] as const) { |
| 515 | + it(`${arm}: the duplicate is told nothing was copied once and to duplicate the package, never to save; the /meta save's sentence is unchanged`, async () => { |
| 516 | + const stack = await bootPlatformKnowledge(arm); |
| 517 | + try { |
| 518 | + const expected = SAVE_VERDICT[arm]; |
| 519 | + const r = await stack.duplicate(authed(BOTH)); |
| 520 | + expect(r.response?.status).toBe(expected.status); |
| 521 | + expect(r.response?.body?.error?.code).toBe(expected.code); |
| 522 | + const message: string = r.response?.body?.error?.message; |
| 523 | + expect(message.startsWith('Duplicating package "pkg-a" is refused, and nothing was copied. The knowledge source "faq" cannot be copied: '), message).toBe(true); |
| 524 | + expect(message).not.toMatch(/save the source|save it again|Nothing was written|was not saved|the save /); |
| 525 | + expect(message.match(/nothing was copied/gi)).toHaveLength(1); |
| 526 | + expect(message).toMatch(arm === 'title' ? /Have someone who may query it duplicate the package\.$/ : /duplicate the package again/); |
| 527 | + expect(stack.protocol.duplicatePackage).not.toHaveBeenCalled(); |
| 528 | + |
| 529 | + // CONTROL: the same verdict at the /meta save door, byte for byte as before. |
| 530 | + const saved = await stack.save(authed(BOTH)); |
| 531 | + expect(saved.response?.status).toBe(expected.status); |
| 532 | + expect(saved.response?.body?.error?.code).toBe(expected.code); |
| 533 | + expect(saved.response?.body?.error?.message).toBe(expected.message); |
| 534 | + expect(stack.saveMetaItem).not.toHaveBeenCalled(); |
| 535 | + } finally { |
| 536 | + await stack.kernel.shutdown(); |
| 537 | + } |
| 538 | + }); |
| 539 | + } |
| 540 | +}); |
0 commit comments