Repository navigation
Expand file tree
/
Copy pathquestion.test.ts
More file actions
105 lines (93 loc) · 3.03 KB
/
Copy pathquestion.test.ts
File metadata and controls
105 lines (93 loc) · 3.03 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
import { describe, expect, test, spyOn, beforeEach, afterEach } from "bun:test"
import { z } from "zod"
import { QuestionTool } from "../../src/tool/question"
import * as QuestionModule from "../../src/question"
const ctx = {
sessionID: "test-session",
messageID: "test-message",
callID: "test-call",
agent: "test-agent",
abort: AbortSignal.any([]),
metadata: () => {},
ask: async () => {},
}
describe("tool.question", () => {
let askSpy: any
beforeEach(() => {
askSpy = spyOn(QuestionModule.Question, "ask").mockImplementation(async () => {
return []
})
})
afterEach(() => {
askSpy.mockRestore()
})
test("should successfully execute with valid question parameters", async () => {
const tool = await QuestionTool.init()
const questions = [
{
question: "What is your favorite color?",
header: "Color",
options: [
{ label: "Red", description: "The color of passion" },
{ label: "Blue", description: "The color of sky" },
],
multiple: false,
},
]
askSpy.mockResolvedValueOnce([["Red"]])
const result = await tool.execute({ questions }, ctx)
expect(askSpy).toHaveBeenCalledTimes(1)
expect(result.title).toBe("Asked 1 question")
})
test("should now pass with a header longer than 12 but less than 30 chars", async () => {
const tool = await QuestionTool.init()
const questions = [
{
question: "What is your favorite animal?",
header: "This Header is Over 12",
options: [{ label: "Dog", description: "Man's best friend" }],
},
]
askSpy.mockResolvedValueOnce([["Dog"]])
const result = await tool.execute({ questions }, ctx)
expect(result.output).toContain(`"What is your favorite animal?"="Dog"`)
})
test("should throw an Error for header exceeding 30 characters", async () => {
const tool = await QuestionTool.init()
const questions = [
{
question: "What is your favorite animal?",
header: "This Header is Definitely More Than Thirty Characters Long",
options: [{ label: "Dog", description: "Man's best friend" }],
},
]
try {
await tool.execute({ questions }, ctx)
// If it reaches here, the test should fail
expect(true).toBe(false)
} catch (e: any) {
expect(e).toBeInstanceOf(Error)
expect(e.cause).toBeInstanceOf(z.ZodError)
}
})
test("should throw an Error for label exceeding 30 characters", async () => {
const tool = await QuestionTool.init()
const questions = [
{
question: "A question with a very long label",
header: "Long Label",
options: [
{ label: "This is a very, very, very long label that will exceed the limit", description: "A description" },
],
},
]
try {
await tool.execute({ questions }, ctx)
// If it reaches here, the test should fail
expect(true).toBe(false)
} catch (e: any) {
expect(e).toBeInstanceOf(Error)
expect(e.cause).toBeInstanceOf(z.ZodError)
}
})
})