|
| 1 | +import { afterEach, describe, expect, test } from "bun:test" |
| 2 | +import { mkdtempSync, rmSync, writeFileSync } from "node:fs" |
| 3 | +import { tmpdir } from "node:os" |
| 4 | +import { join } from "node:path" |
| 5 | + |
| 6 | +import { parseOpenCodeConfigFileWithError } from "./parse-opencode-config-file" |
| 7 | + |
| 8 | +describe("parseOpenCodeConfigFileWithError", () => { |
| 9 | + const tempDirectories: string[] = [] |
| 10 | + |
| 11 | + afterEach(() => { |
| 12 | + for (const directory of tempDirectories.splice(0)) { |
| 13 | + rmSync(directory, { recursive: true, force: true }) |
| 14 | + } |
| 15 | + }) |
| 16 | + |
| 17 | + test("#given a valid object config #when parsing the file #then it returns the parsed config", () => { |
| 18 | + // given |
| 19 | + const directory = mkdtempSync(join(tmpdir(), "omo-parse-config-")) |
| 20 | + tempDirectories.push(directory) |
| 21 | + const filePath = join(directory, "opencode.json") |
| 22 | + writeFileSync(filePath, '{"plugin": ["oh-my-openagent"]}\n', "utf-8") |
| 23 | + |
| 24 | + // when |
| 25 | + const result = parseOpenCodeConfigFileWithError(filePath) |
| 26 | + |
| 27 | + // then |
| 28 | + expect(result).toEqual({ |
| 29 | + config: { plugin: ["oh-my-openagent"] }, |
| 30 | + }) |
| 31 | + }) |
| 32 | + |
| 33 | + test("#given a null config payload #when parsing the file #then it returns a null parse error", () => { |
| 34 | + // given |
| 35 | + const directory = mkdtempSync(join(tmpdir(), "omo-parse-config-")) |
| 36 | + tempDirectories.push(directory) |
| 37 | + const filePath = join(directory, "opencode.json") |
| 38 | + writeFileSync(filePath, "null\n", "utf-8") |
| 39 | + |
| 40 | + // when |
| 41 | + const result = parseOpenCodeConfigFileWithError(filePath) |
| 42 | + |
| 43 | + // then |
| 44 | + expect(result).toEqual({ |
| 45 | + config: null, |
| 46 | + error: `Config file parsed to null/undefined: ${filePath}. Ensure it contains valid JSON.`, |
| 47 | + }) |
| 48 | + }) |
| 49 | +}) |
0 commit comments