@flink-app/test-utils 1.0.0 → 2.0.0-alpha.49
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +12 -0
- package/dist/ai/conversation.d.ts +27 -0
- package/dist/ai/conversation.js +30 -0
- package/dist/ai/index.d.ts +4 -0
- package/dist/ai/index.js +20 -0
- package/dist/ai/mockContext.d.ts +16 -0
- package/dist/ai/mockContext.js +39 -0
- package/dist/ai/mockLLMAdapter.d.ts +90 -0
- package/dist/ai/mockLLMAdapter.js +252 -0
- package/dist/ai/mockTool.d.ts +64 -0
- package/dist/ai/mockTool.js +133 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/package.json +9 -5
- package/spec/ai/conversation.spec.ts +280 -0
- package/spec/ai/mockLLMAdapter.spec.ts +533 -0
- package/spec/ai/mockTool.spec.ts +313 -0
- package/spec/support/jasmine.json +7 -0
- package/src/ai/conversation.ts +54 -0
- package/src/ai/index.ts +4 -0
- package/src/ai/mockContext.ts +41 -0
- package/src/ai/mockLLMAdapter.ts +238 -0
- package/src/ai/mockTool.ts +135 -0
- package/src/index.ts +1 -0
- package/tsconfig.dist.json +4 -0
- package/tsconfig.json +6 -5
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { FlinkContext } from "@flink-app/flink";
|
|
3
|
+
import type { FlinkTool, FlinkToolProps, ToolResult } from "@flink-app/flink/ai";
|
|
4
|
+
|
|
5
|
+
export interface MockToolConfig<Input, Output> {
|
|
6
|
+
name: string;
|
|
7
|
+
description?: string;
|
|
8
|
+
inputSchema: z.ZodType<Input>;
|
|
9
|
+
outputSchema?: z.ZodType<Output>;
|
|
10
|
+
|
|
11
|
+
// Response strategies
|
|
12
|
+
response?: Output; // Canned success response
|
|
13
|
+
error?: { error: string; code?: string }; // Canned error response
|
|
14
|
+
fn?: FlinkTool<any, Input, Output>; // Custom function
|
|
15
|
+
|
|
16
|
+
// Permissions
|
|
17
|
+
permissions?: FlinkToolProps["permissions"];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface MockToolInvocation<Input> {
|
|
21
|
+
input: Input;
|
|
22
|
+
user?: any;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export interface MockToolResult<Input, Output> {
|
|
26
|
+
props: FlinkToolProps;
|
|
27
|
+
fn: FlinkTool<any, Input, Output>;
|
|
28
|
+
|
|
29
|
+
// Invocation tracking
|
|
30
|
+
invocations: MockToolInvocation<Input>[];
|
|
31
|
+
getLastInvocation(): MockToolInvocation<Input> | undefined;
|
|
32
|
+
reset(): void;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Creates a mock tool with tracking and canned responses
|
|
37
|
+
*
|
|
38
|
+
* Features:
|
|
39
|
+
* - Simple canned responses
|
|
40
|
+
* - Error simulation
|
|
41
|
+
* - Custom function support
|
|
42
|
+
* - Automatic invocation tracking
|
|
43
|
+
* - Validation helpers
|
|
44
|
+
*
|
|
45
|
+
* @example
|
|
46
|
+
* // Simple canned response
|
|
47
|
+
* const weatherTool = mockTool({
|
|
48
|
+
* name: "get_weather",
|
|
49
|
+
* inputSchema: z.object({ city: z.string() }),
|
|
50
|
+
* response: { temperature: 22, conditions: "sunny" }
|
|
51
|
+
* });
|
|
52
|
+
*
|
|
53
|
+
* @example
|
|
54
|
+
* // Custom function with tracking
|
|
55
|
+
* const calculatorTool = mockTool({
|
|
56
|
+
* name: "calculate",
|
|
57
|
+
* inputSchema: z.object({ a: z.number(), b: z.number() }),
|
|
58
|
+
* fn: async ({ input }) => ({
|
|
59
|
+
* success: true,
|
|
60
|
+
* data: { result: input.a + input.b }
|
|
61
|
+
* })
|
|
62
|
+
* });
|
|
63
|
+
*
|
|
64
|
+
* @example
|
|
65
|
+
* // Error simulation
|
|
66
|
+
* const failingTool = mockTool({
|
|
67
|
+
* name: "fail",
|
|
68
|
+
* inputSchema: z.object({}),
|
|
69
|
+
* error: { error: "Tool failed", code: "MOCK_ERROR" }
|
|
70
|
+
* });
|
|
71
|
+
*/
|
|
72
|
+
export function mockTool<Input = any, Output = any>(
|
|
73
|
+
config: MockToolConfig<Input, Output>
|
|
74
|
+
): MockToolResult<Input, Output> {
|
|
75
|
+
const invocations: MockToolInvocation<Input>[] = [];
|
|
76
|
+
|
|
77
|
+
// Create the tool props
|
|
78
|
+
const props: FlinkToolProps = {
|
|
79
|
+
id: config.name,
|
|
80
|
+
description: config.description || `Mock tool: ${config.name}`,
|
|
81
|
+
inputSchema: config.inputSchema,
|
|
82
|
+
outputSchema: config.outputSchema,
|
|
83
|
+
permissions: config.permissions,
|
|
84
|
+
};
|
|
85
|
+
|
|
86
|
+
// Create the tool function with invocation tracking
|
|
87
|
+
const fn: FlinkTool<any, Input, Output> = async ({ input, ctx, user }) => {
|
|
88
|
+
// Track invocation
|
|
89
|
+
invocations.push({ input, user });
|
|
90
|
+
|
|
91
|
+
// If custom function provided, use it
|
|
92
|
+
if (config.fn) {
|
|
93
|
+
return config.fn({ input, ctx, user });
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
// If error configured, return error
|
|
97
|
+
if (config.error) {
|
|
98
|
+
return {
|
|
99
|
+
success: false,
|
|
100
|
+
error: config.error.error,
|
|
101
|
+
code: config.error.code,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
// If response configured, return success with data
|
|
106
|
+
if (config.response !== undefined) {
|
|
107
|
+
return {
|
|
108
|
+
success: true,
|
|
109
|
+
data: config.response,
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Default: return empty success
|
|
114
|
+
return {
|
|
115
|
+
success: true,
|
|
116
|
+
data: {} as Output,
|
|
117
|
+
};
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
const getLastInvocation = (): MockToolInvocation<Input> | undefined => {
|
|
121
|
+
return invocations[invocations.length - 1];
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
const reset = (): void => {
|
|
125
|
+
invocations.length = 0;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
return {
|
|
129
|
+
props,
|
|
130
|
+
fn,
|
|
131
|
+
invocations,
|
|
132
|
+
getLastInvocation,
|
|
133
|
+
reset,
|
|
134
|
+
};
|
|
135
|
+
}
|
package/src/index.ts
CHANGED
package/tsconfig.json
CHANGED
|
@@ -8,16 +8,17 @@
|
|
|
8
8
|
"allowSyntheticDefaultImports": true,
|
|
9
9
|
"strict": true,
|
|
10
10
|
"forceConsistentCasingInFileNames": true,
|
|
11
|
-
"module": "
|
|
12
|
-
"moduleResolution": "
|
|
11
|
+
"module": "node16",
|
|
12
|
+
"moduleResolution": "node16",
|
|
13
13
|
"resolveJsonModule": true,
|
|
14
14
|
"isolatedModules": true,
|
|
15
15
|
"noEmit": false,
|
|
16
16
|
"declaration": true,
|
|
17
17
|
"experimentalDecorators": true,
|
|
18
18
|
"checkJs": false,
|
|
19
|
-
"outDir": "dist"
|
|
19
|
+
"outDir": "dist",
|
|
20
|
+
"types": ["jasmine", "node"]
|
|
20
21
|
},
|
|
21
|
-
"include": ["./src/**/*.ts", "./.flink/**/*.ts"],
|
|
22
|
-
"exclude": ["./node_modules/*"]
|
|
22
|
+
"include": ["./src/**/*.ts", "./.flink/**/*.ts", "./spec/**/*.ts"],
|
|
23
|
+
"exclude": ["./node_modules/*", "./dist/*"]
|
|
23
24
|
}
|