|
1 | 1 | import type { PermissionRequest } from "@posthog/shared"; |
2 | 2 | import { describe, expect, it } from "vitest"; |
3 | 3 | import { |
| 4 | + formatPermissionAnswerPrompt, |
4 | 5 | isOtherPermissionOption, |
5 | 6 | planPermissionResponse, |
6 | 7 | } from "./permissionResponse"; |
@@ -78,3 +79,71 @@ describe("planPermissionResponse", () => { |
78 | 79 | expect(plan.resendPromptText).toBeNull(); |
79 | 80 | }); |
80 | 81 | }); |
| 82 | + |
| 83 | +describe("formatPermissionAnswerPrompt", () => { |
| 84 | + const questionPermission = (questions: Array<{ question: string }>) => |
| 85 | + ({ |
| 86 | + taskRunId: "run-1", |
| 87 | + receivedAt: 0, |
| 88 | + toolCall: { |
| 89 | + toolCallId: "tool-1", |
| 90 | + _meta: { codeToolKind: "question", questions }, |
| 91 | + }, |
| 92 | + options: [ |
| 93 | + { optionId: "option_0", name: "MIT", kind: "allow_once" }, |
| 94 | + { optionId: "option_1", name: "Apache 2.0", kind: "allow_once" }, |
| 95 | + ], |
| 96 | + }) as unknown as PermissionRequest; |
| 97 | + |
| 98 | + it("quotes each question above its answer", () => { |
| 99 | + const prompt = formatPermissionAnswerPrompt( |
| 100 | + questionPermission([{ question: "Which license should I use?" }]), |
| 101 | + "option_0", |
| 102 | + undefined, |
| 103 | + { "Which license should I use?": "MIT" }, |
| 104 | + ); |
| 105 | + expect(prompt).toBe("MIT"); |
| 106 | + }); |
| 107 | + |
| 108 | + it("carries every entry of a multi-question answers map", () => { |
| 109 | + const prompt = formatPermissionAnswerPrompt( |
| 110 | + questionPermission([{ question: "Q1?" }, { question: "Q2?" }]), |
| 111 | + "option_0", |
| 112 | + undefined, |
| 113 | + { "Q1?": "A1", "Q2?": "A2" }, |
| 114 | + ); |
| 115 | + expect(prompt).toBe("1. A1\n2. A2"); |
| 116 | + }); |
| 117 | + |
| 118 | + it("falls back to the picked option label when no answers map is sent", () => { |
| 119 | + const prompt = formatPermissionAnswerPrompt( |
| 120 | + questionPermission([{ question: "Which license should I use?" }]), |
| 121 | + "option_1", |
| 122 | + ); |
| 123 | + expect(prompt).toBe("Apache 2.0"); |
| 124 | + }); |
| 125 | + |
| 126 | + it("uses free-text custom input for a question", () => { |
| 127 | + const prompt = formatPermissionAnswerPrompt( |
| 128 | + questionPermission([{ question: "Which license should I use?" }]), |
| 129 | + "_other", |
| 130 | + "BSD, actually", |
| 131 | + ); |
| 132 | + expect(prompt).toBe("BSD, actually"); |
| 133 | + }); |
| 134 | + |
| 135 | + it.each([ |
| 136 | + ["plain approval", "allow", undefined], |
| 137 | + ["plain rejection with feedback", "reject", "not like this"], |
| 138 | + ])( |
| 139 | + "returns null for %s so no resume run is spun", |
| 140 | + (_caseName, optionId, customInput) => { |
| 141 | + const approval = makePermission([ |
| 142 | + { optionId: "allow", kind: "allow_once" }, |
| 143 | + ]); |
| 144 | + expect( |
| 145 | + formatPermissionAnswerPrompt(approval, optionId, customInput), |
| 146 | + ).toBeNull(); |
| 147 | + }, |
| 148 | + ); |
| 149 | +}); |
0 commit comments