Something went wrong. Try again.
opencode v2 mirror
Something went wrong. Try again.
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150import { describe, expect, test } from "bun:test"import type { AgentListOutput, ModelListOutput, Project, ProviderListOutput } from "@opencode/client/promise"import { directoryKey, normalizeAgentList, normalizeProjectInfo, normalizeProviderList, updateProjectInfo,} from "./utils"
describe("normalizeAgentList", () => { test("adapts current agents to the app agent shape", () => { const result = normalizeAgentList([ { id: "build", name: "Build", mode: "primary", hidden: false, color: "primary", model: { id: "gpt-5", providerID: "openai", variant: "high" }, request: { settings: { temperature: 0.2, topP: 0.9 }, headers: {}, body: {} }, system: "Build software", permissions: [{ action: "read", resource: "*", effect: "allow" }], }, ] as AgentListOutput["data"])
expect(result).toEqual([ { name: "build", description: undefined, mode: "primary", hidden: false, temperature: 0.2, topP: 0.9, color: "primary", permission: [{ permission: "read", pattern: "*", action: "allow" }], model: { providerID: "openai", modelID: "gpt-5" }, variant: "high", prompt: "Build software", options: { temperature: 0.2, topP: 0.9 }, steps: undefined, }, ]) })})
describe("normalizeProviderList", () => { test("groups current models into the app provider catalog", () => { const result = normalizeProviderList( [{ id: "openai", name: "OpenAI", package: "@ai-sdk/openai" }] as ProviderListOutput["data"], [ { id: "gpt-5", modelID: "gpt-5", providerID: "openai", name: "GPT-5", capabilities: { tools: true, input: ["text", "image"], output: ["text"] }, variants: [{ id: "high" }], time: { released: 1 }, cost: [{ input: 1, output: 2, cache: { read: 0.1, write: 0.2 } }], status: "active", enabled: true, limit: { context: 128_000, output: 8_192 }, }, { id: "gpt-old", modelID: "gpt-old", providerID: "openai", name: "GPT Old", capabilities: { tools: false, input: ["text"], output: ["text"] }, variants: [], time: { released: 0 }, cost: [], status: "deprecated", enabled: true, limit: { context: 1, output: 1 }, }, ] as ModelListOutput["data"], )
expect(result.connected).toEqual(["openai"]) expect(result.default).toEqual({ openai: "gpt-5" }) expect(result.all.get("openai")?.models["gpt-old"]).toBeUndefined() expect(result.all.get("openai")?.models["gpt-5"]).toMatchObject({ id: "gpt-5", providerID: "openai", capabilities: { toolcall: true, attachment: true }, cost: { input: 1, output: 2 }, variants: { high: {} }, }) })})
describe("normalizeProjectInfo", () => { test("keeps the project VCS backend", () => { const project = { id: "prj", canonical: "/repo", time: { created: 1, updated: 1, active: 1 }, sandboxes: [] } expect(normalizeProjectInfo({ ...project, vcs: "git" } as Project).vcs).toBe("git") expect(normalizeProjectInfo({ ...project, vcs: "hg" } as Project).vcs).toBe("hg") expect(normalizeProjectInfo(project as Project).vcs).toBeUndefined() })})
describe("directoryKey", () => { test("normalizes slashes", () => { expect(String(directoryKey("C:\\Repos\\sst\\opencode"))).toBe("C:/Repos/sst/opencode") expect(String(directoryKey("C:/Repos/sst/opencode"))).toBe("C:/Repos/sst/opencode") })
test("preserves backslashes in posix paths", () => { expect(String(directoryKey("/tmp/foo\\bar"))).toBe("/tmp/foo\\bar") })
test("trims trailing slashes without breaking roots", () => { expect(String(directoryKey("C:/Repos/sst/opencode/"))).toBe("C:/Repos/sst/opencode") expect(String(directoryKey("C:/"))).toBe("C:/") expect(String(directoryKey("/"))).toBe("/") })})
describe("updateProjectInfo", () => { test("applies saved metadata without losing workspace inventory", () => { const update = { id: "project", canonical: "/repo", name: "Repo", icon: { color: "purple" }, time: { created: 1, updated: 2, active: 2 }, sandboxes: ["/repo-sandbox"], } satisfies Project
expect( updateProjectInfo( { ...update, name: "Old name", icon: { color: "gray" }, worktree: "/old-repo", worktrees: [{ directory: "/repo", strategy: "git" }], }, update, ), ).toMatchObject({ name: "Repo", icon: { color: "purple" }, worktree: "/repo", worktrees: [{ directory: "/repo", strategy: "git" }], }) })})