Compare commits

...

1 Commits

Author SHA1 Message Date
Kit Langton a474e92433 test: migrate config template fixtures 2026-05-18 19:13:47 -04:00
2 changed files with 158 additions and 199 deletions
+115 -157
View File
@@ -1,5 +1,5 @@
import { test, expect, describe, mock, afterEach, beforeEach } from "bun:test" import { test, expect, describe, mock, afterEach, beforeEach } from "bun:test"
import { Effect, Layer, Option } from "effect" import { Effect, Exit, Layer, Option } from "effect"
import { NodeFileSystem, NodePath } from "@effect/platform-node" import { NodeFileSystem, NodePath } from "@effect/platform-node"
import { Config } from "@/config/config" import { Config } from "@/config/config"
import { ConfigManaged } from "@/config/managed" import { ConfigManaged } from "@/config/managed"
@@ -13,7 +13,7 @@ import { Account } from "../../src/account/account"
import { AccessToken, AccountID, OrgID } from "../../src/account/schema" import { AccessToken, AccountID, OrgID } from "../../src/account/schema"
import { AppFileSystem } from "@opencode-ai/core/filesystem" import { AppFileSystem } from "@opencode-ai/core/filesystem"
import { Env } from "../../src/env" import { Env } from "../../src/env"
import { provideTestInstance, provideTmpdirInstance, withTestInstance } from "../fixture/fixture" import { provideTestInstance, provideTmpdirInstance, TestInstance, withTestInstance } from "../fixture/fixture"
import { tmpdir } from "../fixture/fixture" import { tmpdir } from "../fixture/fixture"
import { InstanceRuntime } from "@/project/instance-runtime" import { InstanceRuntime } from "@/project/instance-runtime"
import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner" import { CrossSpawnSpawner } from "@opencode-ai/core/cross-spawn-spawner"
@@ -115,6 +115,25 @@ async function writeConfig(dir: string, config: object, name = "opencode.json")
await Filesystem.write(path.join(dir, name), JSON.stringify(config)) await Filesystem.write(path.join(dir, name), JSON.stringify(config))
} }
const writeConfigEffect = (dir: string, config: object, name = "opencode.json") =>
Effect.promise(() => writeConfig(dir, config, name))
function withProcessEnv<A, E, R>(key: string, value: string, effect: Effect.Effect<A, E, R>) {
return Effect.acquireUseRelease(
Effect.sync(() => {
const original = process.env[key]
process.env[key] = value
return original
}),
() => effect,
(original) =>
Effect.sync(() => {
if (original !== undefined) process.env[key] = original
else delete process.env[key]
}),
)
}
async function check(map: (dir: string) => string) { async function check(map: (dir: string) => string) {
if (process.platform !== "win32") return if (process.platform !== "win32") return
await using globalTmp = await tmpdir() await using globalTmp = await tmpdir()
@@ -360,35 +379,31 @@ test("ignores legacy tui keys in opencode config", async () => {
}) })
}) })
test("loads JSONC config file", async () => { it.instance("loads JSONC config file", () =>
await using tmp = await tmpdir({ Effect.gen(function* () {
init: async (dir) => { const test = yield* TestInstance
await Filesystem.write( yield* Effect.promise(() =>
path.join(dir, "opencode.jsonc"), Filesystem.write(
path.join(test.directory, "opencode.jsonc"),
`{ `{
// This is a comment // This is a comment
"$schema": "https://opencode.ai/config.json", "$schema": "https://opencode.ai/config.json",
"model": "test/model", "model": "test/model",
"username": "testuser" "username": "testuser"
}`, }`,
),
) )
}, const config = yield* Config.Service.use((svc) => svc.get())
})
await withTestInstance({
directory: tmp.path,
fn: async (ctx) => {
const config = await load(ctx)
expect(config.model).toBe("test/model") expect(config.model).toBe("test/model")
expect(config.username).toBe("testuser") expect(config.username).toBe("testuser")
}, }),
}) )
})
test("jsonc overrides json in the same directory", async () => { it.instance("jsonc overrides json in the same directory", () =>
await using tmp = await tmpdir({ Effect.gen(function* () {
init: async (dir) => { const test = yield* TestInstance
await writeConfig( yield* writeConfigEffect(
dir, test.directory,
{ {
$schema: "https://opencode.ai/config.json", $schema: "https://opencode.ai/config.json",
model: "base", model: "base",
@@ -396,88 +411,86 @@ test("jsonc overrides json in the same directory", async () => {
}, },
"opencode.jsonc", "opencode.jsonc",
) )
await writeConfig(dir, { yield* writeConfigEffect(test.directory, {
$schema: "https://opencode.ai/config.json", $schema: "https://opencode.ai/config.json",
model: "override", model: "override",
}) })
}, const config = yield* Config.Service.use((svc) => svc.get())
})
await withTestInstance({
directory: tmp.path,
fn: async (ctx) => {
const config = await load(ctx)
expect(config.model).toBe("base") expect(config.model).toBe("base")
expect(config.username).toBe("base") expect(config.username).toBe("base")
}, }),
}) )
})
test("handles environment variable substitution", async () => { it.instance("handles environment variable substitution", () =>
const originalEnv = process.env["TEST_VAR"] withProcessEnv(
process.env["TEST_VAR"] = "test-user" "TEST_VAR",
"test-user",
try { Effect.gen(function* () {
await using tmp = await tmpdir({ const test = yield* TestInstance
init: async (dir) => { yield* writeConfigEffect(test.directory, {
await writeConfig(dir, {
$schema: "https://opencode.ai/config.json", $schema: "https://opencode.ai/config.json",
username: "{env:TEST_VAR}", username: "{env:TEST_VAR}",
}) })
}, const config = yield* Config.Service.use((svc) => svc.get())
})
await withTestInstance({
directory: tmp.path,
fn: async (ctx) => {
const config = await load(ctx)
expect(config.username).toBe("test-user") expect(config.username).toBe("test-user")
}, }),
}) ),
} finally { )
if (originalEnv !== undefined) {
process.env["TEST_VAR"] = originalEnv
} else {
delete process.env["TEST_VAR"]
}
}
})
test("preserves env variables when adding $schema to config", async () => { it.instance("preserves env variables when adding $schema to config", () =>
const originalEnv = process.env["PRESERVE_VAR"] withProcessEnv(
process.env["PRESERVE_VAR"] = "secret_value" "PRESERVE_VAR",
"secret_value",
try { Effect.gen(function* () {
await using tmp = await tmpdir({ const test = yield* TestInstance
init: async (dir) => {
// Config without $schema - should trigger auto-add // Config without $schema - should trigger auto-add
await Filesystem.write( yield* Effect.promise(() =>
path.join(dir, "opencode.json"), Filesystem.write(
path.join(test.directory, "opencode.json"),
JSON.stringify({ JSON.stringify({
username: "{env:PRESERVE_VAR}", username: "{env:PRESERVE_VAR}",
}), }),
),
) )
}, const config = yield* Config.Service.use((svc) => svc.get())
})
await withTestInstance({
directory: tmp.path,
fn: async (ctx) => {
const config = await load(ctx)
expect(config.username).toBe("secret_value") expect(config.username).toBe("secret_value")
// Read the file to verify the env variable was preserved // Read the file to verify the env variable was preserved
const content = await Filesystem.readText(path.join(tmp.path, "opencode.json")) const content = yield* Effect.promise(() => Filesystem.readText(path.join(test.directory, "opencode.json")))
expect(content).toContain("{env:PRESERVE_VAR}") expect(content).toContain("{env:PRESERVE_VAR}")
expect(content).not.toContain("secret_value") expect(content).not.toContain("secret_value")
expect(content).toContain("$schema") expect(content).toContain("$schema")
}, }),
),
)
it.instance("handles file inclusion substitution", () =>
Effect.gen(function* () {
const test = yield* TestInstance
yield* Effect.promise(() => Filesystem.write(path.join(test.directory, "included.txt"), "test-user"))
yield* writeConfigEffect(test.directory, {
$schema: "https://opencode.ai/config.json",
username: "{file:included.txt}",
}) })
} finally { const config = yield* Config.Service.use((svc) => svc.get())
if (originalEnv !== undefined) { expect(config.username).toBe("test-user")
process.env["PRESERVE_VAR"] = originalEnv }),
} else { )
delete process.env["PRESERVE_VAR"]
} it.instance("handles file inclusion with replacement tokens", () =>
} Effect.gen(function* () {
const test = yield* TestInstance
yield* Effect.promise(() =>
Filesystem.write(path.join(test.directory, "included.md"), "const out = await Bun.$`echo hi`"),
)
yield* writeConfigEffect(test.directory, {
$schema: "https://opencode.ai/config.json",
username: "{file:included.md}",
}) })
const config = yield* Config.Service.use((svc) => svc.get())
expect(config.username).toBe("const out = await Bun.$`echo hi`")
}),
)
test("resolves env templates in account config with account token", async () => { test("resolves env templates in account config with account token", async () => {
const originalControlToken = process.env["OPENCODE_CONSOLE_TOKEN"] const originalControlToken = process.env["OPENCODE_CONSOLE_TOKEN"]
@@ -544,80 +557,31 @@ test("resolves env templates in account config with account token", async () =>
} }
}) })
test("handles file inclusion substitution", async () => { it.instance("validates config schema and throws on invalid fields", () =>
await using tmp = await tmpdir({ Effect.gen(function* () {
init: async (dir) => { const test = yield* TestInstance
await Filesystem.write(path.join(dir, "included.txt"), "test-user") yield* writeConfigEffect(test.directory, {
await writeConfig(dir, {
$schema: "https://opencode.ai/config.json",
username: "{file:included.txt}",
})
},
})
await withTestInstance({
directory: tmp.path,
fn: async (ctx) => {
const config = await load(ctx)
expect(config.username).toBe("test-user")
},
})
})
test("handles file inclusion with replacement tokens", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await Filesystem.write(path.join(dir, "included.md"), "const out = await Bun.$`echo hi`")
await writeConfig(dir, {
$schema: "https://opencode.ai/config.json",
username: "{file:included.md}",
})
},
})
await withTestInstance({
directory: tmp.path,
fn: async (ctx) => {
const config = await load(ctx)
expect(config.username).toBe("const out = await Bun.$`echo hi`")
},
})
})
test("validates config schema and throws on invalid fields", async () => {
await using tmp = await tmpdir({
init: async (dir) => {
await writeConfig(dir, {
$schema: "https://opencode.ai/config.json", $schema: "https://opencode.ai/config.json",
invalid_field: "should cause error", invalid_field: "should cause error",
}) })
}, const exit = yield* Config.Service.use((svc) => svc.get()).pipe(Effect.exit)
}) expect(Exit.isFailure(exit)).toBe(true)
await provideTestInstance({ }),
directory: tmp.path, )
fn: async (ctx) => {
// Strict schema should throw an error for invalid fields
await expect(load(ctx)).rejects.toThrow()
},
})
})
test("throws error for invalid JSON", async () => { it.instance("throws error for invalid JSON", () =>
await using tmp = await tmpdir({ Effect.gen(function* () {
init: async (dir) => { const test = yield* TestInstance
await Filesystem.write(path.join(dir, "opencode.json"), "{ invalid json }") yield* Effect.promise(() => Filesystem.write(path.join(test.directory, "opencode.json"), "{ invalid json }"))
}, const exit = yield* Config.Service.use((svc) => svc.get()).pipe(Effect.exit)
}) expect(Exit.isFailure(exit)).toBe(true)
await provideTestInstance({ }),
directory: tmp.path, )
fn: async (ctx) => {
await expect(load(ctx)).rejects.toThrow()
},
})
})
test("handles agent configuration", async () => { it.instance("handles agent configuration", () =>
await using tmp = await tmpdir({ Effect.gen(function* () {
init: async (dir) => { const test = yield* TestInstance
await writeConfig(dir, { yield* writeConfigEffect(test.directory, {
$schema: "https://opencode.ai/config.json", $schema: "https://opencode.ai/config.json",
agent: { agent: {
test_agent: { test_agent: {
@@ -627,12 +591,7 @@ test("handles agent configuration", async () => {
}, },
}, },
}) })
}, const config = yield* Config.Service.use((svc) => svc.get())
})
await withTestInstance({
directory: tmp.path,
fn: async (ctx) => {
const config = await load(ctx)
expect(config.agent?.["test_agent"]).toEqual( expect(config.agent?.["test_agent"]).toEqual(
expect.objectContaining({ expect.objectContaining({
model: "test/model", model: "test/model",
@@ -640,9 +599,8 @@ test("handles agent configuration", async () => {
description: "test agent", description: "test agent",
}), }),
) )
}, }),
}) )
})
test("treats agent variant as model-scoped setting (not provider option)", async () => { test("treats agent variant as model-scoped setting (not provider option)", async () => {
await using tmp = await tmpdir({ await using tmp = await tmpdir({
+1
View File
@@ -72,6 +72,7 @@ Repeated setup work, long sleeps/timeouts, serial integration tests, filesystem/
| Custom provider/model config cases can use Effect-aware instance fixtures | Migrated three more config-heavy provider cases to `it.instance` | 6.07s | 6.12s | keep | Neutral timing within noise, but continues removing manual config file writes on top of the first provider fixture PR. | | Custom provider/model config cases can use Effect-aware instance fixtures | Migrated three more config-heavy provider cases to `it.instance` | 6.07s | 6.12s | keep | Neutral timing within noise, but continues removing manual config file writes on top of the first provider fixture PR. |
| Provider env precedence and model lookup cases can use Effect-aware instance fixtures | Migrated four more provider lookup/default-model cases to `it.instance` | 6.12s | 6.36s | keep | Noisy 5-run median; kept as a small stacked cleanup slice but do not claim speedup from this migration. | | Provider env precedence and model lookup cases can use Effect-aware instance fixtures | Migrated four more provider lookup/default-model cases to `it.instance` | 6.12s | 6.36s | keep | Noisy 5-run median; kept as a small stacked cleanup slice but do not claim speedup from this migration. |
| Simple config load cases can use Effect-aware instance fixtures | Migrated JSON, shell, formatter, and lsp config load cases to `it.instance` | 14.18s | 3.93s | keep | Three-run medians before/after; removes manual `tmpdir` + `withTestInstance` setup from the first simple config block. | | Simple config load cases can use Effect-aware instance fixtures | Migrated JSON, shell, formatter, and lsp config load cases to `it.instance` | 14.18s | 3.93s | keep | Three-run medians before/after; removes manual `tmpdir` + `withTestInstance` setup from the first simple config block. |
| Config template, file include, and simple agent cases can use Effect-aware instance fixtures | Migrated JSONC, env/file substitution, invalid config, and agent config cases to `it.instance` | 1.87s | 1.90s | keep | Stacked on the first config slice; neutral timing but removes more manual `tmpdir` + instance plumbing. |
## Profiling Results ## Profiling Results