-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathFunctionPatterns.test.ts
More file actions
130 lines (111 loc) · 4.35 KB
/
Copy pathFunctionPatterns.test.ts
File metadata and controls
130 lines (111 loc) · 4.35 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
// INTERPRETER-ONLY: spendability covered by conformance/witnesses/real-crypto/function-patterns.json
// (real deploy->call->@bsv/sdk Spend on THIS source, with a pinned expectedState).
import { describe, it, expect } from 'vitest';
import { readFileSync } from 'node:fs';
import { join, dirname } from 'node:path';
import { fileURLToPath } from 'node:url';
import { TestContract, ALICE, signTestMessage } from 'runar-testing';
const __dirname = dirname(fileURLToPath(import.meta.url));
const source = readFileSync(join(__dirname, 'FunctionPatterns.runar.ts'), 'utf8');
const OWNER_PK = ALICE.pubKey;
const OWNER_SIG = signTestMessage(ALICE.privKey);
function make(balance = 10000n) {
return TestContract.fromSource(source, { owner: OWNER_PK, balance });
}
describe('FunctionPatterns', () => {
// --- Public method: deposit ---
describe('deposit', () => {
it('adds funds', () => {
const c = make();
c.call('deposit', { sig: OWNER_SIG, amount: 500n });
expect(c.state.balance).toBe(10500n);
});
it('rejects zero', () => {
const r = make().call('deposit', { sig: OWNER_SIG, amount: 0n });
expect(r.success).toBe(false);
});
it('rejects negative', () => {
const r = make().call('deposit', { sig: OWNER_SIG, amount: -100n });
expect(r.success).toBe(false);
});
it('accumulates across calls', () => {
const c = make();
c.call('deposit', { sig: OWNER_SIG, amount: 100n });
c.call('deposit', { sig: OWNER_SIG, amount: 200n });
c.call('deposit', { sig: OWNER_SIG, amount: 300n });
expect(c.state.balance).toBe(10600n);
});
});
// --- Public method: withdraw (private method + built-in) ---
describe('withdraw', () => {
it('deducts without fee', () => {
const c = make();
c.call('withdraw', { sig: OWNER_SIG, amount: 3000n, feeBps: 0n });
expect(c.state.balance).toBe(7000n);
});
it('deducts with 5% fee', () => {
const c = make();
c.call('withdraw', { sig: OWNER_SIG, amount: 1000n, feeBps: 500n });
expect(c.state.balance).toBe(8950n);
});
it('rejects insufficient balance', () => {
const r = make().call('withdraw', { sig: OWNER_SIG, amount: 20000n, feeBps: 0n });
expect(r.success).toBe(false);
});
it('rejects when fee pushes total over balance', () => {
const r = make().call('withdraw', { sig: OWNER_SIG, amount: 10000n, feeBps: 100n });
expect(r.success).toBe(false);
});
});
// --- Public method: scale (private helper wrapping built-in) ---
describe('scale', () => {
it('doubles', () => {
const c = make();
c.call('scale', { sig: OWNER_SIG, numerator: 2n, denominator: 1n });
expect(c.state.balance).toBe(20000n);
});
it('halves', () => {
const c = make();
c.call('scale', { sig: OWNER_SIG, numerator: 1n, denominator: 2n });
expect(c.state.balance).toBe(5000n);
});
it('three-quarters', () => {
const c = make();
c.call('scale', { sig: OWNER_SIG, numerator: 3n, denominator: 4n });
expect(c.state.balance).toBe(7500n);
});
});
// --- Public method: normalize (composed private helpers) ---
describe('normalize', () => {
it('clamps above max and rounds', () => {
const c = make();
c.call('normalize', { sig: OWNER_SIG, lo: 0n, hi: 8000n, step: 1000n });
expect(c.state.balance).toBe(8000n);
});
it('rounds down non-aligned value', () => {
const c = make(7777n);
c.call('normalize', { sig: OWNER_SIG, lo: 0n, hi: 10000n, step: 1000n });
expect(c.state.balance).toBe(7000n);
});
it('clamps below min', () => {
const c = make(50n);
c.call('normalize', { sig: OWNER_SIG, lo: 1000n, hi: 10000n, step: 500n });
expect(c.state.balance).toBe(1000n);
});
});
// --- Composition ---
describe('multi-step workflows', () => {
it('deposit then withdraw with fee', () => {
const c = make();
c.call('deposit', { sig: OWNER_SIG, amount: 5000n });
c.call('withdraw', { sig: OWNER_SIG, amount: 5000n, feeBps: 200n });
expect(c.state.balance).toBe(9900n);
});
it('scale then normalize', () => {
const c = make();
c.call('scale', { sig: OWNER_SIG, numerator: 3n, denominator: 4n });
c.call('normalize', { sig: OWNER_SIG, lo: 0n, hi: 10000n, step: 1000n });
expect(c.state.balance).toBe(7000n);
});
});
});