location-layer.test.ts 5.1 KB

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