effect.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { test, type TestOptions } from "bun:test"
  2. import { Cause, Effect, Exit, Layer } from "effect"
  3. import type * as Scope from "effect/Scope"
  4. import * as TestClock from "effect/testing/TestClock"
  5. import * as TestConsole from "effect/testing/TestConsole"
  6. import type { Config } from "@/config/config"
  7. import { TestInstance, withTmpdirInstance } from "../fixture/fixture"
  8. type Body<A, E, R> = Effect.Effect<A, E, R> | (() => Effect.Effect<A, E, R>)
  9. type InstanceOptions = { git?: boolean; config?: Partial<Config.Info> }
  10. function isInstanceOptions(options: InstanceOptions | number | TestOptions | undefined): options is InstanceOptions {
  11. return !!options && typeof options === "object" && ("git" in options || "config" in options)
  12. }
  13. function instanceArgs(
  14. options?: InstanceOptions | number | TestOptions,
  15. testOptions?: number | TestOptions,
  16. ): { instanceOptions: InstanceOptions | undefined; testOptions: number | TestOptions | undefined } {
  17. if (typeof options === "number") return { instanceOptions: undefined, testOptions: options }
  18. if (isInstanceOptions(options)) return { instanceOptions: options, testOptions }
  19. return { instanceOptions: undefined, testOptions: options }
  20. }
  21. const body = <A, E, R>(value: Body<A, E, R>) => Effect.suspend(() => (typeof value === "function" ? value() : value))
  22. const run = <A, E, R, E2>(value: Body<A, E, R | Scope.Scope>, layer: Layer.Layer<R, E2>) =>
  23. Effect.gen(function* () {
  24. const exit = yield* body(value).pipe(Effect.scoped, Effect.provide(layer), Effect.exit)
  25. if (Exit.isFailure(exit)) {
  26. for (const err of Cause.prettyErrors(exit.cause)) {
  27. yield* Effect.logError(err)
  28. }
  29. }
  30. return yield* exit
  31. }).pipe(Effect.runPromise)
  32. const make = <R, E>(testLayer: Layer.Layer<R, E>, liveLayer: Layer.Layer<R, E>) => {
  33. const effect = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
  34. test(name, () => run(value, testLayer), opts)
  35. effect.only = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
  36. test.only(name, () => run(value, testLayer), opts)
  37. effect.skip = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
  38. test.skip(name, () => run(value, testLayer), opts)
  39. const live = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
  40. test(name, () => run(value, liveLayer), opts)
  41. live.only = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
  42. test.only(name, () => run(value, liveLayer), opts)
  43. live.skip = <A, E2>(name: string, value: Body<A, E2, R | Scope.Scope>, opts?: number | TestOptions) =>
  44. test.skip(name, () => run(value, liveLayer), opts)
  45. const instance = <A, E2>(
  46. name: string,
  47. value: Body<A, E2, R | TestInstance | Scope.Scope>,
  48. options?: InstanceOptions | number | TestOptions,
  49. opts?: number | TestOptions,
  50. ) => {
  51. const args = instanceArgs(options, opts)
  52. return test(name, () => run(body(value).pipe(withTmpdirInstance(args.instanceOptions)), liveLayer), args.testOptions)
  53. }
  54. instance.only = <A, E2>(
  55. name: string,
  56. value: Body<A, E2, R | TestInstance | Scope.Scope>,
  57. options?: InstanceOptions | number | TestOptions,
  58. opts?: number | TestOptions,
  59. ) => {
  60. const args = instanceArgs(options, opts)
  61. return test.only(name, () => run(body(value).pipe(withTmpdirInstance(args.instanceOptions)), liveLayer), args.testOptions)
  62. }
  63. instance.skip = <A, E2>(
  64. name: string,
  65. value: Body<A, E2, R | TestInstance | Scope.Scope>,
  66. options?: InstanceOptions | number | TestOptions,
  67. opts?: number | TestOptions,
  68. ) => {
  69. const args = instanceArgs(options, opts)
  70. return test.skip(name, () => run(body(value).pipe(withTmpdirInstance(args.instanceOptions)), liveLayer), args.testOptions)
  71. }
  72. return { effect, live, instance }
  73. }
  74. // Test environment with TestClock and TestConsole
  75. const testEnv = Layer.mergeAll(TestConsole.layer, TestClock.layer())
  76. // Live environment - uses real clock, but keeps TestConsole for output capture
  77. const liveEnv = TestConsole.layer
  78. export const it = make(testEnv, liveEnv)
  79. export const testEffect = <R, E>(layer: Layer.Layer<R, E>) =>
  80. make(Layer.provideMerge(layer, testEnv), Layer.provideMerge(layer, liveEnv))