|
| 1 | +import { describe, expect, test } from "bun:test" |
| 2 | +import { AskQuestion } from "../../src/askquestion" |
| 3 | + |
| 4 | +describe("AskQuestion Core", () => { |
| 5 | + test("register and respond", async () => { |
| 6 | + const callID = "test-call-id" |
| 7 | + const sessionID = "test-session-id" |
| 8 | + const messageID = "test-message-id" |
| 9 | + const questions: AskQuestion.Question[] = [ |
| 10 | + { |
| 11 | + id: "q1", |
| 12 | + label: "Q1", |
| 13 | + question: "How are you?", |
| 14 | + options: [ |
| 15 | + { value: "good", label: "Good" }, |
| 16 | + { value: "bad", label: "Bad" }, |
| 17 | + ], |
| 18 | + }, |
| 19 | + ] |
| 20 | + |
| 21 | + const promise = AskQuestion.register(callID, sessionID, messageID, questions) |
| 22 | + |
| 23 | + const pending = AskQuestion.get(callID) |
| 24 | + expect(pending).toBeDefined() |
| 25 | + expect(pending?.questions).toEqual(questions) |
| 26 | + |
| 27 | + const answers: AskQuestion.Answer[] = [ |
| 28 | + { questionId: "q1", values: ["good"] }, |
| 29 | + ] |
| 30 | + |
| 31 | + AskQuestion.respond(callID, answers) |
| 32 | + |
| 33 | + const result = await promise |
| 34 | + expect(result).toEqual(answers) |
| 35 | + expect(AskQuestion.get(callID)).toBeUndefined() |
| 36 | + }) |
| 37 | + |
| 38 | + test("register and cancel", async () => { |
| 39 | + const callID = "test-call-id-2" |
| 40 | + const sessionID = "test-session-id" |
| 41 | + const messageID = "test-message-id" |
| 42 | + const questions: AskQuestion.Question[] = [ |
| 43 | + { |
| 44 | + id: "q1", |
| 45 | + label: "Q1", |
| 46 | + question: "How are you?", |
| 47 | + options: [ |
| 48 | + { value: "good", label: "Good" }, |
| 49 | + { value: "bad", label: "Bad" }, |
| 50 | + ], |
| 51 | + }, |
| 52 | + ] |
| 53 | + |
| 54 | + const promise = AskQuestion.register(callID, sessionID, messageID, questions) |
| 55 | + |
| 56 | + AskQuestion.cancel(callID) |
| 57 | + |
| 58 | + expect(promise).rejects.toThrow("User cancelled the question wizard") |
| 59 | + expect(AskQuestion.get(callID)).toBeUndefined() |
| 60 | + }) |
| 61 | +}) |
| 62 | + |
| 63 | +describe("AskQuestion Detection Logic", () => { |
| 64 | + // Mock detection logic similar to TUI/Web implementation |
| 65 | + function detectPending(messages: any[], partsMap: Record<string, any[]>) { |
| 66 | + for (const message of [...messages].reverse()) { |
| 67 | + const parts = partsMap[message.id] ?? [] |
| 68 | + for (const part of [...parts].reverse()) { |
| 69 | + if (part.type !== "tool") continue |
| 70 | + if (part.tool !== "askquestion") continue |
| 71 | + if (part.state.status !== "running") continue |
| 72 | + |
| 73 | + const metadata = part.state.metadata |
| 74 | + if (metadata?.status !== "waiting") continue |
| 75 | + |
| 76 | + return { |
| 77 | + callID: part.callID, |
| 78 | + messageId: message.id, |
| 79 | + questions: metadata.questions ?? [], |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + return null |
| 84 | + } |
| 85 | + |
| 86 | + test("detects pending askquestion", () => { |
| 87 | + const messages = [{ id: "m1" }, { id: "m2" }] |
| 88 | + const partsMap = { |
| 89 | + m1: [], |
| 90 | + m2: [ |
| 91 | + { |
| 92 | + type: "tool", |
| 93 | + tool: "askquestion", |
| 94 | + callID: "c1", |
| 95 | + state: { |
| 96 | + status: "running", |
| 97 | + metadata: { |
| 98 | + status: "waiting", |
| 99 | + questions: [{ id: "q1", label: "Q1" }], |
| 100 | + }, |
| 101 | + }, |
| 102 | + }, |
| 103 | + ], |
| 104 | + } |
| 105 | + |
| 106 | + const result = detectPending(messages, partsMap) |
| 107 | + expect(result).not.toBeNull() |
| 108 | + expect(result?.callID).toBe("c1") |
| 109 | + expect(result?.questions[0].id).toBe("q1") |
| 110 | + }) |
| 111 | + |
| 112 | + test("ignores completed askquestion", () => { |
| 113 | + const messages = [{ id: "m1" }] |
| 114 | + const partsMap = { |
| 115 | + m1: [ |
| 116 | + { |
| 117 | + type: "tool", |
| 118 | + tool: "askquestion", |
| 119 | + callID: "c1", |
| 120 | + state: { |
| 121 | + status: "completed", |
| 122 | + metadata: { |
| 123 | + status: "completed", |
| 124 | + }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + ], |
| 128 | + } |
| 129 | + |
| 130 | + const result = detectPending(messages, partsMap) |
| 131 | + expect(result).toBeNull() |
| 132 | + }) |
| 133 | + |
| 134 | + test("ignores askquestion in different state", () => { |
| 135 | + const messages = [{ id: "m1" }] |
| 136 | + const partsMap = { |
| 137 | + m1: [ |
| 138 | + { |
| 139 | + type: "tool", |
| 140 | + tool: "askquestion", |
| 141 | + callID: "c1", |
| 142 | + state: { |
| 143 | + status: "running", |
| 144 | + metadata: { |
| 145 | + status: "something-else", |
| 146 | + }, |
| 147 | + }, |
| 148 | + }, |
| 149 | + ], |
| 150 | + } |
| 151 | + |
| 152 | + const result = detectPending(messages, partsMap) |
| 153 | + expect(result).toBeNull() |
| 154 | + }) |
| 155 | +}) |
0 commit comments