location-layer.test.ts 4.5 KB

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