-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathmockHooks.ts
More file actions
113 lines (99 loc) · 2.71 KB
/
mockHooks.ts
File metadata and controls
113 lines (99 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { Task, TaskIdentifier, TaskOutput, TaskPayload } from "@trigger.dev/sdk/v3";
import { MockHooksCallInfo, TaskRunContext } from "./types.js";
/**
* A registry of all hook calls
*/
class MockHooksRegistry {
private hookCalls: Record<string, MockHooksCallInfo> = {};
/**
* Record a hook call
*/
recordHookCall<TPayload = any, TOutput = any, TError = any>(
taskId: string,
hookType: keyof MockHooksCallInfo,
payload?: TPayload,
output?: TOutput,
error?: TError
) {
if (!this.hookCalls[taskId]) {
this.hookCalls[taskId] = {
onStartCalled: false,
onSuccessCalled: false,
onFailureCalled: false,
onCompleteCalled: false,
};
}
const hookCallInfo = this.hookCalls[taskId];
hookCallInfo[hookType] = true;
if (payload !== undefined) {
hookCallInfo.payload = payload;
}
if (output !== undefined) {
hookCallInfo.output = output;
}
if (error !== undefined) {
hookCallInfo.error = error;
}
}
/**
* Get hook calls for a specific task
*/
getHookCallsForTask(taskId: string): MockHooksCallInfo | undefined {
return this.hookCalls[taskId];
}
/**
* Clear all hook calls
*/
clearHookCalls() {
this.hookCalls = {};
}
}
export const mockHooksRegistry = new MockHooksRegistry();
/**
* Create mock hooks for a task
*/
export function createMockHooks<
TIdentifier extends string,
TInput = void,
TOutput = unknown
>(
task: Task<TIdentifier, TInput, TOutput>
) {
return {
onStart: async (payload: TInput, ctx: TaskRunContext) => {
mockHooksRegistry.recordHookCall(task.id, "onStartCalled", payload);
},
onSuccess: async (payload: TInput, output: TOutput) => {
mockHooksRegistry.recordHookCall(task.id, "onSuccessCalled", payload, output);
},
onFailure: async (payload: TInput, error: Error) => {
mockHooksRegistry.recordHookCall(task.id, "onFailureCalled", payload, undefined, error);
},
onComplete: async (payload: TInput, result: { ok: boolean; data?: TOutput; error?: Error }) => {
mockHooksRegistry.recordHookCall(
task.id,
"onCompleteCalled",
payload,
result.ok ? result.data : undefined,
result.ok ? undefined : result.error
);
},
};
}
/**
* Verify that a specific hook was called for a task
*/
export function verifyHookCalled<
TIdentifier extends string,
TInput = void,
TOutput = unknown
>(
task: Task<TIdentifier, TInput, TOutput>,
hookType: keyof Omit<MockHooksCallInfo, "payload" | "output" | "error">
): boolean {
const hookCalls = mockHooksRegistry.getHookCallsForTask(task.id);
if (!hookCalls) {
return false;
}
return hookCalls[hookType];
}