location-layer.test.ts 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import fs from "fs/promises"
  2. import path from "path"
  3. import { describe, expect } from "bun:test"
  4. import { Effect } 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. const it = testEffect(LocationServiceMap.layer)
  13. describe("LocationServiceMap", () => {
  14. it.live("isolates location state while sharing location policy with catalog", () =>
  15. Effect.acquireRelease(
  16. Effect.promise(() => Promise.all([tmpdir(), tmpdir()])),
  17. (dirs) => Effect.promise(() => Promise.all(dirs.map((dir) => dir[Symbol.asyncDispose]())).then(() => undefined)),
  18. ).pipe(
  19. Effect.flatMap(([blocked, allowed]) =>
  20. Effect.gen(function* () {
  21. yield* Effect.promise(() =>
  22. fs.writeFile(
  23. path.join(blocked.path, "opencode.json"),
  24. JSON.stringify({
  25. experimental: { policies: [{ effect: "deny", action: "provider.use", resource: "test" }] },
  26. }),
  27. ),
  28. )
  29. const update = (directory: string) =>
  30. Effect.gen(function* () {
  31. yield* PluginBoot.Service.use((boot) => boot.wait())
  32. const catalog = yield* Catalog.Service
  33. const transform = yield* catalog.transform()
  34. yield* transform((editor) => editor.provider.update(ProviderV2.ID.make("test"), () => {}))
  35. return yield* catalog.provider.all()
  36. }).pipe(Effect.scoped, Effect.provide(LocationServiceMap.get({ directory: AbsolutePath.make(directory) })))
  37. expect((yield* update(blocked.path)).some((provider) => provider.id === ProviderV2.ID.make("test"))).toBe(
  38. false,
  39. )
  40. expect((yield* update(allowed.path)).some((provider) => provider.id === ProviderV2.ID.make("test"))).toBe(
  41. true,
  42. )
  43. }),
  44. ),
  45. ),
  46. )
  47. })