argumentSubstitution.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { describe, expect, test } from "bun:test";
  2. import {
  3. parseArguments,
  4. parseArgumentNames,
  5. generateProgressiveArgumentHint,
  6. substituteArguments,
  7. } from "../argumentSubstitution";
  8. // ─── parseArguments ─────────────────────────────────────────────────────
  9. describe("parseArguments", () => {
  10. test("splits simple arguments", () => {
  11. expect(parseArguments("foo bar baz")).toEqual(["foo", "bar", "baz"]);
  12. });
  13. test("handles quoted strings", () => {
  14. expect(parseArguments('foo "hello world" baz')).toEqual([
  15. "foo",
  16. "hello world",
  17. "baz",
  18. ]);
  19. });
  20. test("handles single-quoted strings", () => {
  21. expect(parseArguments("foo 'hello world' baz")).toEqual([
  22. "foo",
  23. "hello world",
  24. "baz",
  25. ]);
  26. });
  27. test("returns empty for empty string", () => {
  28. expect(parseArguments("")).toEqual([]);
  29. });
  30. test("returns empty for whitespace only", () => {
  31. expect(parseArguments(" ")).toEqual([]);
  32. });
  33. });
  34. // ─── parseArgumentNames ─────────────────────────────────────────────────
  35. describe("parseArgumentNames", () => {
  36. test("parses space-separated string", () => {
  37. expect(parseArgumentNames("foo bar baz")).toEqual(["foo", "bar", "baz"]);
  38. });
  39. test("accepts array input", () => {
  40. expect(parseArgumentNames(["foo", "bar"])).toEqual(["foo", "bar"]);
  41. });
  42. test("filters out numeric-only names", () => {
  43. expect(parseArgumentNames("foo 123 bar")).toEqual(["foo", "bar"]);
  44. });
  45. test("filters out empty strings", () => {
  46. expect(parseArgumentNames(["foo", "", "bar"])).toEqual(["foo", "bar"]);
  47. });
  48. test("returns empty for undefined", () => {
  49. expect(parseArgumentNames(undefined)).toEqual([]);
  50. });
  51. });
  52. // ─── generateProgressiveArgumentHint ────────────────────────────────────
  53. describe("generateProgressiveArgumentHint", () => {
  54. test("shows remaining arguments", () => {
  55. expect(generateProgressiveArgumentHint(["a", "b", "c"], ["x"])).toBe(
  56. "[b] [c]"
  57. );
  58. });
  59. test("returns undefined when all filled", () => {
  60. expect(
  61. generateProgressiveArgumentHint(["a"], ["x"])
  62. ).toBeUndefined();
  63. });
  64. test("shows all when none typed", () => {
  65. expect(generateProgressiveArgumentHint(["a", "b"], [])).toBe("[a] [b]");
  66. });
  67. });
  68. // ─── substituteArguments ────────────────────────────────────────────────
  69. describe("substituteArguments", () => {
  70. test("replaces $ARGUMENTS with full args", () => {
  71. expect(substituteArguments("run $ARGUMENTS", "foo bar")).toBe(
  72. "run foo bar"
  73. );
  74. });
  75. test("replaces indexed $ARGUMENTS[0]", () => {
  76. expect(substituteArguments("run $ARGUMENTS[0]", "foo bar")).toBe("run foo");
  77. });
  78. test("replaces shorthand $0, $1", () => {
  79. expect(substituteArguments("$0 and $1", "hello world")).toBe(
  80. "hello and world"
  81. );
  82. });
  83. test("replaces named arguments", () => {
  84. expect(
  85. substituteArguments("file: $name", "test.txt", true, ["name"])
  86. ).toBe("file: test.txt");
  87. });
  88. test("returns content unchanged for undefined args", () => {
  89. expect(substituteArguments("hello", undefined)).toBe("hello");
  90. });
  91. test("appends ARGUMENTS when no placeholder found", () => {
  92. expect(substituteArguments("run this", "extra")).toBe(
  93. "run this\n\nARGUMENTS: extra"
  94. );
  95. });
  96. test("does not append when appendIfNoPlaceholder is false", () => {
  97. expect(substituteArguments("run this", "extra", false)).toBe("run this");
  98. });
  99. test("does not append for empty args string", () => {
  100. expect(substituteArguments("run this", "")).toBe("run this");
  101. });
  102. });