location-layer.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. import fs from "fs/promises"
  2. import path from "path"
  3. import { describe, expect } from "bun:test"
  4. import { Effect, Equal, Hash, Layer, Schema } from "effect"
  5. import { Tool } from "@opencode-ai/core/public"
  6. import { Catalog } from "@opencode-ai/core/catalog"
  7. import { LocationServiceMap } from "@opencode-ai/core/location-layer"
  8. import { Location } from "@opencode-ai/core/location"
  9. import { PluginBoot } from "@opencode-ai/core/plugin/boot"
  10. import { ProviderV2 } from "@opencode-ai/core/provider"
  11. import { AbsolutePath } from "@opencode-ai/core/schema"
  12. import { tmpdir } from "./fixture/tmpdir"
  13. import { testEffect } from "./lib/effect"
  14. import { toolDefinitions } from "./lib/tool"
  15. import { FSUtil } from "../src/fs-util"
  16. import { Auth } from "../src/auth"
  17. import { EventV2 } from "../src/event"
  18. import { Global } from "../src/global"
  19. import { ModelsDev } from "../src/models-dev"
  20. import { Npm } from "../src/npm"
  21. import { Project } from "../src/project"
  22. import { Reference } from "../src/reference"
  23. import { ToolRegistry } from "../src/tool/registry"
  24. import { ApplicationTools } from "../src/tool/application-tools"
  25. const applicationTools = ApplicationTools.layer
  26. const it = testEffect(
  27. Layer.merge(
  28. applicationTools,
  29. LocationServiceMap.layer.pipe(
  30. Layer.provide(applicationTools),
  31. Layer.provide(
  32. Layer.mergeAll(
  33. Project.defaultLayer,
  34. EventV2.defaultLayer,
  35. Auth.defaultLayer,
  36. Npm.defaultLayer,
  37. ModelsDev.defaultLayer,
  38. FSUtil.defaultLayer,
  39. Global.defaultLayer,
  40. ),
  41. ),
  42. ),
  43. ),
  44. )
  45. describe("LocationServiceMap", () => {
  46. it.effect("compares equivalent location refs by value", () =>
  47. Effect.sync(() => {
  48. const directory = AbsolutePath.make("/project")
  49. expect(Equal.equals(Location.Ref.make({ directory }), Location.Ref.make({ directory }))).toBe(true)
  50. expect(Hash.hash(Location.Ref.make({ directory }))).toBe(
  51. Hash.hash(Location.Ref.make({ directory, workspaceID: undefined })),
  52. )
  53. }),
  54. )
  55. it.live("isolates location state while sharing location policy with catalog", () =>
  56. Effect.acquireRelease(
  57. Effect.promise(() => Promise.all([tmpdir(), tmpdir()])),
  58. (dirs) => Effect.promise(() => Promise.all(dirs.map((dir) => dir[Symbol.asyncDispose]())).then(() => undefined)),
  59. ).pipe(
  60. Effect.flatMap(([blocked, allowed]) =>
  61. Effect.gen(function* () {
  62. yield* (yield* ApplicationTools.Service).register({
  63. application_context: Tool.make({
  64. description: "Read application context",
  65. input: Schema.Struct({}),
  66. output: Schema.Struct({ ok: Schema.Boolean }),
  67. execute: () => Effect.succeed({ ok: true }),
  68. }),
  69. })
  70. yield* Effect.promise(() =>
  71. fs.writeFile(
  72. path.join(blocked.path, "opencode.json"),
  73. JSON.stringify({
  74. experimental: { policies: [{ effect: "deny", action: "provider.use", resource: "test" }] },
  75. }),
  76. ),
  77. )
  78. const update = (directory: string) =>
  79. Effect.gen(function* () {
  80. yield* PluginBoot.Service.use((boot) => boot.wait())
  81. yield* Reference.Service
  82. const catalog = yield* Catalog.Service
  83. const transform = yield* catalog.transform()
  84. yield* transform((editor) => editor.provider.update(ProviderV2.ID.make("test"), () => {}))
  85. return {
  86. providers: yield* catalog.provider.all(),
  87. tools: yield* toolDefinitions(yield* ToolRegistry.Service),
  88. }
  89. }).pipe(
  90. Effect.scoped,
  91. Effect.provide(LocationServiceMap.get(Location.Ref.make({ directory: AbsolutePath.make(directory) }))),
  92. )
  93. const blockedState = yield* update(blocked.path)
  94. expect(blockedState.providers.some((provider) => provider.id === ProviderV2.ID.make("test"))).toBe(false)
  95. expect(blockedState.tools.map((tool) => tool.name).sort()).toEqual([
  96. "application_context",
  97. "apply_patch",
  98. "bash",
  99. "edit",
  100. "glob",
  101. "grep",
  102. "question",
  103. "read",
  104. "skill",
  105. "todowrite",
  106. "webfetch",
  107. "websearch",
  108. "write",
  109. ])
  110. const allowedState = yield* update(allowed.path)
  111. expect(allowedState.providers.some((provider) => provider.id === ProviderV2.ID.make("test"))).toBe(true)
  112. expect(allowedState.tools.map((tool) => tool.name).sort()).toEqual([
  113. "application_context",
  114. "apply_patch",
  115. "bash",
  116. "edit",
  117. "glob",
  118. "grep",
  119. "question",
  120. "read",
  121. "skill",
  122. "todowrite",
  123. "webfetch",
  124. "websearch",
  125. "write",
  126. ])
  127. }),
  128. ),
  129. ),
  130. )
  131. })