File size: 1,846 Bytes
08b4888
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import { describe, test, expect } from "vitest";
import type { PredictReturn, PredictFunction } from "../types";

describe("PredictReturn generic types", () => {
	test("PredictReturn with default unknown type", () => {
		const result: PredictReturn = {
			type: "data",
			time: new Date(),
			data: ["any", "data"],
			endpoint: "/predict",
			fn_index: 0
		};
		expect(result.data).toBeDefined();
	});

	test("PredictReturn with custom type", () => {
		type MyData = { text: string; confidence: number };
		const result: PredictReturn<MyData> = {
			type: "data",
			time: new Date(),
			data: { text: "hello", confidence: 0.95 },
			endpoint: "/predict",
			fn_index: 0
		};
		expect(result.data.text).toBe("hello");
		expect(result.data.confidence).toBe(0.95);
	});

	test("PredictReturn with array type", () => {
		type ArrayData = string[];
		const result: PredictReturn<ArrayData> = {
			type: "data",
			time: new Date(),
			data: ["hello", "world"],
			endpoint: "/predict",
			fn_index: 0
		};
		expect(result.data[0]).toBe("hello");
		expect(result.data.length).toBe(2);
	});

	test("PredictFunction type signature allows generic parameter", () => {
		// This test verifies that PredictFunction accepts a generic type parameter
		type MyResponse = { output: string };

		// Mock function that matches PredictFunction signature
		const mockPredict: PredictFunction = async <T = unknown>(
			endpoint: string | number,
			data?: unknown[] | Record<string, unknown>,
			event_data?: unknown
		): Promise<PredictReturn<T>> => {
			return {
				type: "data",
				time: new Date(),
				data: { output: "test" } as T,
				endpoint: String(endpoint),
				fn_index: 0
			};
		};

		// Type check: calling with generic parameter
		const typedResult = mockPredict<MyResponse>("/predict", ["input"]);
		expect(typedResult).toBeDefined();
	});
});