Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions test/parse.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand Down Expand Up @@ -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"}]'
Expand Down