location-layer.test.ts 4.4 KB

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