question.test.ts 3.3 KB

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