question.test.ts 3.3 KB

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