location-layer.test.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 { AppFileSystem } from "../src/filesystem"
  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. const it = testEffect(
  20. LocationServiceMap.layer.pipe(
  21. Layer.provide(
  22. Layer.mergeAll(
  23. Project.defaultLayer,
  24. EventV2.defaultLayer,
  25. Auth.defaultLayer,
  26. Npm.defaultLayer,
  27. ModelsDev.defaultLayer,
  28. AppFileSystem.defaultLayer,
  29. Global.defaultLayer,
  30. ),
  31. ),
  32. ),
  33. )
  34. describe("LocationServiceMap", () => {
  35. it.live("isolates location state while sharing location policy with catalog", () =>
  36. Effect.acquireRelease(
  37. Effect.promise(() => Promise.all([tmpdir(), tmpdir()])),
  38. (dirs) => Effect.promise(() => Promise.all(dirs.map((dir) => dir[Symbol.asyncDispose]())).then(() => undefined)),
  39. ).pipe(
  40. Effect.flatMap(([blocked, allowed]) =>
  41. Effect.gen(function* () {
  42. yield* Effect.promise(() =>
  43. fs.writeFile(
  44. path.join(blocked.path, "opencode.json"),
  45. JSON.stringify({
  46. experimental: { policies: [{ effect: "deny", action: "provider.use", resource: "test" }] },
  47. }),
  48. ),
  49. )
  50. const update = (directory: string) =>
  51. Effect.gen(function* () {
  52. yield* PluginBoot.Service.use((boot) => boot.wait())
  53. const catalog = yield* Catalog.Service
  54. const transform = yield* catalog.transform()
  55. yield* transform((editor) => editor.provider.update(ProviderV2.ID.make("test"), () => {}))
  56. return yield* catalog.provider.all()
  57. }).pipe(Effect.scoped, Effect.provide(LocationServiceMap.get({ directory: AbsolutePath.make(directory) })))
  58. expect((yield* update(blocked.path)).some((provider) => provider.id === ProviderV2.ID.make("test"))).toBe(
  59. false,
  60. )
  61. expect((yield* update(allowed.path)).some((provider) => provider.id === ProviderV2.ID.make("test"))).toBe(
  62. true,
  63. )
  64. }),
  65. ),
  66. ),
  67. )
  68. })