mirror of
https://github.com/paperclipai/paperclip.git
synced 2026-12-02 02:30:43 +01:00
fix(ui): preserve canonical drafts in classic question cards
Reuse the canonical question form for pending structured questions so provider drafts, persisted edits, validation and exact submitted text survive both task surfaces. Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
1 parent
bb56af7fbd
commit
c2b0c804cb
2 files changed
+91
-1
No files matched your search
@@ -86,6 +86,12 @@ vi.mock("@/lib/router", () => ({
|
||||
|
||||
vi.mock("@/api/connection-intents", () => ({ connectionIntentsApi: connectionIntentsApiMocks }));
|
||||
|
||||
vi.mock("./task-chat/TaskChatRichInput", () => ({
|
||||
TaskChatRichInput: ({ value, onChange }: { value: string; onChange: (value: string) => void }) => (
|
||||
<textarea aria-label="Canonical answer" value={value} onChange={(event) => onChange(event.target.value)} />
|
||||
),
|
||||
}));
|
||||
|
||||
function renderCard(
|
||||
props: Partial<ComponentProps<typeof IssueThreadInteractionCard>> = {},
|
||||
) {
|
||||
@@ -121,9 +127,53 @@ afterEach(() => {
|
||||
container?.remove();
|
||||
root = null;
|
||||
container = null;
|
||||
localStorage.clear();
|
||||
});
|
||||
|
||||
describe("IssueThreadInteractionCard", () => {
|
||||
const canonicalDraftInteraction = {
|
||||
...pendingAskUserQuestionsInteraction,
|
||||
payload: {
|
||||
...pendingAskUserQuestionsInteraction.payload,
|
||||
questions: [{ id: "draft", prompt: "Edit draft", required: true, selectionMode: "single" as const, options: [{ id: "draft_text", label: "Write an answer", freeText: true }], allowOther: false }],
|
||||
questionSet: {
|
||||
schema: "paperclip.question_set.v1" as const,
|
||||
questions: [{ id: "draft", prompt: "Edit draft", required: true, answerMode: "text" as const, initialText: " Provider draft\n漢字\\n " }],
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
async function editCanonical(host: HTMLDivElement, value: string) {
|
||||
const textarea = host.querySelector<HTMLTextAreaElement>("textarea")!;
|
||||
await act(() => {
|
||||
Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, "value")!.set!.call(textarea, value);
|
||||
textarea.dispatchEvent(new Event("input", { bubbles: true }));
|
||||
});
|
||||
}
|
||||
|
||||
it("presents canonical drafts and submits only explicitly edited exact text", async () => {
|
||||
const submit = vi.fn();
|
||||
const host = renderCard({ interaction: canonicalDraftInteraction, onSubmitInteractionAnswers: submit });
|
||||
expect(host.querySelector<HTMLTextAreaElement>("textarea")!.value).toBe(" Provider draft\n漢字\\n ");
|
||||
expect(submit).not.toHaveBeenCalled();
|
||||
const edited = "\n Operator edit 漢字\\n\n ";
|
||||
await editCanonical(host, edited);
|
||||
await act(() => Array.from(host.querySelectorAll("button")).find((button) => button.textContent === "Submit answers")!.click());
|
||||
expect(submit).toHaveBeenCalledExactlyOnceWith(canonicalDraftInteraction, [{ questionId: "draft", optionIds: [], otherText: edited }]);
|
||||
});
|
||||
|
||||
it("keeps an explicit cleared canonical draft after reopening and requires an answer", async () => {
|
||||
const submit = vi.fn();
|
||||
const host = renderCard({ interaction: canonicalDraftInteraction, onSubmitInteractionAnswers: submit });
|
||||
await editCanonical(host, "");
|
||||
await act(() => root!.unmount());
|
||||
host.remove();
|
||||
const reopened = renderCard({ interaction: canonicalDraftInteraction, onSubmitInteractionAnswers: submit });
|
||||
expect(reopened.querySelector<HTMLTextAreaElement>("textarea")!.value).toBe("");
|
||||
expect(Array.from(reopened.querySelectorAll("button")).find((button) => button.textContent === "Submit answers")!.disabled).toBe(true);
|
||||
expect(submit).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it("opens the shared connection setup for the addressed user", async () => {
|
||||
connectionIntentsApiMocks.setupOptions.mockResolvedValue({ existingConnections: [] });
|
||||
const host = renderCard({
|
||||
|
||||
@@ -44,6 +44,7 @@ import { ProposalJustification } from "../pages/secrets/proposal-review";
|
||||
import { DropdownMenu, DropdownMenuContent, DropdownMenuItem, DropdownMenuTrigger } from "./ui/dropdown-menu";
|
||||
import { AppLogo } from "@/pages/apps/AppLogo";
|
||||
import { ConnectionIntentInteractionBody } from "@/features/connections/ConnectionIntentInteractionBody";
|
||||
import { QuestionForm } from "./task-chat/QuestionForm";
|
||||
|
||||
const OTHER_ANSWER_ID = "__paperclip_other__";
|
||||
|
||||
@@ -1194,7 +1195,46 @@ function AskUserQuestionsCard({
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{interaction.status === "pending" ? (
|
||||
{interaction.status === "pending" && interaction.payload.questionSet ? (
|
||||
<QuestionForm
|
||||
id={interaction.id}
|
||||
questionSet={interaction.payload.questionSet}
|
||||
draftKey={`issue-question:${interaction.companyId}:${interaction.id}`}
|
||||
disabled={!onSubmitInteractionAnswers}
|
||||
initialResponse={interaction.result?.answers ? {
|
||||
schema: "paperclip.question_response.v1",
|
||||
answers: Object.fromEntries(interaction.result.answers.map((answer) => [
|
||||
answer.questionId,
|
||||
interaction.payload.questionSet!.questions.find((question) => question.id === answer.questionId)?.answerMode === "text"
|
||||
? { text: answer.otherText ?? "" }
|
||||
: { selectedOptionIds: answer.optionIds, ...(typeof answer.otherText === "string" ? { customText: answer.otherText } : {}) },
|
||||
])),
|
||||
} : null}
|
||||
onSubmit={async (response) => {
|
||||
const answers = interaction.payload.questionSet!.questions.map((question) => {
|
||||
const answer = response.answers[question.id];
|
||||
const otherText = question.answerMode === "text" ? answer?.text : answer?.customText?.trim();
|
||||
return {
|
||||
questionId: question.id,
|
||||
optionIds: question.answerMode === "text" ? [] : (answer?.selectedOptionIds ?? []),
|
||||
...(otherText !== undefined ? { otherText } : {}),
|
||||
};
|
||||
});
|
||||
try {
|
||||
await onSubmitInteractionAnswers?.(interaction, answers);
|
||||
} catch (error) {
|
||||
throw new Error(resolutionErrorMessage(error));
|
||||
}
|
||||
}}
|
||||
onCancel={onCancelInteraction ? async () => {
|
||||
try {
|
||||
await onCancelInteraction(interaction);
|
||||
} catch (error) {
|
||||
throw new Error(resolutionErrorMessage(error));
|
||||
}
|
||||
} : undefined}
|
||||
/>
|
||||
) : interaction.status === "pending" ? (
|
||||
<div className="space-y-4">
|
||||
{questions.map((question, index) => {
|
||||
const hasFreeTextOption = question.options.some(
|
||||
|
||||
Reference in new issue
Block a user