|
| 1 | +import type { Event } from '@workflow/world'; |
| 2 | +import * as nanoid from 'nanoid'; |
| 3 | +import { monotonicFactory } from 'ulid'; |
| 4 | +import { describe, expect, it, vi } from 'vitest'; |
| 5 | +import { createCorrelationIdGenerator } from './correlation-id.js'; |
| 6 | +import { EventsConsumer } from './events-consumer.js'; |
| 7 | +import type { WorkflowOrchestratorContext } from './private.js'; |
| 8 | +import { ReplayPayloadCache } from './replay-payload-cache.js'; |
| 9 | +import { dehydrateStepReturnValue } from './serialization.js'; |
| 10 | +import { createUseStep } from './step.js'; |
| 11 | +import { createContext } from './vm/index.js'; |
| 12 | +import { createCreateHook } from './workflow/hook.js'; |
| 13 | +import { createSleep } from './workflow/sleep.js'; |
| 14 | + |
| 15 | +/** |
| 16 | + * Correlation-id stability seen through the primitives that actually mint ids, |
| 17 | + * rather than through the generator alone: that a step's id survives another |
| 18 | + * kind of entity being created alongside it, and that a replay consumes an event |
| 19 | + * log carrying the ids a same-seeded replay derives. |
| 20 | + * |
| 21 | + * The rest of the replay suites author their event logs with literal correlation |
| 22 | + * ids from the shared sequence and pin themselves to it. These fixtures derive |
| 23 | + * their ids instead, so they hold under either scheme. |
| 24 | + */ |
| 25 | + |
| 26 | +const SEED = 'test'; |
| 27 | +const FIXED_TIMESTAMP = 1753481739458; |
| 28 | + |
| 29 | +function setupWorkflowContext( |
| 30 | + events: Event[], |
| 31 | + perKind: boolean |
| 32 | +): WorkflowOrchestratorContext { |
| 33 | + const context = createContext({ |
| 34 | + seed: SEED, |
| 35 | + fixedTimestamp: FIXED_TIMESTAMP, |
| 36 | + }); |
| 37 | + const ulid = monotonicFactory(() => context.globalThis.Math.random()); |
| 38 | + return { |
| 39 | + runId: 'wrun_test', |
| 40 | + encryptionKey: undefined, |
| 41 | + replayPayloadCache: new ReplayPayloadCache(undefined), |
| 42 | + globalThis: context.globalThis, |
| 43 | + eventsConsumer: new EventsConsumer(events, { |
| 44 | + onUnconsumedEvent: () => {}, |
| 45 | + getPromiseQueue: () => Promise.resolve(), |
| 46 | + }), |
| 47 | + invocationsQueue: new Map(), |
| 48 | + generateCorrelationId: createCorrelationIdGenerator({ |
| 49 | + seed: SEED, |
| 50 | + fixedTimestamp: FIXED_TIMESTAMP, |
| 51 | + positional: () => ulid(FIXED_TIMESTAMP), |
| 52 | + perKind, |
| 53 | + }), |
| 54 | + generateNanoid: nanoid.customRandom(nanoid.urlAlphabet, 21, (size) => |
| 55 | + new Uint8Array(size).map(() => 256 * context.globalThis.Math.random()) |
| 56 | + ), |
| 57 | + onWorkflowError: vi.fn(), |
| 58 | + promiseQueue: Promise.resolve(), |
| 59 | + pendingDeliveries: 0, |
| 60 | + pendingDeliveryBarriers: new Map(), |
| 61 | + }; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * The id the next step of a replay would claim. Nothing in the log resolves the |
| 66 | + * step, so the returned promise stays pending by design: the queue item is what |
| 67 | + * we are after. |
| 68 | + */ |
| 69 | +function probeStepId( |
| 70 | + perKind: boolean, |
| 71 | + before?: (ctx: WorkflowOrchestratorContext) => void |
| 72 | +): string { |
| 73 | + const ctx = setupWorkflowContext([], perKind); |
| 74 | + before?.(ctx); |
| 75 | + void createUseStep(ctx)('add')(1, 2).catch(() => {}); |
| 76 | + const item = [...ctx.invocationsQueue.values()].find( |
| 77 | + (entry) => entry.type === 'step' |
| 78 | + ); |
| 79 | + if (!item) { |
| 80 | + throw new Error('expected a step invocation'); |
| 81 | + } |
| 82 | + return item.correlationId; |
| 83 | +} |
| 84 | + |
| 85 | +function createHookAndSleep(ctx: WorkflowOrchestratorContext): void { |
| 86 | + createCreateHook(ctx)(); |
| 87 | + void createSleep(ctx)('1h').catch(() => {}); |
| 88 | +} |
| 89 | + |
| 90 | +describe('correlation ids through the replay primitives', () => { |
| 91 | + it('keeps a step id when a hook and a sleep are created before it', () => { |
| 92 | + expect(probeStepId(true, createHookAndSleep)).toBe(probeStepId(true)); |
| 93 | + }); |
| 94 | + |
| 95 | + it('renumbers that step under one sequence shared by every kind', () => { |
| 96 | + // The failure this PR removes, and the reason the assertion above is worth |
| 97 | + // making: with a shared sequence the hook and the sleep consume the two |
| 98 | + // ordinals the step would otherwise have drawn from. |
| 99 | + expect(probeStepId(false, createHookAndSleep)).not.toBe(probeStepId(false)); |
| 100 | + }); |
| 101 | + |
| 102 | + it('consumes a step_completed authored with the derived id', async () => { |
| 103 | + const correlationId = probeStepId(true, createHookAndSleep); |
| 104 | + const ctx = setupWorkflowContext( |
| 105 | + [ |
| 106 | + { |
| 107 | + eventId: 'evnt_0', |
| 108 | + runId: 'wrun_test', |
| 109 | + eventType: 'step_completed', |
| 110 | + correlationId, |
| 111 | + eventData: { |
| 112 | + stepName: 'add', |
| 113 | + result: await dehydrateStepReturnValue(3, 'wrun_test', undefined), |
| 114 | + }, |
| 115 | + createdAt: new Date(), |
| 116 | + }, |
| 117 | + ], |
| 118 | + true |
| 119 | + ); |
| 120 | + createHookAndSleep(ctx); |
| 121 | + await expect(createUseStep(ctx)('add')(1, 2)).resolves.toBe(3); |
| 122 | + expect(ctx.onWorkflowError).not.toHaveBeenCalled(); |
| 123 | + }); |
| 124 | +}); |
0 commit comments