From 8cc7662bc488368680ad251a1eeddfb8e009066c Mon Sep 17 00:00:00 2001 From: iborazzi Date: Fri, 18 Sep 2026 09:11:25 +0300 Subject: [PATCH] test: add coverage for transactions receipt command --- test/commands/transactions.test.ts | 91 ++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100644 test/commands/transactions.test.ts diff --git a/test/commands/transactions.test.ts b/test/commands/transactions.test.ts new file mode 100644 index 0000000..7712391 --- /dev/null +++ b/test/commands/transactions.test.ts @@ -0,0 +1,91 @@ +import { mkdtempSync, rmSync, writeFileSync } from "node:fs" +import { tmpdir } from "node:os" +import { join } from "node:path" +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest" +import { transactionsCommand } from "../../src/commands/transactions.js" +import { type CommandTestContext, createCommandTestContext } from "../mocks.js" + +function writeTempJson(data: unknown): string { + const dir = mkdtempSync(join(tmpdir(), "opensea-cli-transactions-")) + const file = join(dir, "request.json") + writeFileSync(file, JSON.stringify(data)) + return file +} + +describe("transactionsCommand", () => { + let ctx: CommandTestContext + + beforeEach(() => { + ctx = createCommandTestContext() + }) + + afterEach(() => { + vi.restoreAllMocks() + }) + + it("creates command with the receipt subcommand", () => { + const cmd = transactionsCommand(ctx.getClient, ctx.getFormat) + expect(cmd.name()).toBe("transactions") + expect(cmd.commands.map(c => c.name())).toContain("receipt") + }) + + it("receipt posts the parsed request body to the receipt endpoint", async () => { + ctx.mockClient.post.mockResolvedValue({ status: "confirmed" }) + const body = { swap_quote: { id: "quote-1" } } + const file = writeTempJson(body) + + const cmd = transactionsCommand(ctx.getClient, ctx.getFormat) + try { + await cmd.parseAsync(["receipt", "--request", file], { from: "user" }) + } finally { + rmSync(file, { force: true }) + } + + expect(ctx.mockClient.post).toHaveBeenCalledWith( + "/api/v2/transactions/receipt", + body, + ) + expect(ctx.consoleSpy).toHaveBeenCalledWith( + JSON.stringify({ status: "confirmed" }, null, 2), + ) + }) + + it("names the option and the path when the request file does not exist", async () => { + const dir = mkdtempSync(join(tmpdir(), "opensea-cli-transactions-")) + const missing = join(dir, "absent.json") + + const cmd = transactionsCommand(ctx.getClient, ctx.getFormat) + try { + await expect( + cmd.parseAsync(["receipt", "--request", missing], { from: "user" }), + ).rejects.toThrow(`--request: could not read or parse '${missing}': `) + } finally { + rmSync(dir, { recursive: true, force: true }) + } + expect(ctx.mockClient.post).not.toHaveBeenCalled() + }) + + it("names the option and the path when the request file is not valid JSON", async () => { + const dir = mkdtempSync(join(tmpdir(), "opensea-cli-transactions-")) + const invalid = join(dir, "invalid.json") + writeFileSync(invalid, "{ not json }") + + const cmd = transactionsCommand(ctx.getClient, ctx.getFormat) + try { + await expect( + cmd.parseAsync(["receipt", "--request", invalid], { from: "user" }), + ).rejects.toThrow(`--request: could not read or parse '${invalid}': `) + } finally { + rmSync(dir, { recursive: true, force: true }) + } + expect(ctx.mockClient.post).not.toHaveBeenCalled() + }) + + it("requires --request", async () => { + const cmd = transactionsCommand(ctx.getClient, ctx.getFormat) + await expect( + cmd.parseAsync(["receipt"], { from: "user" }), + ).rejects.toThrow() + expect(ctx.mockClient.post).not.toHaveBeenCalled() + }) +}) \ No newline at end of file