question.test.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { describe, expect } from "bun:test"
  2. import { LayerNode } from "@kirincode-ai/core/effect/layer-node"
  3. import { Effect, Fiber, Queue } from "effect"
  4. import { QuestionTool } from "../../src/tool/question"
  5. import { Question } from "../../src/question"
  6. import { SessionID, MessageID } from "../../src/session/schema"
  7. import { Agent } from "../../src/agent/agent"
  8. import { Truncate } from "@/tool/truncate"
  9. import { testEffect } from "../lib/effect"
  10. import { EventV2Bridge } from "../../src/event-v2-bridge"
  11. const ctx = {
  12. sessionID: SessionID.make("ses_test-session"),
  13. messageID: MessageID.make("msg_test-message"),
  14. callID: "test-call",
  15. agent: "test-agent",
  16. abort: AbortSignal.any([]),
  17. messages: [],
  18. metadata: () => Effect.void,
  19. ask: () => Effect.void,
  20. }
  21. const it = testEffect(
  22. LayerNode.compile(LayerNode.group([Question.node, EventV2Bridge.node, Truncate.node, Agent.node])),
  23. )
  24. const pending = Effect.fn("QuestionToolTest.pending")(function* (question: Question.Interface) {
  25. const events = yield* EventV2Bridge.Service
  26. const asked = yield* Queue.unbounded<void>()
  27. const off = yield* events.listen((event) => {
  28. if (event.type === Question.Event.Asked.type) Queue.offerUnsafe(asked, undefined)
  29. return Effect.void
  30. })
  31. yield* Effect.addFinalizer(() => off)
  32. for (;;) {
  33. const items = yield* question.list()
  34. const item = items[0]
  35. if (item) return item
  36. yield* Queue.take(asked).pipe(Effect.timeout("2 seconds"))
  37. }
  38. })
  39. describe("tool.question", () => {
  40. it.instance("should successfully execute with valid question parameters", () =>
  41. Effect.gen(function* () {
  42. const question = yield* Question.Service
  43. const toolInfo = yield* QuestionTool
  44. const tool = yield* toolInfo.init()
  45. const questions = [
  46. {
  47. question: "What is your favorite color?",
  48. header: "Color",
  49. options: [
  50. { label: "Red", description: "The color of passion" },
  51. { label: "Blue", description: "The color of sky" },
  52. ],
  53. multiple: false,
  54. },
  55. ]
  56. const fiber = yield* tool.execute({ questions }, ctx).pipe(Effect.forkScoped)
  57. const item = yield* pending(question)
  58. yield* question.reply({ requestID: item.id, answers: [["Red"]] })
  59. const result = yield* Fiber.join(fiber)
  60. expect(result.title).toBe("Asked 1 question")
  61. }),
  62. )
  63. it.instance("should now pass with a header longer than 12 but less than 30 chars", () =>
  64. Effect.gen(function* () {
  65. const question = yield* Question.Service
  66. const toolInfo = yield* QuestionTool
  67. const tool = yield* toolInfo.init()
  68. const questions = [
  69. {
  70. question: "What is your favorite animal?",
  71. header: "This Header is Over 12",
  72. options: [{ label: "Dog", description: "Man's best friend" }],
  73. },
  74. ]
  75. const fiber = yield* tool.execute({ questions }, ctx).pipe(Effect.forkScoped)
  76. const item = yield* pending(question)
  77. yield* question.reply({ requestID: item.id, answers: [["Dog"]] })
  78. const result = yield* Fiber.join(fiber)
  79. expect(result.output).toContain(`"What is your favorite animal?"="Dog"`)
  80. }),
  81. )
  82. // intentionally removed the zod validation due to tool call errors, hoping prompting is gonna be good enough
  83. // test("should throw an Error for header exceeding 30 characters", async () => {
  84. // const tool = await QuestionTool.init()
  85. // const questions = [
  86. // {
  87. // question: "What is your favorite animal?",
  88. // header: "This Header is Definitely More Than Thirty Characters Long",
  89. // options: [{ label: "Dog", description: "Man's best friend" }],
  90. // },
  91. // ]
  92. // try {
  93. // await tool.execute({ questions }, ctx)
  94. // // If it reaches here, the test should fail
  95. // expect(true).toBe(false)
  96. // } catch (e: any) {
  97. // expect(e).toBeInstanceOf(Error)
  98. // expect(e.cause).toBeInstanceOf(z.ZodError)
  99. // }
  100. // })
  101. // test("should throw an Error for label exceeding 30 characters", async () => {
  102. // const tool = await QuestionTool.init()
  103. // const questions = [
  104. // {
  105. // question: "A question with a very long label",
  106. // header: "Long Label",
  107. // options: [
  108. // { label: "This is a very, very, very long label that will exceed the limit", description: "A description" },
  109. // ],
  110. // },
  111. // ]
  112. // try {
  113. // await tool.execute({ questions }, ctx)
  114. // // If it reaches here, the test should fail
  115. // expect(true).toBe(false)
  116. // } catch (e: any) {
  117. // expect(e).toBeInstanceOf(Error)
  118. // expect(e.cause).toBeInstanceOf(z.ZodError)
  119. // }
  120. // })
  121. })