|
| 1 | +import { checkAutoApproval } from ".." |
| 2 | + |
| 3 | +describe("Follow-up question auto-approval", () => { |
| 4 | + const baseState = { |
| 5 | + autoApprovalEnabled: true, |
| 6 | + alwaysAllowFollowupQuestions: true, |
| 7 | + followupAutoApproveTimeoutMs: 10_000, |
| 8 | + } |
| 9 | + |
| 10 | + const followupText = (suggest: unknown) => JSON.stringify({ question: "Pick one?", suggest }) as string |
| 11 | + |
| 12 | + const run = (state: Record<string, unknown>, text: string) => |
| 13 | + checkAutoApproval({ state: state as never, ask: "followup", text }) |
| 14 | + |
| 15 | + it("schedules a timeout that auto-answers with the first valid suggestion", async () => { |
| 16 | + const result = await run(baseState, followupText([{ answer: "Yes, proceed" }])) |
| 17 | + |
| 18 | + expect(result.decision).toBe("timeout") |
| 19 | + if (result.decision === "timeout") { |
| 20 | + expect(result.timeout).toBe(10_000) |
| 21 | + expect(result.fn()).toEqual({ |
| 22 | + askResponse: "messageResponse", |
| 23 | + text: "Yes, proceed", |
| 24 | + }) |
| 25 | + } |
| 26 | + }) |
| 27 | + |
| 28 | + it("skips a blank or missing first answer and uses the next valid suggestion (issue #1226)", async () => { |
| 29 | + // Mirrors a malformed model response where JSON round-tripping drops |
| 30 | + // `answer: undefined` and the first item is unusable. |
| 31 | + const result = await run(baseState, followupText([{}, { answer: " " }, { answer: "Valid answer" }])) |
| 32 | + |
| 33 | + expect(result.decision).toBe("timeout") |
| 34 | + if (result.decision === "timeout") { |
| 35 | + expect(result.fn()).toEqual({ |
| 36 | + askResponse: "messageResponse", |
| 37 | + text: "Valid answer", |
| 38 | + }) |
| 39 | + } |
| 40 | + }) |
| 41 | + |
| 42 | + it("falls back to asking when every suggestion answer is blank or missing (issue #1226)", async () => { |
| 43 | + // Before the #1226 fix this scheduled a timeout that auto-answered the |
| 44 | + // follow-up with `undefined` text, silently accepting an empty answer. |
| 45 | + const result = await run(baseState, followupText([{ answer: "" }, { answer: " \n\t " }, {}])) |
| 46 | + |
| 47 | + expect(result).toEqual({ decision: "ask" }) |
| 48 | + }) |
| 49 | + |
| 50 | + it("falls back to asking when the suggestion answer is not a string", async () => { |
| 51 | + const result = await run(baseState, followupText([{ answer: 42 }])) |
| 52 | + |
| 53 | + expect(result).toEqual({ decision: "ask" }) |
| 54 | + }) |
| 55 | + |
| 56 | + it("falls back to asking when the follow-up has no suggestions", async () => { |
| 57 | + const result = await run(baseState, JSON.stringify({ question: "Pick one?" })) |
| 58 | + |
| 59 | + expect(result).toEqual({ decision: "ask" }) |
| 60 | + }) |
| 61 | + |
| 62 | + it("falls back to asking when the follow-up text is not valid JSON", async () => { |
| 63 | + const result = await run(baseState, "not-json") |
| 64 | + |
| 65 | + expect(result).toEqual({ decision: "ask" }) |
| 66 | + }) |
| 67 | + |
| 68 | + it("falls back to asking when the auto-approve timeout is not positive", async () => { |
| 69 | + const result = await run({ ...baseState, followupAutoApproveTimeoutMs: 0 }, followupText([{ answer: "Yes" }])) |
| 70 | + |
| 71 | + expect(result).toEqual({ decision: "ask" }) |
| 72 | + }) |
| 73 | + |
| 74 | + it("does not auto-approve when follow-up auto-approval is disabled", async () => { |
| 75 | + const result = await run( |
| 76 | + { ...baseState, alwaysAllowFollowupQuestions: false }, |
| 77 | + followupText([{ answer: "Yes" }]), |
| 78 | + ) |
| 79 | + |
| 80 | + expect(result).toEqual({ decision: "ask" }) |
| 81 | + }) |
| 82 | + |
| 83 | + it("does not auto-approve when global auto-approval is disabled", async () => { |
| 84 | + const result = await run({ ...baseState, autoApprovalEnabled: false }, followupText([{ answer: "Yes" }])) |
| 85 | + |
| 86 | + expect(result).toEqual({ decision: "ask" }) |
| 87 | + }) |
| 88 | +}) |
0 commit comments