watcher.test.ts 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. import { $ } from "bun"
  2. import { describe, expect } from "bun:test"
  3. import fs from "fs/promises"
  4. import path from "path"
  5. import { ConfigProvider, Deferred, Duration, Effect, Fiber, Layer, Option, Stream } from "effect"
  6. import { Config } from "@opencode-ai/core/config"
  7. import { EventV2 } from "@opencode-ai/core/event"
  8. import { FSUtil } from "@opencode-ai/core/fs-util"
  9. import { Watcher } from "@opencode-ai/core/filesystem/watcher"
  10. import { Git } from "@opencode-ai/core/git"
  11. import { Location } from "@opencode-ai/core/location"
  12. import { AbsolutePath } from "@opencode-ai/core/schema"
  13. import { location } from "../fixture/location"
  14. import { tmpdir } from "../fixture/tmpdir"
  15. import { testEffect } from "../lib/effect"
  16. const describeWatcher = Watcher.hasNativeBinding() && !process.env.CI ? describe : describe.skip
  17. type WatcherEvent = { file: string; event: "add" | "change" | "unlink" }
  18. const it = testEffect(Layer.mergeAll(FSUtil.defaultLayer, EventV2.defaultLayer))
  19. const configLayer = Layer.succeed(
  20. Config.Service,
  21. Config.Service.of({
  22. entries: () => Effect.succeed([]),
  23. }),
  24. )
  25. const flagsLayer = ConfigProvider.layer(
  26. ConfigProvider.fromUnknown({
  27. OPENCODE_EXPERIMENTAL_FILEWATCHER: "true",
  28. OPENCODE_EXPERIMENTAL_DISABLE_FILEWATCHER: "false",
  29. }),
  30. )
  31. function provide(directory: string, vcs?: Location.Interface["vcs"]) {
  32. const locationLayer = Layer.succeed(
  33. Location.Service,
  34. Location.Service.of(location({ directory: AbsolutePath.make(directory) }, { vcs })),
  35. )
  36. return Effect.provide(
  37. Watcher.layer.pipe(
  38. Layer.provide(configLayer),
  39. Layer.provide(Git.defaultLayer),
  40. Layer.provide(locationLayer),
  41. Layer.provide(flagsLayer),
  42. ),
  43. )
  44. }
  45. function withTmp<A, E, R>(
  46. f: (directory: string, vcs?: Location.Interface["vcs"]) => Effect.Effect<A, E, R>,
  47. options?: { git?: boolean; init?: (directory: string) => Promise<void> },
  48. ) {
  49. return Effect.acquireRelease(
  50. Effect.promise(async () => {
  51. const tmp = await tmpdir()
  52. if (!options?.git) return { tmp, vcs: undefined }
  53. await $`git init`.cwd(tmp.path).quiet()
  54. await $`git config core.fsmonitor false`.cwd(tmp.path).quiet()
  55. await $`git config commit.gpgsign false`.cwd(tmp.path).quiet()
  56. await $`git config user.email test@opencode.test`.cwd(tmp.path).quiet()
  57. await $`git config user.name Test`.cwd(tmp.path).quiet()
  58. await $`git commit --allow-empty -m root`.cwd(tmp.path).quiet()
  59. await options.init?.(tmp.path)
  60. return { tmp, vcs: { type: "git" as const, store: AbsolutePath.make(path.join(tmp.path, ".git")) } }
  61. }),
  62. ({ tmp }) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  63. ).pipe(Effect.flatMap(({ tmp, vcs }) => f(tmp.path, vcs).pipe(provide(tmp.path, vcs))))
  64. }
  65. function wait(check: (event: WatcherEvent) => boolean) {
  66. return Effect.gen(function* () {
  67. const events = yield* EventV2.Service
  68. const deferred = yield* Deferred.make<WatcherEvent>()
  69. const fiber = yield* events.subscribe(Watcher.Event.Updated).pipe(
  70. Stream.runForEach((event) => {
  71. if (!check(event.data)) return Effect.void
  72. return Deferred.succeed(deferred, event.data).pipe(Effect.asVoid)
  73. }),
  74. Effect.forkScoped,
  75. )
  76. yield* Effect.yieldNow
  77. return { deferred, fiber }
  78. })
  79. }
  80. function maybeNextUpdate<E>(
  81. check: (event: WatcherEvent) => boolean,
  82. trigger: Effect.Effect<void, E>,
  83. timeout: Duration.Input = "5 seconds",
  84. ) {
  85. return Effect.acquireUseRelease(
  86. wait(check),
  87. ({ deferred }) => trigger.pipe(Effect.andThen(Deferred.await(deferred)), Effect.timeoutOption(timeout)),
  88. ({ fiber }) => Fiber.interrupt(fiber),
  89. )
  90. }
  91. function nextUpdate<E>(check: (event: WatcherEvent) => boolean, trigger: Effect.Effect<void, E>) {
  92. return Effect.gen(function* () {
  93. const result = yield* maybeNextUpdate(check, trigger)
  94. if (Option.isSome(result)) return result.value
  95. return yield* Effect.fail(new Error("timed out waiting for file watcher update"))
  96. })
  97. }
  98. function eventuallyUpdate<E>(check: (event: WatcherEvent) => boolean, trigger: () => Effect.Effect<void, E>) {
  99. return Effect.gen(function* () {
  100. while (true) {
  101. const result = yield* maybeNextUpdate(check, trigger(), "250 millis")
  102. if (Option.isSome(result)) return result.value
  103. }
  104. }).pipe(
  105. Effect.timeoutOrElse({
  106. duration: "5 seconds",
  107. orElse: () => Effect.fail(new Error("timed out waiting for file watcher readiness")),
  108. }),
  109. )
  110. }
  111. function noUpdate<E>(check: (event: WatcherEvent) => boolean, trigger: Effect.Effect<void, E>, timeout = 500) {
  112. return Effect.acquireUseRelease(
  113. wait(check),
  114. ({ deferred }) =>
  115. trigger.pipe(
  116. Effect.andThen(Deferred.await(deferred)),
  117. Effect.timeoutOption(`${timeout} millis`),
  118. Effect.tap((result) => Effect.sync(() => expect(result).toEqual(Option.none()))),
  119. ),
  120. ({ fiber }) => Fiber.interrupt(fiber),
  121. )
  122. }
  123. function ready(directory: string) {
  124. const file = path.join(directory, `.watcher-${Math.random().toString(36).slice(2)}`)
  125. return Effect.gen(function* () {
  126. const fs = yield* FSUtil.Service
  127. yield* eventuallyUpdate(
  128. (event) => event.file === file,
  129. () => fs.writeFileString(file, `ready-${Math.random()}`),
  130. ).pipe(Effect.ensuring(fs.remove(file, { force: true }).pipe(Effect.ignore)), Effect.asVoid)
  131. })
  132. }
  133. describeWatcher("Watcher", () => {
  134. it.live("publishes root create, update, and delete events", () =>
  135. withTmp(
  136. (directory) =>
  137. Effect.gen(function* () {
  138. const fs = yield* FSUtil.Service
  139. const file = path.join(directory, "watch.txt")
  140. yield* ready(directory)
  141. for (const item of [
  142. { event: "add" as const, trigger: fs.writeFileString(file, "a") },
  143. { event: "change" as const, trigger: fs.writeFileString(file, "b") },
  144. { event: "unlink" as const, trigger: fs.remove(file) },
  145. ]) {
  146. expect(
  147. yield* nextUpdate((event) => event.file === file && event.event === item.event, item.trigger),
  148. ).toEqual({
  149. file,
  150. event: item.event,
  151. })
  152. }
  153. }),
  154. { git: true },
  155. ),
  156. )
  157. it.live("watches non-git roots", () =>
  158. withTmp((directory) =>
  159. Effect.gen(function* () {
  160. const fs = yield* FSUtil.Service
  161. const file = path.join(directory, "plain.txt")
  162. yield* ready(directory)
  163. expect(yield* nextUpdate((event) => event.file === file, fs.writeFileString(file, "plain"))).toEqual({
  164. file,
  165. event: "add",
  166. })
  167. }),
  168. ),
  169. )
  170. it.live("cleanup stops publishing events", () =>
  171. Effect.gen(function* () {
  172. const events = yield* EventV2.Service
  173. const fs = yield* FSUtil.Service
  174. const tmp = yield* Effect.acquireRelease(
  175. Effect.promise(() => tmpdir()),
  176. (tmp) => Effect.promise(() => tmp[Symbol.asyncDispose]()),
  177. )
  178. yield* ready(tmp.path).pipe(provide(tmp.path), Effect.scoped)
  179. const file = path.join(tmp.path, "after-dispose.txt")
  180. yield* noUpdate((event) => event.file === file, fs.writeFileString(file, "gone")).pipe(
  181. Effect.provideService(EventV2.Service, events),
  182. )
  183. }).pipe(Effect.provide(Layer.mergeAll(FSUtil.defaultLayer, EventV2.defaultLayer))),
  184. )
  185. it.live("ignores .git/index changes", () =>
  186. withTmp(
  187. (directory) =>
  188. Effect.gen(function* () {
  189. const fs = yield* FSUtil.Service
  190. const index = path.join(directory, ".git", "index")
  191. yield* ready(directory)
  192. yield* noUpdate(
  193. (event) => event.file === index,
  194. fs
  195. .writeFileString(path.join(directory, "tracked.txt"), "a")
  196. .pipe(Effect.andThen(Effect.promise(() => $`git add .`.cwd(directory).quiet())), Effect.asVoid),
  197. )
  198. }),
  199. { git: true },
  200. ),
  201. )
  202. it.live("publishes .git/HEAD events", () =>
  203. withTmp(
  204. (directory) =>
  205. Effect.gen(function* () {
  206. const fs = yield* FSUtil.Service
  207. const head = path.join(directory, ".git", "HEAD")
  208. const branch = `watch-${Math.random().toString(36).slice(2)}`
  209. yield* ready(directory)
  210. yield* Effect.promise(() => $`git branch ${branch}`.cwd(directory).quiet())
  211. expect(
  212. yield* nextUpdate((event) => event.file === head, fs.writeFileString(head, `ref: refs/heads/${branch}\n`)),
  213. ).toEqual({
  214. file: head,
  215. event: "change",
  216. })
  217. }),
  218. { git: true },
  219. ),
  220. )
  221. const describeSymlink = process.platform !== "win32" ? describe : describe.skip
  222. describeSymlink("symlinked .git", () => {
  223. it.live("publishes .git/HEAD events through a symlinked .git directory", () =>
  224. withTmp(
  225. (directory) =>
  226. Effect.gen(function* () {
  227. const afs = yield* FSUtil.Service
  228. const actual = path.join(directory, "..", `actual_${path.basename(directory)}`)
  229. yield* Effect.addFinalizer(() => Effect.promise(() => fs.rm(actual, { recursive: true, force: true })))
  230. yield* ready(directory)
  231. const head = path.join(directory, ".git", "HEAD")
  232. const branch = `watch-${Math.random().toString(36).slice(2)}`
  233. yield* Effect.promise(() => $`git branch ${branch}`.cwd(directory).quiet())
  234. expect(
  235. yield* nextUpdate(
  236. (event) => event.file === path.join(actual, "HEAD"),
  237. afs.writeFileString(head, `ref: refs/heads/${branch}\n`),
  238. ),
  239. ).toEqual({ file: path.join(actual, "HEAD"), event: "change" })
  240. }),
  241. {
  242. git: true,
  243. init: async (directory) => {
  244. const actual = path.join(directory, "..", `actual_${path.basename(directory)}`)
  245. await fs.rename(path.join(directory, ".git"), actual)
  246. await fs.symlink(actual, path.join(directory, ".git"))
  247. },
  248. },
  249. ),
  250. )
  251. })
  252. })