reference.test.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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 path = AbsolutePath.make("/docs")
  19. const source = new Reference.LocalSource({
  20. type: "local",
  21. path,
  22. description: "Use for API documentation",
  23. hidden: true,
  24. })
  25. yield* references.transform((editor) => editor.add("docs", source)).pipe(Scope.provide(scope))
  26. expect(yield* references.list()).toEqual([
  27. new Reference.Info({ name: "docs", path, description: "Use for API documentation", hidden: true, source }),
  28. ])
  29. yield* Scope.close(scope, Exit.void)
  30. expect(yield* references.list()).toEqual([])
  31. }).pipe(
  32. Effect.provide(Reference.layer),
  33. Effect.provide(cache),
  34. Effect.provide(EventV2.defaultLayer),
  35. Effect.provide(Global.defaultLayer),
  36. ),
  37. )
  38. it.effect("derives Git paths without exposing cache operations", () =>
  39. Effect.gen(function* () {
  40. const references = yield* Reference.Service
  41. const repository = Repository.parseRemote("owner/repo")
  42. const source = new Reference.GitSource({ type: "git", repository: "owner/repo", branch: "main" })
  43. yield* references.transform((editor) => editor.add("sdk", source))
  44. expect(yield* references.list()).toEqual([
  45. new Reference.Info({
  46. name: "sdk",
  47. path: AbsolutePath.make(Repository.cachePath(Global.Path.repos, repository)),
  48. source,
  49. }),
  50. ])
  51. }).pipe(
  52. Effect.scoped,
  53. Effect.provide(Reference.layer),
  54. Effect.provide(cache),
  55. Effect.provide(EventV2.defaultLayer),
  56. Effect.provide(Global.defaultLayer),
  57. ),
  58. )
  59. it.effect("preserves configured Git descriptions", () =>
  60. Effect.gen(function* () {
  61. const references = yield* Reference.Service
  62. const repository = Repository.parseRemote("owner/repo")
  63. const source = new Reference.GitSource({
  64. type: "git",
  65. repository: "owner/repo",
  66. description: "Use for SDK implementation details",
  67. })
  68. yield* references.transform((editor) => editor.add("sdk", source))
  69. expect(yield* references.list()).toEqual([
  70. new Reference.Info({
  71. name: "sdk",
  72. path: AbsolutePath.make(Repository.cachePath(Global.Path.repos, repository)),
  73. description: "Use for SDK implementation details",
  74. source,
  75. }),
  76. ])
  77. }).pipe(
  78. Effect.scoped,
  79. Effect.provide(Reference.layer),
  80. Effect.provide(cache),
  81. Effect.provide(EventV2.defaultLayer),
  82. Effect.provide(Global.defaultLayer),
  83. ),
  84. )
  85. })