ask-user-question.test.ts 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. AskUserQuestionAnsweredInputSchema,
  4. AskUserQuestionInputSchema,
  5. } from "../src/tools/ask-user-question.js";
  6. const validInput = {
  7. questions: [
  8. {
  9. header: "Library",
  10. question: "Which date library should we use?",
  11. options: [
  12. { label: "date-fns", description: "Small functional helpers" },
  13. { label: "Luxon", description: "Richer timezone model" },
  14. ],
  15. },
  16. ],
  17. };
  18. describe("AskUserQuestion contracts", () => {
  19. it("accepts a valid single-select question", () => {
  20. const parsed = AskUserQuestionInputSchema.parse(validInput);
  21. expect(parsed.questions[0]?.multiSelect).toBe(false);
  22. });
  23. it("rejects duplicate question text", () => {
  24. const result = AskUserQuestionInputSchema.safeParse({
  25. questions: [validInput.questions[0], validInput.questions[0]],
  26. });
  27. expect(result.success).toBe(false);
  28. expect(result.error?.issues[0]?.message).toContain("Question texts must be unique");
  29. });
  30. it("rejects duplicate option labels and model-provided Other", () => {
  31. const duplicate = AskUserQuestionInputSchema.safeParse({
  32. questions: [
  33. {
  34. header: "Choice",
  35. question: "Which option should we use?",
  36. options: [
  37. { label: "Same", description: "First" },
  38. { label: "Same", description: "Second" },
  39. ],
  40. },
  41. ],
  42. });
  43. const other = AskUserQuestionInputSchema.safeParse({
  44. questions: [
  45. {
  46. header: "Choice",
  47. question: "Which option should we use?",
  48. options: [
  49. { label: "Recommended", description: "Use the recommended path" },
  50. { label: "Other", description: "Custom answer" },
  51. ],
  52. },
  53. ],
  54. });
  55. expect(duplicate.success).toBe(false);
  56. expect(duplicate.error?.issues[0]?.message).toContain("Option labels must be unique");
  57. expect(other.success).toBe(false);
  58. expect(other.error?.issues[0]?.message).toContain("Do not include an Other option");
  59. });
  60. it("requires the interaction to collect an answers field before execution", () => {
  61. const missing = AskUserQuestionAnsweredInputSchema.safeParse(validInput);
  62. const answered = AskUserQuestionAnsweredInputSchema.safeParse({
  63. ...validInput,
  64. answers: {
  65. "Which date library should we use?": "date-fns",
  66. },
  67. });
  68. expect(missing.success).toBe(false);
  69. expect(answered.success).toBe(true);
  70. });
  71. it("accepts partial answers without requiring every question", () => {
  72. const partial = AskUserQuestionAnsweredInputSchema.safeParse({
  73. questions: [
  74. validInput.questions[0],
  75. {
  76. header: "Runtime",
  77. question: "Which runtime should we target?",
  78. options: [
  79. { label: "Node", description: "Target Node.js" },
  80. { label: "Browser", description: "Target browsers" },
  81. ],
  82. },
  83. ],
  84. answers: {
  85. "Which runtime should we target?": "Node",
  86. },
  87. });
  88. expect(partial.success).toBe(true);
  89. });
  90. it("rejects blank values while still allowing omitted answers", () => {
  91. const blank = AskUserQuestionAnsweredInputSchema.safeParse({
  92. ...validInput,
  93. answers: { "Which date library should we use?": " " },
  94. });
  95. expect(blank.success).toBe(false);
  96. expect(blank.error?.issues[0]?.message).toContain("Blank answer is not allowed");
  97. });
  98. it("accepts an explicit empty answer map for automatic continuation", () => {
  99. const autoResolved = AskUserQuestionAnsweredInputSchema.safeParse({
  100. ...validInput,
  101. answers: {},
  102. });
  103. expect(autoResolved.success).toBe(true);
  104. });
  105. it("accepts markdown and valid HTML fragment previews", () => {
  106. const markdown = AskUserQuestionInputSchema.safeParse(
  107. inputWithPreview("Use `Array<string>` for typed lists."),
  108. );
  109. const html = AskUserQuestionInputSchema.safeParse(
  110. inputWithPreview("<section><h2>Dense table</h2><p>Shows every file.</p></section>"),
  111. );
  112. expect(markdown.success).toBe(true);
  113. expect(html.success).toBe(true);
  114. });
  115. it("rejects unsafe or document-shaped HTML previews", () => {
  116. const documentPreview = AskUserQuestionInputSchema.safeParse(
  117. inputWithPreview("<!doctype html><html><body>Full page</body></html>"),
  118. );
  119. const scriptedPreview = AskUserQuestionInputSchema.safeParse(
  120. inputWithPreview("<div>Preview</div><script>alert(1)</script>"),
  121. );
  122. const stylePreview = AskUserQuestionInputSchema.safeParse(
  123. inputWithPreview("<style>.x{color:red}</style><div>Preview</div>"),
  124. );
  125. const commentOnlyPreview = AskUserQuestionInputSchema.safeParse(
  126. inputWithPreview("<!-- no visible tag -->"),
  127. );
  128. expect(documentPreview.success).toBe(false);
  129. expect(documentPreview.error?.issues[0]?.message).toContain("fragment");
  130. expect(scriptedPreview.success).toBe(false);
  131. expect(scriptedPreview.error?.issues[0]?.message).toContain("script or style");
  132. expect(stylePreview.success).toBe(false);
  133. expect(stylePreview.error?.issues[0]?.message).toContain("script or style");
  134. expect(commentOnlyPreview.success).toBe(false);
  135. expect(commentOnlyPreview.error?.issues[0]?.message).toContain("HTML tag");
  136. });
  137. });
  138. function inputWithPreview(preview: string): typeof validInput {
  139. return {
  140. questions: [
  141. {
  142. ...validInput.questions[0],
  143. options: [
  144. { label: "Compact", description: "Small layout", preview },
  145. { label: "Detailed", description: "Expanded layout" },
  146. ],
  147. },
  148. ],
  149. };
  150. }