|
8 | 8 | // environment. |
9 | 9 |
|
10 | 10 | import { describe, it, expect, vi } from 'vitest'; |
| 11 | +import { LiteKernel } from '@objectstack/core'; |
| 12 | +import type { Plugin, PluginContext } from '@objectstack/core'; |
11 | 13 | import { AutomationEngine } from './engine.js'; |
12 | 14 | import type { FlowTrigger, FlowTriggerBinding } from './engine.js'; |
13 | 15 | import { InMemoryFlowActivationStore } from './flow-activation-store.js'; |
14 | 16 | import { registerSubflowNode } from './builtin/subflow-node.js'; |
15 | | -import { packagedFlowDefaultReader } from './plugin.js'; |
| 17 | +import { AutomationServicePlugin, packagedFlowDefaultReader } from './plugin.js'; |
16 | 18 | import type { AutomationContext } from '@objectstack/spec/contracts'; |
17 | 19 |
|
18 | 20 | function createTestLogger(): any { |
@@ -445,6 +447,179 @@ describe('#34655 (seat ruling on S2) — a declared `.custom` override does not |
445 | 447 | expect(stateOf(engine, 'draft_contract')).toMatchObject({ enabled: true }); |
446 | 448 | expect(reader).toHaveBeenCalledTimes(asked + 1); |
447 | 449 | }); |
| 450 | + |
| 451 | + it('(L2 S4) a change to the loader\'s set reaches a held override with no ledger row, both ways', async () => { |
| 452 | + const defaults: Record<string, boolean | undefined> = { draft_contract: false }; |
| 453 | + const { engine, isBound } = bootWithLoaderDefaults(defaults); |
| 454 | + engine.registerFlow('draft_contract', packagedFlow('draft_contract', { description: 'customized' }), OVERRIDE); |
| 455 | + expect(isBound('draft_contract')).toBe(false); |
| 456 | + |
| 457 | + // The base is upgraded to ship it on; the held override body is not re-registered. |
| 458 | + defaults.draft_contract = undefined; |
| 459 | + expect(engine.refreshPackagedFlowDefaults()).toEqual(['draft_contract']); |
| 460 | + expect(isBound('draft_contract')).toBe(true); |
| 461 | + expect(stateOf(engine, 'draft_contract')).toMatchObject({ enabled: true, bound: true }); |
| 462 | + expect((await engine.execute('draft_contract')).success).toBe(true); |
| 463 | + |
| 464 | + // And back to off. |
| 465 | + defaults.draft_contract = false; |
| 466 | + expect(engine.refreshPackagedFlowDefaults()).toEqual(['draft_contract']); |
| 467 | + expect(isBound('draft_contract')).toBe(false); |
| 468 | + expect(stateOf(engine, 'draft_contract')).toMatchObject({ enabled: false }); |
| 469 | + expect((await engine.execute('draft_contract')).code).toBe('FLOW_DISABLED'); |
| 470 | + }); |
| 471 | + |
| 472 | + it('(L2 S4) CONTROL: the refresh moves nothing a ledger row or the loader\'s own body decides', async () => { |
| 473 | + const defaults: Record<string, boolean | undefined> = { draft_contract: undefined, on_absent: undefined }; |
| 474 | + const { engine, isBound } = bootWithLoaderDefaults(defaults); |
| 475 | + engine.registerFlow('draft_contract', packagedFlow('draft_contract', { description: 'customized' }), OVERRIDE); |
| 476 | + await engine.toggleFlow('draft_contract', false); |
| 477 | + engine.registerFlow('on_absent', packagedFlow('on_absent', { defaultActive: false }), PULL); |
| 478 | + expect(isBound('draft_contract')).toBe(false); |
| 479 | + expect(isBound('on_absent')).toBe(false); |
| 480 | + |
| 481 | + // The reader flips both; the row decides the override, the loader's body decides its own flow. |
| 482 | + defaults.draft_contract = true; |
| 483 | + defaults.on_absent = true; |
| 484 | + expect(engine.refreshPackagedFlowDefaults()).toEqual([]); |
| 485 | + expect(isBound('draft_contract')).toBe(false); |
| 486 | + expect(isBound('on_absent')).toBe(false); |
| 487 | + }); |
| 488 | + |
| 489 | + it('(L2 N13) a body rolled back from the history reads the default of the set installed now', async () => { |
| 490 | + const defaults: Record<string, boolean | undefined> = { draft_contract: false }; |
| 491 | + const { engine } = bootWithLoaderDefaults(defaults); |
| 492 | + engine.registerFlow('draft_contract', packagedFlow('draft_contract', { description: 'v1' }), OVERRIDE); |
| 493 | + engine.registerFlow('draft_contract', packagedFlow('draft_contract', { description: 'v2', version: 2 }), OVERRIDE); |
| 494 | + const [first] = engine.getFlowVersionHistory('draft_contract').map((h) => h.version); |
| 495 | + expect(stateOf(engine, 'draft_contract')).toMatchObject({ enabled: false }); |
| 496 | + |
| 497 | + defaults.draft_contract = undefined; |
| 498 | + engine.refreshPackagedFlowDefaults(); |
| 499 | + engine.rollbackFlow('draft_contract', first); |
| 500 | + expect(((await engine.getFlow('draft_contract')) as { description?: string } | null)?.description).toBe('v1'); |
| 501 | + expect(stateOf(engine, 'draft_contract')).toMatchObject({ enabled: true }); |
| 502 | + }); |
| 503 | +}); |
| 504 | + |
| 505 | +describe('#34655 (L2 S4) — a hot upgrade that flips the base\'s default reaches an override the mutation sync armed', () => { |
| 506 | + const BASE = 'com.acme.crm'; |
| 507 | + const FLOW = 'draft_contract'; |
| 508 | + /** The pair's declared override, as the protocol's view serves it in the base's slot. */ |
| 509 | + const served = (label: string) => |
| 510 | + packagedFlow(FLOW, { label, _packageId: BASE, overrides: { package: BASE, basedOn: '1.0.0' } }); |
| 511 | + |
| 512 | + /** |
| 513 | + * The automation plugin over a protocol stand-in: the view serves the |
| 514 | + * override, the loader's set holds the base's own body with `baseDefault`, |
| 515 | + * and the test raises the mutation signal and `metadata:reloaded`. No |
| 516 | + * activation ledger: no row decides. |
| 517 | + */ |
| 518 | + async function bootComposition(baseDefault: boolean | undefined) { |
| 519 | + const state = { view: [served('OVERRIDE')], baseDefault }; |
| 520 | + const listeners: Array<(evt: { type: string; name: string; state: string; organizationId?: string | null }) => void> = []; |
| 521 | + const captured: { ctx?: PluginContext } = {}; |
| 522 | + const kernel = new LiteKernel({ logger: { level: 'silent' } } as never); |
| 523 | + kernel.use({ |
| 524 | + name: 'fake-composition', |
| 525 | + version: '1.0.0', |
| 526 | + async init(ctx: PluginContext) { |
| 527 | + captured.ctx = ctx; |
| 528 | + const register = (ctx as unknown as { registerService(n: string, s: unknown): void }).registerService; |
| 529 | + register('objectql', { registry: { listItems: () => [], getObject: () => undefined } }); |
| 530 | + register('protocol', { |
| 531 | + packagedArtifactOwner: (r: { type: string; name: string }) => (r.type === 'flow' && r.name === FLOW ? BASE : undefined), |
| 532 | + shippedArtifactsOf: (type: string, name: string) => |
| 533 | + type === 'flow' && name === FLOW |
| 534 | + ? [packagedFlow(FLOW, { |
| 535 | + label: 'BASE', |
| 536 | + _packageId: BASE, |
| 537 | + ...(state.baseDefault === undefined ? {} : { defaultActive: state.baseDefault }), |
| 538 | + })] |
| 539 | + : [], |
| 540 | + async getMetaItemsForExecution(q: { type: string }) { |
| 541 | + return { items: q.type === 'flow' ? state.view : [] }; |
| 542 | + }, |
| 543 | + onMetadataMutation(listener: (typeof listeners)[number]) { |
| 544 | + listeners.push(listener); |
| 545 | + return () => listeners.splice(listeners.indexOf(listener), 1); |
| 546 | + }, |
| 547 | + }); |
| 548 | + }, |
| 549 | + } as Plugin); |
| 550 | + kernel.use(new AutomationServicePlugin({ suspendedRunStore: 'memory' })); |
| 551 | + await kernel.bootstrap(); |
| 552 | + const engine = kernel.getService<AutomationEngine>('automation'); |
| 553 | + const recordChange = recordingTrigger('record_change'); |
| 554 | + engine.registerTrigger(recordChange.trigger); |
| 555 | + const armedLabel = async () => ((await engine.getFlow(FLOW)) as { label?: string } | null)?.label; |
| 556 | + return { |
| 557 | + kernel, |
| 558 | + engine, |
| 559 | + isBound: recordChange.isBound, |
| 560 | + registrations: () => engine.getFlowVersionHistory(FLOW).length, |
| 561 | + /** A direct active save of the override (no reload): the mutation sync arms it and marks its slot. */ |
| 562 | + async saveOverride(label: string) { |
| 563 | + state.view = [served(label)]; |
| 564 | + listeners.forEach((l) => l({ type: 'flow', name: FLOW, state: 'active', organizationId: null })); |
| 565 | + await vi.waitFor(async () => expect(await armedLabel()).toBe(label)); |
| 566 | + }, |
| 567 | + /** A hot upgrade of the base package: its own body's declaration moves, then the reload. */ |
| 568 | + async upgradeBase(next: boolean | undefined) { |
| 569 | + state.baseDefault = next; |
| 570 | + await captured.ctx!.trigger('metadata:reloaded', { changed: [`app/${BASE}`] }); |
| 571 | + }, |
| 572 | + }; |
| 573 | + } |
| 574 | + |
| 575 | + it('the base moves from on to off: the override stops', async () => { |
| 576 | + const c = await bootComposition(undefined); |
| 577 | + try { |
| 578 | + await c.saveOverride('OVERRIDE-2'); |
| 579 | + expect(c.isBound(FLOW)).toBe(true); |
| 580 | + const registrations = c.registrations(); |
| 581 | + |
| 582 | + await c.upgradeBase(false); |
| 583 | + expect(c.registrations(), 'the re-sync kept the body the mutation sync armed').toBe(registrations); |
| 584 | + expect(c.isBound(FLOW)).toBe(false); |
| 585 | + expect(stateOf(c.engine, FLOW)).toMatchObject({ enabled: false }); |
| 586 | + expect((await c.engine.execute(FLOW)).code).toBe('FLOW_DISABLED'); |
| 587 | + } finally { |
| 588 | + await c.kernel.shutdown(); |
| 589 | + } |
| 590 | + }); |
| 591 | + |
| 592 | + it('the base moves from off to on: the override starts', async () => { |
| 593 | + const c = await bootComposition(false); |
| 594 | + try { |
| 595 | + await c.saveOverride('OVERRIDE-2'); |
| 596 | + expect(c.isBound(FLOW)).toBe(false); |
| 597 | + const registrations = c.registrations(); |
| 598 | + |
| 599 | + await c.upgradeBase(true); |
| 600 | + expect(c.registrations(), 'the re-sync kept the body the mutation sync armed').toBe(registrations); |
| 601 | + expect(c.isBound(FLOW)).toBe(true); |
| 602 | + expect(stateOf(c.engine, FLOW)).toMatchObject({ enabled: true, bound: true }); |
| 603 | + expect((await c.engine.execute(FLOW)).success).toBe(true); |
| 604 | + } finally { |
| 605 | + await c.kernel.shutdown(); |
| 606 | + } |
| 607 | + }); |
| 608 | + |
| 609 | + it('CONTROL: a reload that leaves the base\'s declaration as it was moves nothing', async () => { |
| 610 | + const c = await bootComposition(false); |
| 611 | + try { |
| 612 | + await c.saveOverride('OVERRIDE-2'); |
| 613 | + const registrations = c.registrations(); |
| 614 | + |
| 615 | + await c.upgradeBase(false); |
| 616 | + expect(c.registrations()).toBe(registrations); |
| 617 | + expect(c.isBound(FLOW)).toBe(false); |
| 618 | + expect((await c.engine.execute(FLOW)).code).toBe('FLOW_DISABLED'); |
| 619 | + } finally { |
| 620 | + await c.kernel.shutdown(); |
| 621 | + } |
| 622 | + }); |
448 | 623 | }); |
449 | 624 |
|
450 | 625 | describe('#34655 — packagedFlowDefaultReader reads the package\'s own body off the loader\'s set', () => { |
|
0 commit comments