@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.
@@ -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
@@ -1,2 +1,3 @@
1
1
  export * from "./http";
2
2
  export * from "./mocks";
3
+ export * from "./ai";
@@ -0,0 +1,4 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "exclude": ["spec/**/*.ts", "node_modules/*", "dist/*"]
4
+ }
package/tsconfig.json CHANGED
@@ -8,16 +8,17 @@
8
8
  "allowSyntheticDefaultImports": true,
9
9
  "strict": true,
10
10
  "forceConsistentCasingInFileNames": true,
11
- "module": "commonjs",
12
- "moduleResolution": "node",
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
  }