reference.test.ts 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import { describe, expect } from "bun:test"
  2. import { Effect, Exit, Layer, Scope } from "effect"
  3. import { AbsolutePath } from "@opencode-ai/core/schema"
  4. import { Global } from "@opencode-ai/core/global"
  5. import { Reference } from "@opencode-ai/core/reference"
  6. import { Repository } from "@opencode-ai/core/repository"
  7. import { RepositoryCache } from "@opencode-ai/core/repository-cache"
  8. import { EventV2 } from "@opencode-ai/core/event"
  9. import { it } from "./lib/effect"
  10. const cache = Layer.mock(RepositoryCache.Service, {
  11. ensure: () => Effect.die("unexpected Git materialization"),
  12. })
  13. describe("Reference", () => {
  14. it.effect("registers normalized sources for the owning scope", () =>
  15. Effect.gen(function* () {
  16. const references = yield* Reference.Service
  17. const scope = yield* Scope.make()
  18. const update = yield* references.transform().pipe(Effect.provideService(Scope.Scope, scope))
  19. const path = AbsolutePath.make("/docs")
  20. yield* update((editor) => editor.add("docs", new Reference.LocalSource({ type: "local", path })))
  21. expect(yield* references.list()).toEqual([
  22. new Reference.Info({ name: "docs", path, source: new Reference.LocalSource({ type: "local", path }) }),
  23. ])
  24. yield* Scope.close(scope, Exit.void)
  25. expect(yield* references.list()).toEqual([])
  26. }).pipe(
  27. Effect.provide(Reference.layer),
  28. Effect.provide(cache),
  29. Effect.provide(EventV2.defaultLayer),
  30. Effect.provide(Global.defaultLayer),
  31. ),
  32. )
  33. it.effect("derives Git paths without exposing cache operations", () =>
  34. Effect.gen(function* () {
  35. const references = yield* Reference.Service
  36. const update = yield* references.transform()
  37. const repository = Repository.parseRemote("owner/repo")
  38. const source = new Reference.GitSource({ type: "git", repository: "owner/repo", branch: "main" })
  39. yield* update((editor) => editor.add("sdk", source))
  40. expect(yield* references.list()).toEqual([
  41. new Reference.Info({
  42. name: "sdk",
  43. path: AbsolutePath.make(Repository.cachePath(Global.Path.repos, repository)),
  44. source,
  45. }),
  46. ])
  47. }).pipe(
  48. Effect.scoped,
  49. Effect.provide(Reference.layer),
  50. Effect.provide(cache),
  51. Effect.provide(EventV2.defaultLayer),
  52. Effect.provide(Global.defaultLayer),
  53. ),
  54. )
  55. })