effect.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. import { test, type TestOptions } from "bun:test"
  2. import { ConfigV1 } from "@kirincode-ai/core/v1/config/config"
  3. import { Cause, Duration, Effect, Exit, Layer } from "effect"
  4. import * as Scope from "effect/Scope"
  5. import * as TestClock from "effect/testing/TestClock"
  6. import * as TestConsole from "effect/testing/TestConsole"
  7. import { memoMap } from "@kirincode-ai/core/effect/memo-map"
  8. import type { Config } from "@/config/config"
  9. import { TestInstance, withTmpdirInstance } from "../fixture/fixture"
  10. import { InstanceStore } from "@/project/instance-store"
  11. type Body<A, E, R> = Effect.Effect<A, E, R> | (() => Effect.Effect<A, E, R>)
  12. type InstanceOptions<E, R> = {
  13. git?: boolean
  14. config?: Partial<ConfigV1.Info> | (() => Partial<ConfigV1.Info>)
  15. init?: (directory: string) => Effect.Effect<void, E, R>
  16. }
  17. function isInstanceOptions<E, R>(
  18. options: InstanceOptions<E, R> | number | TestOptions | undefined,
  19. ): options is InstanceOptions<E, R> {
  20. return !!options && typeof options === "object" && ("git" in options || "config" in options || "init" in options)
  21. }
  22. function instanceArgs<E, R>(
  23. options?: InstanceOptions<E, R> | number | TestOptions,
  24. testOptions?: number | TestOptions,
  25. ): { instanceOptions: InstanceOptions<E, R> | undefined; testOptions: number | TestOptions | undefined } {
  26. if (typeof options === "number") return { instanceOptions: undefined, testOptions: options }
  27. if (isInstanceOptions(options)) return { instanceOptions: options, testOptions }
  28. return { instanceOptions: undefined, testOptions: options }
  29. }
  30. const body = <A, E, R>(value: Body<A, E, R>) => Effect.suspend(() => (typeof value === "function" ? value() : value))
  31. type Runner = <A, E, R, E2>(value: Body<A, E, R | Scope.Scope>, layer: Layer.Layer<R, E2>) => Promise<A>
  32. const isolatedRun: Runner = (value, layer) =>
  33. Effect.gen(function* () {
  34. const exit = yield* body(value).pipe(Effect.scoped, Effect.provide(layer), Effect.exit)
  35. if (Exit.isFailure(exit)) {
  36. for (const err of Cause.prettyErrors(exit.cause)) {
  37. yield* Effect.logError(err)
  38. }
  39. }
  40. return yield* exit
  41. }).pipe(Effect.runPromise)
  42. // Builds the test layer through the shared process-wide memoMap so cached
  43. // services (Bus, Session, …) match Server.Default's instances. Use for tests
  44. // that publish to an in-process HTTP server and need pub/sub identity with
  45. // the server's handlers.
  46. const sharedRun: Runner = (value, layer) =>
  47. Effect.gen(function* () {
  48. const scope = yield* Scope.make()
  49. const ctx = yield* Layer.buildWithMemoMap(layer, memoMap, scope)
  50. const exit = yield* body(value).pipe(Effect.scoped, Effect.provide(ctx), Effect.exit)
  51. yield* Scope.close(scope, Exit.void)
  52. if (Exit.isFailure(exit)) {
  53. for (const err of Cause.prettyErrors(exit.cause)) {
  54. yield* Effect.logError(err)
  55. }
  56. }
  57. return yield* exit
  58. }).pipe(Effect.runPromise)
  59. const make = <R, E>(testLayer: Layer.Layer<R, E>, liveLayer: Layer.Layer<R, E>, run: Runner = isolatedRun) => {
  60. const effect = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
  61. test(name, () => run(value, testLayer), opts)
  62. effect.only = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
  63. test.only(name, () => run(value, testLayer), opts)
  64. effect.skip = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
  65. test.skip(name, () => run(value, testLayer), opts)
  66. const live = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
  67. test(name, () => run(value, liveLayer), opts)
  68. live.only = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
  69. test.only(name, () => run(value, liveLayer), opts)
  70. live.skip = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
  71. test.skip(name, () => run(value, liveLayer), opts)
  72. const instance = <A, E2, E3 = never>(
  73. name: string,
  74. value: Body<A, E2, R | InstanceStore.Service | TestInstance | Scope.Scope>,
  75. options?: InstanceOptions<E3, R | Scope.Scope> | number | TestOptions,
  76. opts?: number | TestOptions,
  77. ) => {
  78. const args = instanceArgs(options, opts)
  79. return test(
  80. name,
  81. () => run(body(value).pipe(withTmpdirInstance(args.instanceOptions)), liveLayer),
  82. args.testOptions,
  83. )
  84. }
  85. instance.only = <A, E2, E3 = never>(
  86. name: string,
  87. value: Body<A, E2, R | InstanceStore.Service | TestInstance | Scope.Scope>,
  88. options?: InstanceOptions<E3, R | Scope.Scope> | number | TestOptions,
  89. opts?: number | TestOptions,
  90. ) => {
  91. const args = instanceArgs(options, opts)
  92. return test.only(
  93. name,
  94. () => run(body(value).pipe(withTmpdirInstance(args.instanceOptions)), liveLayer),
  95. args.testOptions,
  96. )
  97. }
  98. instance.skip = <A, E2, E3 = never>(
  99. name: string,
  100. value: Body<A, E2, R | InstanceStore.Service | TestInstance | Scope.Scope>,
  101. options?: InstanceOptions<E3, R | Scope.Scope> | number | TestOptions,
  102. opts?: number | TestOptions,
  103. ) => {
  104. const args = instanceArgs(options, opts)
  105. return test.skip(
  106. name,
  107. () => run(body(value).pipe(withTmpdirInstance(args.instanceOptions)), liveLayer),
  108. args.testOptions,
  109. )
  110. }
  111. return { effect, live, instance }
  112. }
  113. // Test environment with TestClock and TestConsole
  114. const testEnv = Layer.mergeAll(TestConsole.layer, TestClock.layer())
  115. // Live environment - uses real clock, but keeps TestConsole for output capture
  116. const liveEnv = TestConsole.layer
  117. export const it = make<never, never>(testEnv, liveEnv)
  118. export const testEffect = <R, E>(layer: Layer.Layer<R, E>) =>
  119. make<R, E>(Layer.provideMerge(layer, testEnv), Layer.provideMerge(layer, liveEnv))
  120. // Variant of `testEffect` that builds the test layer through the shared
  121. // process-wide memoMap so services like Bus/Session resolve to the same
  122. // instances Server.Default uses. Use when a test needs pub/sub identity with
  123. // an in-process HTTP server — most tests should stick with `testEffect`.
  124. export const testEffectShared = <R, E>(layer: Layer.Layer<R, E>) =>
  125. make<R, E>(Layer.provideMerge(layer, testEnv), Layer.provideMerge(layer, liveEnv), sharedRun)
  126. export const awaitWithTimeout = <A, E, R>(
  127. self: Effect.Effect<A, E, R>,
  128. message: string,
  129. duration: Duration.Input = "2 seconds",
  130. ) =>
  131. self.pipe(
  132. Effect.timeoutOrElse({
  133. duration,
  134. orElse: () => Effect.fail(new Error(message)),
  135. }),
  136. )
  137. export const pollWithTimeout = <A, E, R>(
  138. self: Effect.Effect<A | undefined, E, R>,
  139. message: string,
  140. duration: Duration.Input = "5 seconds",
  141. ) =>
  142. Effect.gen(function* () {
  143. while (true) {
  144. const result = yield* self
  145. if (result !== undefined) return result
  146. yield* Effect.sleep("20 millis")
  147. }
  148. }).pipe(
  149. Effect.timeoutOrElse({
  150. duration,
  151. orElse: () => Effect.fail(new Error(message)),
  152. }),
  153. )