|
| 1 | +import { describe, expect, test, spyOn, beforeEach, afterEach } from "bun:test" |
| 2 | +import { z } from "zod" |
| 3 | +import { QuestionTool } from "../../src/tool/question" |
| 4 | +import * as QuestionModule from "../../src/question" |
| 5 | + |
| 6 | +const ctx = { |
| 7 | + sessionID: "test-session", |
| 8 | + messageID: "test-message", |
| 9 | + callID: "test-call", |
| 10 | + agent: "test-agent", |
| 11 | + abort: AbortSignal.any([]), |
| 12 | + metadata: () => {}, |
| 13 | + ask: async () => {}, |
| 14 | +} |
| 15 | + |
| 16 | +describe("tool.question", () => { |
| 17 | + let askSpy: any; |
| 18 | + |
| 19 | + beforeEach(() => { |
| 20 | + askSpy = spyOn(QuestionModule.Question, "ask").mockImplementation(async () => { |
| 21 | + return [] |
| 22 | + }) |
| 23 | + }) |
| 24 | + |
| 25 | + afterEach(() => { |
| 26 | + askSpy.mockRestore() |
| 27 | + }) |
| 28 | + |
| 29 | + test("should successfully execute with valid question parameters", async () => { |
| 30 | + const tool = await QuestionTool.init() |
| 31 | + const questions = [ |
| 32 | + { |
| 33 | + question: "What is your favorite color?", |
| 34 | + header: "Color", |
| 35 | + options: [ |
| 36 | + { label: "Red", description: "The color of passion" }, |
| 37 | + { label: "Blue", description: "The color of sky" }, |
| 38 | + ], |
| 39 | + multiple: false, |
| 40 | + }, |
| 41 | + ] |
| 42 | + |
| 43 | + askSpy.mockResolvedValueOnce([["Red"]]) |
| 44 | + |
| 45 | + const result = await tool.execute( |
| 46 | + { questions }, |
| 47 | + ctx, |
| 48 | + ) |
| 49 | + expect(askSpy).toHaveBeenCalledTimes(1) |
| 50 | + expect(result.title).toBe("Asked 1 question") |
| 51 | + }) |
| 52 | + |
| 53 | + test("should now pass with a header longer than 12 but less than 30 chars", async () => { |
| 54 | + const tool = await QuestionTool.init() |
| 55 | + const questions = [ |
| 56 | + { |
| 57 | + question: "What is your favorite animal?", |
| 58 | + header: "This Header is Over 12", |
| 59 | + options: [{ label: "Dog", description: "Man's best friend" }], |
| 60 | + }, |
| 61 | + ] |
| 62 | + |
| 63 | + askSpy.mockResolvedValueOnce([["Dog"]]) |
| 64 | + |
| 65 | + const result = await tool.execute({ questions }, ctx) |
| 66 | + expect(result.output).toContain(`"What is your favorite animal?"="Dog"`) |
| 67 | + }) |
| 68 | + |
| 69 | + test("should throw an Error for header exceeding 30 characters", async () => { |
| 70 | + const tool = await QuestionTool.init() |
| 71 | + const questions = [ |
| 72 | + { |
| 73 | + question: "What is your favorite animal?", |
| 74 | + header: "This Header is Definitely More Than Thirty Characters Long", |
| 75 | + options: [{ label: "Dog", description: "Man's best friend" }], |
| 76 | + }, |
| 77 | + ] |
| 78 | + try { |
| 79 | + await tool.execute({ questions }, ctx) |
| 80 | + // If it reaches here, the test should fail |
| 81 | + expect(true).toBe(false) |
| 82 | + } catch (e: any) { |
| 83 | + expect(e).toBeInstanceOf(Error) |
| 84 | + expect(e.cause).toBeInstanceOf(z.ZodError) |
| 85 | + } |
| 86 | + }) |
| 87 | + |
| 88 | + test("should throw an Error for label exceeding 30 characters", async () => { |
| 89 | + const tool = await QuestionTool.init() |
| 90 | + const questions = [ |
| 91 | + { |
| 92 | + question: "A question with a very long label", |
| 93 | + header: "Long Label", |
| 94 | + options: [{ label: "This is a very, very, very long label that will exceed the limit", description: "A description" }], |
| 95 | + }, |
| 96 | + ] |
| 97 | + try { |
| 98 | + await tool.execute({ questions }, ctx) |
| 99 | + // If it reaches here, the test should fail |
| 100 | + expect(true).toBe(false) |
| 101 | + } catch (e: any) { |
| 102 | + expect(e).toBeInstanceOf(Error) |
| 103 | + expect(e.cause).toBeInstanceOf(z.ZodError) |
| 104 | + } |
| 105 | + }) |
| 106 | +}) |
| 107 | + |
0 commit comments