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