-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathengine.test.ts
More file actions
143 lines (129 loc) · 4.53 KB
/
Copy pathengine.test.ts
File metadata and controls
143 lines (129 loc) · 4.53 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { describe, it, expect, beforeEach } from "vitest";
import * as fs from "fs";
import * as path from "path";
import * as os from "os";
import { reconcileC2B, C2BConfirmation } from "./engine";
import { getDb, closeDb } from "../db/client";
import { nanoid } from "nanoid";
function makeC2B(overrides: Partial<C2BConfirmation> = {}): C2BConfirmation {
return {
TransactionType: "Pay Bill",
TransID: `TX${nanoid(6).toUpperCase()}`,
TransTime: "20260420120000",
TransAmount: "500.00",
BusinessShortCode: "174379",
BillRefNumber: "ACME-202604",
MSISDN: "254711223344",
FirstName: "Test",
...overrides,
};
}
function setupSchema() {
const db = getDb();
const sql = fs.readFileSync(
path.resolve(__dirname, "../db/schema.sql"),
"utf8"
);
db.exec(sql);
}
function seedOneChama() {
const db = getDb();
const chamaId = nanoid();
db.prepare(
`INSERT INTO chamas (id, code, name, paybill, account_ref_prefix, contribution_cents) VALUES (?, 'ACME', 'Acme', '174379', 'ACME', 50000)`
).run(chamaId);
const alice = nanoid();
db.prepare(
`INSERT INTO users (id, msisdn, full_name) VALUES (?, '+254711223344', 'Alice')`
).run(alice);
db.prepare(
`INSERT INTO memberships (id, chama_id, user_id) VALUES (?, ?, ?)`
).run(nanoid(), chamaId, alice);
const bob = nanoid();
db.prepare(
`INSERT INTO users (id, msisdn, full_name) VALUES (?, '+254722334455', 'Bob')`
).run(bob);
db.prepare(
`INSERT INTO memberships (id, chama_id, user_id) VALUES (?, ?, ?)`
).run(nanoid(), chamaId, bob);
// Open cycle 2026-04
db.prepare(
`INSERT INTO contribution_cycles (id, chama_id, period, opens_at, closes_at, expected_cents) VALUES (?, ?, '2026-04', '2026-04-01T00:00:00Z', '2026-12-31T23:59:59Z', 50000)`
).run(nanoid(), chamaId);
return { chamaId, alice, bob };
}
describe("reconcileC2B", () => {
beforeEach(() => {
const tmp = path.join(
os.tmpdir(),
`chamapay-test-${nanoid(6)}.sqlite`
);
process.env.DATABASE_URL = `file:${tmp}`;
closeDb();
setupSchema();
});
it("matches exact account-ref + msisdn with confidence 1.0", () => {
const { chamaId, alice } = seedOneChama();
const payload = makeC2B({
MSISDN: "254711223344",
BillRefNumber: "ACME-202604",
});
const r = reconcileC2B(payload, JSON.stringify(payload));
expect(r.status).toBe("matched");
expect(r.chamaId).toBe(chamaId);
expect(r.userId).toBe(alice);
expect(r.confidence).toBe(1.0);
});
it("is idempotent — same TransID twice returns duplicate", () => {
seedOneChama();
const p = makeC2B();
const a = reconcileC2B(p, JSON.stringify(p));
const b = reconcileC2B(p, JSON.stringify(p));
expect(a.status).toBe("matched");
expect(b.status).toBe("duplicate");
expect(b.paymentId).toBe(a.paymentId);
});
it("matches by MSISDN alone when account-ref is missing", () => {
const { chamaId, alice } = seedOneChama();
const p = makeC2B({ BillRefNumber: undefined, MSISDN: "0711223344" });
const r = reconcileC2B(p, JSON.stringify(p));
expect(r.status).toBe("matched");
expect(r.chamaId).toBe(chamaId);
expect(r.userId).toBe(alice);
expect(r.confidence).toBeGreaterThanOrEqual(0.9);
});
it("marks unmatched when MSISDN belongs to nobody", () => {
seedOneChama();
const p = makeC2B({ MSISDN: "254799999999", BillRefNumber: "XYZ-202604" });
const r = reconcileC2B(p, JSON.stringify(p));
expect(r.status).toBe("unmatched");
expect(r.userId).toBeNull();
});
it("writes balanced double-entry ledger rows on match", () => {
const { chamaId } = seedOneChama();
const p = makeC2B();
reconcileC2B(p, JSON.stringify(p));
const rows = getDb()
.prepare(
`SELECT direction, amount_cents FROM ledger_entries WHERE chama_id = ?`
)
.all(chamaId) as any[];
expect(rows).toHaveLength(2);
const debits = rows
.filter((r) => r.direction === "debit")
.reduce((a, r) => a + r.amount_cents, 0);
const credits = rows
.filter((r) => r.direction === "credit")
.reduce((a, r) => a + r.amount_cents, 0);
expect(debits).toBe(credits);
expect(debits).toBe(50000);
});
it("accepts period in 202604, 2026-04, and 2026-W17 formats without crashing", () => {
seedOneChama();
for (const period of ["202604", "2026-04", "2026-W17-ALICE"]) {
const p = makeC2B({ BillRefNumber: `ACME-${period}` });
const r = reconcileC2B(p, JSON.stringify(p));
expect(r.status === "matched" || r.status === "unmatched").toBe(true);
}
});
});