forked from Dshield-xyz/Dshield
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreport.test.ts
More file actions
129 lines (119 loc) · 3.71 KB
/
Copy pathreport.test.ts
File metadata and controls
129 lines (119 loc) · 3.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import { describe, it, expect } from "vitest";
import {
formatReportText,
formatActivityCsv,
formatActivityJson,
type ComplianceReport,
type ActivityItem,
} from "./report";
const base: ComplianceReport = {
network: "Testnet",
poolId: "CABC",
commitment: "0xdeadbeef",
nullifierHash: "0x00aa",
integrityOk: true,
depositConfirmed: true,
leafIndex: 9,
withdrawn: true,
depositTx: { hash: "abc123", at: "2026-06-26T00:00:00Z" },
withdrawTx: { hash: "def456", at: "2026-06-26T01:00:00Z" },
generatedAt: 0,
};
describe("formatReportText", () => {
it("includes the key on-chain facts", () => {
const t = formatReportText(base);
expect(t).toContain("DShield Compliance Report");
expect(t).toContain("leaf #9");
expect(t).toContain("Withdrawn");
expect(t).toContain(base.commitment);
expect(t).toContain("abc123");
});
it("never leaks an amount, address, or the spendable note", () => {
const t = formatReportText(base).toLowerCase();
expect(t).not.toContain("usdc");
expect(t).not.toContain("amount");
// The exported report must not carry the bearer-spendable note secret
// (serialized notes are prefixed "dshield-v1-") — sharing the report
// with a third party (e.g. an auditor) would otherwise let them drain
// the note.
expect(t).not.toContain("dshield-v1-");
});
it("renders the unconfirmed / unspent / no-tx case", () => {
const t = formatReportText({
...base,
depositConfirmed: false,
leafIndex: null,
withdrawn: false,
depositTx: null,
withdrawTx: null,
});
expect(t).toContain("Not found on-chain");
expect(t).toContain("In pool (unspent)");
expect(t).toContain("Deposit tx n/a");
});
});
const activity: ActivityItem[] = [
{
type: "deposit",
timestamp: 1750000000000,
commitment: "0xdeposit1",
amount: "1000000000",
poolId: "CPOOL1",
},
{
type: "withdrawal",
timestamp: 1750000001000,
commitment: "0xdeposit1",
amount: "1000000000",
poolId: "CPOOL1",
},
{
type: "compliance",
timestamp: 1750000002000,
commitment: "0xkychash, with a comma",
amount: "0",
},
];
describe("formatActivityCsv", () => {
it("emits a header row followed by one row per item", () => {
const csv = formatActivityCsv(activity);
const lines = csv.split("\n");
expect(lines[0]).toBe(
"type,date,amount_usdc,amount_stroops,commitment,pool_id",
);
expect(lines).toHaveLength(4);
expect(lines[1]).toBe(
`deposit,${new Date(1750000000000).toISOString()},100,1000000000,0xdeposit1,CPOOL1`,
);
});
it("leaves amount and pool_id blank for compliance rows", () => {
const csv = formatActivityCsv(activity);
const complianceLine = csv.split("\n")[3];
expect(complianceLine.startsWith("compliance,")).toBe(true);
expect(complianceLine).toContain(",,,");
});
it("quotes and escapes fields containing commas", () => {
const csv = formatActivityCsv(activity);
expect(csv).toContain('"0xkychash, with a comma"');
});
});
describe("formatActivityJson", () => {
it("round-trips as valid JSON with one object per item", () => {
const parsed = JSON.parse(formatActivityJson(activity));
expect(parsed).toHaveLength(3);
expect(parsed[0]).toMatchObject({
type: "deposit",
amountUsdc: "100",
amountStroops: "1000000000",
commitment: "0xdeposit1",
poolId: "CPOOL1",
});
});
it("nulls amount and poolId for compliance rows", () => {
const parsed = JSON.parse(formatActivityJson(activity));
const compliance = parsed[2];
expect(compliance.amountUsdc).toBeNull();
expect(compliance.amountStroops).toBeNull();
expect(compliance.poolId).toBeNull();
});
});