-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAuction.test.ts
More file actions
74 lines (63 loc) · 2.76 KB
/
Auction.test.ts
File metadata and controls
74 lines (63 loc) · 2.76 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
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, BOB, CHARLIE, signTestMessage } from 'runar-testing';
const __dirname = dirname(fileURLToPath(import.meta.url));
const source = readFileSync(join(__dirname, 'Auction.runar.ts'), 'utf8');
// ALICE = auctioneer, BOB = bidder A, CHARLIE = bidder B
const AUCTIONEER_SIG = signTestMessage(ALICE.privKey);
const BOB_SIG = signTestMessage(BOB.privKey);
const CHARLIE_SIG = signTestMessage(CHARLIE.privKey);
const DEADLINE = 500000n;
describe('Auction', () => {
function makeAuction(highestBid = 0n) {
const auction = TestContract.fromSource(source, {
auctioneer: ALICE.pubKey,
highestBidder: BOB.pubKey,
highestBid,
deadline: DEADLINE,
});
// Set locktime before deadline (bidding is open)
auction.setMockPreimage({ locktime: DEADLINE - 1n });
return auction;
}
it('accepts a valid bid above current highest', () => {
const auction = makeAuction(100n);
const result = auction.call('bid', { sig: CHARLIE_SIG, bidder: CHARLIE.pubKey, bidAmount: 200n });
expect(result.success).toBe(true);
expect(auction.state.highestBid).toBe(200n);
expect(auction.state.highestBidder).toBe(CHARLIE.pubKey);
});
it('rejects a bid below current highest', () => {
const auction = makeAuction(100n);
const result = auction.call('bid', { sig: CHARLIE_SIG, bidder: CHARLIE.pubKey, bidAmount: 50n });
expect(result.success).toBe(false);
});
it('rejects a bid after deadline', () => {
const auction = makeAuction(100n);
auction.setMockPreimage({ locktime: DEADLINE + 1n });
const result = auction.call('bid', { sig: CHARLIE_SIG, bidder: CHARLIE.pubKey, bidAmount: 200n });
expect(result.success).toBe(false);
});
it('allows close after deadline', () => {
const auction = makeAuction(100n);
auction.setMockPreimage({ locktime: DEADLINE });
const result = auction.call('close', { sig: AUCTIONEER_SIG });
expect(result.success).toBe(true);
});
it('rejects close before deadline', () => {
const auction = makeAuction(100n);
auction.setMockPreimage({ locktime: DEADLINE - 1n });
const result = auction.call('close', { sig: AUCTIONEER_SIG });
expect(result.success).toBe(false);
});
it('tracks multiple bids in sequence', () => {
const auction = makeAuction(0n);
auction.call('bid', { sig: BOB_SIG, bidder: BOB.pubKey, bidAmount: 100n });
expect(auction.state.highestBid).toBe(100n);
auction.call('bid', { sig: CHARLIE_SIG, bidder: CHARLIE.pubKey, bidAmount: 200n });
expect(auction.state.highestBid).toBe(200n);
expect(auction.state.highestBidder).toBe(CHARLIE.pubKey);
});
});