layer-node.test.ts 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. import { describe, expect, test } from "bun:test"
  2. import { Context, Effect, Layer } from "effect"
  3. import { LayerNode } from "@opencode-ai/core/effect/layer-node"
  4. class Value extends Context.Service<Value, { readonly value: string }>()("test/LayerNodeValue") {}
  5. class Greeting extends Context.Service<Greeting, { readonly value: string }>()("test/LayerNodeGreeting") {}
  6. class Left extends Context.Service<Left, { readonly value: string }>()("test/LayerNodeLeft") {}
  7. class Right extends Context.Service<Right, { readonly value: string }>()("test/LayerNodeRight") {}
  8. class Database extends Context.Service<Database, { readonly name: string }>()("test/GraphDatabase") {}
  9. class Users extends Context.Service<Users, { readonly list: Effect.Effect<string[]> }>()("test/GraphUsers") {}
  10. class App extends Context.Service<App, { readonly run: Effect.Effect<string[]> }>()("test/GraphApp") {}
  11. const tags = LayerNode.tags({ app: [] })
  12. const make = tags.make("app")
  13. const build = <A, E>(root: LayerNode.Node<A, E, any>, replacements?: readonly LayerNode.Replacement[]) =>
  14. LayerNode.compile(root, new Map(replacements?.map((item) => [item.source, item.replacement]))) as Layer.Layer<A, E>
  15. const valueLayer = Layer.succeed(Value, Value.of({ value: "production" }))
  16. const greetingLayer = Layer.effect(
  17. Greeting,
  18. Effect.map(Value, (value) => Greeting.of({ value: `hello ${value.value}` })),
  19. )
  20. const value = make({ service: Value, layer: valueLayer, deps: [] })
  21. const greeting = make({ service: Greeting, layer: greetingLayer, deps: [value] })
  22. describe("layer node", () => {
  23. test("builds an untagged graph", async () => {
  24. const value = LayerNode.make({ service: Value, layer: valueLayer, deps: [] })
  25. const greeting = LayerNode.make({ service: Greeting, layer: greetingLayer, deps: [value] })
  26. const program = Effect.map(Greeting, (item) => item.value).pipe(
  27. Effect.provide(LayerNode.compile(LayerNode.group([greeting]))),
  28. )
  29. expect(await Effect.runPromise(program)).toBe("hello production")
  30. })
  31. test("builds a dependency graph", async () => {
  32. const program = Effect.map(Greeting, (item) => item.value).pipe(Effect.provide(build(LayerNode.group([greeting]))))
  33. expect(await Effect.runPromise(program)).toBe("hello production")
  34. })
  35. test("exposes roots but hides transitive dependencies", () => {
  36. const layer = build(LayerNode.group([greeting]))
  37. const check: Layer.Layer<Greeting> = layer
  38. void check
  39. })
  40. test("preserves branch-specific implementations across roots", async () => {
  41. const firstValue = make({ service: Value, layer: Layer.succeed(Value, Value.of({ value: "first" })), deps: [] })
  42. const secondValue = make({ service: Value, layer: Layer.succeed(Value, Value.of({ value: "second" })), deps: [] })
  43. const leftLayer = Layer.effect(
  44. Left,
  45. Effect.map(Value, (item) => Left.of({ value: item.value })),
  46. )
  47. const rightLayer = Layer.effect(
  48. Right,
  49. Effect.map(Value, (item) => Right.of({ value: item.value })),
  50. )
  51. const left = make({ service: Left, layer: leftLayer, deps: [firstValue] })
  52. const right = make({ service: Right, layer: rightLayer, deps: [secondValue] })
  53. const layer = build(LayerNode.group([left, right]))
  54. const program = Effect.gen(function* () {
  55. return [(yield* Left).value, (yield* Right).value]
  56. }).pipe(Effect.provide(layer))
  57. expect(await Effect.runPromise(program)).toEqual(["first", "second"])
  58. })
  59. test("requires unbound nodes to be bound before compilation", async () => {
  60. const unbound = LayerNode.unbound(Value, tags.values.app)
  61. const greeting = make({ service: Greeting, layer: greetingLayer, deps: [unbound] })
  62. const tree = LayerNode.group([greeting])
  63. expect(() => LayerNode.compile(tree)).toThrow("Unbound layer node: test/LayerNodeValue")
  64. const bound = LayerNode.bind(tree, unbound, value)
  65. const layer = LayerNode.compile(bound) as Layer.Layer<Greeting>
  66. const program = Effect.map(Greeting, (item) => item.value).pipe(Effect.provide(layer))
  67. expect(await Effect.runPromise(program)).toBe("hello production")
  68. })
  69. test("replaces a layer by identity", async () => {
  70. const replacement = Layer.succeed(Value, Value.of({ value: "simulation" }))
  71. const program = Effect.map(Greeting, (item) => item.value).pipe(
  72. Effect.provide(build(LayerNode.group([greeting]), [LayerNode.replace(valueLayer, replacement)])),
  73. )
  74. expect(await Effect.runPromise(program)).toBe("hello simulation")
  75. })
  76. test("replaces every use of the same layer", async () => {
  77. const leftLayer = Layer.effect(
  78. Left,
  79. Effect.map(Value, (item) => Left.of({ value: item.value })),
  80. )
  81. const rightLayer = Layer.effect(
  82. Right,
  83. Effect.map(Value, (item) => Right.of({ value: item.value })),
  84. )
  85. const left = make({ service: Left, layer: leftLayer, deps: [value] })
  86. const right = make({ service: Right, layer: rightLayer, deps: [value] })
  87. const replacement = Layer.succeed(Value, Value.of({ value: "replaced" }))
  88. const layer = build(LayerNode.group([left, right]), [LayerNode.replace(valueLayer, replacement)])
  89. const program = Effect.gen(function* () {
  90. return [(yield* Left).value, (yield* Right).value]
  91. }).pipe(Effect.provide(layer))
  92. expect(await Effect.runPromise(program)).toEqual(["replaced", "replaced"])
  93. })
  94. test("does not acquire an unused replacement", async () => {
  95. let acquisitions = 0
  96. const other = Layer.succeed(Value, Value.of({ value: "other" }))
  97. const replacement = Layer.effect(
  98. Value,
  99. Effect.sync(() => {
  100. acquisitions++
  101. return Value.of({ value: "replacement" })
  102. }),
  103. )
  104. await Effect.runPromise(
  105. Effect.map(Greeting, (item) => item.value).pipe(
  106. Effect.provide(build(LayerNode.group([greeting]), [LayerNode.replace(other, replacement)])),
  107. ),
  108. )
  109. expect(acquisitions).toBe(0)
  110. })
  111. test("hoists and compiles tagged graphs", async () => {
  112. const tags = LayerNode.tags({ location: ["global"], global: [] })
  113. const global = tags.make("global")
  114. const location = tags.make("location")
  115. const database = global({
  116. service: Database,
  117. layer: Layer.succeed(Database, Database.of({ name: "Alice" })),
  118. deps: [],
  119. })
  120. const users = location({
  121. service: Users,
  122. layer: Layer.effect(
  123. Users,
  124. Effect.gen(function* () {
  125. const db = yield* Database
  126. return Users.of({ list: Effect.succeed([db.name]) })
  127. }),
  128. ),
  129. deps: [database],
  130. })
  131. const app = location({
  132. service: App,
  133. layer: Layer.effect(
  134. App,
  135. Effect.gen(function* () {
  136. const service = yield* Users
  137. return App.of({ run: service.list })
  138. }),
  139. ),
  140. deps: [users],
  141. })
  142. const result = LayerNode.hoist(LayerNode.group([app]), tags.values.global)
  143. expect(result.node.dependencies[0]?.dependencies[0]?.dependencies[0]).toMatchObject({
  144. kind: "group",
  145. dependencies: [],
  146. })
  147. expect(result.hoisted.dependencies).toEqual([database])
  148. const layer = LayerNode.compile(result.node).pipe(
  149. Layer.provide(LayerNode.compile(result.hoisted)),
  150. ) as unknown as Layer.Layer<App>
  151. const program = Effect.gen(function* () {
  152. return yield* (yield* App).run
  153. }).pipe(Effect.provide(layer))
  154. expect(await Effect.runPromise(program)).toEqual(["Alice"])
  155. })
  156. test("rejects conflicting hoisted implementations", () => {
  157. const tags = LayerNode.tags({ location: ["global"], global: [] })
  158. const global = tags.make("global")
  159. const location = tags.make("location")
  160. const first = global({
  161. service: Database,
  162. layer: Layer.succeed(Database, Database.of({ name: "first" })),
  163. deps: [],
  164. })
  165. const second = global({
  166. service: Database,
  167. layer: Layer.succeed(Database, Database.of({ name: "second" })),
  168. deps: [],
  169. })
  170. const left = location({
  171. service: Users,
  172. layer: Layer.effect(Users, Effect.as(Database, Users.of({ list: Effect.succeed([]) }))),
  173. deps: [first],
  174. })
  175. const right = location({
  176. service: App,
  177. layer: Layer.effect(App, Effect.as(Database, App.of({ run: Effect.succeed([]) }))),
  178. deps: [second],
  179. })
  180. expect(() => LayerNode.hoist(LayerNode.group([left, right]), tags.values.global)).toThrow(
  181. "Tag global has conflicting implementations for test/GraphDatabase",
  182. )
  183. })
  184. test("treats dependency groups as transparent while hoisting", () => {
  185. const tags = LayerNode.tags({ location: ["global"], global: [] })
  186. const global = tags.make("global")
  187. const location = tags.make("location")
  188. const database = global({
  189. service: Database,
  190. layer: Layer.succeed(Database, Database.of({ name: "Alice" })),
  191. deps: [],
  192. })
  193. const users = location({
  194. service: Users,
  195. layer: Layer.effect(Users, Effect.as(Database, Users.of({ list: Effect.succeed([]) }))),
  196. deps: [LayerNode.group([database])],
  197. })
  198. const result = LayerNode.hoist(LayerNode.group([users]), tags.values.global)
  199. expect(result.node.dependencies[0]?.dependencies[0]?.dependencies[0]).toMatchObject({
  200. kind: "group",
  201. dependencies: [],
  202. })
  203. })
  204. })