|
1 | 1 | import { runInContext } from 'node:vm'; |
2 | 2 | import type { WorkflowRuntimeError } from '@workflow/errors'; |
3 | 3 | import { describe, expect, it } from 'vitest'; |
| 4 | +import { getStepFunction, registerStepFunction } from './private.js'; |
4 | 5 | import { |
5 | 6 | dehydrateStepArguments, |
6 | 7 | dehydrateStepReturnValue, |
7 | 8 | dehydrateWorkflowArguments, |
8 | 9 | dehydrateWorkflowReturnValue, |
| 10 | + getCommonRevivers, |
9 | 11 | getStreamType, |
| 12 | + getWorkflowReducers, |
10 | 13 | hydrateWorkflowArguments, |
11 | 14 | } from './serialization.js'; |
12 | | -import { STREAM_NAME_SYMBOL } from './symbols.js'; |
| 15 | +import { STEP_FUNCTION_NAME_SYMBOL, STREAM_NAME_SYMBOL } from './symbols.js'; |
13 | 16 | import { createContext } from './vm/index.js'; |
14 | 17 |
|
15 | 18 | describe('getStreamType', () => { |
@@ -783,3 +786,130 @@ describe('step return value', () => { |
783 | 786 | ); |
784 | 787 | }); |
785 | 788 | }); |
| 789 | + |
| 790 | +describe('step function serialization', () => { |
| 791 | + const { globalThis: vmGlobalThis } = createContext({ |
| 792 | + seed: 'test', |
| 793 | + fixedTimestamp: 1714857600000, |
| 794 | + }); |
| 795 | + |
| 796 | + it('should detect step function by checking for STEP_FUNCTION_NAME_SYMBOL', () => { |
| 797 | + const stepName = 'myStep'; |
| 798 | + const stepFn = async (x: number) => x * 2; |
| 799 | + |
| 800 | + // Attach the symbol like useStep() does |
| 801 | + Object.defineProperty(stepFn, STEP_FUNCTION_NAME_SYMBOL, { |
| 802 | + value: stepName, |
| 803 | + writable: false, |
| 804 | + enumerable: false, |
| 805 | + configurable: false, |
| 806 | + }); |
| 807 | + |
| 808 | + // Verify the symbol is attached correctly |
| 809 | + expect((stepFn as any)[STEP_FUNCTION_NAME_SYMBOL]).toBe(stepName); |
| 810 | + }); |
| 811 | + |
| 812 | + it('should not have STEP_FUNCTION_NAME_SYMBOL on regular functions', () => { |
| 813 | + const regularFn = async (x: number) => x * 2; |
| 814 | + |
| 815 | + // Regular functions should not have the symbol |
| 816 | + expect((regularFn as any)[STEP_FUNCTION_NAME_SYMBOL]).toBeUndefined(); |
| 817 | + }); |
| 818 | + |
| 819 | + it('should lookup registered step function by name', () => { |
| 820 | + const stepName = 'myRegisteredStep'; |
| 821 | + const stepFn = async (x: number) => x * 2; |
| 822 | + |
| 823 | + // Register the step function |
| 824 | + registerStepFunction(stepName, stepFn); |
| 825 | + |
| 826 | + // Should be retrievable by name |
| 827 | + const retrieved = getStepFunction(stepName); |
| 828 | + expect(retrieved).toBe(stepFn); |
| 829 | + }); |
| 830 | + |
| 831 | + it('should return undefined for non-existent registered step function', () => { |
| 832 | + const retrieved = getStepFunction('nonExistentStep'); |
| 833 | + expect(retrieved).toBeUndefined(); |
| 834 | + }); |
| 835 | + |
| 836 | + it('should deserialize step function name through reviver', () => { |
| 837 | + const stepName = 'testStep'; |
| 838 | + const stepFn = async () => 42; |
| 839 | + |
| 840 | + // Register the step function |
| 841 | + registerStepFunction(stepName, stepFn); |
| 842 | + |
| 843 | + // Get the reviver and test it directly |
| 844 | + const revivers = getCommonRevivers(vmGlobalThis); |
| 845 | + const result = revivers.StepFunction(stepName); |
| 846 | + |
| 847 | + expect(result).toBe(stepFn); |
| 848 | + }); |
| 849 | + |
| 850 | + it('should throw error when reviver cannot find registered step function', () => { |
| 851 | + const revivers = getCommonRevivers(vmGlobalThis); |
| 852 | + |
| 853 | + let err: Error | undefined; |
| 854 | + try { |
| 855 | + revivers.StepFunction('nonExistentStep'); |
| 856 | + } catch (err_) { |
| 857 | + err = err_ as Error; |
| 858 | + } |
| 859 | + |
| 860 | + expect(err).toBeDefined(); |
| 861 | + expect(err?.message).toContain('Step function "nonExistentStep" not found'); |
| 862 | + expect(err?.message).toContain('Make sure the step function is registered'); |
| 863 | + }); |
| 864 | + |
| 865 | + it('should dehydrate step function passed as argument to a step', () => { |
| 866 | + const stepName = 'step//workflows/test.ts//myStep'; |
| 867 | + const stepFn = async (x: number) => x * 2; |
| 868 | + |
| 869 | + // Register the step function |
| 870 | + registerStepFunction(stepName, stepFn); |
| 871 | + |
| 872 | + // Attach the symbol to the function (like the SWC compiler would) |
| 873 | + Object.defineProperty(stepFn, STEP_FUNCTION_NAME_SYMBOL, { |
| 874 | + value: stepName, |
| 875 | + writable: false, |
| 876 | + enumerable: false, |
| 877 | + configurable: false, |
| 878 | + }); |
| 879 | + |
| 880 | + // Simulate passing a step function as an argument within a workflow |
| 881 | + // When calling a step from within a workflow context |
| 882 | + const args = [stepFn, 42]; |
| 883 | + |
| 884 | + // This should serialize the step function by its name using the reducer |
| 885 | + const dehydrated = dehydrateStepArguments(args, globalThis); |
| 886 | + |
| 887 | + // Verify it dehydrated successfully |
| 888 | + expect(dehydrated).toBeDefined(); |
| 889 | + expect(Array.isArray(dehydrated)).toBe(true); |
| 890 | + // The dehydrated structure is the flattened format from devalue |
| 891 | + // It should contain the step function serialized as its name |
| 892 | + expect(dehydrated).toContain(stepName); |
| 893 | + expect(dehydrated).toContain(42); |
| 894 | + }); |
| 895 | + |
| 896 | + it('should serialize step function to name through reducer', () => { |
| 897 | + const stepName = 'step//workflows/test.ts//anotherStep'; |
| 898 | + const stepFn = async () => 'result'; |
| 899 | + |
| 900 | + // Attach the symbol to the function (like the SWC compiler would) |
| 901 | + Object.defineProperty(stepFn, STEP_FUNCTION_NAME_SYMBOL, { |
| 902 | + value: stepName, |
| 903 | + writable: false, |
| 904 | + enumerable: false, |
| 905 | + configurable: false, |
| 906 | + }); |
| 907 | + |
| 908 | + // Get the reducer and verify it detects the step function |
| 909 | + const reducer = getWorkflowReducers(globalThis).StepFunction; |
| 910 | + const result = reducer(stepFn); |
| 911 | + |
| 912 | + // Should return the step name |
| 913 | + expect(result).toBe(stepName); |
| 914 | + }); |
| 915 | +}); |
0 commit comments