| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- import fs from "fs/promises"
- import path from "path"
- import { describe, expect } from "bun:test"
- import { Effect, Layer, Schema } from "effect"
- import { Tool } from "@opencode-ai/core/public"
- import { Catalog } from "@opencode-ai/core/catalog"
- import { LocationServiceMap } from "@opencode-ai/core/location-layer"
- import { PluginBoot } from "@opencode-ai/core/plugin/boot"
- import { ProviderV2 } from "@opencode-ai/core/provider"
- import { AbsolutePath } from "@opencode-ai/core/schema"
- import { tmpdir } from "./fixture/tmpdir"
- import { testEffect } from "./lib/effect"
- import { FSUtil } from "../src/fs-util"
- import { Auth } from "../src/auth"
- import { EventV2 } from "../src/event"
- import { Global } from "../src/global"
- import { ModelsDev } from "../src/models-dev"
- import { Npm } from "../src/npm"
- import { Project } from "../src/project"
- import { ProjectReference } from "../src/project-reference"
- import { LocationSearch } from "../src/location-search"
- import { ToolRegistry } from "../src/tool/registry"
- import { ApplicationTools } from "../src/tool/application-tools"
- const applicationTools = ApplicationTools.layer
- const it = testEffect(
- Layer.merge(
- applicationTools,
- LocationServiceMap.layer.pipe(
- Layer.provide(
- Layer.mergeAll(
- Project.defaultLayer,
- EventV2.defaultLayer,
- Auth.defaultLayer,
- Npm.defaultLayer,
- ModelsDev.defaultLayer,
- FSUtil.defaultLayer,
- Global.defaultLayer,
- ),
- ),
- ),
- ),
- )
- describe("LocationServiceMap", () => {
- it.live("isolates location state while sharing location policy with catalog", () =>
- Effect.acquireRelease(
- Effect.promise(() => Promise.all([tmpdir(), tmpdir()])),
- (dirs) => Effect.promise(() => Promise.all(dirs.map((dir) => dir[Symbol.asyncDispose]())).then(() => undefined)),
- ).pipe(
- Effect.flatMap(([blocked, allowed]) =>
- Effect.gen(function* () {
- yield* (yield* ApplicationTools.Service).attach({
- application_context: Tool.make({
- description: "Read application context",
- parameters: Schema.Struct({}),
- success: Schema.Struct({ ok: Schema.Boolean }),
- execute: () => Effect.succeed({ ok: true }),
- }),
- })
- yield* Effect.promise(() =>
- fs.writeFile(
- path.join(blocked.path, "opencode.json"),
- JSON.stringify({
- experimental: { policies: [{ effect: "deny", action: "provider.use", resource: "test" }] },
- }),
- ),
- )
- const update = (directory: string) =>
- Effect.gen(function* () {
- yield* PluginBoot.Service.use((boot) => boot.wait())
- yield* ProjectReference.Service
- yield* LocationSearch.Service
- const catalog = yield* Catalog.Service
- const transform = yield* catalog.transform()
- yield* transform((editor) => editor.provider.update(ProviderV2.ID.make("test"), () => {}))
- return {
- providers: yield* catalog.provider.all(),
- tools: yield* (yield* ToolRegistry.Service).definitions(),
- }
- }).pipe(Effect.scoped, Effect.provide(LocationServiceMap.get({ directory: AbsolutePath.make(directory) })))
- const blockedState = yield* update(blocked.path)
- expect(blockedState.providers.some((provider) => provider.id === ProviderV2.ID.make("test"))).toBe(false)
- expect(blockedState.tools.map((tool) => tool.name).sort()).toEqual([
- "application_context",
- "apply_patch",
- "bash",
- "edit",
- "glob",
- "grep",
- "question",
- "read",
- "skill",
- "todowrite",
- "webfetch",
- "websearch",
- "write",
- ])
- const allowedState = yield* update(allowed.path)
- expect(allowedState.providers.some((provider) => provider.id === ProviderV2.ID.make("test"))).toBe(true)
- expect(allowedState.tools.map((tool) => tool.name).sort()).toEqual([
- "application_context",
- "apply_patch",
- "bash",
- "edit",
- "glob",
- "grep",
- "question",
- "read",
- "skill",
- "todowrite",
- "webfetch",
- "websearch",
- "write",
- ])
- }),
- ),
- ),
- )
- })
|