Skip to content

Commit 0b3a1c2

Browse files
authored
test(opencode): simplify config effect tests (#29019)
1 parent 1ccd14b commit 0b3a1c2

25 files changed

Lines changed: 464 additions & 701 deletions

File tree

packages/opencode/src/effect/service-use.ts renamed to packages/core/src/effect/service-use.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,26 @@ type ServiceUse<Identifier, Shape> = {
1515
}
1616

1717
export const serviceUse = <Identifier, Shape>(tag: Context.Service<Identifier, Shape>) => {
18+
const cache = new Map<string, (...args: unknown[]) => Effect.Effect<unknown, unknown, unknown>>()
1819
// This is the only dynamic boundary: TypeScript knows the accessor shape,
1920
// but Proxy property names are runtime values.
2021
const access = new Proxy(
2122
{},
2223
{
2324
get: (_, key) => {
2425
if (typeof key !== "string") return undefined
25-
return (...args: unknown[]) =>
26+
const cached = cache.get(key)
27+
if (cached) return cached
28+
const accessor = (...args: unknown[]) =>
2629
tag.use((service) => {
2730
// oxlint-disable-next-line typescript-eslint/no-unsafe-type-assertion -- Proxy keys are checked at runtime.
2831
const method = service[key as keyof Shape]
2932
if (typeof method !== "function") return Effect.die(new Error(`Service method not found: ${key}`))
3033
// oxlint-disable-next-line typescript-eslint/no-unsafe-type-assertion -- ServiceUse exposes only Effect-returning methods.
3134
return (method as (...args: unknown[]) => Effect.Effect<unknown, unknown, unknown>)(...args)
3235
})
36+
cache.set(key, accessor)
37+
return accessor
3338
},
3439
},
3540
)

packages/core/src/filesystem.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@ import { dirname, join, relative, resolve as pathResolve } from "path"
33
import { realpathSync } from "fs"
44
import * as NFS from "fs/promises"
55
import { lookup } from "mime-types"
6-
import { Effect, FileSystem, Layer, Schema, Context } from "effect"
6+
import { Context, Effect, FileSystem, Layer, Schema } from "effect"
77
import type { PlatformError } from "effect/PlatformError"
88
import { Glob } from "./util/glob"
9+
import { serviceUse } from "./effect/service-use"
910

1011
export namespace AppFileSystem {
1112
export class FileSystemError extends Schema.TaggedErrorClass<FileSystemError>()("FileSystemError", {
@@ -39,6 +40,8 @@ export namespace AppFileSystem {
3940

4041
export class Service extends Context.Service<Service, Interface>()("@opencode/FileSystem") {}
4142

43+
export const use = serviceUse(Service)
44+
4245
export const layer = Layer.effect(
4346
Service,
4447
Effect.gen(function* () {

packages/opencode/src/account/account.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Cache, Clock, Duration, Effect, Layer, Option, Schema, SchemaGetter, Context } from "effect"
2-
import { serviceUse } from "@/effect/service-use"
2+
import { serviceUse } from "@opencode-ai/core/effect/service-use"
33
import {
44
FetchHttpClient,
55
HttpClient,

packages/opencode/src/account/repo.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { eq } from "drizzle-orm"
2-
import { serviceUse } from "@/effect/service-use"
2+
import { serviceUse } from "@opencode-ai/core/effect/service-use"
33
import { Effect, Layer, Option, Schema, Context } from "effect"
44

55
import { Database } from "@/storage/db"

packages/opencode/src/agent/agent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Config } from "@/config/config"
2-
import { serviceUse } from "@/effect/service-use"
2+
import { serviceUse } from "@opencode-ai/core/effect/service-use"
33
import { Provider } from "@/provider/provider"
44
import { ModelID, ProviderID } from "../provider/schema"
55
import { generateObject, streamObject, type ModelMessage } from "ai"

packages/opencode/src/bus/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { BusEvent } from "./bus-event"
55
import { GlobalBus } from "./global"
66
import { InstanceState } from "@/effect/instance-state"
77
import { makeRuntime } from "@/effect/run-service"
8-
import { serviceUse } from "@/effect/service-use"
8+
import { serviceUse } from "@opencode-ai/core/effect/service-use"
99
import { Identifier } from "@/id/id"
1010
import type { InstanceContext } from "@/project/instance-context"
1111
import { InstanceRef } from "@/effect/instance-ref"

packages/opencode/src/config/config.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import * as Log from "@opencode-ai/core/util/log"
2-
import { serviceUse } from "@/effect/service-use"
2+
import { serviceUse } from "@opencode-ai/core/effect/service-use"
33
import path from "path"
44
import { pathToFileURL } from "url"
55
import os from "os"

packages/opencode/src/control-plane/workspace.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Context, Effect, FiberMap, Iterable, Layer, Schema, Stream } from "effect"
2-
import { serviceUse } from "@/effect/service-use"
2+
import { serviceUse } from "@opencode-ai/core/effect/service-use"
33
import { FetchHttpClient, HttpBody, HttpClient, HttpClientError, HttpClientRequest } from "effect/unstable/http"
44
import { Database } from "@/storage/db"
55
import { asc } from "drizzle-orm"

packages/opencode/src/env/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Context, Effect, Layer } from "effect"
2-
import { serviceUse } from "@/effect/service-use"
2+
import { serviceUse } from "@opencode-ai/core/effect/service-use"
33
import { InstanceState } from "@/effect/instance-state"
44

55
type State = Record<string, string | undefined>

packages/opencode/src/file/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BusEvent } from "@/bus/bus-event"
2-
import { serviceUse } from "@/effect/service-use"
2+
import { serviceUse } from "@opencode-ai/core/effect/service-use"
33
import { InstanceState } from "@/effect/instance-state"
44

55
import { AppFileSystem } from "@opencode-ai/core/filesystem"

0 commit comments

Comments
 (0)