slashCommands.test.ts 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import { describe, expect, it } from "vitest";
  2. import type { ZCodeSlashCommand } from "@zcode/shared";
  3. import {
  4. extractActiveSlashQuery,
  5. filterSlashCommands,
  6. replaceActiveSlashCommand,
  7. } from "../src/lib/slashCommands.js";
  8. const commands: ZCodeSlashCommand[] = [
  9. {
  10. name: "agent-browser",
  11. description: "Browser automation CLI for AI agents",
  12. },
  13. {
  14. name: "ai-elements",
  15. description: "Build AI chat interfaces using ai-elements components",
  16. },
  17. {
  18. name: "plain-docs",
  19. description: "Reference plain-text docs",
  20. },
  21. ];
  22. describe("extractActiveSlashQuery", () => {
  23. it("returns the query when the current line starts with slash", () => {
  24. expect(extractActiveSlashQuery("/agent"))?.toBe("agent");
  25. expect(extractActiveSlashQuery("hello\n/agent"))?.toBe("agent");
  26. });
  27. it("returns empty string for a fresh slash trigger", () => {
  28. expect(extractActiveSlashQuery("/"))?.toBe("");
  29. });
  30. it("returns null when slash is not at the beginning of the line", () => {
  31. expect(extractActiveSlashQuery("hello /agent")).toBeNull();
  32. expect(extractActiveSlashQuery("/agent test")).toBeNull();
  33. expect(extractActiveSlashQuery("/agent\ttest")).toBeNull();
  34. });
  35. });
  36. describe("filterSlashCommands", () => {
  37. it("keeps original order when query is empty", () => {
  38. expect(filterSlashCommands(commands, "").map((command) => command.name)).toEqual([
  39. "agent-browser",
  40. "ai-elements",
  41. "plain-docs",
  42. ]);
  43. });
  44. it("prefers prefix matches over weaker fuzzy matches", () => {
  45. expect(filterSlashCommands(commands, "ai").map((command) => command.name)).toEqual([
  46. "ai-elements",
  47. "plain-docs",
  48. "agent-browser",
  49. ]);
  50. });
  51. it("supports subsequence fuzzy matching", () => {
  52. expect(filterSlashCommands(commands, "aeb").map((command) => command.name)).toEqual([
  53. "agent-browser",
  54. ]);
  55. });
  56. });
  57. describe("replaceActiveSlashCommand", () => {
  58. it("replaces the active slash token and moves cursor after the command", () => {
  59. expect(replaceActiveSlashCommand("/ag", "", "agent-browser")).toEqual({
  60. cursorOffset: 15,
  61. text: "/agent-browser ",
  62. });
  63. });
  64. it("preserves text after the slash token once whitespace starts", () => {
  65. expect(replaceActiveSlashCommand("hello\n/ag", " remaining text", "agent-browser")).toEqual({
  66. cursorOffset: 21,
  67. text: "hello\n/agent-browser remaining text",
  68. });
  69. });
  70. it("drops the unmatched token tail after the cursor", () => {
  71. expect(replaceActiveSlashCommand("/ag", "ent-browser", "agent-browser")).toEqual({
  72. cursorOffset: 15,
  73. text: "/agent-browser ",
  74. });
  75. });
  76. });