Formatting
This commit is contained in:
@@ -3,14 +3,25 @@ import { mkdtempSync, rmSync } from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
import { addFakeResource, createMockAppService, createMockEngine, DEFAULT_SEED, reserveResource } from "../mocks/engine";
|
||||
import {
|
||||
addFakeResource,
|
||||
createMockAppService,
|
||||
createMockEngine,
|
||||
DEFAULT_SEED,
|
||||
reserveResource,
|
||||
} from "../mocks/engine";
|
||||
import { type Engine } from "@xo-cash/engine";
|
||||
import { p2pkhTemplate } from "../mocks/template-p2pkh";
|
||||
import { AppService } from "../../../src/services/app";
|
||||
|
||||
import { handleResourceCommand } from "../../../src/cli/commands/resource";
|
||||
import { CommandError } from "../../../src/cli/commands/types";
|
||||
import { createCommandDeps, createMockIO, expectLogs, type LogExpectation } from "../mocks/command";
|
||||
import {
|
||||
createCommandDeps,
|
||||
createMockIO,
|
||||
expectLogs,
|
||||
type LogExpectation,
|
||||
} from "../mocks/command";
|
||||
|
||||
type TestCase = {
|
||||
name: string;
|
||||
@@ -94,7 +105,10 @@ const testCases: TestCase[] = [
|
||||
},
|
||||
{
|
||||
name: "throws when unreserve called with non-existent UTXO",
|
||||
inputs: ["unreserve", "0000000000000000000000000000000000000000000000000000000000000000:0"],
|
||||
inputs: [
|
||||
"unreserve",
|
||||
"0000000000000000000000000000000000000000000000000000000000000000:0",
|
||||
],
|
||||
shouldThrow: true,
|
||||
expectedEvent: "resource.unreserve.utxo_missing",
|
||||
},
|
||||
@@ -119,32 +133,50 @@ describe("resource command", () => {
|
||||
rmSync(tempDir, { recursive: true, force: true });
|
||||
});
|
||||
|
||||
test.each(testCases)("$name", async ({ inputs, options, shouldThrow, expectedEvent, expectedData, logs }) => {
|
||||
const { io, spies } = createMockIO();
|
||||
test.each(testCases)(
|
||||
"$name",
|
||||
async ({
|
||||
inputs,
|
||||
options,
|
||||
shouldThrow,
|
||||
expectedEvent,
|
||||
expectedData,
|
||||
logs,
|
||||
}) => {
|
||||
const { io, spies } = createMockIO();
|
||||
|
||||
if (shouldThrow) {
|
||||
try {
|
||||
await handleResourceCommand(createCommandDeps(app, io), inputs, options ?? {});
|
||||
expect.fail("Expected command to throw");
|
||||
} catch (error) {
|
||||
if (expectedEvent) {
|
||||
expect(error).toBeInstanceOf(CommandError);
|
||||
expect((error as CommandError).event).toBe(expectedEvent);
|
||||
if (shouldThrow) {
|
||||
try {
|
||||
await handleResourceCommand(
|
||||
createCommandDeps(app, io),
|
||||
inputs,
|
||||
options ?? {},
|
||||
);
|
||||
expect.fail("Expected command to throw");
|
||||
} catch (error) {
|
||||
if (expectedEvent) {
|
||||
expect(error).toBeInstanceOf(CommandError);
|
||||
expect((error as CommandError).event).toBe(expectedEvent);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const result = await handleResourceCommand(
|
||||
createCommandDeps(app, io),
|
||||
inputs,
|
||||
options ?? {},
|
||||
);
|
||||
if (expectedData) {
|
||||
Object.entries(expectedData).forEach(([key, value]) => {
|
||||
expect(result[key as keyof typeof result]).toEqual(value);
|
||||
});
|
||||
}
|
||||
}
|
||||
} else {
|
||||
const result = await handleResourceCommand(createCommandDeps(app, io), inputs, options ?? {});
|
||||
if (expectedData) {
|
||||
Object.entries(expectedData).forEach(([key, value]) => {
|
||||
expect(result[key as keyof typeof result]).toEqual(value);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (logs) {
|
||||
expectLogs(spies, logs);
|
||||
}
|
||||
});
|
||||
if (logs) {
|
||||
expectLogs(spies, logs);
|
||||
}
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
describe("resource command with populated data", () => {
|
||||
@@ -169,7 +201,11 @@ describe("resource command with populated data", () => {
|
||||
await addFakeResource(engine, { valueSatoshis: 25000 });
|
||||
|
||||
const { io, spies } = createMockIO();
|
||||
const result = await handleResourceCommand(createCommandDeps(app, io), ["list"], {});
|
||||
const result = await handleResourceCommand(
|
||||
createCommandDeps(app, io),
|
||||
["list"],
|
||||
{},
|
||||
);
|
||||
|
||||
expect(result.count).toBe(2);
|
||||
expectLogs(spies, [{ out: "Total resources: 2" }]);
|
||||
@@ -187,21 +223,38 @@ describe("resource command with populated data", () => {
|
||||
|
||||
test("list excludes reserved resources by default", async () => {
|
||||
await addFakeResource(engine, { valueSatoshis: 50000 });
|
||||
await addFakeResource(engine, { valueSatoshis: 25000, reservedBy: "inv-123" });
|
||||
await addFakeResource(engine, {
|
||||
valueSatoshis: 25000,
|
||||
reservedBy: "inv-123",
|
||||
});
|
||||
|
||||
const { io } = createMockIO();
|
||||
const result = await handleResourceCommand(createCommandDeps(app, io), ["list"], {});
|
||||
const result = await handleResourceCommand(
|
||||
createCommandDeps(app, io),
|
||||
["list"],
|
||||
{},
|
||||
);
|
||||
|
||||
expect(result.count).toBe(1);
|
||||
});
|
||||
|
||||
test("list reserved shows only reserved resources", async () => {
|
||||
await addFakeResource(engine, { valueSatoshis: 50000 });
|
||||
await addFakeResource(engine, { valueSatoshis: 25000, reservedBy: "inv-123" });
|
||||
await addFakeResource(engine, { valueSatoshis: 10000, reservedBy: "inv-456" });
|
||||
await addFakeResource(engine, {
|
||||
valueSatoshis: 25000,
|
||||
reservedBy: "inv-123",
|
||||
});
|
||||
await addFakeResource(engine, {
|
||||
valueSatoshis: 10000,
|
||||
reservedBy: "inv-456",
|
||||
});
|
||||
|
||||
const { io, spies } = createMockIO();
|
||||
const result = await handleResourceCommand(createCommandDeps(app, io), ["list", "reserved"], {});
|
||||
const result = await handleResourceCommand(
|
||||
createCommandDeps(app, io),
|
||||
["list", "reserved"],
|
||||
{},
|
||||
);
|
||||
|
||||
expect(result.count).toBe(2);
|
||||
expectLogs(spies, [{ out: "reserved for inv-123" }]);
|
||||
@@ -209,29 +262,45 @@ describe("resource command with populated data", () => {
|
||||
|
||||
test("list all shows both reserved and unreserved", async () => {
|
||||
await addFakeResource(engine, { valueSatoshis: 50000 });
|
||||
await addFakeResource(engine, { valueSatoshis: 25000, reservedBy: "inv-123" });
|
||||
await addFakeResource(engine, {
|
||||
valueSatoshis: 25000,
|
||||
reservedBy: "inv-123",
|
||||
});
|
||||
|
||||
const { io } = createMockIO();
|
||||
const result = await handleResourceCommand(createCommandDeps(app, io), ["list", "all"], {});
|
||||
const result = await handleResourceCommand(
|
||||
createCommandDeps(app, io),
|
||||
["list", "all"],
|
||||
{},
|
||||
);
|
||||
|
||||
expect(result.count).toBe(2);
|
||||
});
|
||||
|
||||
test("unreserve releases a reserved UTXO", async () => {
|
||||
const resource = await addFakeResource(engine, { valueSatoshis: 25000, reservedBy: "inv-123" });
|
||||
const resource = await addFakeResource(engine, {
|
||||
valueSatoshis: 25000,
|
||||
reservedBy: "inv-123",
|
||||
});
|
||||
|
||||
const { io, spies } = createMockIO();
|
||||
await handleResourceCommand(
|
||||
createCommandDeps(app, io),
|
||||
["unreserve", `${resource.outpointTransactionHash}:${resource.outpointIndex}`],
|
||||
[
|
||||
"unreserve",
|
||||
`${resource.outpointTransactionHash}:${resource.outpointIndex}`,
|
||||
],
|
||||
{},
|
||||
);
|
||||
|
||||
expectLogs(spies, [{ out: "Unreserved" }, { out: "was reserved for inv-123" }]);
|
||||
expectLogs(spies, [
|
||||
{ out: "Unreserved" },
|
||||
{ out: "was reserved for inv-123" },
|
||||
]);
|
||||
|
||||
const resources = await engine.listUnspentOutputsData();
|
||||
const target = resources.find(
|
||||
r => r.outpointTransactionHash === resource.outpointTransactionHash,
|
||||
(r) => r.outpointTransactionHash === resource.outpointTransactionHash,
|
||||
);
|
||||
expect(target?.reservedBy).toBeUndefined();
|
||||
});
|
||||
@@ -242,7 +311,10 @@ describe("resource command with populated data", () => {
|
||||
const { io, spies } = createMockIO();
|
||||
await handleResourceCommand(
|
||||
createCommandDeps(app, io),
|
||||
["unreserve", `${resource.outpointTransactionHash}:${resource.outpointIndex}`],
|
||||
[
|
||||
"unreserve",
|
||||
`${resource.outpointTransactionHash}:${resource.outpointIndex}`,
|
||||
],
|
||||
{},
|
||||
);
|
||||
|
||||
@@ -251,17 +323,27 @@ describe("resource command with populated data", () => {
|
||||
|
||||
test("unreserve-all releases all reserved UTXOs", async () => {
|
||||
await addFakeResource(engine, { valueSatoshis: 50000 });
|
||||
await addFakeResource(engine, { valueSatoshis: 25000, reservedBy: "inv-123" });
|
||||
await addFakeResource(engine, { valueSatoshis: 10000, reservedBy: "inv-456" });
|
||||
await addFakeResource(engine, {
|
||||
valueSatoshis: 25000,
|
||||
reservedBy: "inv-123",
|
||||
});
|
||||
await addFakeResource(engine, {
|
||||
valueSatoshis: 10000,
|
||||
reservedBy: "inv-456",
|
||||
});
|
||||
|
||||
const { io, spies } = createMockIO();
|
||||
const result = await handleResourceCommand(createCommandDeps(app, io), ["unreserve-all"], {});
|
||||
const result = await handleResourceCommand(
|
||||
createCommandDeps(app, io),
|
||||
["unreserve-all"],
|
||||
{},
|
||||
);
|
||||
|
||||
expect(result.count).toBe(2);
|
||||
expectLogs(spies, [{ out: "Unreserved" }, { out: "2" }]);
|
||||
|
||||
const resources = await engine.listUnspentOutputsData();
|
||||
const reserved = resources.filter(r => r.reservedBy);
|
||||
const reserved = resources.filter((r) => r.reservedBy);
|
||||
expect(reserved).toHaveLength(0);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user