forked from hmarr/openai-chat-tokens
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfunctions.test.ts
211 lines (171 loc) · 4.69 KB
/
functions.test.ts
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import { describe, test } from "vitest"
import type { FunctionDef, ObjectProp, Prop } from "./functions.js"
import {
formatFunctionDefinitions,
formatObjectProperties,
formatType,
isAnyOfProp,
} from "./functions.js"
describe("isAnyOfProp", () => {
test("true", ({ expect }) => {
const result = isAnyOfProp({ anyOf: [] })
expect(result).toBe(true)
})
test("false", ({ expect }) => {
const result = isAnyOfProp({ type: "null" })
expect(result).toBe(false)
})
})
describe("formatFunctionDefinitions", () => {
test("empty array", ({ expect }) => {
const result = formatFunctionDefinitions([])
expect(result).toBe(`namespace functions {
} // namespace functions`)
})
test("single function with no parameters", ({ expect }) => {
const functions: FunctionDef[] = [
{
name: "testFunction",
description: "A test function",
parameters: { type: "object" },
},
]
const result = formatFunctionDefinitions(functions)
expect(result).toBe(`namespace functions {
// A test function
type testFunction = () => any;
} // namespace functions`)
})
test("single function with parameters", ({ expect }) => {
const functions: FunctionDef[] = [
{
name: "testFunction",
description: "A test function",
parameters: {
type: "object",
properties: {
param1: { type: "string" },
param2: { type: "number" },
},
required: ["param1"],
},
},
]
const result = formatFunctionDefinitions(functions)
expect(result).toBe(`namespace functions {
// A test function
type testFunction = (_: {
param1: string,
param2?: number,
}) => any;
} // namespace functions`)
})
test("multiple functions", ({ expect }) => {
const functions: FunctionDef[] = [
{
name: "functionOne",
description: "First function",
parameters: { type: "object" },
},
{
name: "functionTwo",
description: "Second function",
parameters: {
type: "object",
properties: { param1: { type: "boolean" } },
},
},
]
const result = formatFunctionDefinitions(functions)
expect(result).toBe(`namespace functions {
// First function
type functionOne = () => any;
// Second function
type functionTwo = (_: {
param1?: boolean,
}) => any;
} // namespace functions`)
})
})
describe("formatObjectProperties", () => {
test("empty object", ({ expect }) => {
const obj: ObjectProp = { type: "object" }
const result = formatObjectProperties(obj, 0)
expect(result).toBe("")
})
test("single required property", ({ expect }) => {
const obj: ObjectProp = {
type: "object",
properties: { prop1: { type: "string" } },
required: ["prop1"],
}
const result = formatObjectProperties(obj, 0)
expect(result).toBe("prop1: string,")
})
test("multiple properties with optional ones", ({ expect }) => {
const obj: ObjectProp = {
type: "object",
properties: { prop1: { type: "string" }, prop2: { type: "number" } },
required: ["prop1"],
}
const result = formatObjectProperties(obj, 0)
expect(result).toBe("prop1: string,\nprop2?: number,")
})
test("nested objects", ({ expect }) => {
const obj: ObjectProp = {
type: "object",
properties: {
prop1: { type: "object", properties: { subProp: { type: "boolean" } } },
},
required: ["prop1"],
}
const result = formatObjectProperties(obj, 0)
expect(result).toBe("prop1: {\n subProp?: boolean,\n},")
})
})
describe("formatType", () => {
test("string", ({ expect }) => {
const param: Prop = { type: "string" }
const result = formatType(param, 0)
expect(result).toBe("string")
})
test("number", ({ expect }) => {
const param: Prop = { type: "number" }
const result = formatType(param, 0)
expect(result).toBe("number")
})
test("integer", ({ expect }) => {
const param: Prop = { type: "integer" }
const result = formatType(param, 0)
expect(result).toBe("number")
})
test("boolean", ({ expect }) => {
const param: Prop = { type: "boolean" }
const result = formatType(param, 0)
expect(result).toBe("boolean")
})
test("null", ({ expect }) => {
const param: Prop = { type: "null" }
const result = formatType(param, 0)
expect(result).toBe("null")
})
test("object", ({ expect }) => {
const param: Prop = {
type: "object",
properties: { prop1: { type: "string" } },
required: ["prop1"],
}
const result = formatType(param, 0)
expect(result).toBe("{\n prop1: string,\n}")
})
test("array", ({ expect }) => {
const param: Prop = { type: "array", items: { type: "string" } }
const result = formatType(param, 0)
expect(result).toBe("string[]")
})
test("anyOf", ({ expect }) => {
const param: Prop = { anyOf: [{ type: "string" }, { type: "number" }] }
const result = formatType(param, 0)
expect(result).toBe("string | number")
})
})