mirror of
https://github.com/anomalyco/opencode.git
synced 2026-06-01 19:05:38 +00:00
test(provider): migrate config-backed cases to Effect runner (#26969)
This commit is contained in:
@@ -15,6 +15,7 @@ import { Env } from "../../src/env"
|
|||||||
import { Effect } from "effect"
|
import { Effect } from "effect"
|
||||||
import { AppRuntime } from "../../src/effect/app-runtime"
|
import { AppRuntime } from "../../src/effect/app-runtime"
|
||||||
import { makeRuntime } from "../../src/effect/run-service"
|
import { makeRuntime } from "../../src/effect/run-service"
|
||||||
|
import { testEffect } from "../lib/effect"
|
||||||
|
|
||||||
const env = makeRuntime(Env.Service, Env.defaultLayer)
|
const env = makeRuntime(Env.Service, Env.defaultLayer)
|
||||||
const set = (k: string, v: string) => env.runSync((svc) => svc.set(k, v))
|
const set = (k: string, v: string) => env.runSync((svc) => svc.set(k, v))
|
||||||
@@ -70,6 +71,8 @@ function paid(providers: Awaited<ReturnType<typeof list>>) {
|
|||||||
return Object.values(item.models).filter((model) => model.cost.input > 0).length
|
return Object.values(item.models).filter((model) => model.cost.input > 0).length
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const it = testEffect(Provider.defaultLayer)
|
||||||
|
|
||||||
test("provider loaded from env variable", async () => {
|
test("provider loaded from env variable", async () => {
|
||||||
await using tmp = await tmpdir({
|
await using tmp = await tmpdir({
|
||||||
init: async (dir) => {
|
init: async (dir) => {
|
||||||
@@ -515,85 +518,69 @@ test("defaultModel respects config model setting", async () => {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
test("provider with baseURL from config", async () => {
|
it.instance(
|
||||||
await using tmp = await tmpdir({
|
"provider with baseURL from config",
|
||||||
init: async (dir) => {
|
Effect.gen(function* () {
|
||||||
await Bun.write(
|
const providers = yield* Provider.Service.use((provider) => provider.list())
|
||||||
path.join(dir, "opencode.json"),
|
expect(providers[ProviderID.make("custom-openai")]).toBeDefined()
|
||||||
JSON.stringify({
|
expect(providers[ProviderID.make("custom-openai")].options.baseURL).toBe("https://custom.openai.com/v1")
|
||||||
$schema: "https://opencode.ai/config.json",
|
}),
|
||||||
provider: {
|
{
|
||||||
"custom-openai": {
|
config: {
|
||||||
name: "Custom OpenAI",
|
provider: {
|
||||||
npm: "@ai-sdk/openai-compatible",
|
"custom-openai": {
|
||||||
env: [],
|
name: "Custom OpenAI",
|
||||||
models: {
|
npm: "@ai-sdk/openai-compatible",
|
||||||
"gpt-4": {
|
env: [],
|
||||||
name: "GPT-4",
|
models: {
|
||||||
tool_call: true,
|
"gpt-4": {
|
||||||
limit: { context: 128000, output: 4096 },
|
name: "GPT-4",
|
||||||
},
|
tool_call: true,
|
||||||
},
|
limit: { context: 128000, output: 4096 },
|
||||||
options: {
|
|
||||||
apiKey: "test-key",
|
|
||||||
baseURL: "https://custom.openai.com/v1",
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}),
|
options: {
|
||||||
)
|
apiKey: "test-key",
|
||||||
|
baseURL: "https://custom.openai.com/v1",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
})
|
},
|
||||||
await WithInstance.provide({
|
)
|
||||||
directory: tmp.path,
|
|
||||||
fn: async () => {
|
|
||||||
const providers = await list()
|
|
||||||
expect(providers[ProviderID.make("custom-openai")]).toBeDefined()
|
|
||||||
expect(providers[ProviderID.make("custom-openai")].options.baseURL).toBe("https://custom.openai.com/v1")
|
|
||||||
},
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
test("model cost defaults to zero when not specified", async () => {
|
it.instance(
|
||||||
await using tmp = await tmpdir({
|
"model cost defaults to zero when not specified",
|
||||||
init: async (dir) => {
|
Effect.gen(function* () {
|
||||||
await Bun.write(
|
const providers = yield* Provider.Service.use((provider) => provider.list())
|
||||||
path.join(dir, "opencode.json"),
|
const model = providers[ProviderID.make("test-provider")].models["test-model"]
|
||||||
JSON.stringify({
|
expect(model.cost.input).toBe(0)
|
||||||
$schema: "https://opencode.ai/config.json",
|
expect(model.cost.output).toBe(0)
|
||||||
provider: {
|
expect(model.cost.cache.read).toBe(0)
|
||||||
"test-provider": {
|
expect(model.cost.cache.write).toBe(0)
|
||||||
name: "Test Provider",
|
}),
|
||||||
npm: "@ai-sdk/openai-compatible",
|
{
|
||||||
env: [],
|
config: {
|
||||||
models: {
|
provider: {
|
||||||
"test-model": {
|
"test-provider": {
|
||||||
name: "Test Model",
|
name: "Test Provider",
|
||||||
tool_call: true,
|
npm: "@ai-sdk/openai-compatible",
|
||||||
limit: { context: 128000, output: 4096 },
|
env: [],
|
||||||
},
|
models: {
|
||||||
},
|
"test-model": {
|
||||||
options: {
|
name: "Test Model",
|
||||||
apiKey: "test-key",
|
tool_call: true,
|
||||||
},
|
limit: { context: 128000, output: 4096 },
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}),
|
options: {
|
||||||
)
|
apiKey: "test-key",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
})
|
},
|
||||||
await WithInstance.provide({
|
)
|
||||||
directory: tmp.path,
|
|
||||||
fn: async () => {
|
|
||||||
const providers = await list()
|
|
||||||
const model = providers[ProviderID.make("test-provider")].models["test-model"]
|
|
||||||
expect(model.cost.input).toBe(0)
|
|
||||||
expect(model.cost.output).toBe(0)
|
|
||||||
expect(model.cost.cache.read).toBe(0)
|
|
||||||
expect(model.cost.cache.write).toBe(0)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
})
|
|
||||||
|
|
||||||
test("model options are merged from existing model", async () => {
|
test("model options are merged from existing model", async () => {
|
||||||
await using tmp = await tmpdir({
|
await using tmp = await tmpdir({
|
||||||
|
|||||||
Reference in New Issue
Block a user