location-layer.test.ts 3.8 KB

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