Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions src/preflight/InvoiceCloneabilityValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,12 +179,19 @@ export class InvoiceCloneabilityValidator {
const deadlineMs = invoice.deadline * 1_000;
const minFutureMs = nowMs + this._options.minDeadlineBufferMs;

if (deadlineMs <= minFutureMs) {
if (deadlineMs <= nowMs) {
reports.push({
field: "deadline",
valid: false,
reason: `Deadline (${new Date(deadlineMs).toISOString()}) is already expired as of the current ledger time (${new Date(nowMs).toISOString()}).`,
suggestedFix: "Create a new invoice or pass a newDeadline override that is set after the current ledger time.",
});
} else if (deadlineMs <= minFutureMs) {
const shortfallSec = Math.ceil((minFutureMs - deadlineMs) / 1_000);
reports.push({
field: "deadline",
valid: false,
reason: `Deadline (${new Date(deadlineMs).toISOString()}) is in the past or too close to now (buffer: ${this._options.minDeadlineBufferMs}ms). Shortfall: ${shortfallSec}s.`,
reason: `Deadline (${new Date(deadlineMs).toISOString()}) is too close to now (buffer: ${this._options.minDeadlineBufferMs}ms). Shortfall: ${shortfallSec}s.`,
suggestedFix: `Pass a \`newDeadline\` override that is at least ${this._options.minDeadlineBufferMs / 1_000}s in the future when calling cloneInvoice().`,
});
}
Expand Down
13 changes: 11 additions & 2 deletions test/invoiceCloneabilityValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,15 @@ describe("InvoiceCloneabilityValidator — status check", () => {
const statusField = report.fieldReports.find((f) => f.field === "status");
expect(statusField).toBeUndefined();
});

it("blocks cloning an already expired invoice", async () => {
const invoice = makeInvoice({ deadline: PAST_DEADLINE });
const validator = new InvoiceCloneabilityValidator({ rpcUrl: "https://rpc.example.com" });
const report = await validator.validate(invoice);

expect(report.cloneable).toBe(false);
expect(report.fieldReports.some((field) => field.field === "deadline")).toBe(true);
});
});

describe("InvoiceCloneabilityValidator — deadline check", () => {
Expand All @@ -128,8 +137,8 @@ describe("InvoiceCloneabilityValidator — deadline check", () => {
const deadlineField = report.fieldReports.find((f) => f.field === "deadline");
expect(deadlineField).toBeDefined();
expect(deadlineField!.valid).toBe(false);
expect(deadlineField!.reason).toMatch(/past|buffer/i);
expect(deadlineField!.suggestedFix).toMatch(/newDeadline/);
expect(deadlineField!.reason).toMatch(/expired|past|buffer/i);
expect(deadlineField!.suggestedFix).toMatch(/newDeadline|new invoice/i);
});

it("passes when deadline is sufficiently in the future", async () => {
Expand Down
31 changes: 30 additions & 1 deletion test/roundingAuditor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@

import { describe, it, expect } from "vitest";
import { auditSplitRounding, RoundingOverflowError } from "../src/invoice/rounding.js";
import { calculateSplitAmounts, computeAmounts } from "../src/invoice/calculator.js";
import {
calculateInvoiceBreakdown,
calculateInvoiceSubtotal,
calculateSplitAmounts,
computeAmounts,
} from "../src/invoice/calculator.js";
import type { SplitLine } from "../src/types.js";

// ---------------------------------------------------------------------------
Expand Down Expand Up @@ -393,3 +398,27 @@ describe("calculateSplitAmounts (calculator integration)", () => {
expect(() => calculateSplitAmounts(1000n, splits)).toThrow(RoundingOverflowError);
});
});

describe("invoice subtotal and fee breakdown", () => {
it("calculates the subtotal before fees are applied", () => {
expect(calculateInvoiceSubtotal([10n, 15n, 25n, 5n])).toBe(55n);
});

it("returns subtotal, fee and total separately for downstream receipts", () => {
const result = calculateInvoiceBreakdown(1_000n, 250);

expect(result.subtotal).toBe(1_000n);
expect(result.fee).toBe(25n);
expect(result.total).toBe(1_025n);
});

it("supports configurable rounding mode", () => {
const result = auditSplitRounding(1n, [
{ recipientId: "A", ratio: 0.5 },
{ recipientId: "B", ratio: 0.5 },
], { mode: "bankers" });

expect(sumAmounts(result.amounts)).toBe(1n);
expect(Object.values(result.amounts).sort((a, b) => Number(a - b))).toEqual([0n, 1n]);
});
});