From 1042a36d908b94a89061a3377ecddeff041b73f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?ibrahim=20=C3=B6zsara=C3=A7?= <160004724+iborazzi@users.noreply.github.com> Date: Sun, 23 Aug 2026 15:53:29 +0300 Subject: [PATCH] test(assets): add unit tests for transfer subcommand --- test/commands/assets.test.ts | 63 ++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 test/commands/assets.test.ts diff --git a/test/commands/assets.test.ts b/test/commands/assets.test.ts new file mode 100644 index 0000000..2cff325 --- /dev/null +++ b/test/commands/assets.test.ts @@ -0,0 +1,63 @@ +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 { assetsCommand } from "../../src/commands/assets.js" +import { type CommandTestContext, createCommandTestContext } from "../mocks.js" + +describe("assetsCommand", () => { + let ctx: CommandTestContext + let dir: string + + beforeEach(() => { + ctx = createCommandTestContext() + dir = mkdtempSync(join(tmpdir(), "assets-test-")) + }) + + afterEach(() => { + vi.restoreAllMocks() + rmSync(dir, { recursive: true, force: true }) + }) + + it("creates command with correct subcommands", () => { + const cmd = assetsCommand(ctx.getClient, ctx.getFormat) + expect(cmd.name()).toBe("assets") + const subcommands = cmd.commands.map(c => c.name()) + expect(subcommands).toContain("transfer") + }) + + it("transfer subcommand reads body file and posts it", async () => { + const bodyPath = join(dir, "transfer.json") + const body = { chain: "base", to: "0xabc", amount: "1" } + writeFileSync(bodyPath, JSON.stringify(body)) + ctx.mockClient.post.mockResolvedValue({ transactions: [] }) + + const cmd = assetsCommand(ctx.getClient, ctx.getFormat) + await cmd.parseAsync(["transfer", "--body", bodyPath], { from: "user" }) + + expect(ctx.mockClient.post).toHaveBeenCalledWith( + "/api/v2/assets/transfer", + body, + ) + }) + + it("transfer subcommand throws when --body file does not exist", async () => { + const cmd = assetsCommand(ctx.getClient, ctx.getFormat) + await expect( + cmd.parseAsync( + ["transfer", "--body", join(dir, "does-not-exist.json")], + { from: "user" }, + ), + ).rejects.toThrow(/Could not read --body/) + }) + + it("transfer subcommand throws when --body file is invalid JSON", async () => { + const bodyPath = join(dir, "bad.json") + writeFileSync(bodyPath, "{ not valid json") + + const cmd = assetsCommand(ctx.getClient, ctx.getFormat) + await expect( + cmd.parseAsync(["transfer", "--body", bodyPath], { from: "user" }), + ).rejects.toThrow(/Could not parse --body/) + }) +})