2025-12-30 15:06:41 +09:00
|
|
|
import { describe, it, expect, spyOn, afterEach } from "bun:test"
|
|
|
|
|
import * as config from "./config"
|
|
|
|
|
|
|
|
|
|
describe("config check", () => {
|
|
|
|
|
describe("validateConfig", () => {
|
|
|
|
|
it("returns valid: false for non-existent file", () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given non-existent file path
|
|
|
|
|
// when validating
|
2025-12-30 15:06:41 +09:00
|
|
|
const result = config.validateConfig("/non/existent/path.json")
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then should indicate invalid
|
2025-12-30 15:06:41 +09:00
|
|
|
expect(result.valid).toBe(false)
|
|
|
|
|
expect(result.errors.length).toBeGreaterThan(0)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe("getConfigInfo", () => {
|
|
|
|
|
it("returns exists: false when no config found", () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given no config file exists
|
|
|
|
|
// when getting config info
|
2025-12-30 15:06:41 +09:00
|
|
|
const info = config.getConfigInfo()
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then should handle gracefully
|
2025-12-30 15:06:41 +09:00
|
|
|
expect(typeof info.exists).toBe("boolean")
|
|
|
|
|
expect(typeof info.valid).toBe("boolean")
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe("checkConfigValidity", () => {
|
|
|
|
|
let getInfoSpy: ReturnType<typeof spyOn>
|
|
|
|
|
|
|
|
|
|
afterEach(() => {
|
|
|
|
|
getInfoSpy?.mockRestore()
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("returns pass when no config exists (uses defaults)", async () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given no config file
|
2025-12-30 15:06:41 +09:00
|
|
|
getInfoSpy = spyOn(config, "getConfigInfo").mockReturnValue({
|
|
|
|
|
exists: false,
|
|
|
|
|
path: null,
|
|
|
|
|
format: null,
|
|
|
|
|
valid: true,
|
|
|
|
|
errors: [],
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// when checking validity
|
2025-12-30 15:06:41 +09:00
|
|
|
const result = await config.checkConfigValidity()
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then should pass with default message
|
2025-12-30 15:06:41 +09:00
|
|
|
expect(result.status).toBe("pass")
|
|
|
|
|
expect(result.message).toContain("default")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("returns pass when config is valid", async () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given valid config
|
2025-12-30 15:06:41 +09:00
|
|
|
getInfoSpy = spyOn(config, "getConfigInfo").mockReturnValue({
|
|
|
|
|
exists: true,
|
|
|
|
|
path: "/home/user/.config/opencode/oh-my-opencode.json",
|
|
|
|
|
format: "json",
|
|
|
|
|
valid: true,
|
|
|
|
|
errors: [],
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// when checking validity
|
2025-12-30 15:06:41 +09:00
|
|
|
const result = await config.checkConfigValidity()
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then should pass
|
2025-12-30 15:06:41 +09:00
|
|
|
expect(result.status).toBe("pass")
|
|
|
|
|
expect(result.message).toContain("JSON")
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it("returns fail when config has validation errors", async () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given invalid config
|
2025-12-30 15:06:41 +09:00
|
|
|
getInfoSpy = spyOn(config, "getConfigInfo").mockReturnValue({
|
|
|
|
|
exists: true,
|
|
|
|
|
path: "/home/user/.config/opencode/oh-my-opencode.json",
|
|
|
|
|
format: "json",
|
|
|
|
|
valid: false,
|
|
|
|
|
errors: ["agents.oracle: Invalid model format"],
|
|
|
|
|
})
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// when checking validity
|
2025-12-30 15:06:41 +09:00
|
|
|
const result = await config.checkConfigValidity()
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then should fail with errors
|
2025-12-30 15:06:41 +09:00
|
|
|
expect(result.status).toBe("fail")
|
|
|
|
|
expect(result.details?.some((d) => d.includes("Error"))).toBe(true)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
describe("getConfigCheckDefinition", () => {
|
|
|
|
|
it("returns valid check definition", () => {
|
2026-02-01 16:47:50 +09:00
|
|
|
// given
|
|
|
|
|
// when getting definition
|
2025-12-30 15:06:41 +09:00
|
|
|
const def = config.getConfigCheckDefinition()
|
|
|
|
|
|
2026-02-01 16:47:50 +09:00
|
|
|
// then should have required properties
|
2025-12-30 15:06:41 +09:00
|
|
|
expect(def.id).toBe("config-validation")
|
|
|
|
|
expect(def.category).toBe("configuration")
|
|
|
|
|
expect(def.critical).toBe(false)
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
})
|