location-layer.test.ts 5.1 KB

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