node-build.test.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. import { describe, expect, test } from "bun:test"
  2. import { Context, Effect, Layer, LayerMap, Option } from "effect"
  3. import { LayerNode } from "@opencode-ai/core/effect/layer-node"
  4. import { Node } from "@opencode-ai/core/effect/app-node"
  5. import { NodeBuild } from "@opencode-ai/core/effect/app-node-builder"
  6. import { Location } from "@opencode-ai/core/location"
  7. import { LocationServiceMap } from "@opencode-ai/core/location-service-map"
  8. import type { LocationError, LocationServices } from "@opencode-ai/core/location-services"
  9. import { Project } from "@opencode-ai/core/project"
  10. import { AbsolutePath } from "@opencode-ai/core/schema"
  11. import { tmpdir } from "../../fixture/tmpdir"
  12. class Value extends Context.Service<Value, { readonly value: string }>()("test/TagValue") {}
  13. class Result extends Context.Service<Result, { readonly value: string }>()("test/TagResult") {}
  14. class CycleA extends Context.Service<CycleA, {}>()("test/NodeBuildA") {}
  15. class CycleB extends Context.Service<CycleB, { readonly directory: AbsolutePath }>()("test/NodeBuildB") {}
  16. describe("node build", () => {
  17. test("does not build a location service map when the graph does not require it", async () => {
  18. const result = Node.makeGlobalNode({
  19. service: Result,
  20. layer: Layer.succeed(Result, Result.of({ value: "plain" })),
  21. deps: [],
  22. })
  23. const layer = NodeBuild.build(LayerNode.group([result]))
  24. const program = Effect.gen(function* () {
  25. expect(Option.isNone(yield* Effect.serviceOption(LocationServiceMap.Service))).toBe(true)
  26. return (yield* Result).value
  27. }).pipe(Effect.provide(layer))
  28. expect(await Effect.runPromise(program)).toBe("plain")
  29. })
  30. test("detects cycles through a bound location service map", () => {
  31. const a = Node.makeGlobalNode({
  32. service: CycleA,
  33. layer: Layer.effect(CycleA, Effect.as(LocationServiceMap.Service, CycleA.of({}))),
  34. deps: [LocationServiceMap.node],
  35. })
  36. const b = Node.makeGlobalNode({
  37. service: CycleB,
  38. layer: Layer.effect(
  39. CycleB,
  40. Effect.map(CycleA, () => CycleB.of({ directory: AbsolutePath.make(process.cwd()) })),
  41. ),
  42. deps: [a],
  43. })
  44. const mapEffect = Effect.gen(function* () {
  45. const service = yield* CycleB
  46. return yield* LayerMap.make(
  47. (ref: Location.Ref) =>
  48. Layer.succeed(
  49. Location.Service,
  50. Location.Service.of({
  51. directory: ref.directory,
  52. workspaceID: ref.workspaceID,
  53. project: { id: Project.ID.global, directory: service.directory },
  54. }),
  55. ),
  56. { idleTimeToLive: "1 minute" },
  57. )
  58. }) as unknown as Effect.Effect<LayerMap.LayerMap<Location.Ref, LocationServices, LocationError>, never, CycleB>
  59. const mapLayer = Layer.effect(LocationServiceMap.Service, mapEffect)
  60. const map = Node.makeGlobalNode({ service: LocationServiceMap.Service, layer: mapLayer, deps: [b] })
  61. const graph = LayerNode.bind(LayerNode.group([a]), LocationServiceMap.node, map)
  62. expect(() => NodeBuild.build(graph)).toThrow("Cycle detected in layer tree")
  63. })
  64. test("shares top-level project with location services", async () => {
  65. await using tmp = await tmpdir()
  66. let acquisitions = 0
  67. const projectLayer = Layer.effect(
  68. Project.Service,
  69. Effect.sync(() => {
  70. acquisitions++
  71. return Project.Service.of({
  72. directories: () => Effect.succeed([]),
  73. resolve: (directory) => Effect.succeed({ id: Project.ID.global, directory }),
  74. commit: () => Effect.void,
  75. })
  76. }),
  77. )
  78. const layer = NodeBuild.build(LayerNode.group([Project.node, LocationServiceMap.node]), [
  79. LayerNode.replace(Project.layer, projectLayer),
  80. ])
  81. const ref = Location.Ref.make({ directory: AbsolutePath.make(tmp.path) })
  82. const program = Effect.gen(function* () {
  83. yield* Project.Service
  84. const locations = yield* LocationServiceMap.Service
  85. expect(Option.isSome(yield* Effect.serviceOption(LocationServiceMap.Service))).toBe(true)
  86. return yield* Location.Service.pipe(Effect.provide(locations.get(ref)))
  87. }).pipe(Effect.provide(layer))
  88. expect((await Effect.runPromise(program)).directory).toBe(ref.directory)
  89. expect(acquisitions).toBe(1)
  90. })
  91. test("returns a composed application layer", async () => {
  92. const value = Node.makeGlobalNode({
  93. service: Value,
  94. layer: Layer.succeed(Value, Value.of({ value: "value" })),
  95. deps: [],
  96. })
  97. const result = Node.makeGlobalNode({
  98. service: Result,
  99. layer: Layer.effect(
  100. Result,
  101. Effect.gen(function* () {
  102. return Result.of({ value: (yield* Value).value })
  103. }),
  104. ),
  105. deps: [value],
  106. })
  107. const serviceLayer = NodeBuild.build(LayerNode.group([result]))
  108. const program = Effect.gen(function* () {
  109. expect(Option.isNone(yield* Effect.serviceOption(LocationServiceMap.Service))).toBe(true)
  110. return (yield* Result).value
  111. }).pipe(Effect.provide(serviceLayer))
  112. expect(await Effect.runPromise(program)).toBe("value")
  113. })
  114. })