edit.test.ts 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692
  1. import { afterAll, afterEach, describe, test, expect } from "bun:test"
  2. import path from "path"
  3. import fs from "fs/promises"
  4. import { Effect, Layer, ManagedRuntime } from "effect"
  5. import { EditTool } from "../../src/tool/edit"
  6. import { Instance } from "../../src/project/instance"
  7. import { tmpdir } from "../fixture/fixture"
  8. import { LSP } from "../../src/lsp"
  9. import { AppFileSystem } from "@opencode-ai/shared/filesystem"
  10. import { Format } from "../../src/format"
  11. import { Agent } from "../../src/agent/agent"
  12. import { Bus } from "../../src/bus"
  13. import { BusEvent } from "../../src/bus/bus-event"
  14. import { Truncate } from "../../src/tool"
  15. import { SessionID, MessageID } from "../../src/session/schema"
  16. const ctx = {
  17. sessionID: SessionID.make("ses_test-edit-session"),
  18. messageID: MessageID.make(""),
  19. callID: "",
  20. agent: "build",
  21. abort: AbortSignal.any([]),
  22. messages: [],
  23. metadata: () => Effect.void,
  24. ask: () => Effect.void,
  25. }
  26. afterEach(async () => {
  27. await Instance.disposeAll()
  28. })
  29. const runtime = ManagedRuntime.make(
  30. Layer.mergeAll(
  31. LSP.defaultLayer,
  32. AppFileSystem.defaultLayer,
  33. Format.defaultLayer,
  34. Bus.layer,
  35. Truncate.defaultLayer,
  36. Agent.defaultLayer,
  37. ),
  38. )
  39. afterAll(async () => {
  40. await runtime.dispose()
  41. })
  42. const resolve = () =>
  43. runtime.runPromise(
  44. Effect.gen(function* () {
  45. const info = yield* EditTool
  46. return yield* info.init()
  47. }),
  48. )
  49. const subscribeBus = <D extends BusEvent.Definition>(def: D, callback: () => unknown) =>
  50. runtime.runPromise(Bus.Service.use((bus) => bus.subscribeCallback(def, callback)))
  51. async function onceBus<D extends BusEvent.Definition>(def: D) {
  52. const result = Promise.withResolvers<void>()
  53. const unsub = await subscribeBus(def, () => {
  54. unsub()
  55. result.resolve()
  56. })
  57. return {
  58. wait: result.promise,
  59. unsub,
  60. }
  61. }
  62. describe("tool.edit", () => {
  63. describe("creating new files", () => {
  64. test("creates new file when oldString is empty", async () => {
  65. await using tmp = await tmpdir()
  66. const filepath = path.join(tmp.path, "newfile.txt")
  67. await Instance.provide({
  68. directory: tmp.path,
  69. fn: async () => {
  70. const edit = await resolve()
  71. const result = await Effect.runPromise(
  72. edit.execute(
  73. {
  74. filePath: filepath,
  75. oldString: "",
  76. newString: "new content",
  77. },
  78. ctx,
  79. ),
  80. )
  81. expect(result.metadata.diff).toContain("new content")
  82. const content = await fs.readFile(filepath, "utf-8")
  83. expect(content).toBe("new content")
  84. },
  85. })
  86. })
  87. test("creates new file with nested directories", async () => {
  88. await using tmp = await tmpdir()
  89. const filepath = path.join(tmp.path, "nested", "dir", "file.txt")
  90. await Instance.provide({
  91. directory: tmp.path,
  92. fn: async () => {
  93. const edit = await resolve()
  94. await Effect.runPromise(
  95. edit.execute(
  96. {
  97. filePath: filepath,
  98. oldString: "",
  99. newString: "nested file",
  100. },
  101. ctx,
  102. ),
  103. )
  104. const content = await fs.readFile(filepath, "utf-8")
  105. expect(content).toBe("nested file")
  106. },
  107. })
  108. })
  109. test("emits add event for new files", async () => {
  110. await using tmp = await tmpdir()
  111. const filepath = path.join(tmp.path, "new.txt")
  112. await Instance.provide({
  113. directory: tmp.path,
  114. fn: async () => {
  115. const { FileWatcher } = await import("../../src/file/watcher")
  116. const updated = await onceBus(FileWatcher.Event.Updated)
  117. try {
  118. const edit = await resolve()
  119. await Effect.runPromise(
  120. edit.execute(
  121. {
  122. filePath: filepath,
  123. oldString: "",
  124. newString: "content",
  125. },
  126. ctx,
  127. ),
  128. )
  129. await updated.wait
  130. } finally {
  131. updated.unsub()
  132. }
  133. },
  134. })
  135. })
  136. })
  137. describe("editing existing files", () => {
  138. test("replaces text in existing file", async () => {
  139. await using tmp = await tmpdir()
  140. const filepath = path.join(tmp.path, "existing.txt")
  141. await fs.writeFile(filepath, "old content here", "utf-8")
  142. await Instance.provide({
  143. directory: tmp.path,
  144. fn: async () => {
  145. const edit = await resolve()
  146. const result = await Effect.runPromise(
  147. edit.execute(
  148. {
  149. filePath: filepath,
  150. oldString: "old content",
  151. newString: "new content",
  152. },
  153. ctx,
  154. ),
  155. )
  156. expect(result.output).toContain("Edit applied successfully")
  157. const content = await fs.readFile(filepath, "utf-8")
  158. expect(content).toBe("new content here")
  159. },
  160. })
  161. })
  162. test("throws error when file does not exist", async () => {
  163. await using tmp = await tmpdir()
  164. const filepath = path.join(tmp.path, "nonexistent.txt")
  165. await Instance.provide({
  166. directory: tmp.path,
  167. fn: async () => {
  168. const edit = await resolve()
  169. await expect(
  170. Effect.runPromise(
  171. edit.execute(
  172. {
  173. filePath: filepath,
  174. oldString: "old",
  175. newString: "new",
  176. },
  177. ctx,
  178. ),
  179. ),
  180. ).rejects.toThrow("not found")
  181. },
  182. })
  183. })
  184. test("throws error when oldString equals newString", async () => {
  185. await using tmp = await tmpdir()
  186. const filepath = path.join(tmp.path, "file.txt")
  187. await fs.writeFile(filepath, "content", "utf-8")
  188. await Instance.provide({
  189. directory: tmp.path,
  190. fn: async () => {
  191. const edit = await resolve()
  192. await expect(
  193. Effect.runPromise(
  194. edit.execute(
  195. {
  196. filePath: filepath,
  197. oldString: "same",
  198. newString: "same",
  199. },
  200. ctx,
  201. ),
  202. ),
  203. ).rejects.toThrow("identical")
  204. },
  205. })
  206. })
  207. test("throws error when oldString not found in file", async () => {
  208. await using tmp = await tmpdir()
  209. const filepath = path.join(tmp.path, "file.txt")
  210. await fs.writeFile(filepath, "actual content", "utf-8")
  211. await Instance.provide({
  212. directory: tmp.path,
  213. fn: async () => {
  214. const edit = await resolve()
  215. await expect(
  216. Effect.runPromise(
  217. edit.execute(
  218. {
  219. filePath: filepath,
  220. oldString: "not in file",
  221. newString: "replacement",
  222. },
  223. ctx,
  224. ),
  225. ),
  226. ).rejects.toThrow()
  227. },
  228. })
  229. })
  230. test("replaces all occurrences with replaceAll option", async () => {
  231. await using tmp = await tmpdir()
  232. const filepath = path.join(tmp.path, "file.txt")
  233. await fs.writeFile(filepath, "foo bar foo baz foo", "utf-8")
  234. await Instance.provide({
  235. directory: tmp.path,
  236. fn: async () => {
  237. const edit = await resolve()
  238. await Effect.runPromise(
  239. edit.execute(
  240. {
  241. filePath: filepath,
  242. oldString: "foo",
  243. newString: "qux",
  244. replaceAll: true,
  245. },
  246. ctx,
  247. ),
  248. )
  249. const content = await fs.readFile(filepath, "utf-8")
  250. expect(content).toBe("qux bar qux baz qux")
  251. },
  252. })
  253. })
  254. test("emits change event for existing files", async () => {
  255. await using tmp = await tmpdir()
  256. const filepath = path.join(tmp.path, "file.txt")
  257. await fs.writeFile(filepath, "original", "utf-8")
  258. await Instance.provide({
  259. directory: tmp.path,
  260. fn: async () => {
  261. const { FileWatcher } = await import("../../src/file/watcher")
  262. const updated = await onceBus(FileWatcher.Event.Updated)
  263. try {
  264. const edit = await resolve()
  265. await Effect.runPromise(
  266. edit.execute(
  267. {
  268. filePath: filepath,
  269. oldString: "original",
  270. newString: "modified",
  271. },
  272. ctx,
  273. ),
  274. )
  275. await updated.wait
  276. } finally {
  277. updated.unsub()
  278. }
  279. },
  280. })
  281. })
  282. })
  283. describe("edge cases", () => {
  284. test("handles multiline replacements", async () => {
  285. await using tmp = await tmpdir()
  286. const filepath = path.join(tmp.path, "file.txt")
  287. await fs.writeFile(filepath, "line1\nline2\nline3", "utf-8")
  288. await Instance.provide({
  289. directory: tmp.path,
  290. fn: async () => {
  291. const edit = await resolve()
  292. await Effect.runPromise(
  293. edit.execute(
  294. {
  295. filePath: filepath,
  296. oldString: "line2",
  297. newString: "new line 2\nextra line",
  298. },
  299. ctx,
  300. ),
  301. )
  302. const content = await fs.readFile(filepath, "utf-8")
  303. expect(content).toBe("line1\nnew line 2\nextra line\nline3")
  304. },
  305. })
  306. })
  307. test("handles CRLF line endings", async () => {
  308. await using tmp = await tmpdir()
  309. const filepath = path.join(tmp.path, "file.txt")
  310. await fs.writeFile(filepath, "line1\r\nold\r\nline3", "utf-8")
  311. await Instance.provide({
  312. directory: tmp.path,
  313. fn: async () => {
  314. const edit = await resolve()
  315. await Effect.runPromise(
  316. edit.execute(
  317. {
  318. filePath: filepath,
  319. oldString: "old",
  320. newString: "new",
  321. },
  322. ctx,
  323. ),
  324. )
  325. const content = await fs.readFile(filepath, "utf-8")
  326. expect(content).toBe("line1\r\nnew\r\nline3")
  327. },
  328. })
  329. })
  330. test("throws error when oldString equals newString", async () => {
  331. await using tmp = await tmpdir()
  332. const filepath = path.join(tmp.path, "file.txt")
  333. await fs.writeFile(filepath, "content", "utf-8")
  334. await Instance.provide({
  335. directory: tmp.path,
  336. fn: async () => {
  337. const edit = await resolve()
  338. await expect(
  339. Effect.runPromise(
  340. edit.execute(
  341. {
  342. filePath: filepath,
  343. oldString: "",
  344. newString: "",
  345. },
  346. ctx,
  347. ),
  348. ),
  349. ).rejects.toThrow("identical")
  350. },
  351. })
  352. })
  353. test("throws error when path is directory", async () => {
  354. await using tmp = await tmpdir()
  355. const dirpath = path.join(tmp.path, "adir")
  356. await fs.mkdir(dirpath)
  357. await Instance.provide({
  358. directory: tmp.path,
  359. fn: async () => {
  360. const edit = await resolve()
  361. await expect(
  362. Effect.runPromise(
  363. edit.execute(
  364. {
  365. filePath: dirpath,
  366. oldString: "old",
  367. newString: "new",
  368. },
  369. ctx,
  370. ),
  371. ),
  372. ).rejects.toThrow("directory")
  373. },
  374. })
  375. })
  376. test("tracks file diff statistics", async () => {
  377. await using tmp = await tmpdir()
  378. const filepath = path.join(tmp.path, "file.txt")
  379. await fs.writeFile(filepath, "line1\nline2\nline3", "utf-8")
  380. await Instance.provide({
  381. directory: tmp.path,
  382. fn: async () => {
  383. const edit = await resolve()
  384. const result = await Effect.runPromise(
  385. edit.execute(
  386. {
  387. filePath: filepath,
  388. oldString: "line2",
  389. newString: "new line a\nnew line b",
  390. },
  391. ctx,
  392. ),
  393. )
  394. expect(result.metadata.filediff).toBeDefined()
  395. expect(result.metadata.filediff.file).toBe(filepath)
  396. expect(result.metadata.filediff.additions).toBeGreaterThan(0)
  397. },
  398. })
  399. })
  400. })
  401. describe("line endings", () => {
  402. const old = "alpha\nbeta\ngamma"
  403. const next = "alpha\nbeta-updated\ngamma"
  404. const alt = "alpha\nbeta\nomega"
  405. const normalize = (text: string, ending: "\n" | "\r\n") => {
  406. const normalized = text.replaceAll("\r\n", "\n")
  407. if (ending === "\n") return normalized
  408. return normalized.replaceAll("\n", "\r\n")
  409. }
  410. const count = (content: string) => {
  411. const crlf = content.match(/\r\n/g)?.length ?? 0
  412. const lf = content.match(/\n/g)?.length ?? 0
  413. return {
  414. crlf,
  415. lf: lf - crlf,
  416. }
  417. }
  418. const expectLf = (content: string) => {
  419. const counts = count(content)
  420. expect(counts.crlf).toBe(0)
  421. expect(counts.lf).toBeGreaterThan(0)
  422. }
  423. const expectCrlf = (content: string) => {
  424. const counts = count(content)
  425. expect(counts.lf).toBe(0)
  426. expect(counts.crlf).toBeGreaterThan(0)
  427. }
  428. type Input = {
  429. content: string
  430. oldString: string
  431. newString: string
  432. replaceAll?: boolean
  433. }
  434. const apply = async (input: Input) => {
  435. await using tmp = await tmpdir({
  436. init: async (dir) => {
  437. await Bun.write(path.join(dir, "test.txt"), input.content)
  438. },
  439. })
  440. return await Instance.provide({
  441. directory: tmp.path,
  442. fn: async () => {
  443. const edit = await resolve()
  444. const filePath = path.join(tmp.path, "test.txt")
  445. await Effect.runPromise(
  446. edit.execute(
  447. {
  448. filePath,
  449. oldString: input.oldString,
  450. newString: input.newString,
  451. replaceAll: input.replaceAll,
  452. },
  453. ctx,
  454. ),
  455. )
  456. return await Bun.file(filePath).text()
  457. },
  458. })
  459. }
  460. test("preserves LF with LF multi-line strings", async () => {
  461. const content = normalize(old + "\n", "\n")
  462. const output = await apply({
  463. content,
  464. oldString: normalize(old, "\n"),
  465. newString: normalize(next, "\n"),
  466. })
  467. expect(output).toBe(normalize(next + "\n", "\n"))
  468. expectLf(output)
  469. })
  470. test("preserves CRLF with CRLF multi-line strings", async () => {
  471. const content = normalize(old + "\n", "\r\n")
  472. const output = await apply({
  473. content,
  474. oldString: normalize(old, "\r\n"),
  475. newString: normalize(next, "\r\n"),
  476. })
  477. expect(output).toBe(normalize(next + "\n", "\r\n"))
  478. expectCrlf(output)
  479. })
  480. test("preserves LF when old/new use CRLF", async () => {
  481. const content = normalize(old + "\n", "\n")
  482. const output = await apply({
  483. content,
  484. oldString: normalize(old, "\r\n"),
  485. newString: normalize(next, "\r\n"),
  486. })
  487. expect(output).toBe(normalize(next + "\n", "\n"))
  488. expectLf(output)
  489. })
  490. test("preserves CRLF when old/new use LF", async () => {
  491. const content = normalize(old + "\n", "\r\n")
  492. const output = await apply({
  493. content,
  494. oldString: normalize(old, "\n"),
  495. newString: normalize(next, "\n"),
  496. })
  497. expect(output).toBe(normalize(next + "\n", "\r\n"))
  498. expectCrlf(output)
  499. })
  500. test("preserves LF when newString uses CRLF", async () => {
  501. const content = normalize(old + "\n", "\n")
  502. const output = await apply({
  503. content,
  504. oldString: normalize(old, "\n"),
  505. newString: normalize(next, "\r\n"),
  506. })
  507. expect(output).toBe(normalize(next + "\n", "\n"))
  508. expectLf(output)
  509. })
  510. test("preserves CRLF when newString uses LF", async () => {
  511. const content = normalize(old + "\n", "\r\n")
  512. const output = await apply({
  513. content,
  514. oldString: normalize(old, "\r\n"),
  515. newString: normalize(next, "\n"),
  516. })
  517. expect(output).toBe(normalize(next + "\n", "\r\n"))
  518. expectCrlf(output)
  519. })
  520. test("preserves LF with mixed old/new line endings", async () => {
  521. const content = normalize(old + "\n", "\n")
  522. const output = await apply({
  523. content,
  524. oldString: "alpha\nbeta\r\ngamma",
  525. newString: "alpha\r\nbeta\nomega",
  526. })
  527. expect(output).toBe(normalize(alt + "\n", "\n"))
  528. expectLf(output)
  529. })
  530. test("preserves CRLF with mixed old/new line endings", async () => {
  531. const content = normalize(old + "\n", "\r\n")
  532. const output = await apply({
  533. content,
  534. oldString: "alpha\r\nbeta\ngamma",
  535. newString: "alpha\nbeta\r\nomega",
  536. })
  537. expect(output).toBe(normalize(alt + "\n", "\r\n"))
  538. expectCrlf(output)
  539. })
  540. test("replaceAll preserves LF for multi-line blocks", async () => {
  541. const blockOld = "alpha\nbeta"
  542. const blockNew = "alpha\nbeta-updated"
  543. const content = normalize(blockOld + "\n" + blockOld + "\n", "\n")
  544. const output = await apply({
  545. content,
  546. oldString: normalize(blockOld, "\n"),
  547. newString: normalize(blockNew, "\n"),
  548. replaceAll: true,
  549. })
  550. expect(output).toBe(normalize(blockNew + "\n" + blockNew + "\n", "\n"))
  551. expectLf(output)
  552. })
  553. test("replaceAll preserves CRLF for multi-line blocks", async () => {
  554. const blockOld = "alpha\nbeta"
  555. const blockNew = "alpha\nbeta-updated"
  556. const content = normalize(blockOld + "\n" + blockOld + "\n", "\r\n")
  557. const output = await apply({
  558. content,
  559. oldString: normalize(blockOld, "\r\n"),
  560. newString: normalize(blockNew, "\r\n"),
  561. replaceAll: true,
  562. })
  563. expect(output).toBe(normalize(blockNew + "\n" + blockNew + "\n", "\r\n"))
  564. expectCrlf(output)
  565. })
  566. })
  567. describe("concurrent editing", () => {
  568. test("preserves concurrent edits to different sections of the same file", async () => {
  569. await using tmp = await tmpdir()
  570. const filepath = path.join(tmp.path, "file.txt")
  571. await fs.writeFile(filepath, "top = 0\nmiddle = keep\nbottom = 0\n", "utf-8")
  572. await Instance.provide({
  573. directory: tmp.path,
  574. fn: async () => {
  575. const edit = await resolve()
  576. let asks = 0
  577. const firstAsk = Promise.withResolvers<void>()
  578. const delayedCtx = {
  579. ...ctx,
  580. ask: () =>
  581. Effect.gen(function* () {
  582. asks++
  583. if (asks !== 1) return
  584. firstAsk.resolve()
  585. yield* Effect.promise(() => Bun.sleep(50))
  586. }),
  587. }
  588. const promise1 = Effect.runPromise(
  589. edit.execute(
  590. {
  591. filePath: filepath,
  592. oldString: "top = 0",
  593. newString: "top = 1",
  594. },
  595. delayedCtx,
  596. ),
  597. )
  598. await firstAsk.promise
  599. const promise2 = Effect.runPromise(
  600. edit.execute(
  601. {
  602. filePath: filepath,
  603. oldString: "bottom = 0",
  604. newString: "bottom = 2",
  605. },
  606. delayedCtx,
  607. ),
  608. )
  609. const results = await Promise.allSettled([promise1, promise2])
  610. expect(results[0]?.status).toBe("fulfilled")
  611. expect(results[1]?.status).toBe("fulfilled")
  612. expect(await fs.readFile(filepath, "utf-8")).toBe("top = 1\nmiddle = keep\nbottom = 2\n")
  613. },
  614. })
  615. })
  616. })
  617. })