utils.test.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. import { mock, describe, expect, test } from "bun:test";
  2. // Mock log.ts to cut the heavy dependency chain
  3. mock.module("src/utils/log.ts", () => ({
  4. logError: () => {},
  5. logToFile: () => {},
  6. getLogDisplayTitle: () => "",
  7. logEvent: () => {},
  8. logMCPError: () => {},
  9. logMCPDebug: () => {},
  10. dateToFilename: (d: Date) => d.toISOString().replace(/[:.]/g, "-"),
  11. getLogFilePath: () => "/tmp/mock-log",
  12. attachErrorLogSink: () => {},
  13. getInMemoryErrors: () => [],
  14. loadErrorLogs: async () => [],
  15. getErrorLogByIndex: async () => null,
  16. captureAPIRequest: () => {},
  17. _resetErrorLogForTesting: () => {},
  18. }));
  19. const {
  20. normalizeQuotes,
  21. stripTrailingWhitespace,
  22. findActualString,
  23. preserveQuoteStyle,
  24. applyEditToFile,
  25. LEFT_SINGLE_CURLY_QUOTE,
  26. RIGHT_SINGLE_CURLY_QUOTE,
  27. LEFT_DOUBLE_CURLY_QUOTE,
  28. RIGHT_DOUBLE_CURLY_QUOTE,
  29. } = await import("../utils");
  30. // ─── normalizeQuotes ────────────────────────────────────────────────────
  31. describe("normalizeQuotes", () => {
  32. test("converts left single curly to straight", () => {
  33. expect(normalizeQuotes(`${LEFT_SINGLE_CURLY_QUOTE}hello`)).toBe("'hello");
  34. });
  35. test("converts right single curly to straight", () => {
  36. expect(normalizeQuotes(`hello${RIGHT_SINGLE_CURLY_QUOTE}`)).toBe("hello'");
  37. });
  38. test("converts left double curly to straight", () => {
  39. expect(normalizeQuotes(`${LEFT_DOUBLE_CURLY_QUOTE}hello`)).toBe('"hello');
  40. });
  41. test("converts right double curly to straight", () => {
  42. expect(normalizeQuotes(`hello${RIGHT_DOUBLE_CURLY_QUOTE}`)).toBe('hello"');
  43. });
  44. test("leaves straight quotes unchanged", () => {
  45. expect(normalizeQuotes("'hello' \"world\"")).toBe("'hello' \"world\"");
  46. });
  47. test("handles empty string", () => {
  48. expect(normalizeQuotes("")).toBe("");
  49. });
  50. });
  51. // ─── stripTrailingWhitespace ────────────────────────────────────────────
  52. describe("stripTrailingWhitespace", () => {
  53. test("strips trailing spaces from lines", () => {
  54. expect(stripTrailingWhitespace("hello \nworld ")).toBe("hello\nworld");
  55. });
  56. test("strips trailing tabs", () => {
  57. expect(stripTrailingWhitespace("hello\t\nworld\t")).toBe("hello\nworld");
  58. });
  59. test("preserves leading whitespace", () => {
  60. expect(stripTrailingWhitespace(" hello \n world ")).toBe(
  61. " hello\n world"
  62. );
  63. });
  64. test("handles empty string", () => {
  65. expect(stripTrailingWhitespace("")).toBe("");
  66. });
  67. test("handles CRLF line endings", () => {
  68. expect(stripTrailingWhitespace("hello \r\nworld ")).toBe(
  69. "hello\r\nworld"
  70. );
  71. });
  72. test("handles no trailing whitespace", () => {
  73. expect(stripTrailingWhitespace("hello\nworld")).toBe("hello\nworld");
  74. });
  75. });
  76. // ─── findActualString ───────────────────────────────────────────────────
  77. describe("findActualString", () => {
  78. test("finds exact match", () => {
  79. expect(findActualString("hello world", "hello")).toBe("hello");
  80. });
  81. test("finds match with curly quotes normalized", () => {
  82. const fileContent = `${LEFT_DOUBLE_CURLY_QUOTE}hello${RIGHT_DOUBLE_CURLY_QUOTE}`;
  83. const result = findActualString(fileContent, '"hello"');
  84. expect(result).not.toBeNull();
  85. });
  86. test("returns null when not found", () => {
  87. expect(findActualString("hello world", "xyz")).toBeNull();
  88. });
  89. test("returns null for empty search in non-empty content", () => {
  90. // Empty string is always found at index 0 via includes()
  91. const result = findActualString("hello", "");
  92. expect(result).toBe("");
  93. });
  94. });
  95. // ─── preserveQuoteStyle ─────────────────────────────────────────────────
  96. describe("preserveQuoteStyle", () => {
  97. test("returns newString unchanged when no normalization happened", () => {
  98. expect(preserveQuoteStyle("hello", "hello", "world")).toBe("world");
  99. });
  100. test("converts straight double quotes to curly in replacement", () => {
  101. const oldString = '"hello"';
  102. const actualOldString = `${LEFT_DOUBLE_CURLY_QUOTE}hello${RIGHT_DOUBLE_CURLY_QUOTE}`;
  103. const newString = '"world"';
  104. const result = preserveQuoteStyle(oldString, actualOldString, newString);
  105. expect(result).toContain(LEFT_DOUBLE_CURLY_QUOTE);
  106. expect(result).toContain(RIGHT_DOUBLE_CURLY_QUOTE);
  107. });
  108. });
  109. // ─── applyEditToFile ────────────────────────────────────────────────────
  110. describe("applyEditToFile", () => {
  111. test("replaces first occurrence by default", () => {
  112. expect(applyEditToFile("foo bar foo", "foo", "baz")).toBe("baz bar foo");
  113. });
  114. test("replaces all occurrences with replaceAll=true", () => {
  115. expect(applyEditToFile("foo bar foo", "foo", "baz", true)).toBe(
  116. "baz bar baz"
  117. );
  118. });
  119. test("handles deletion (empty newString) with trailing newline", () => {
  120. const result = applyEditToFile("line1\nline2\nline3\n", "line2", "");
  121. expect(result).toBe("line1\nline3\n");
  122. });
  123. test("handles deletion without trailing newline", () => {
  124. const result = applyEditToFile("foobar", "foo", "");
  125. expect(result).toBe("bar");
  126. });
  127. test("handles no match (returns original)", () => {
  128. expect(applyEditToFile("hello world", "xyz", "abc")).toBe("hello world");
  129. });
  130. test("handles empty original content with insertion", () => {
  131. expect(applyEditToFile("", "", "new content")).toBe("new content");
  132. });
  133. });