watcher.test.ts 9.8 KB

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