mobileInputZoom.test.ts 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. import { readFileSync } from "node:fs";
  2. import { resolve } from "node:path";
  3. import { describe, expect, it } from "vitest";
  4. import {
  5. shouldResetLexicalEditorAfterSubmit,
  6. shouldSubmitLexicalEnter,
  7. shouldSubmitLexicalModifiedEnter,
  8. } from "@/LexicalChatInput.js";
  9. import {
  10. isPrimaryFollowupModifierPressed,
  11. resolveOppositeFollowupDelivery,
  12. resolveFollowupModifierTooltip,
  13. shouldEnableModifiedEnterSubmit,
  14. shouldReverseFollowupDeliveryForPointer,
  15. } from "@/v4/composer/followupModeSettings.js";
  16. import { cn } from "@/components/lib/utils.js";
  17. import {
  18. resolveChatEnterShortcut,
  19. resolveChatEnterSubmits,
  20. resolveMobileInputTextSizeClassName,
  21. shouldAvoidIosInputFocusZoom,
  22. } from "@/lib/mobileTextInput.js";
  23. function readSource(path: string) {
  24. return readFileSync(resolve(process.cwd(), path), "utf8");
  25. }
  26. describe("mobile text input zoom guard", () => {
  27. it("keeps modified submit on desktop drafts and Windows coarse-pointer viewports", () => {
  28. expect(
  29. shouldEnableModifiedEnterSubmit({
  30. inputRoutingMode: "startNow",
  31. isMobileTextInputViewport: true,
  32. isWebRemoteControl: false,
  33. }),
  34. ).toBe(true);
  35. expect(
  36. shouldEnableModifiedEnterSubmit({
  37. inputRoutingMode: "enqueue",
  38. isMobileTextInputViewport: true,
  39. isWebRemoteControl: false,
  40. }),
  41. ).toBe(true);
  42. expect(
  43. shouldEnableModifiedEnterSubmit({
  44. inputRoutingMode: "enqueue",
  45. isMobileTextInputViewport: true,
  46. isWebRemoteControl: true,
  47. }),
  48. ).toBe(false);
  49. expect(
  50. shouldEnableModifiedEnterSubmit({
  51. inputRoutingMode: "reject",
  52. isMobileTextInputViewport: false,
  53. isWebRemoteControl: false,
  54. }),
  55. ).toBe(false);
  56. });
  57. it("resolves Cmd/Ctrl+Enter as send-now for queue and queue for guide", () => {
  58. expect(resolveOppositeFollowupDelivery("queue")).toBe("startNow");
  59. expect(resolveOppositeFollowupDelivery("guide")).toBe("queue");
  60. const composerSource = readSource("packages/ui/src/v4/ConversationComposer.tsx");
  61. expect(composerSource).not.toContain('if (routingMode === "startNow")');
  62. expect(
  63. shouldSubmitLexicalModifiedEnter({
  64. ctrlKey: true,
  65. modifiedEnterSubmits: true,
  66. text: "opposite delivery",
  67. }),
  68. ).toBe(true);
  69. expect(
  70. shouldSubmitLexicalModifiedEnter({
  71. metaKey: true,
  72. modifiedEnterSubmits: true,
  73. text: "opposite delivery",
  74. }),
  75. ).toBe(true);
  76. expect(
  77. shouldSubmitLexicalModifiedEnter({
  78. ctrlKey: true,
  79. isComposing: true,
  80. modifiedEnterSubmits: true,
  81. text: "IME",
  82. }),
  83. ).toBe(false);
  84. expect(
  85. shouldSubmitLexicalModifiedEnter({
  86. ctrlKey: true,
  87. modifiedEnterSubmits: true,
  88. shiftKey: true,
  89. text: "keep newline",
  90. }),
  91. ).toBe(false);
  92. });
  93. it("only clears the Lexical editor when the submit callback accepts the clear", () => {
  94. expect(shouldResetLexicalEditorAfterSubmit()).toBe(true);
  95. expect(shouldResetLexicalEditorAfterSubmit(true)).toBe(true);
  96. expect(shouldResetLexicalEditorAfterSubmit(false)).toBe(false);
  97. });
  98. it("only reverses pointer delivery for an enabled desktop modifier click", () => {
  99. expect(
  100. shouldReverseFollowupDeliveryForPointer({ enabled: true, metaKey: true, ctrlKey: false }),
  101. ).toBe(true);
  102. expect(
  103. shouldReverseFollowupDeliveryForPointer({ enabled: true, metaKey: false, ctrlKey: true }),
  104. ).toBe(true);
  105. expect(
  106. shouldReverseFollowupDeliveryForPointer({ enabled: true, metaKey: false, ctrlKey: false }),
  107. ).toBe(false);
  108. expect(
  109. shouldReverseFollowupDeliveryForPointer({ enabled: false, metaKey: true, ctrlKey: false }),
  110. ).toBe(false);
  111. });
  112. it("resolves the automatic opposite-action tooltip with a platform shortcut", () => {
  113. expect(
  114. resolveFollowupModifierTooltip({
  115. enabled: true,
  116. canSend: true,
  117. modifierPressed: true,
  118. followupMode: "queue",
  119. isApplePlatform: true,
  120. }),
  121. ).toEqual({
  122. delivery: "startNow",
  123. shortcut: "⌘ + Enter",
  124. titleId: "chat.followup.sendNow",
  125. });
  126. expect(
  127. resolveFollowupModifierTooltip({
  128. enabled: true,
  129. canSend: true,
  130. modifierPressed: true,
  131. followupMode: "guide",
  132. isApplePlatform: false,
  133. }),
  134. ).toEqual({
  135. delivery: "queue",
  136. shortcut: "Ctrl + Enter",
  137. titleId: "chat.followup.addToQueue",
  138. });
  139. expect(
  140. resolveFollowupModifierTooltip({
  141. enabled: true,
  142. canSend: false,
  143. modifierPressed: true,
  144. followupMode: "queue",
  145. isApplePlatform: true,
  146. }),
  147. ).toBeNull();
  148. });
  149. it("tracks only the platform primary follow-up modifier", () => {
  150. expect(
  151. isPrimaryFollowupModifierPressed({ isApplePlatform: true, metaKey: true, ctrlKey: false }),
  152. ).toBe(true);
  153. expect(
  154. isPrimaryFollowupModifierPressed({ isApplePlatform: true, metaKey: false, ctrlKey: true }),
  155. ).toBe(false);
  156. expect(
  157. isPrimaryFollowupModifierPressed({ isApplePlatform: false, metaKey: false, ctrlKey: true }),
  158. ).toBe(true);
  159. expect(
  160. isPrimaryFollowupModifierPressed({ isApplePlatform: false, metaKey: true, ctrlKey: false }),
  161. ).toBe(false);
  162. });
  163. it("keeps Enter as newline for mobile web remote control only", () => {
  164. const mobileRemoteEnterSubmits = resolveChatEnterSubmits({
  165. isMobileTextInputViewport: true,
  166. preferEnterNewline: true,
  167. });
  168. expect(mobileRemoteEnterSubmits).toBe(false);
  169. expect(resolveChatEnterShortcut({ enterSubmits: mobileRemoteEnterSubmits })).toBeUndefined();
  170. expect(
  171. shouldSubmitLexicalEnter({
  172. enterSubmits: mobileRemoteEnterSubmits,
  173. text: "first line",
  174. }),
  175. ).toBe(false);
  176. expect(
  177. resolveChatEnterSubmits({
  178. isMobileTextInputViewport: false,
  179. preferEnterNewline: true,
  180. }),
  181. ).toBe(true);
  182. });
  183. it("keeps desktop and non-remote input typography unchanged", () => {
  184. expect(
  185. shouldAvoidIosInputFocusZoom({
  186. isMobileTextInputViewport: false,
  187. isWebRemoteControl: true,
  188. }),
  189. ).toBe(false);
  190. expect(
  191. shouldAvoidIosInputFocusZoom({
  192. isMobileTextInputViewport: true,
  193. isWebRemoteControl: false,
  194. }),
  195. ).toBe(false);
  196. expect(
  197. resolveMobileInputTextSizeClassName({
  198. avoidIosInputFocusZoom: false,
  199. }),
  200. ).toBe("text-ui-base leading-5");
  201. });
  202. it("uses a fixed 16px token for mobile remote input even with a 12px UI preference", () => {
  203. expect(
  204. shouldAvoidIosInputFocusZoom({
  205. isMobileTextInputViewport: true,
  206. isWebRemoteControl: true,
  207. }),
  208. ).toBe(true);
  209. const className = resolveMobileInputTextSizeClassName({
  210. avoidIosInputFocusZoom: true,
  211. });
  212. const stylesSource = readSource("packages/ui/src/styles.css");
  213. expect(className).toBe("text-mobile-input-safe leading-6");
  214. expect(cn("text-ui-base", className)).toBe("text-mobile-input-safe leading-6");
  215. expect(cn("text-ui-base", "text-ui-xl")).toBe("text-ui-xl");
  216. expect(cn("text-ui-base", "text-ui-caption")).toBe("text-ui-caption");
  217. expect(stylesSource).toContain("--text-mobile-input-safe: 16px;");
  218. expect(stylesSource).toContain("--text-ui-xl: calc(var(--ui-font-size) + 4px);");
  219. expect(stylesSource).toContain("--text-ui-lg: calc(var(--ui-font-size) + 2px);");
  220. });
  221. it("wires the zoom guard through remote composer inputs", () => {
  222. const composerSource = readSource("packages/ui/src/v4/ConversationComposer.tsx");
  223. const interactionDialogSource = readSource("packages/ui/src/v4/V4InteractionDialogs.tsx");
  224. const permissionDialogSource = readSource("packages/ui/src/PermissionDialog.tsx");
  225. const sessionPaneSource = readSource("packages/ui/src/v4/SessionPane.tsx");
  226. const promptEditorSource = readSource("packages/ui/src/prompt-editor/ChatPromptEditor.tsx");
  227. const lexicalInputSource = readSource("packages/ui/src/LexicalChatInput.tsx");
  228. expect(composerSource).toContain("shouldAvoidIosInputFocusZoom({");
  229. expect(composerSource).toContain("isWebRemoteControl,");
  230. expect(composerSource).toContain("isMobileTextInputViewport,");
  231. expect(composerSource).toContain("preferEnterNewline: isWebRemoteControl,");
  232. expect(composerSource).toContain("avoidIosInputFocusZoom={avoidIosInputFocusZoom}");
  233. expect(composerSource).toContain("compactPlaceholder={compactMobilePlaceholder}");
  234. expect(sessionPaneSource).toContain("isWebRemoteControl={compactForRemoteControl}");
  235. expect(interactionDialogSource).toContain("isWebRemoteControl={isWebRemoteControl}");
  236. expect(permissionDialogSource).toContain("useIsMobileTextInputViewport");
  237. expect(permissionDialogSource).toContain("resolveMobileInputTextSizeClassName({");
  238. expect(permissionDialogSource).toContain("avoidIosInputFocusZoom");
  239. expect(promptEditorSource).toContain("avoidIosInputFocusZoom={avoidIosInputFocusZoom}");
  240. expect(promptEditorSource).toContain("compactPlaceholder={compactPlaceholder}");
  241. expect(lexicalInputSource).toContain("resolveMobileInputTextSizeClassName({");
  242. expect(lexicalInputSource).toContain('compactPlaceholder ? "line-clamp-2" : ""');
  243. expect(lexicalInputSource).not.toContain("user-scalable=no");
  244. expect(lexicalInputSource).not.toContain("maximum-scale");
  245. });
  246. });