From fa797339292d3724cbbfcc6415a91c9b1abd6ba2 Mon Sep 17 00:00:00 2001 From: iborazzi Date: Thu, 20 Aug 2026 08:46:53 +0300 Subject: [PATCH] test(parse): add unit tests for readJsonBodyOption --- test/parse.test.ts | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/test/parse.test.ts b/test/parse.test.ts index 04c1232..f26be5f 100644 --- a/test/parse.test.ts +++ b/test/parse.test.ts @@ -1,8 +1,12 @@ +import { mkdirSync, rmSync, writeFileSync } from "node:fs" +import { tmpdir } from "node:os" +import { join } from "node:path" import { describe, expect, it } from "vitest" import { parseFloatOption, parseIntOption, parseTraitsOption, + readJsonBodyOption, } from "../src/parse.js" describe("parseIntOption", () => { @@ -45,6 +49,43 @@ describe("parseFloatOption", () => { }) }) +describe("readJsonBodyOption", () => { + const tmpDir = join(tmpdir(), "opensea-cli-test-" + Date.now()) + + it("reads and parses a valid JSON file", () => { + mkdirSync(tmpDir, { recursive: true }) + const filePath = join(tmpDir, "valid.json") + writeFileSync(filePath, JSON.stringify({ key: "value", num: 100 })) + + const result = readJsonBodyOption<{ key: string; num: number }>( + filePath, + "--body", + ) + expect(result).toEqual({ key: "value", num: 100 }) + + rmSync(tmpDir, { recursive: true, force: true }) + }) + + it("throws formatted error when file does not exist", () => { + const nonExistentPath = join(tmpdir(), "non-existent-file-99999.json") + expect(() => readJsonBodyOption(nonExistentPath, "--body")).toThrow( + `Could not read --body from '${nonExistentPath}'`, + ) + }) + + it("throws formatted error when file contains invalid JSON", () => { + mkdirSync(tmpDir, { recursive: true }) + const filePath = join(tmpDir, "invalid.json") + writeFileSync(filePath, "{ invalid json structure }") + + expect(() => readJsonBodyOption(filePath, "--body")).toThrow( + `Could not parse --body '${filePath}' as JSON`, + ) + + rmSync(tmpDir, { recursive: true, force: true }) + }) +}) + describe("parseTraitsOption", () => { it("returns the normalized JSON string for a valid filter array", () => { const input = '[{"traitType":"Background","value":"Red"}]'