mirror of
https://github.com/Zproger/Aurum.git
synced 2026-11-18 08:02:49 +07:00
test: юнит-тесты на форматирование и разбивки крипто-портфеля + версия 1.1.6
Vitest для чистой логики: lib/format.ts (заодно поймал баг в trimTrailingZeros на "-0.000") и расчёт разбивки по риску/монете/сети на вкладке Crypto — вынес их из компонентов в отдельные тестируемые функции. Тесты подключены в CI. Версия приложения обновлена до 1.1.6.
This commit is contained in:
40
frontend/src/components/crypto/CryptoAllocationBody.test.ts
Normal file
40
frontend/src/components/crypto/CryptoAllocationBody.test.ts
Normal file
@@ -0,0 +1,40 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { makeHolding } from "@/test/cryptoFixtures";
|
||||
import { buildSlices } from "./CryptoAllocationBody";
|
||||
|
||||
describe("buildSlices (per-coin allocation)", () => {
|
||||
it("keys each slice by asset_id, one per holding", () => {
|
||||
const holdings = [makeHolding({ asset_id: 7, name: "Bitcoin", symbol: "BTC", value: "100" })];
|
||||
const slices = buildSlices(holdings);
|
||||
expect(slices).toEqual([{ key: "7", name: "Bitcoin", symbol: "BTC", amount: 100, color: "var(--series-1)" }]);
|
||||
});
|
||||
|
||||
it("excludes unpriced and exactly-zero holdings", () => {
|
||||
const holdings = [makeHolding({ value: null }), makeHolding({ value: "0" })];
|
||||
expect(buildSlices(holdings)).toEqual([]);
|
||||
});
|
||||
|
||||
it("sorts slices by amount descending", () => {
|
||||
const holdings = [
|
||||
makeHolding({ symbol: "SMALL", value: "10" }),
|
||||
makeHolding({ symbol: "BIG", value: "90" }),
|
||||
];
|
||||
expect(buildSlices(holdings).map((s) => s.symbol)).toEqual(["BIG", "SMALL"]);
|
||||
});
|
||||
|
||||
it("folds every coin past the top 7 into one 'Other' slice", () => {
|
||||
const holdings = Array.from({ length: 9 }, (_, i) => makeHolding({ symbol: `C${i}`, value: String(9 - i) }));
|
||||
const slices = buildSlices(holdings);
|
||||
expect(slices).toHaveLength(8);
|
||||
const other = slices.find((s) => s.key === "other")!;
|
||||
expect(other.name).toBe("Other");
|
||||
expect(other.symbol).toBeNull();
|
||||
// The two smallest holdings: value 2 and value 1.
|
||||
expect(other.amount).toBe(3);
|
||||
expect(other.color).toBe("var(--series-other)");
|
||||
});
|
||||
|
||||
it("returns nothing at all when every holding is unpriced", () => {
|
||||
expect(buildSlices([makeHolding({ value: null })])).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -26,7 +26,7 @@ const SERIES_COLORS = [
|
||||
const OTHER_COLOR = "var(--series-other)";
|
||||
const MAX_SLICES = SERIES_COLORS.length;
|
||||
|
||||
interface Slice {
|
||||
export interface Slice {
|
||||
key: string;
|
||||
name: string;
|
||||
symbol: string | null;
|
||||
@@ -34,7 +34,9 @@ interface Slice {
|
||||
color: string;
|
||||
}
|
||||
|
||||
function buildSlices(holdings: CryptoHolding[]): Slice[] {
|
||||
// Exported for direct unit testing (see CryptoAllocationBody.test.ts) —
|
||||
// the donut/table JSX below isn't worth rendering just to check the math.
|
||||
export function buildSlices(holdings: CryptoHolding[]): Slice[] {
|
||||
const priced = holdings
|
||||
.filter((h) => h.value !== null && Number(h.value) > 0)
|
||||
.map((h) => ({ key: String(h.asset_id), name: h.name, symbol: h.symbol, amount: Number(h.value) }))
|
||||
|
||||
@@ -0,0 +1,59 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { makeHolding } from "@/test/cryptoFixtures";
|
||||
import { buildSlices } from "./CryptoNetworkAllocationBody";
|
||||
|
||||
const UNSET = "No network set";
|
||||
|
||||
describe("buildSlices (network allocation)", () => {
|
||||
it("groups holdings by network, summing their value", () => {
|
||||
const holdings = [
|
||||
makeHolding({ network: "Ethereum", value: "100" }),
|
||||
makeHolding({ network: "Ethereum", value: "50" }),
|
||||
makeHolding({ network: "Tron", value: "30" }),
|
||||
];
|
||||
const slices = buildSlices(holdings, UNSET);
|
||||
const byLabel = Object.fromEntries(slices.map((s) => [s.label, s.amount]));
|
||||
expect(byLabel["Ethereum"]).toBe(150);
|
||||
expect(byLabel["Tron"]).toBe(30);
|
||||
});
|
||||
|
||||
it("folds holdings with no network into one unset bucket instead of dropping them", () => {
|
||||
const holdings = [makeHolding({ network: null, value: "40" }), makeHolding({ network: null, value: "10" })];
|
||||
const slices = buildSlices(holdings, UNSET);
|
||||
expect(slices).toHaveLength(1);
|
||||
expect(slices[0].label).toBe(UNSET);
|
||||
expect(slices[0].amount).toBe(50);
|
||||
});
|
||||
|
||||
it("excludes unpriced holdings entirely", () => {
|
||||
const holdings = [makeHolding({ network: "Ethereum", value: null })];
|
||||
expect(buildSlices(holdings, UNSET)).toEqual([]);
|
||||
});
|
||||
|
||||
it("sorts slices by amount descending", () => {
|
||||
const holdings = [
|
||||
makeHolding({ network: "Small", value: "10" }),
|
||||
makeHolding({ network: "Big", value: "90" }),
|
||||
];
|
||||
const slices = buildSlices(holdings, UNSET);
|
||||
expect(slices.map((s) => s.label)).toEqual(["Big", "Small"]);
|
||||
});
|
||||
|
||||
it("assigns each of the first 7 slices its own distinct color", () => {
|
||||
const holdings = Array.from({ length: 7 }, (_, i) => makeHolding({ network: `Net${i}`, value: String(10 - i) }));
|
||||
const slices = buildSlices(holdings, UNSET);
|
||||
expect(slices).toHaveLength(7);
|
||||
expect(new Set(slices.map((s) => s.color)).size).toBe(7);
|
||||
});
|
||||
|
||||
it("folds every network past the top 7 into one '__other__' slice", () => {
|
||||
// 9 distinct networks — 7 keep their own slice, 2 fold into "other".
|
||||
const holdings = Array.from({ length: 9 }, (_, i) => makeHolding({ network: `Net${i}`, value: String(9 - i) }));
|
||||
const slices = buildSlices(holdings, UNSET);
|
||||
expect(slices).toHaveLength(8); // 7 top + 1 "other"
|
||||
const other = slices.find((s) => s.key === "__other__")!;
|
||||
expect(other).toBeDefined();
|
||||
// The two smallest: Net7 (value 2) and Net8 (value 1).
|
||||
expect(other.amount).toBe(3);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,83 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { makeHolding } from "@/test/cryptoFixtures";
|
||||
import { computeRiskTiers } from "./CryptoRiskAllocationBody";
|
||||
|
||||
describe("computeRiskTiers", () => {
|
||||
it("always returns exactly the three tiers, in low/medium/high order", () => {
|
||||
const tiers = computeRiskTiers([]);
|
||||
expect(tiers.map((t) => t.level)).toEqual(["low", "medium", "high"]);
|
||||
});
|
||||
|
||||
it("splits total portfolio value by risk_level", () => {
|
||||
const holdings = [
|
||||
makeHolding({ symbol: "BTC", risk_level: "low", value: "600" }),
|
||||
makeHolding({ symbol: "ETH", risk_level: "medium", value: "300" }),
|
||||
makeHolding({ symbol: "SHIB", risk_level: "high", value: "100" }),
|
||||
];
|
||||
const tiers = computeRiskTiers(holdings);
|
||||
const byLevel = Object.fromEntries(tiers.map((t) => [t.level, t]));
|
||||
|
||||
expect(byLevel.low.value).toBe(600);
|
||||
expect(byLevel.low.percent).toBeCloseTo(60);
|
||||
expect(byLevel.medium.percent).toBeCloseTo(30);
|
||||
expect(byLevel.high.percent).toBeCloseTo(10);
|
||||
});
|
||||
|
||||
it("sums multiple holdings in the same tier instead of overwriting", () => {
|
||||
const holdings = [
|
||||
makeHolding({ symbol: "SHIB", risk_level: "high", value: "100" }),
|
||||
makeHolding({ symbol: "PEPE", risk_level: "high", value: "50" }),
|
||||
];
|
||||
const high = computeRiskTiers(holdings).find((t) => t.level === "high")!;
|
||||
expect(high.value).toBe(150);
|
||||
expect(high.items).toHaveLength(2);
|
||||
});
|
||||
|
||||
it("excludes unpriced holdings from every percentage, not just their own tier", () => {
|
||||
const holdings = [
|
||||
makeHolding({ symbol: "BTC", risk_level: "low", value: "100" }),
|
||||
makeHolding({ symbol: "UNPRICED", risk_level: "low", value: null }),
|
||||
];
|
||||
const low = computeRiskTiers(holdings).find((t) => t.level === "low")!;
|
||||
expect(low.value).toBe(100);
|
||||
expect(low.percent).toBe(100);
|
||||
expect(low.items).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("sorts a tier's items by value descending", () => {
|
||||
const holdings = [
|
||||
makeHolding({ symbol: "SMALL", risk_level: "high", value: "10" }),
|
||||
makeHolding({ symbol: "BIG", risk_level: "high", value: "90" }),
|
||||
];
|
||||
const high = computeRiskTiers(holdings).find((t) => t.level === "high")!;
|
||||
expect(high.items.map((i) => i.symbol)).toEqual(["BIG", "SMALL"]);
|
||||
});
|
||||
|
||||
it("computes each item's percent of its own tier, not of the whole portfolio", () => {
|
||||
const holdings = [
|
||||
makeHolding({ symbol: "BIG", risk_level: "high", value: "90" }),
|
||||
makeHolding({ symbol: "SMALL", risk_level: "high", value: "10" }),
|
||||
makeHolding({ symbol: "OTHER_TIER", risk_level: "low", value: "900" }),
|
||||
];
|
||||
const high = computeRiskTiers(holdings).find((t) => t.level === "high")!;
|
||||
// 90/100 and 10/100 of the *high tier's* 100, not of the 1000 total.
|
||||
expect(high.items.find((i) => i.symbol === "BIG")!.percent).toBeCloseTo(90);
|
||||
expect(high.items.find((i) => i.symbol === "SMALL")!.percent).toBeCloseTo(10);
|
||||
});
|
||||
|
||||
it("leaves an empty tier at 0%, not NaN, when nothing is held in it", () => {
|
||||
const holdings = [makeHolding({ risk_level: "high", value: "100" })];
|
||||
const low = computeRiskTiers(holdings).find((t) => t.level === "low")!;
|
||||
expect(low.value).toBe(0);
|
||||
expect(low.percent).toBe(0);
|
||||
expect(low.items).toEqual([]);
|
||||
});
|
||||
|
||||
it("leaves every tier at 0% when nothing is priced at all, instead of dividing by zero", () => {
|
||||
const holdings = [makeHolding({ risk_level: "high", value: null })];
|
||||
for (const tier of computeRiskTiers(holdings)) {
|
||||
expect(tier.percent).toBe(0);
|
||||
expect(Number.isNaN(tier.percent)).toBe(false);
|
||||
}
|
||||
});
|
||||
});
|
||||
@@ -8,16 +8,60 @@ interface CryptoRiskAllocationBodyProps {
|
||||
hidden: boolean;
|
||||
}
|
||||
|
||||
// Same status-color convention as NetWorthPage's own RiskAllocationCard
|
||||
// (low reads as "good", high as "critical") — not a categorical hue, so
|
||||
// hardcoded here rather than threaded through from the backend.
|
||||
// Same traffic-light colors as the holdings table's own RiskLevelPicker
|
||||
// (green/orange/red) — kept in sync so a coin's risk reads identically in
|
||||
// both places on the Crypto tab. --series-4 is the dataviz skill's
|
||||
// validated yellow/orange slot.
|
||||
const TIER_COLOR: Record<RiskLevel, string> = {
|
||||
low: "var(--success)",
|
||||
medium: "var(--text-muted)",
|
||||
medium: "var(--series-4)",
|
||||
high: "var(--danger)",
|
||||
};
|
||||
const RISK_LEVELS: RiskLevel[] = ["low", "medium", "high"];
|
||||
|
||||
export interface RiskTierItem {
|
||||
symbol: string;
|
||||
name: string;
|
||||
value: number;
|
||||
percent: number;
|
||||
}
|
||||
|
||||
export interface RiskTier {
|
||||
level: RiskLevel;
|
||||
value: number;
|
||||
percent: number;
|
||||
items: RiskTierItem[];
|
||||
}
|
||||
|
||||
/** Groups priced holdings into the three RiskLevel tiers with each tier's
|
||||
* share of total portfolio value, and each item's share within its own
|
||||
* tier — pulled out of the component so it's testable without rendering
|
||||
* (see format.test.ts-style coverage in CryptoRiskAllocationBody.test.ts). */
|
||||
export function computeRiskTiers(holdings: CryptoHolding[]): RiskTier[] {
|
||||
// Unpriced holdings (CoinGecko never successfully priced them yet) can't
|
||||
// contribute a % of portfolio value — same "best data on hand" principle
|
||||
// the rest of the Crypto tab already follows for a null value/price.
|
||||
const priced = holdings.filter((h) => h.value !== null);
|
||||
const totalValue = priced.reduce((sum, h) => sum + Number(h.value), 0);
|
||||
|
||||
return RISK_LEVELS.map((level) => {
|
||||
const items = priced
|
||||
.filter((h) => h.risk_level === level)
|
||||
.map((h) => ({ symbol: h.symbol, name: h.name, value: Number(h.value) }))
|
||||
.sort((a, b) => b.value - a.value);
|
||||
const tierValue = items.reduce((sum, item) => sum + item.value, 0);
|
||||
return {
|
||||
level,
|
||||
value: tierValue,
|
||||
percent: totalValue > 0 ? (tierValue / totalValue) * 100 : 0,
|
||||
items: items.map((item) => ({
|
||||
...item,
|
||||
percent: tierValue > 0 ? (item.value / tierValue) * 100 : 0,
|
||||
})),
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/** The "Risk levels" tab's content inside CryptoOverviewCard — same
|
||||
* three-tier layout as NetWorthPage's own RiskAllocationCard, but scoped to
|
||||
* crypto holdings only (not the whole net worth), so "am I overweight on
|
||||
@@ -34,28 +78,7 @@ export function CryptoRiskAllocationBody({ holdings, isLoading, hidden }: Crypto
|
||||
return <p className="py-10 text-center text-sm text-text-muted">{t("crypto.empty")}</p>;
|
||||
}
|
||||
|
||||
// Unpriced holdings (CoinGecko never successfully priced them yet) can't
|
||||
// contribute a % of portfolio value — same "best data on hand" principle
|
||||
// the rest of the Crypto tab already follows for a null value/price.
|
||||
const priced = holdings.filter((h) => h.value !== null);
|
||||
const totalValue = priced.reduce((sum, h) => sum + Number(h.value), 0);
|
||||
|
||||
const tiers = RISK_LEVELS.map((level) => {
|
||||
const items = priced
|
||||
.filter((h) => h.risk_level === level)
|
||||
.map((h) => ({ symbol: h.symbol, name: h.name, value: Number(h.value) }))
|
||||
.sort((a, b) => b.value - a.value);
|
||||
const tierValue = items.reduce((sum, item) => sum + item.value, 0);
|
||||
return {
|
||||
level,
|
||||
value: tierValue,
|
||||
percent: totalValue > 0 ? (tierValue / totalValue) * 100 : 0,
|
||||
items: items.map((item) => ({
|
||||
...item,
|
||||
percent: tierValue > 0 ? (item.value / tierValue) * 100 : 0,
|
||||
})),
|
||||
};
|
||||
});
|
||||
const tiers = computeRiskTiers(holdings);
|
||||
|
||||
return (
|
||||
<div className="grid grid-cols-1 gap-3 sm:grid-cols-3">
|
||||
|
||||
Reference in New Issue
Block a user