Formatting
This commit is contained in:
@@ -11,18 +11,40 @@
|
||||
*/
|
||||
|
||||
import { expect, test, describe, beforeEach, afterEach } from "vitest";
|
||||
import { existsSync, mkdtempSync, readFileSync, readdirSync, rmSync, writeFileSync } from "node:fs";
|
||||
import {
|
||||
existsSync,
|
||||
mkdtempSync,
|
||||
readFileSync,
|
||||
readdirSync,
|
||||
rmSync,
|
||||
writeFileSync,
|
||||
} from "node:fs";
|
||||
import { tmpdir } from "node:os";
|
||||
import path from "node:path";
|
||||
|
||||
import { addFakeResource, createMockAppService, createMockEngine, DEFAULT_SEED, randomTxHash } from "../mocks/engine";
|
||||
import {
|
||||
addFakeResource,
|
||||
createMockAppService,
|
||||
createMockEngine,
|
||||
DEFAULT_SEED,
|
||||
randomTxHash,
|
||||
} from "../mocks/engine";
|
||||
import { type Engine } from "@xo-cash/engine";
|
||||
import { p2pkhTemplate, p2pkhTemplateIdentifier } from "../mocks/template-p2pkh";
|
||||
import {
|
||||
p2pkhTemplate,
|
||||
p2pkhTemplateIdentifier,
|
||||
} from "../mocks/template-p2pkh";
|
||||
import { AppService } from "../../../src/services/app";
|
||||
|
||||
import { handleInvitationCommand } from "../../../src/cli/commands/invitation";
|
||||
import { CommandError, CommandPaths } from "../../../src/cli/commands/types";
|
||||
import { createCommandDeps, createMockIO, createMockPaths, expectLogs, type LogExpectation } from "../mocks/command";
|
||||
import {
|
||||
createCommandDeps,
|
||||
createMockIO,
|
||||
createMockPaths,
|
||||
expectLogs,
|
||||
type LogExpectation,
|
||||
} from "../mocks/command";
|
||||
|
||||
// ============================================================================
|
||||
// Error Cases - Validate argument parsing and error handling
|
||||
@@ -150,7 +172,11 @@ describe("invitation command - error cases", () => {
|
||||
const { io } = createMockIO();
|
||||
|
||||
try {
|
||||
await handleInvitationCommand(createCommandDeps(app, io, paths), inputs, {});
|
||||
await handleInvitationCommand(
|
||||
createCommandDeps(app, io, paths),
|
||||
inputs,
|
||||
{},
|
||||
);
|
||||
expect.fail("Expected command to throw");
|
||||
} catch (error) {
|
||||
expect(error).toBeInstanceOf(CommandError);
|
||||
@@ -211,7 +237,10 @@ describe("invitation command - receive flow", () => {
|
||||
{},
|
||||
);
|
||||
|
||||
const expectedFile = path.join(tempDir, `inv-${result.invitationIdentifier}.json`);
|
||||
const expectedFile = path.join(
|
||||
tempDir,
|
||||
`inv-${result.invitationIdentifier}.json`,
|
||||
);
|
||||
expect(existsSync(expectedFile)).toBe(true);
|
||||
});
|
||||
|
||||
@@ -325,8 +354,12 @@ describe("invitation command - request satoshis flow", () => {
|
||||
);
|
||||
expect(invitation).toBeDefined();
|
||||
|
||||
const variables = invitation?.data.commits.flatMap((c) => c.data.variables ?? []);
|
||||
const requestedSatoshis = variables?.find((v) => v.variableIdentifier === "requestedSatoshis");
|
||||
const variables = invitation?.data.commits.flatMap(
|
||||
(c) => c.data.variables ?? [],
|
||||
);
|
||||
const requestedSatoshis = variables?.find(
|
||||
(v) => v.variableIdentifier === "requestedSatoshis",
|
||||
);
|
||||
expect(requestedSatoshis).toBeDefined();
|
||||
expect(requestedSatoshis?.value).toBe("10000");
|
||||
});
|
||||
@@ -347,8 +380,12 @@ describe("invitation command - request satoshis flow", () => {
|
||||
(inv) => inv.data.invitationIdentifier === result.invitationIdentifier,
|
||||
);
|
||||
|
||||
const variables = invitation?.data.commits.flatMap((c) => c.data.variables ?? []);
|
||||
const requestedSatoshis = variables?.find((v) => v.variableIdentifier === "requestedSatoshis");
|
||||
const variables = invitation?.data.commits.flatMap(
|
||||
(c) => c.data.variables ?? [],
|
||||
);
|
||||
const requestedSatoshis = variables?.find(
|
||||
(v) => v.variableIdentifier === "requestedSatoshis",
|
||||
);
|
||||
expect(requestedSatoshis?.roleIdentifier).toBe("receiver");
|
||||
});
|
||||
});
|
||||
@@ -388,7 +425,8 @@ describe("invitation command - send flow with resources", () => {
|
||||
["create", "Wallet (P2PKH)", "sendSatoshis"],
|
||||
{
|
||||
varTransferredSatoshis: "10000",
|
||||
varRecipientLockingscript: "76a91489abcdefabbaabbaabbaabbaabbaabbaabbaabba88ac",
|
||||
varRecipientLockingscript:
|
||||
"76a91489abcdefabbaabbaabbaabbaabbaabbaabbaabba88ac",
|
||||
role: "sender",
|
||||
},
|
||||
);
|
||||
@@ -408,7 +446,8 @@ describe("invitation command - send flow with resources", () => {
|
||||
["create", "Wallet (P2PKH)", "sendSatoshis"],
|
||||
{
|
||||
varTransferredSatoshis: "10000",
|
||||
varRecipientLockingscript: "76a91489abcdefabbaabbaabbaabbaabbaabbaabbaabba88ac",
|
||||
varRecipientLockingscript:
|
||||
"76a91489abcdefabbaabbaabbaabbaabbaabbaabbaabba88ac",
|
||||
role: "sender",
|
||||
},
|
||||
);
|
||||
@@ -418,8 +457,12 @@ describe("invitation command - send flow with resources", () => {
|
||||
);
|
||||
expect(invitation).toBeDefined();
|
||||
|
||||
const variables = invitation?.data.commits.flatMap((c) => c.data.variables ?? []);
|
||||
const transferredSatoshis = variables?.find((v) => v.variableIdentifier === "transferredSatoshis");
|
||||
const variables = invitation?.data.commits.flatMap(
|
||||
(c) => c.data.variables ?? [],
|
||||
);
|
||||
const transferredSatoshis = variables?.find(
|
||||
(v) => v.variableIdentifier === "transferredSatoshis",
|
||||
);
|
||||
expect(transferredSatoshis).toBeDefined();
|
||||
expect(transferredSatoshis?.value).toBe("10000");
|
||||
});
|
||||
@@ -436,8 +479,10 @@ describe("invitation command - send flow with resources", () => {
|
||||
["create", "Wallet (P2PKH)", "sendSatoshis"],
|
||||
{
|
||||
varTransferredSatoshis: "10000",
|
||||
varRecipientLockingscript: "76a91489abcdefabbaabbaabbaabbaabbaabbaabbaabba88ac",
|
||||
addInput: "0000000000000000000000000000000000000000000000000000000000000000:0",
|
||||
varRecipientLockingscript:
|
||||
"76a91489abcdefabbaabbaabbaabbaabbaabbaabbaabba88ac",
|
||||
addInput:
|
||||
"0000000000000000000000000000000000000000000000000000000000000000:0",
|
||||
role: "sender",
|
||||
},
|
||||
);
|
||||
@@ -516,10 +561,15 @@ describe("invitation command - multi-step append", () => {
|
||||
expectLogs(spies, [{ out: "Invitation appended" }]);
|
||||
|
||||
const invitation = app.invitations.find(
|
||||
(inv) => inv.data.invitationIdentifier === createResult.invitationIdentifier,
|
||||
(inv) =>
|
||||
inv.data.invitationIdentifier === createResult.invitationIdentifier,
|
||||
);
|
||||
const variables = invitation?.data.commits.flatMap(
|
||||
(c) => c.data.variables ?? [],
|
||||
);
|
||||
const requestedSatoshis = variables?.find(
|
||||
(v) => v.variableIdentifier === "requestedSatoshis",
|
||||
);
|
||||
const variables = invitation?.data.commits.flatMap((c) => c.data.variables ?? []);
|
||||
const requestedSatoshis = variables?.find((v) => v.variableIdentifier === "requestedSatoshis");
|
||||
expect(requestedSatoshis?.value).toBe("25000");
|
||||
});
|
||||
|
||||
@@ -569,7 +619,10 @@ describe("invitation command - multi-step append", () => {
|
||||
|
||||
expectLogs(spies, [{ out: "Invitation updated" }]);
|
||||
|
||||
const expectedFile = path.join(tempDir, `inv-${createResult.invitationIdentifier}.json`);
|
||||
const expectedFile = path.join(
|
||||
tempDir,
|
||||
`inv-${createResult.invitationIdentifier}.json`,
|
||||
);
|
||||
expect(existsSync(expectedFile)).toBe(true);
|
||||
});
|
||||
|
||||
@@ -594,12 +647,17 @@ describe("invitation command - multi-step append", () => {
|
||||
);
|
||||
|
||||
const invitation = app.invitations.find(
|
||||
(inv) => inv.data.invitationIdentifier === createResult.invitationIdentifier,
|
||||
(inv) =>
|
||||
inv.data.invitationIdentifier === createResult.invitationIdentifier,
|
||||
);
|
||||
expect(invitation?.data.commits.length).toBeGreaterThan(1);
|
||||
|
||||
const variables = invitation?.data.commits.flatMap((c) => c.data.variables ?? []);
|
||||
const requestedSatoshis = variables?.find((v) => v.variableIdentifier === "requestedSatoshis");
|
||||
const variables = invitation?.data.commits.flatMap(
|
||||
(c) => c.data.variables ?? [],
|
||||
);
|
||||
const requestedSatoshis = variables?.find(
|
||||
(v) => v.variableIdentifier === "requestedSatoshis",
|
||||
);
|
||||
expect(requestedSatoshis?.value).toBe("10000");
|
||||
});
|
||||
});
|
||||
@@ -724,7 +782,10 @@ describe("invitation command - list and inspect", () => {
|
||||
{},
|
||||
);
|
||||
|
||||
const invitationFilePath = path.join(tempDir, `inv-${createResult.invitationIdentifier}.json`);
|
||||
const invitationFilePath = path.join(
|
||||
tempDir,
|
||||
`inv-${createResult.invitationIdentifier}.json`,
|
||||
);
|
||||
|
||||
const { io: inspectIO } = createMockIO();
|
||||
|
||||
@@ -813,7 +874,9 @@ describe("invitation command - sign flow", () => {
|
||||
{},
|
||||
);
|
||||
|
||||
expect(signResult.invitationIdentifier).toBe(createResult.invitationIdentifier);
|
||||
expect(signResult.invitationIdentifier).toBe(
|
||||
createResult.invitationIdentifier,
|
||||
);
|
||||
expectLogs(spies, [{ out: "Invitation signed" }]);
|
||||
});
|
||||
|
||||
@@ -840,7 +903,8 @@ describe("invitation command - sign flow", () => {
|
||||
);
|
||||
|
||||
const invitation = app.invitations.find(
|
||||
(inv) => inv.data.invitationIdentifier === createResult.invitationIdentifier,
|
||||
(inv) =>
|
||||
inv.data.invitationIdentifier === createResult.invitationIdentifier,
|
||||
);
|
||||
|
||||
expect(invitation).toBeDefined();
|
||||
@@ -921,7 +985,10 @@ describe("invitation command - import flow", () => {
|
||||
{},
|
||||
);
|
||||
|
||||
const invitationFilePath = path.join(tempDir, `inv-${createResult.invitationIdentifier}.json`);
|
||||
const invitationFilePath = path.join(
|
||||
tempDir,
|
||||
`inv-${createResult.invitationIdentifier}.json`,
|
||||
);
|
||||
|
||||
const secondApp = await createMockAppService(engine);
|
||||
const { io: importIO } = createMockIO();
|
||||
@@ -965,7 +1032,10 @@ describe("invitation command - import flow", () => {
|
||||
{},
|
||||
);
|
||||
|
||||
const invitationFilePath = path.join(tempDir, `inv-${createResult.invitationIdentifier}.json`);
|
||||
const invitationFilePath = path.join(
|
||||
tempDir,
|
||||
`inv-${createResult.invitationIdentifier}.json`,
|
||||
);
|
||||
|
||||
const secondApp = await createMockAppService(engine);
|
||||
const { io: importIO } = createMockIO();
|
||||
@@ -991,7 +1061,10 @@ describe("invitation command - import flow", () => {
|
||||
{},
|
||||
);
|
||||
|
||||
const invitationFilePath = path.join(tempDir, `inv-${createResult.invitationIdentifier}.json`);
|
||||
const invitationFilePath = path.join(
|
||||
tempDir,
|
||||
`inv-${createResult.invitationIdentifier}.json`,
|
||||
);
|
||||
|
||||
const secondApp = await createMockAppService(engine);
|
||||
const { io: importIO } = createMockIO();
|
||||
@@ -1044,7 +1117,8 @@ describe("invitation command - auto-inputs flow", () => {
|
||||
["create", "Wallet (P2PKH)", "sendSatoshis"],
|
||||
{
|
||||
varTransferredSatoshis: "10000",
|
||||
varRecipientLockingscript: "76a91489abcdefabbaabbaabbaabbaabbaabbaabbaabba88ac",
|
||||
varRecipientLockingscript:
|
||||
"76a91489abcdefabbaabbaabbaabbaabbaabbaabbaabba88ac",
|
||||
role: "sender",
|
||||
autoInputs: "true",
|
||||
},
|
||||
@@ -1052,7 +1126,9 @@ describe("invitation command - auto-inputs flow", () => {
|
||||
expect.fail("Expected command to throw");
|
||||
} catch (error) {
|
||||
expect(error).toBeInstanceOf(CommandError);
|
||||
expect((error as CommandError).event).toBe("invitation.create.append_params_failed");
|
||||
expect((error as CommandError).event).toBe(
|
||||
"invitation.create.append_params_failed",
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1068,7 +1144,8 @@ describe("invitation command - auto-inputs flow", () => {
|
||||
["create", "Wallet (P2PKH)", "sendSatoshis"],
|
||||
{
|
||||
varTransferredSatoshis: "10000",
|
||||
varRecipientLockingscript: "76a91489abcdefabbaabbaabbaabbaabbaabbaabbaabba88ac",
|
||||
varRecipientLockingscript:
|
||||
"76a91489abcdefabbaabbaabbaabbaabbaabbaabbaabba88ac",
|
||||
role: "sender",
|
||||
autoInputs: "true",
|
||||
},
|
||||
@@ -1117,7 +1194,9 @@ describe("invitation command - broadcast flow", () => {
|
||||
expect.fail("Expected command to throw");
|
||||
} catch (error) {
|
||||
expect(error).toBeInstanceOf(CommandError);
|
||||
expect((error as CommandError).event).toBe("invitation.broadcast.not_found");
|
||||
expect((error as CommandError).event).toBe(
|
||||
"invitation.broadcast.not_found",
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
@@ -1188,11 +1267,14 @@ describe("invitation command - full lifecycle", () => {
|
||||
{},
|
||||
);
|
||||
|
||||
expect(signResult.invitationIdentifier).toBe(createResult.invitationIdentifier);
|
||||
expect(signResult.invitationIdentifier).toBe(
|
||||
createResult.invitationIdentifier,
|
||||
);
|
||||
expectLogs(signSpies, [{ out: "Invitation signed" }]);
|
||||
|
||||
const invitation = app.invitations.find(
|
||||
(inv) => inv.data.invitationIdentifier === createResult.invitationIdentifier,
|
||||
(inv) =>
|
||||
inv.data.invitationIdentifier === createResult.invitationIdentifier,
|
||||
);
|
||||
expect(invitation).toBeDefined();
|
||||
expect(invitation?.data.commits.length).toBeGreaterThan(0);
|
||||
@@ -1210,7 +1292,10 @@ describe("invitation command - full lifecycle", () => {
|
||||
{},
|
||||
);
|
||||
|
||||
const invitationFilePath = path.join(tempDir, `inv-${createResult.invitationIdentifier}.json`);
|
||||
const invitationFilePath = path.join(
|
||||
tempDir,
|
||||
`inv-${createResult.invitationIdentifier}.json`,
|
||||
);
|
||||
expect(existsSync(invitationFilePath)).toBe(true);
|
||||
|
||||
const { io: inspectIO } = createMockIO();
|
||||
@@ -1254,7 +1339,9 @@ describe("invitation command - full lifecycle", () => {
|
||||
{},
|
||||
);
|
||||
|
||||
expect(signResult.invitationIdentifier).toBe(createResult.invitationIdentifier);
|
||||
expect(signResult.invitationIdentifier).toBe(
|
||||
createResult.invitationIdentifier,
|
||||
);
|
||||
expectLogs(signSpies, [{ out: "Invitation signed" }]);
|
||||
});
|
||||
|
||||
@@ -1270,7 +1357,10 @@ describe("invitation command - full lifecycle", () => {
|
||||
{},
|
||||
);
|
||||
|
||||
const invitationFilePath = path.join(tempDir, `inv-${createResult.invitationIdentifier}.json`);
|
||||
const invitationFilePath = path.join(
|
||||
tempDir,
|
||||
`inv-${createResult.invitationIdentifier}.json`,
|
||||
);
|
||||
|
||||
const afterCreate = JSON.parse(readFileSync(invitationFilePath, "utf-8"));
|
||||
const createCommitCount = afterCreate.commits?.length ?? 0;
|
||||
@@ -1335,7 +1425,9 @@ describe("invitation command - full lifecycle", () => {
|
||||
{},
|
||||
);
|
||||
|
||||
expect(reqResult.invitationIdentifier).toBe(createResult.invitationIdentifier);
|
||||
expect(reqResult.invitationIdentifier).toBe(
|
||||
createResult.invitationIdentifier,
|
||||
);
|
||||
expect(spies.out).toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user