|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #39388 — a package-bound flow's run records the checksum its history row is |
| 5 | + * keyed by. |
| 6 | + * |
| 7 | + * The run-identity pinning (#28405, ADR-0146 D9 item 4) holds that a run |
| 8 | + * records the checksum of the flow version it ran, and that checksum is the |
| 9 | + * one the version's `sys_metadata_history` row is keyed by: a resume finds the |
| 10 | + * body again by that hash. The body the engine is handed for a flow bound to a |
| 11 | + * package is a SERVED body, and the read stamps the derived ADR-0010 |
| 12 | + * provenance on it (`_packageId`; `_packageVersion` and `_provenance` where an |
| 13 | + * artifact or a hydration states them), which the write door strips before it |
| 14 | + * stores and hashes the body. Hashed as handed over, the run's checksum named |
| 15 | + * no history row. |
| 16 | + * |
| 17 | + * One kernel over sqlite, the real metadata protocol (the producer behind |
| 18 | + * `PUT /meta/flow/:name`) and the real engine: a flow saved into an authored |
| 19 | + * package parks a run, and the run row's `flow_checksum` is compared with the |
| 20 | + * history row the save wrote — never with a hash this test computes. A loose |
| 21 | + * (package-less) flow is pinned beside it, unchanged. |
| 22 | + * |
| 23 | + * Its own file, not a describe in `flow-version-pinning.integration.test.ts` |
| 24 | + * (the suite's shared harness): that file is held by another open change. |
| 25 | + */ |
| 26 | + |
| 27 | +import { describe, it, expect, afterEach, vi } from 'vitest'; |
| 28 | +import { ObjectKernel } from '@objectstack/core'; |
| 29 | +import { ObjectQLPlugin, type ObjectQL } from '@objectstack/objectql'; |
| 30 | +import { SqlDriver } from '@objectstack/driver-sql'; |
| 31 | + |
| 32 | +import { AutomationServicePlugin } from './plugin.js'; |
| 33 | +import type { AutomationEngine } from './engine.js'; |
| 34 | +import { installDefaultOrganization } from './default-organization.testkit.js'; |
| 35 | + |
| 36 | +/** `ObjectStackProtocolImplementation`'s save door — what `PUT /meta/flow/:name` calls. */ |
| 37 | +interface MetadataWriteDoors { |
| 38 | + saveMetaItem(request: { type: string; name: string; item: unknown; packageId?: string }): Promise<unknown>; |
| 39 | +} |
| 40 | + |
| 41 | +const SYSTEM = { isSystem: true } as never; |
| 42 | +/** An authored package the package-bound save goes into, as `POST /packages` installs one. */ |
| 43 | +const PKG = 'app.expenses'; |
| 44 | + |
| 45 | +/** start → a screen with one required field (the run parks) → end. */ |
| 46 | +function reviewFlow(name: string) { |
| 47 | + return { |
| 48 | + name, |
| 49 | + label: 'Expense review', |
| 50 | + type: 'screen', |
| 51 | + version: 1, |
| 52 | + nodes: [ |
| 53 | + { id: 'start', type: 'start', label: 'Start' }, |
| 54 | + { |
| 55 | + id: 'approve', |
| 56 | + type: 'screen', |
| 57 | + label: 'Approve', |
| 58 | + config: { fields: [{ name: 'decision', label: 'Decision', type: 'text', required: true }] }, |
| 59 | + }, |
| 60 | + { id: 'end', type: 'end', label: 'End' }, |
| 61 | + ], |
| 62 | + edges: [ |
| 63 | + { id: 'e1', source: 'start', target: 'approve' }, |
| 64 | + { id: 'e2', source: 'approve', target: 'end' }, |
| 65 | + ], |
| 66 | + }; |
| 67 | +} |
| 68 | + |
| 69 | +describe('[#39388] a run of a package-bound flow records the checksum its history row is keyed by', () => { |
| 70 | + let kernel: ObjectKernel | undefined; |
| 71 | + |
| 72 | + afterEach(async () => { |
| 73 | + try { await kernel?.shutdown(); } catch { /* noop */ } |
| 74 | + kernel = undefined; |
| 75 | + }); |
| 76 | + |
| 77 | + async function boot() { |
| 78 | + kernel = new ObjectKernel({ logger: { level: 'fatal' }, gracefulShutdown: false } as never); |
| 79 | + await kernel.use(new ObjectQLPlugin()); |
| 80 | + await kernel.use({ |
| 81 | + name: 'test.sqlite-memory', type: 'standard', version: '1.0.0', dependencies: [], |
| 82 | + async init(ctx: { registerService(name: string, service: unknown): void }) { |
| 83 | + const driver = new SqlDriver({ client: 'better-sqlite3', connection: { filename: ':memory:' }, useNullAsDefault: true }); |
| 84 | + await driver.connect(); |
| 85 | + ctx.registerService('driver.default', driver); |
| 86 | + }, |
| 87 | + async start() {}, |
| 88 | + } as never); |
| 89 | + await kernel.use(new AutomationServicePlugin()); |
| 90 | + await kernel.bootstrap(); |
| 91 | + const ql = kernel.getService<ObjectQL>('objectql'); |
| 92 | + await installDefaultOrganization(ql); |
| 93 | + (ql.registry as unknown as { installPackage(m: object, how: string): void }) |
| 94 | + .installPackage({ id: PKG, name: PKG, version: '1.0.0', type: 'app', namespace: 'expenses' }, 'authored'); |
| 95 | + return { |
| 96 | + ql, |
| 97 | + automation: kernel.getService<AutomationEngine>('automation'), |
| 98 | + protocol: kernel.getService<MetadataWriteDoors>('protocol'), |
| 99 | + }; |
| 100 | + } |
| 101 | + |
| 102 | + /** The checksum the save keyed the flow's one history row by, and the parked run's recorded identity. */ |
| 103 | + async function parkAndRead(life: Awaited<ReturnType<typeof boot>>, name: string) { |
| 104 | + await vi.waitFor(async () => { |
| 105 | + expect((await life.automation.getFlow(name))?.version).toBe(1); |
| 106 | + }, { timeout: 3000, interval: 20 }); |
| 107 | + const run = await life.automation.execute(name, {} as never); |
| 108 | + expect(run.status).toBe('paused'); |
| 109 | + const history = await life.ql.find('sys_metadata_history', { |
| 110 | + where: { type: 'flow', name }, context: SYSTEM, |
| 111 | + }) as Array<Record<string, unknown>>; |
| 112 | + expect(history, 'the save wrote one history row').toHaveLength(1); |
| 113 | + const parked = await life.ql.findOne('sys_automation_run', { where: { id: run.runId }, context: SYSTEM }); |
| 114 | + return { historyChecksum: history[0]!.checksum, runChecksum: parked?.flow_checksum }; |
| 115 | + } |
| 116 | + |
| 117 | + it('a flow saved into a package: the run checksum is its history row\'s, though the armed body is stamped with the package', async () => { |
| 118 | + const life = await boot(); |
| 119 | + const name = 'expense_review_pkg'; |
| 120 | + await life.protocol.saveMetaItem({ type: 'flow', name, item: reviewFlow(name), packageId: PKG }); |
| 121 | + |
| 122 | + const { historyChecksum, runChecksum } = await parkAndRead(life, name); |
| 123 | + expect(historyChecksum).toMatch(/^sha256:/); |
| 124 | + expect(runChecksum).toBe(historyChecksum); |
| 125 | + }); |
| 126 | + |
| 127 | + it('a loose (package-less) flow: the run checksum is its history row\'s, as before', async () => { |
| 128 | + const life = await boot(); |
| 129 | + const name = 'expense_review_loose'; |
| 130 | + // The loose row is laid straight into the store once (no history row), |
| 131 | + // and the save below runs in place: a package-less CREATE is not the |
| 132 | + // door this pin is about (ADR-0070 D1 moves it to refusal). |
| 133 | + await life.ql.insert('sys_metadata', { |
| 134 | + type: 'flow', name, namespace: 'default', package_id: null, scope: 'platform', strategy: 'merge', |
| 135 | + state: 'active', version: 0, metadata: JSON.stringify({ ...reviewFlow(name), version: 0 }), |
| 136 | + }, { context: SYSTEM } as never); |
| 137 | + await life.protocol.saveMetaItem({ type: 'flow', name, item: reviewFlow(name) }); |
| 138 | + |
| 139 | + const { historyChecksum, runChecksum } = await parkAndRead(life, name); |
| 140 | + expect(historyChecksum).toMatch(/^sha256:/); |
| 141 | + expect(runChecksum).toBe(historyChecksum); |
| 142 | + }); |
| 143 | +}); |
0 commit comments