|
| 1 | +import { mkdtempSync, rmSync, writeFileSync } from "node:fs" |
| 2 | +import { tmpdir } from "node:os" |
| 3 | +import { join } from "node:path" |
| 4 | +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest" |
| 5 | +import { transactionsCommand } from "../../src/commands/transactions.js" |
| 6 | +import { type CommandTestContext, createCommandTestContext } from "../mocks.js" |
| 7 | + |
| 8 | +function writeTempFile(contents: string): { dir: string; file: string } { |
| 9 | + const dir = mkdtempSync(join(tmpdir(), "opensea-cli-transactions-")) |
| 10 | + const file = join(dir, "request.json") |
| 11 | + writeFileSync(file, contents) |
| 12 | + return { dir, file } |
| 13 | +} |
| 14 | + |
| 15 | +describe("transactionsCommand", () => { |
| 16 | + let ctx: CommandTestContext |
| 17 | + |
| 18 | + beforeEach(() => { |
| 19 | + ctx = createCommandTestContext() |
| 20 | + }) |
| 21 | + |
| 22 | + afterEach(() => { |
| 23 | + vi.restoreAllMocks() |
| 24 | + }) |
| 25 | + |
| 26 | + it("creates the receipt subcommand", () => { |
| 27 | + const cmd = transactionsCommand(ctx.getClient, ctx.getFormat) |
| 28 | + |
| 29 | + expect(cmd.name()).toBe("transactions") |
| 30 | + expect(cmd.commands.map(command => command.name())).toContain("receipt") |
| 31 | + }) |
| 32 | + |
| 33 | + it("posts the parsed request body to the receipt endpoint", async () => { |
| 34 | + ctx.mockClient.post.mockResolvedValue({ status: "confirmed" }) |
| 35 | + const body = { swap_quote: { id: "quote-1" } } |
| 36 | + const { dir, file } = writeTempFile(JSON.stringify(body)) |
| 37 | + |
| 38 | + const cmd = transactionsCommand(ctx.getClient, ctx.getFormat) |
| 39 | + try { |
| 40 | + await cmd.parseAsync(["receipt", "--request", file], { from: "user" }) |
| 41 | + } finally { |
| 42 | + rmSync(dir, { recursive: true, force: true }) |
| 43 | + } |
| 44 | + |
| 45 | + expect(ctx.mockClient.post).toHaveBeenCalledWith( |
| 46 | + "/api/v2/transactions/receipt", |
| 47 | + body, |
| 48 | + ) |
| 49 | + expect(ctx.consoleSpy).toHaveBeenCalledWith( |
| 50 | + JSON.stringify({ status: "confirmed" }, null, 2), |
| 51 | + ) |
| 52 | + }) |
| 53 | + |
| 54 | + it("names the option and path when the request file is missing", async () => { |
| 55 | + const dir = mkdtempSync(join(tmpdir(), "opensea-cli-transactions-")) |
| 56 | + const missing = join(dir, "missing.json") |
| 57 | + |
| 58 | + const cmd = transactionsCommand(ctx.getClient, ctx.getFormat) |
| 59 | + try { |
| 60 | + await expect( |
| 61 | + cmd.parseAsync(["receipt", "--request", missing], { from: "user" }), |
| 62 | + ).rejects.toThrow(`--request: could not read or parse '${missing}': `) |
| 63 | + } finally { |
| 64 | + rmSync(dir, { recursive: true, force: true }) |
| 65 | + } |
| 66 | + expect(ctx.mockClient.post).not.toHaveBeenCalled() |
| 67 | + }) |
| 68 | + |
| 69 | + it("names the option and path when the request file is invalid JSON", async () => { |
| 70 | + const { dir, file } = writeTempFile("{ not json }") |
| 71 | + |
| 72 | + const cmd = transactionsCommand(ctx.getClient, ctx.getFormat) |
| 73 | + try { |
| 74 | + await expect( |
| 75 | + cmd.parseAsync(["receipt", "--request", file], { from: "user" }), |
| 76 | + ).rejects.toThrow(`--request: could not read or parse '${file}': `) |
| 77 | + } finally { |
| 78 | + rmSync(dir, { recursive: true, force: true }) |
| 79 | + } |
| 80 | + expect(ctx.mockClient.post).not.toHaveBeenCalled() |
| 81 | + }) |
| 82 | + |
| 83 | + it("requires --request", async () => { |
| 84 | + const cmd = transactionsCommand(ctx.getClient, ctx.getFormat) |
| 85 | + const receipt = cmd.commands.find(command => command.name() === "receipt") |
| 86 | + expect(receipt).toBeDefined() |
| 87 | + receipt?.exitOverride() |
| 88 | + |
| 89 | + await expect(cmd.parseAsync(["receipt"], { from: "user" })).rejects.toThrow( |
| 90 | + "error: required option '--request <file>' not specified", |
| 91 | + ) |
| 92 | + expect(ctx.mockClient.post).not.toHaveBeenCalled() |
| 93 | + }) |
| 94 | +}) |
0 commit comments