= {}, +): UserInputAttachmentAnswerPayload { + return { + requestId: ApprovalRequestId.make("request-1"), + questionTextById: { scope: "Which repository?" }, + answers: { scope: "Use the private repository" }, + attachmentsByQuestionId: {}, + ...overrides, + }; +} + +describe("getQuestionTextPreview", () => { + it("joins the question texts", () => { + expect( + getQuestionTextPreview( + answer({ questionTextById: { scope: "Which repository?", name: "What name?" } }), + ), + ).toBe("Which repository? · What name?"); + }); + + it("normalizes whitespace and skips blank texts", () => { + expect( + getQuestionTextPreview( + answer({ questionTextById: { scope: "Which\nrepository?", x: " " } }), + ), + ).toBe("Which repository?"); + }); + + it("returns an empty string without question texts", () => { + expect(getQuestionTextPreview(answer({ questionTextById: undefined }))).toBe(""); + }); +}); diff --git a/packages/client-runtime/src/work-log/userInput.ts b/packages/client-runtime/src/work-log/userInput.ts index 18dea7dd83c29feb8436a03d1b3128239cacd7fc..d1bb4370efbfa2aac46ad57edc6cc0d9e1adb7e2 100644 --- a/packages/client-runtime/src/work-log/userInput.ts +++ b/packages/client-runtime/src/work-log/userInput.ts @@ -163,6 +163,13 @@ export function getQuestionAnswerText(value: unknown): string { return nested ? getQuestionAnswerText(nested.answers) : ""; } +export function getQuestionTextPreview(answer: UserInputAttachmentAnswerPayload): string { + return Object.values(answer.questionTextById ?? {}) + .map((text) => text.replace(/\s+/g, " ").trim()) + .filter(Boolean) + .join(" · "); +} + export function getQuestionAnswerPreview(answer: UserInputAttachmentAnswerPayload): string { const answers = Object.values(answer.answers).map(getQuestionAnswerText).filter(Boolean); const attachments = Object.values(answer.attachmentsByQuestionId)