embedded-search-prelude.test.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. import { describe, expect, it } from "vitest";
  2. import {
  3. applyEmbeddedSearchPrelude,
  4. buildEmbeddedSearchPreludeContent,
  5. } from "../src/exec/embedded-search-prelude.js";
  6. describe("embedded search bash prelude", () => {
  7. it("wraps find and grep through the internal CLI backend", () => {
  8. const command = applyEmbeddedSearchPrelude("grep needle file.txt", {
  9. kind: "embedded-search",
  10. backend: {
  11. kind: "internal-cli",
  12. command: "zcode",
  13. args: ["__internal-search"],
  14. },
  15. });
  16. expect(command).toContain("unalias find 2>/dev/null || true");
  17. expect(command).toContain("unalias grep 2>/dev/null || true");
  18. expect(command).toContain('command -v zcode >/dev/null 2>&1 || { command find "$@"; return; }');
  19. expect(command).toContain('command zcode __internal-search find "$@"');
  20. expect(command).toContain(
  21. 'case "$_zcode_grep_arg" in -*-filter*|-*-pager*|-*-view*|-*-format-open*|-*-config*|---*|-@*|-*-save-config*|-[Zz]*|-[!-]*[Zz]*|--null|--null-data) command grep "$@"; return ;; esac',
  22. );
  23. expect(command).toContain('command -v zcode >/dev/null 2>&1 || { command grep "$@"; return; }');
  24. expect(command).toContain('command zcode __internal-search grep "$@"');
  25. expect(command).not.toContain("command -v rg");
  26. expect(command.endsWith("grep needle file.txt")).toBe(true);
  27. });
  28. it("can build prelude content without appending the user command", () => {
  29. const content = buildEmbeddedSearchPreludeContent({
  30. kind: "embedded-search",
  31. backend: {
  32. kind: "internal-cli",
  33. command: "zcode",
  34. args: ["__internal-search"],
  35. },
  36. });
  37. expect(content).toContain("unalias find 2>/dev/null || true");
  38. expect(content).toContain('command zcode __internal-search find "$@"');
  39. expect(content).not.toContain("grep needle file.txt");
  40. });
  41. it("quotes backend command parts for shell functions", () => {
  42. const command = applyEmbeddedSearchPrelude("find . -name '*.ts'", {
  43. kind: "embedded-search",
  44. backend: {
  45. kind: "internal-cli",
  46. command: "/tmp/z code",
  47. args: ["__internal-search", "quoted'value"],
  48. },
  49. });
  50. expect(command).toContain(
  51. 'command -v \'/tmp/z code\' >/dev/null 2>&1 || { command grep "$@"; return; }',
  52. );
  53. expect(command).toContain(
  54. 'command \'/tmp/z code\' __internal-search \'quoted\'\\\'\'value\' grep "$@"',
  55. );
  56. });
  57. it("preserves backend env assignments for Electron Node-mode dispatch", () => {
  58. const command = applyEmbeddedSearchPrelude("find . -name '*.ts'", {
  59. kind: "embedded-search",
  60. backend: {
  61. kind: "internal-cli",
  62. command: "/Applications/ZCode Helper",
  63. args: ["/tmp/zcode.cjs", "__internal-search"],
  64. env: { ELECTRON_RUN_AS_NODE: "1" },
  65. },
  66. });
  67. expect(command).toContain(
  68. 'ELECTRON_RUN_AS_NODE=1 command \'/Applications/ZCode Helper\' /tmp/zcode.cjs __internal-search find "$@"',
  69. );
  70. expect(command).toContain(
  71. 'ELECTRON_RUN_AS_NODE=1 command \'/Applications/ZCode Helper\' /tmp/zcode.cjs __internal-search grep "$@"',
  72. );
  73. });
  74. it("keeps an argv0-dispatch backend behind the same contract", () => {
  75. const command = applyEmbeddedSearchPrelude("find . -name '*.ts'", {
  76. kind: "embedded-search",
  77. backend: {
  78. kind: "argv0-dispatch",
  79. command: "zcode",
  80. },
  81. });
  82. expect(command).toContain('command -v zcode >/dev/null 2>&1 || { command find "$@"; return; }');
  83. expect(command).toContain('ARGV0=bfs command zcode -S dfs -regextype findutils-default "$@"');
  84. expect(command).toContain('command -v zcode >/dev/null 2>&1 || { command grep "$@"; return; }');
  85. expect(command).toContain(
  86. 'ARGV0=ugrep command zcode -G --ignore-files --hidden -I --exclude-dir=.git --exclude-dir=.svn --exclude-dir=.hg --exclude-dir=.bzr --exclude-dir=.jj --exclude-dir=.sl "$@"',
  87. );
  88. expect(command).toContain("if ! (unalias rg 2>/dev/null; command -v rg) >/dev/null 2>&1; then");
  89. expect(command).toContain('ARGV0=rg command zcode "$@"');
  90. });
  91. it("executes bundled bfs and ugrep directly and supplies rg only when unavailable", () => {
  92. const command = applyEmbeddedSearchPrelude("grep -Rni needle .", {
  93. kind: "embedded-search",
  94. backend: {
  95. kind: "native-binaries",
  96. findCommand: "/opt/zcode/tools/bfs/bfs",
  97. grepCommand: "/opt/zcode/tools/ugrep/ugrep",
  98. rgCommand: "/opt/zcode/tools/ripgrep/rg",
  99. },
  100. });
  101. expect(command).toContain(
  102. 'command /opt/zcode/tools/bfs/bfs -S dfs -regextype findutils-default "$@"',
  103. );
  104. expect(command).toContain(
  105. 'command /opt/zcode/tools/ugrep/ugrep -G --ignore-files --hidden -I --exclude-dir=.git --exclude-dir=.svn --exclude-dir=.hg --exclude-dir=.bzr --exclude-dir=.jj --exclude-dir=.sl "$@"',
  106. );
  107. expect(command).toContain("if ! (unalias rg 2>/dev/null; command -v rg) >/dev/null 2>&1; then");
  108. expect(command).toContain('command /opt/zcode/tools/ripgrep/rg "$@"');
  109. expect(command).not.toContain("__internal-search");
  110. expect(command.endsWith("grep -Rni needle .")).toBe(true);
  111. });
  112. it("keeps the rg fallback while find and grep enhancements are disabled", () => {
  113. const command = applyEmbeddedSearchPrelude("find . && grep needle file.txt && rg needle", {
  114. kind: "embedded-search",
  115. backend: {
  116. kind: "native-binaries",
  117. findCommand: "/opt/zcode/tools/bfs/bfs",
  118. grepCommand: "/opt/zcode/tools/ugrep/ugrep",
  119. rgCommand: "/opt/zcode/tools/ripgrep/rg",
  120. },
  121. findAndGrepEnabled: false,
  122. });
  123. expect(command).not.toContain("unalias find");
  124. expect(command).not.toContain("unalias grep");
  125. expect(command).not.toContain("/opt/zcode/tools/bfs/bfs");
  126. expect(command).not.toContain("/opt/zcode/tools/ugrep/ugrep");
  127. expect(command).toContain("command /opt/zcode/tools/ripgrep/rg");
  128. expect(command.endsWith("find . && grep needle file.txt && rg needle")).toBe(true);
  129. });
  130. it("does not advertise a same-name rg fallback when no bundled binary is available", () => {
  131. const command = applyEmbeddedSearchPrelude("command -v rg || true", {
  132. kind: "embedded-search",
  133. backend: {
  134. kind: "native-binaries",
  135. findCommand: "bfs",
  136. grepCommand: "ugrep",
  137. rgCommand: "rg",
  138. },
  139. });
  140. expect(command).not.toContain("command -v rg) >/dev/null");
  141. expect(command).not.toContain("rg() {");
  142. });
  143. it("converts Windows backend paths for Git Bash shell functions", () => {
  144. const command = applyEmbeddedSearchPrelude(
  145. "grep needle file.txt",
  146. {
  147. kind: "embedded-search",
  148. backend: {
  149. kind: "internal-cli",
  150. command: "C:\\Program Files\\ZCode\\zcode.exe",
  151. args: ["C:\\Users\\me\\z-code\\apps\\zcode-cli\\src\\run.ts", "__internal-search"],
  152. },
  153. },
  154. { shellDialect: "git-bash" },
  155. );
  156. expect(command).toContain(
  157. "command -v '/c/Program Files/ZCode/zcode.exe' >/dev/null 2>&1 || { command grep \"$@\"; return; }",
  158. );
  159. expect(command).toContain(
  160. "command '/c/Program Files/ZCode/zcode.exe' /c/Users/me/z-code/apps/zcode-cli/src/run.ts __internal-search grep \"$@\"",
  161. );
  162. });
  163. it("keeps system find and converts bundled ugrep and rg paths for Git Bash", () => {
  164. const command = applyEmbeddedSearchPrelude(
  165. "find . -name '*.ts'",
  166. {
  167. kind: "embedded-search",
  168. backend: {
  169. kind: "native-binaries",
  170. findCommand: "C:\\Program Files\\ZCode\\tools\\bfs\\bfs.exe",
  171. grepCommand: "C:\\Program Files\\ZCode\\tools\\ugrep\\ugrep.exe",
  172. rgCommand: "C:\\Program Files\\ZCode\\tools\\ripgrep\\rg.exe",
  173. },
  174. },
  175. { shellDialect: "git-bash" },
  176. );
  177. expect(command).not.toContain("unalias find");
  178. expect(command).not.toContain("find() {");
  179. expect(command).toContain(
  180. "command '/c/Program Files/ZCode/tools/ugrep/ugrep.exe' -G --ignore-files --hidden -I",
  181. );
  182. expect(command).toContain("command '/c/Program Files/ZCode/tools/ripgrep/rg.exe' \"$@\"");
  183. });
  184. it("does not inject POSIX shell functions for CMD or legacy shell fallback", () => {
  185. const prelude = {
  186. kind: "embedded-search" as const,
  187. backend: {
  188. kind: "internal-cli" as const,
  189. command: "zcode",
  190. args: ["__internal-search"],
  191. },
  192. };
  193. expect(
  194. applyEmbeddedSearchPrelude("grep needle file.txt", prelude, { shellDialect: "cmd" }),
  195. ).toBe("grep needle file.txt");
  196. expect(
  197. applyEmbeddedSearchPrelude("grep needle file.txt", prelude, {
  198. shellDialect: "legacy-shell",
  199. }),
  200. ).toBe("grep needle file.txt");
  201. });
  202. it("leaves shell commands unchanged without a prelude", () => {
  203. expect(applyEmbeddedSearchPrelude("echo ok")).toBe("echo ok");
  204. });
  205. });