hive-stream 3.0.2 → 3.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/DOCUMENTATION.md +50 -2
- package/README.md +282 -4
- package/dist/adapters/base.adapter.d.ts +5 -0
- package/dist/adapters/base.adapter.js +9 -0
- package/dist/adapters/base.adapter.js.map +1 -1
- package/dist/adapters/mongodb.adapter.d.ts +6 -6
- package/dist/adapters/mongodb.adapter.js +36 -21
- package/dist/adapters/mongodb.adapter.js.map +1 -1
- package/dist/adapters/postgresql.adapter.d.ts +7 -0
- package/dist/adapters/postgresql.adapter.js +46 -19
- package/dist/adapters/postgresql.adapter.js.map +1 -1
- package/dist/adapters/sqlite.adapter.d.ts +4 -0
- package/dist/adapters/sqlite.adapter.js +10 -0
- package/dist/adapters/sqlite.adapter.js.map +1 -1
- package/dist/api.d.ts +13 -3
- package/dist/api.js +96 -62
- package/dist/api.js.map +1 -1
- package/dist/builders.d.ts +176 -0
- package/dist/builders.js +727 -0
- package/dist/builders.js.map +1 -0
- package/dist/config.d.ts +16 -1
- package/dist/config.js +95 -3
- package/dist/config.js.map +1 -1
- package/dist/contracts/auctionhouse.contract.d.ts +4 -0
- package/dist/contracts/auctionhouse.contract.js +234 -0
- package/dist/contracts/auctionhouse.contract.js.map +1 -0
- package/dist/contracts/booking.contract.d.ts +4 -0
- package/dist/contracts/booking.contract.js +225 -0
- package/dist/contracts/booking.contract.js.map +1 -0
- package/dist/contracts/bountyboard.contract.d.ts +4 -0
- package/dist/contracts/bountyboard.contract.js +233 -0
- package/dist/contracts/bountyboard.contract.js.map +1 -0
- package/dist/contracts/bundlemarketplace.contract.d.ts +4 -0
- package/dist/contracts/bundlemarketplace.contract.js +195 -0
- package/dist/contracts/bundlemarketplace.contract.js.map +1 -0
- package/dist/contracts/charitymatch.contract.d.ts +4 -0
- package/dist/contracts/charitymatch.contract.js +172 -0
- package/dist/contracts/charitymatch.contract.js.map +1 -0
- package/dist/contracts/coinflip.contract.js +25 -22
- package/dist/contracts/coinflip.contract.js.map +1 -1
- package/dist/contracts/crowdfund.contract.d.ts +4 -0
- package/dist/contracts/crowdfund.contract.js +290 -0
- package/dist/contracts/crowdfund.contract.js.map +1 -0
- package/dist/contracts/dcabot.contract.d.ts +4 -0
- package/dist/contracts/dcabot.contract.js +217 -0
- package/dist/contracts/dcabot.contract.js.map +1 -0
- package/dist/contracts/dice.contract.js +25 -22
- package/dist/contracts/dice.contract.js.map +1 -1
- package/dist/contracts/domainregistry.contract.d.ts +4 -0
- package/dist/contracts/domainregistry.contract.js +232 -0
- package/dist/contracts/domainregistry.contract.js.map +1 -0
- package/dist/contracts/exchange.contract.js +209 -168
- package/dist/contracts/exchange.contract.js.map +1 -1
- package/dist/contracts/fanclub.contract.d.ts +4 -0
- package/dist/contracts/fanclub.contract.js +193 -0
- package/dist/contracts/fanclub.contract.js.map +1 -0
- package/dist/contracts/giftcard.contract.d.ts +4 -0
- package/dist/contracts/giftcard.contract.js +158 -0
- package/dist/contracts/giftcard.contract.js.map +1 -0
- package/dist/contracts/grantrounds.contract.d.ts +4 -0
- package/dist/contracts/grantrounds.contract.js +265 -0
- package/dist/contracts/grantrounds.contract.js.map +1 -0
- package/dist/contracts/groupbuy.contract.d.ts +4 -0
- package/dist/contracts/groupbuy.contract.js +198 -0
- package/dist/contracts/groupbuy.contract.js.map +1 -0
- package/dist/contracts/helpers.d.ts +66 -0
- package/dist/contracts/helpers.js +166 -0
- package/dist/contracts/helpers.js.map +1 -0
- package/dist/contracts/insurancepool.contract.d.ts +4 -0
- package/dist/contracts/insurancepool.contract.js +281 -0
- package/dist/contracts/insurancepool.contract.js.map +1 -0
- package/dist/contracts/invoice.contract.d.ts +4 -0
- package/dist/contracts/invoice.contract.js +193 -0
- package/dist/contracts/invoice.contract.js.map +1 -0
- package/dist/contracts/launchpad.contract.d.ts +4 -0
- package/dist/contracts/launchpad.contract.js +225 -0
- package/dist/contracts/launchpad.contract.js.map +1 -0
- package/dist/contracts/lotto.contract.js +53 -37
- package/dist/contracts/lotto.contract.js.map +1 -1
- package/dist/contracts/multisigtreasury.contract.d.ts +4 -0
- package/dist/contracts/multisigtreasury.contract.js +245 -0
- package/dist/contracts/multisigtreasury.contract.js.map +1 -0
- package/dist/contracts/nft.contract.d.ts +1 -0
- package/dist/contracts/nft.contract.js +234 -195
- package/dist/contracts/nft.contract.js.map +1 -1
- package/dist/contracts/oraclebounty.contract.d.ts +4 -0
- package/dist/contracts/oraclebounty.contract.js +250 -0
- package/dist/contracts/oraclebounty.contract.js.map +1 -0
- package/dist/contracts/payroll.contract.d.ts +4 -0
- package/dist/contracts/payroll.contract.js +232 -0
- package/dist/contracts/payroll.contract.js.map +1 -0
- package/dist/contracts/paywall.contract.d.ts +4 -0
- package/dist/contracts/paywall.contract.js +185 -0
- package/dist/contracts/paywall.contract.js.map +1 -0
- package/dist/contracts/poll.contract.js +2 -0
- package/dist/contracts/poll.contract.js.map +1 -1
- package/dist/contracts/predictionmarket.contract.d.ts +4 -0
- package/dist/contracts/predictionmarket.contract.js +213 -0
- package/dist/contracts/predictionmarket.contract.js.map +1 -0
- package/dist/contracts/proposaltimelock.contract.d.ts +4 -0
- package/dist/contracts/proposaltimelock.contract.js +250 -0
- package/dist/contracts/proposaltimelock.contract.js.map +1 -0
- package/dist/contracts/questpass.contract.d.ts +4 -0
- package/dist/contracts/questpass.contract.js +214 -0
- package/dist/contracts/questpass.contract.js.map +1 -0
- package/dist/contracts/referral.contract.d.ts +4 -0
- package/dist/contracts/referral.contract.js +238 -0
- package/dist/contracts/referral.contract.js.map +1 -0
- package/dist/contracts/rental.contract.d.ts +4 -0
- package/dist/contracts/rental.contract.js +221 -0
- package/dist/contracts/rental.contract.js.map +1 -0
- package/dist/contracts/revenuesplit.contract.d.ts +4 -0
- package/dist/contracts/revenuesplit.contract.js +211 -0
- package/dist/contracts/revenuesplit.contract.js.map +1 -0
- package/dist/contracts/rps.contract.js +48 -20
- package/dist/contracts/rps.contract.js.map +1 -1
- package/dist/contracts/savings.contract.d.ts +4 -0
- package/dist/contracts/savings.contract.js +208 -0
- package/dist/contracts/savings.contract.js.map +1 -0
- package/dist/contracts/subscription.contract.d.ts +4 -0
- package/dist/contracts/subscription.contract.js +241 -0
- package/dist/contracts/subscription.contract.js.map +1 -0
- package/dist/contracts/sweepstakes.contract.d.ts +4 -0
- package/dist/contracts/sweepstakes.contract.js +209 -0
- package/dist/contracts/sweepstakes.contract.js.map +1 -0
- package/dist/contracts/ticketing.contract.d.ts +4 -0
- package/dist/contracts/ticketing.contract.js +185 -0
- package/dist/contracts/ticketing.contract.js.map +1 -0
- package/dist/contracts/tipjar.contract.js +2 -0
- package/dist/contracts/tipjar.contract.js.map +1 -1
- package/dist/contracts/token.contract.js +135 -125
- package/dist/contracts/token.contract.js.map +1 -1
- package/dist/index.d.ts +40 -0
- package/dist/index.js +72 -1
- package/dist/index.js.map +1 -1
- package/dist/metadata.d.ts +20 -0
- package/dist/metadata.js +320 -1
- package/dist/metadata.js.map +1 -1
- package/dist/providers/block-provider.d.ts +22 -0
- package/dist/providers/block-provider.js +3 -0
- package/dist/providers/block-provider.js.map +1 -0
- package/dist/providers/haf-client.d.ts +30 -0
- package/dist/providers/haf-client.js +119 -0
- package/dist/providers/haf-client.js.map +1 -0
- package/dist/providers/haf-provider.d.ts +49 -0
- package/dist/providers/haf-provider.js +256 -0
- package/dist/providers/haf-provider.js.map +1 -0
- package/dist/providers/hive-provider.d.ts +13 -0
- package/dist/providers/hive-provider.js +25 -0
- package/dist/providers/hive-provider.js.map +1 -0
- package/dist/providers/index.d.ts +4 -0
- package/dist/providers/index.js +21 -0
- package/dist/providers/index.js.map +1 -0
- package/dist/streamer.d.ts +65 -4
- package/dist/streamer.js +768 -72
- package/dist/streamer.js.map +1 -1
- package/dist/types/hive-stream.d.ts +317 -0
- package/dist/utils.d.ts +33 -0
- package/dist/utils.js +198 -2
- package/dist/utils.js.map +1 -1
- package/package.json +16 -1
- package/.claude/settings.local.json +0 -12
- package/.env.example +0 -3
- package/.travis.yml +0 -11
- package/AGENTS.md +0 -35
- package/CLAUDE.md +0 -75
- package/ecosystem.config.js +0 -17
- package/examples/contracts/README.md +0 -8
- package/examples/contracts/exchange.ts +0 -38
- package/examples/contracts/poll.ts +0 -21
- package/examples/contracts/rps.ts +0 -19
- package/examples/contracts/tipjar.ts +0 -19
- package/jest.config.js +0 -9
- package/test-contract-block.md +0 -19
- package/tests/actions.spec.ts +0 -252
- package/tests/adapters/actions-persistence.spec.ts +0 -144
- package/tests/adapters/postgresql.adapter.spec.ts +0 -127
- package/tests/adapters/sqlite.adapter.spec.ts +0 -181
- package/tests/config-input.spec.ts +0 -90
- package/tests/contracts/coinflip.contract.spec.ts +0 -94
- package/tests/contracts/dice.contract.spec.ts +0 -87
- package/tests/contracts/entrants.json +0 -729
- package/tests/contracts/exchange.contract.spec.ts +0 -84
- package/tests/contracts/lotto.contract.spec.ts +0 -59
- package/tests/contracts/nft.contract.spec.ts +0 -948
- package/tests/contracts/token.contract.spec.ts +0 -90
- package/tests/exchanges/coingecko.exchange.spec.ts +0 -169
- package/tests/exchanges/exchange.base.spec.ts +0 -246
- package/tests/helpers/mock-adapter.ts +0 -214
- package/tests/helpers/mock-fetch.ts +0 -165
- package/tests/hive-chain-features.spec.ts +0 -319
- package/tests/hive-rates.spec.ts +0 -443
- package/tests/integration/hive-rates.integration.spec.ts +0 -35
- package/tests/metadata.spec.ts +0 -63
- package/tests/setup.ts +0 -30
- package/tests/streamer-actions.spec.ts +0 -274
- package/tests/streamer.spec.ts +0 -342
- package/tests/types/rates.spec.ts +0 -216
- package/tests/utils.spec.ts +0 -113
- package/tsconfig.build.json +0 -4
- package/tslint.json +0 -21
- package/wallaby.js +0 -26
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { Streamer, createPollContract } from 'hive-stream';
|
|
2
|
-
import { SqliteAdapter } from 'hive-stream';
|
|
3
|
-
|
|
4
|
-
async function main() {
|
|
5
|
-
const streamer = new Streamer({
|
|
6
|
-
JSON_ID: 'hivestream',
|
|
7
|
-
PAYLOAD_IDENTIFIER: 'hive_stream'
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
await streamer.registerAdapter(new SqliteAdapter());
|
|
11
|
-
await streamer.registerContract(createPollContract());
|
|
12
|
-
|
|
13
|
-
streamer.start();
|
|
14
|
-
|
|
15
|
-
// Create poll:
|
|
16
|
-
// {"hive_stream": {"contract":"polls","action":"createPoll","payload":{"pollId":"launch","question":"Ship it?","options":["yes","no"],"durationHours":24}}}
|
|
17
|
-
// Vote:
|
|
18
|
-
// {"hive_stream": {"contract":"polls","action":"vote","payload":{"pollId":"launch","option":0}}}
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
main();
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { Streamer, createRpsContract } from 'hive-stream';
|
|
2
|
-
import { SqliteAdapter } from 'hive-stream';
|
|
3
|
-
|
|
4
|
-
async function main() {
|
|
5
|
-
const streamer = new Streamer({
|
|
6
|
-
JSON_ID: 'hivestream',
|
|
7
|
-
PAYLOAD_IDENTIFIER: 'hive_stream'
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
await streamer.registerAdapter(new SqliteAdapter());
|
|
11
|
-
await streamer.registerContract(createRpsContract({ account: 'my-rps-bank' }));
|
|
12
|
-
|
|
13
|
-
streamer.start();
|
|
14
|
-
|
|
15
|
-
// User sends transfer with memo:
|
|
16
|
-
// {"hive_stream": {"contract":"rps","action":"play","payload":{"move":"rock","seed":"optional"}}}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
main();
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { Streamer, createTipJarContract } from 'hive-stream';
|
|
2
|
-
import { SqliteAdapter } from 'hive-stream';
|
|
3
|
-
|
|
4
|
-
async function main() {
|
|
5
|
-
const streamer = new Streamer({
|
|
6
|
-
JSON_ID: 'hivestream',
|
|
7
|
-
PAYLOAD_IDENTIFIER: 'hive_stream'
|
|
8
|
-
});
|
|
9
|
-
|
|
10
|
-
await streamer.registerAdapter(new SqliteAdapter());
|
|
11
|
-
await streamer.registerContract(createTipJarContract({ name: 'tipjar' }));
|
|
12
|
-
|
|
13
|
-
streamer.start();
|
|
14
|
-
|
|
15
|
-
// User sends transfer with memo:
|
|
16
|
-
// {"hive_stream": {"contract":"tipjar","action":"tip","payload":{"message":"Great project!"}}}
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
main();
|
package/jest.config.js
DELETED
package/test-contract-block.md
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
42189228
|
|
2
|
-
|
|
3
|
-
hive_keychain.requestCustomJson('beggars', 'hivestream', 'active', JSON.stringify({ hive_stream: { contract: 'hivedice', action: 'roll', payload: { roll: 22, direction: 'lesserThan'} } }), 'Test', function(response) {
|
|
4
|
-
console.log(response);
|
|
5
|
-
});
|
|
6
|
-
|
|
7
|
-
42203941
|
|
8
|
-
Transfer memo payload
|
|
9
|
-
|
|
10
|
-
{"hive_stream":{"contract":"hivedice","action":"roll","payload":{"roll":95,"direction":"greaterThan"}}}
|
|
11
|
-
|
|
12
|
-
42208330
|
|
13
|
-
Transfer memo, amount higher than max
|
|
14
|
-
|
|
15
|
-
42209768
|
|
16
|
-
Transfer memo, valid bet
|
|
17
|
-
|
|
18
|
-
42210252
|
|
19
|
-
Transfer memo, valid bet and greater than
|
package/tests/actions.spec.ts
DELETED
|
@@ -1,252 +0,0 @@
|
|
|
1
|
-
import { TimeAction } from '../src/actions';
|
|
2
|
-
|
|
3
|
-
describe('TimeAction', () => {
|
|
4
|
-
describe('Constructor and Validation', () => {
|
|
5
|
-
test('Should create a valid TimeAction with required parameters', () => {
|
|
6
|
-
const action = new TimeAction('1m', 'test-action', 'testcontract', 'testmethod');
|
|
7
|
-
|
|
8
|
-
expect(action.timeValue).toBe('1m');
|
|
9
|
-
expect(action.id).toBe('test-action');
|
|
10
|
-
expect(action.contractName).toBe('testcontract');
|
|
11
|
-
expect(action.contractAction).toBe('testmethod');
|
|
12
|
-
expect(action.payload).toEqual({});
|
|
13
|
-
expect(action.enabled).toBe(true);
|
|
14
|
-
expect(action.executionCount).toBe(0);
|
|
15
|
-
expect(action.date).toBeInstanceOf(Date);
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
test('Should create TimeAction with all optional parameters', () => {
|
|
19
|
-
const testDate = new Date('2023-01-01T00:00:00Z');
|
|
20
|
-
const payload = { test: 'data' };
|
|
21
|
-
|
|
22
|
-
const action = new TimeAction(
|
|
23
|
-
'1h',
|
|
24
|
-
'test-action',
|
|
25
|
-
'testcontract',
|
|
26
|
-
'testmethod',
|
|
27
|
-
payload,
|
|
28
|
-
testDate,
|
|
29
|
-
false,
|
|
30
|
-
5,
|
|
31
|
-
10,
|
|
32
|
-
'America/New_York'
|
|
33
|
-
);
|
|
34
|
-
|
|
35
|
-
expect(action.timeValue).toBe('1h');
|
|
36
|
-
expect(action.payload).toEqual(payload);
|
|
37
|
-
expect(action.date).toEqual(testDate);
|
|
38
|
-
expect(action.enabled).toBe(false);
|
|
39
|
-
expect(action.executionCount).toBe(5);
|
|
40
|
-
expect(action.maxExecutions).toBe(10);
|
|
41
|
-
expect(action.timezone).toBe('America/New_York');
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
test('Should throw error for invalid timeValue', () => {
|
|
45
|
-
expect(() => {
|
|
46
|
-
new TimeAction('invalid', 'test-action', 'testcontract', 'testmethod');
|
|
47
|
-
}).toThrow('Invalid timeValue \'invalid\'');
|
|
48
|
-
});
|
|
49
|
-
|
|
50
|
-
test('Should throw error for empty timeValue', () => {
|
|
51
|
-
expect(() => {
|
|
52
|
-
new TimeAction('', 'test-action', 'testcontract', 'testmethod');
|
|
53
|
-
}).toThrow('timeValue must be a non-empty string');
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
test('Should throw error for invalid id format', () => {
|
|
57
|
-
expect(() => {
|
|
58
|
-
new TimeAction('1m', 'test action!', 'testcontract', 'testmethod');
|
|
59
|
-
}).toThrow('id can only contain alphanumeric characters, underscores, and hyphens');
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
test('Should throw error for too long id', () => {
|
|
63
|
-
const longId = 'a'.repeat(256);
|
|
64
|
-
expect(() => {
|
|
65
|
-
new TimeAction('1m', longId, 'testcontract', 'testmethod');
|
|
66
|
-
}).toThrow('id must not exceed 255 characters');
|
|
67
|
-
});
|
|
68
|
-
|
|
69
|
-
test('Should throw error for empty contractName', () => {
|
|
70
|
-
expect(() => {
|
|
71
|
-
new TimeAction('1m', 'test-action', '', 'testmethod');
|
|
72
|
-
}).toThrow('contractName must be a non-empty string');
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
test('Should throw error for empty contractAction', () => {
|
|
76
|
-
expect(() => {
|
|
77
|
-
new TimeAction('1m', 'test-action', 'testcontract', '');
|
|
78
|
-
}).toThrow('contractAction must be a non-empty string');
|
|
79
|
-
});
|
|
80
|
-
|
|
81
|
-
test('Should throw error for invalid date string', () => {
|
|
82
|
-
expect(() => {
|
|
83
|
-
new TimeAction('1m', 'test-action', 'testcontract', 'testmethod', {}, 'invalid-date');
|
|
84
|
-
}).toThrow('Invalid date string \'invalid-date\'');
|
|
85
|
-
});
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
describe('Methods', () => {
|
|
89
|
-
let action: TimeAction;
|
|
90
|
-
|
|
91
|
-
beforeEach(() => {
|
|
92
|
-
action = new TimeAction('1m', 'test-action', 'testcontract', 'testmethod');
|
|
93
|
-
});
|
|
94
|
-
|
|
95
|
-
test('Should reset date and lastExecution', async () => {
|
|
96
|
-
const originalDate = action.date;
|
|
97
|
-
action.lastExecution = new Date();
|
|
98
|
-
|
|
99
|
-
// Wait a bit to ensure time difference
|
|
100
|
-
await new Promise(resolve => setTimeout(resolve, 10));
|
|
101
|
-
|
|
102
|
-
action.reset();
|
|
103
|
-
|
|
104
|
-
expect(action.date.getTime()).toBeGreaterThan(originalDate.getTime());
|
|
105
|
-
expect(action.lastExecution).toBeUndefined();
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
test('Should disable and enable action', () => {
|
|
109
|
-
expect(action.enabled).toBe(true);
|
|
110
|
-
|
|
111
|
-
action.disable();
|
|
112
|
-
expect(action.enabled).toBe(false);
|
|
113
|
-
|
|
114
|
-
action.enable();
|
|
115
|
-
expect(action.enabled).toBe(true);
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
test('Should track execution count and max executions', () => {
|
|
119
|
-
action.maxExecutions = 3;
|
|
120
|
-
|
|
121
|
-
expect(action.hasReachedMaxExecutions()).toBe(false);
|
|
122
|
-
|
|
123
|
-
action.incrementExecutionCount();
|
|
124
|
-
expect(action.executionCount).toBe(1);
|
|
125
|
-
expect(action.lastExecution).toBeInstanceOf(Date);
|
|
126
|
-
expect(action.hasReachedMaxExecutions()).toBe(false);
|
|
127
|
-
|
|
128
|
-
action.incrementExecutionCount();
|
|
129
|
-
action.incrementExecutionCount();
|
|
130
|
-
expect(action.executionCount).toBe(3);
|
|
131
|
-
expect(action.hasReachedMaxExecutions()).toBe(true);
|
|
132
|
-
});
|
|
133
|
-
|
|
134
|
-
test('Should not reach max executions when maxExecutions is undefined', () => {
|
|
135
|
-
action.executionCount = 1000;
|
|
136
|
-
expect(action.hasReachedMaxExecutions()).toBe(false);
|
|
137
|
-
});
|
|
138
|
-
});
|
|
139
|
-
|
|
140
|
-
describe('Serialization', () => {
|
|
141
|
-
test('Should serialize to JSON correctly', () => {
|
|
142
|
-
const testDate = new Date('2023-01-01T00:00:00Z');
|
|
143
|
-
const lastExecution = new Date('2023-01-01T01:00:00Z');
|
|
144
|
-
const payload = { test: 'data' };
|
|
145
|
-
|
|
146
|
-
const action = new TimeAction(
|
|
147
|
-
'1h',
|
|
148
|
-
'test-action',
|
|
149
|
-
'testcontract',
|
|
150
|
-
'testmethod',
|
|
151
|
-
payload,
|
|
152
|
-
testDate,
|
|
153
|
-
false,
|
|
154
|
-
5,
|
|
155
|
-
10,
|
|
156
|
-
'UTC'
|
|
157
|
-
);
|
|
158
|
-
action.lastExecution = lastExecution;
|
|
159
|
-
|
|
160
|
-
const json = action.toJSON();
|
|
161
|
-
|
|
162
|
-
expect(json).toEqual({
|
|
163
|
-
timeValue: '1h',
|
|
164
|
-
id: 'test-action',
|
|
165
|
-
contractName: 'testcontract',
|
|
166
|
-
contractAction: 'testmethod',
|
|
167
|
-
payload: payload,
|
|
168
|
-
date: testDate.toISOString(),
|
|
169
|
-
enabled: false,
|
|
170
|
-
lastExecution: lastExecution.toISOString(),
|
|
171
|
-
executionCount: 5,
|
|
172
|
-
maxExecutions: 10,
|
|
173
|
-
timezone: 'UTC'
|
|
174
|
-
});
|
|
175
|
-
});
|
|
176
|
-
|
|
177
|
-
test('Should deserialize from JSON correctly', () => {
|
|
178
|
-
const testDate = new Date('2023-01-01T00:00:00Z');
|
|
179
|
-
const lastExecution = new Date('2023-01-01T01:00:00Z');
|
|
180
|
-
const payload = { test: 'data' };
|
|
181
|
-
|
|
182
|
-
const jsonData = {
|
|
183
|
-
timeValue: '1h',
|
|
184
|
-
id: 'test-action',
|
|
185
|
-
contractName: 'testcontract',
|
|
186
|
-
contractAction: 'testmethod',
|
|
187
|
-
payload: payload,
|
|
188
|
-
date: testDate.toISOString(),
|
|
189
|
-
enabled: false,
|
|
190
|
-
lastExecution: lastExecution.toISOString(),
|
|
191
|
-
executionCount: 5,
|
|
192
|
-
maxExecutions: 10,
|
|
193
|
-
timezone: 'UTC'
|
|
194
|
-
};
|
|
195
|
-
|
|
196
|
-
const action = TimeAction.fromJSON(jsonData);
|
|
197
|
-
|
|
198
|
-
expect(action.timeValue).toBe('1h');
|
|
199
|
-
expect(action.id).toBe('test-action');
|
|
200
|
-
expect(action.contractName).toBe('testcontract');
|
|
201
|
-
expect(action.contractAction).toBe('testmethod');
|
|
202
|
-
expect(action.payload).toEqual(payload);
|
|
203
|
-
expect(action.date).toEqual(testDate);
|
|
204
|
-
expect(action.enabled).toBe(false);
|
|
205
|
-
expect(action.lastExecution).toEqual(lastExecution);
|
|
206
|
-
expect(action.executionCount).toBe(5);
|
|
207
|
-
expect(action.maxExecutions).toBe(10);
|
|
208
|
-
expect(action.timezone).toBe('UTC');
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
test('Should handle serialization without optional fields', () => {
|
|
212
|
-
const action = new TimeAction('1m', 'test-action', 'testcontract', 'testmethod');
|
|
213
|
-
|
|
214
|
-
const json = action.toJSON();
|
|
215
|
-
const restored = TimeAction.fromJSON(json);
|
|
216
|
-
|
|
217
|
-
expect(restored.timeValue).toBe(action.timeValue);
|
|
218
|
-
expect(restored.id).toBe(action.id);
|
|
219
|
-
expect(restored.contractName).toBe(action.contractName);
|
|
220
|
-
expect(restored.contractAction).toBe(action.contractAction);
|
|
221
|
-
expect(restored.payload).toEqual({});
|
|
222
|
-
expect(restored.enabled).toBe(true);
|
|
223
|
-
expect(restored.executionCount).toBe(0);
|
|
224
|
-
expect(restored.lastExecution).toBeUndefined();
|
|
225
|
-
expect(restored.maxExecutions).toBeUndefined();
|
|
226
|
-
});
|
|
227
|
-
});
|
|
228
|
-
|
|
229
|
-
describe('Static Methods', () => {
|
|
230
|
-
test('Should return valid time values', () => {
|
|
231
|
-
const validValues = TimeAction.getValidTimeValues();
|
|
232
|
-
|
|
233
|
-
expect(validValues).toContain('3s');
|
|
234
|
-
expect(validValues).toContain('1m');
|
|
235
|
-
expect(validValues).toContain('1h');
|
|
236
|
-
expect(validValues).toContain('24h');
|
|
237
|
-
expect(validValues).toContain('week');
|
|
238
|
-
expect(Array.isArray(validValues)).toBe(true);
|
|
239
|
-
});
|
|
240
|
-
|
|
241
|
-
test('Should return a copy of valid time values', () => {
|
|
242
|
-
const validValues1 = TimeAction.getValidTimeValues();
|
|
243
|
-
const validValues2 = TimeAction.getValidTimeValues();
|
|
244
|
-
|
|
245
|
-
expect(validValues1).toEqual(validValues2);
|
|
246
|
-
expect(validValues1).not.toBe(validValues2); // Different array instances
|
|
247
|
-
|
|
248
|
-
validValues1.push('invalid');
|
|
249
|
-
expect(validValues2).not.toContain('invalid');
|
|
250
|
-
});
|
|
251
|
-
});
|
|
252
|
-
});
|
|
@@ -1,144 +0,0 @@
|
|
|
1
|
-
import { TimeAction } from '../../src/actions';
|
|
2
|
-
import { SqliteAdapter } from '../../src/adapters/sqlite.adapter';
|
|
3
|
-
import { MongodbAdapter } from '../../src/adapters/mongodb.adapter';
|
|
4
|
-
import fs from 'fs';
|
|
5
|
-
import path from 'path';
|
|
6
|
-
|
|
7
|
-
describe('Actions Persistence', () => {
|
|
8
|
-
describe('SqliteAdapter', () => {
|
|
9
|
-
let adapter: SqliteAdapter;
|
|
10
|
-
let testDbPath: string;
|
|
11
|
-
|
|
12
|
-
beforeEach(async () => {
|
|
13
|
-
// Create unique database path for each test
|
|
14
|
-
testDbPath = path.resolve(__dirname, `../../src/adapters/hive-stream-test-${Date.now()}-${Math.random()}.db`);
|
|
15
|
-
|
|
16
|
-
// Clean up any existing test database
|
|
17
|
-
if (fs.existsSync(testDbPath)) {
|
|
18
|
-
fs.unlinkSync(testDbPath);
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
adapter = new SqliteAdapter(testDbPath);
|
|
22
|
-
await adapter.create();
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
afterEach(async () => {
|
|
26
|
-
if (adapter && adapter.getDb()) {
|
|
27
|
-
await adapter.destroy();
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
// Clean up test database
|
|
31
|
-
if (fs.existsSync(testDbPath)) {
|
|
32
|
-
fs.unlinkSync(testDbPath);
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
test('Should save and load actions correctly', async () => {
|
|
37
|
-
const testActions = [
|
|
38
|
-
new TimeAction('1m', 'action1', 'contract1', 'method1', { data: 'test1' }),
|
|
39
|
-
new TimeAction('5m', 'action2', 'contract2', 'method2', { data: 'test2' }, new Date('2023-01-01'), false, 5, 10)
|
|
40
|
-
];
|
|
41
|
-
|
|
42
|
-
// Serialize actions as they would be in the streamer
|
|
43
|
-
const serializedActions = testActions.map(a => a.toJSON());
|
|
44
|
-
|
|
45
|
-
const stateData = {
|
|
46
|
-
lastBlockNumber: 12345,
|
|
47
|
-
actions: serializedActions
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
await adapter.saveState(stateData);
|
|
51
|
-
|
|
52
|
-
const loadedActions = await adapter.loadActions();
|
|
53
|
-
|
|
54
|
-
expect(loadedActions).toHaveLength(2);
|
|
55
|
-
|
|
56
|
-
const action1 = loadedActions.find(a => a.id === 'action1');
|
|
57
|
-
const action2 = loadedActions.find(a => a.id === 'action2');
|
|
58
|
-
|
|
59
|
-
expect(action1).toBeDefined();
|
|
60
|
-
expect(action1?.timeValue).toBe('1m');
|
|
61
|
-
expect(action1?.contractName).toBe('contract1');
|
|
62
|
-
expect(action1?.contractAction).toBe('method1');
|
|
63
|
-
expect(action1?.payload).toEqual({ data: 'test1' });
|
|
64
|
-
expect(action1?.enabled).toBe(true);
|
|
65
|
-
expect(action1?.executionCount).toBe(0);
|
|
66
|
-
|
|
67
|
-
expect(action2).toBeDefined();
|
|
68
|
-
expect(action2?.timeValue).toBe('5m');
|
|
69
|
-
expect(action2?.contractName).toBe('contract2');
|
|
70
|
-
expect(action2?.contractAction).toBe('method2');
|
|
71
|
-
expect(action2?.payload).toEqual({ data: 'test2' });
|
|
72
|
-
expect(action2?.enabled).toBe(false);
|
|
73
|
-
expect(action2?.executionCount).toBe(5);
|
|
74
|
-
expect(action2?.maxExecutions).toBe(10);
|
|
75
|
-
expect(action2?.date).toEqual(new Date('2023-01-01'));
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
test('Should handle empty actions array', async () => {
|
|
79
|
-
const stateData = {
|
|
80
|
-
lastBlockNumber: 12345,
|
|
81
|
-
actions: []
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
await adapter.saveState(stateData);
|
|
85
|
-
const loadedActions = await adapter.loadActions();
|
|
86
|
-
|
|
87
|
-
expect(loadedActions).toHaveLength(0);
|
|
88
|
-
expect(Array.isArray(loadedActions)).toBe(true);
|
|
89
|
-
});
|
|
90
|
-
|
|
91
|
-
test('Should handle corrupted action data gracefully', async () => {
|
|
92
|
-
const stateData = {
|
|
93
|
-
lastBlockNumber: 12345,
|
|
94
|
-
actions: [
|
|
95
|
-
{ timeValue: '1m', id: 'valid-action', contractName: 'test', contractAction: 'test' },
|
|
96
|
-
{ invalid: 'data' }, // Invalid action
|
|
97
|
-
null, // Null action
|
|
98
|
-
'invalid string' // Invalid type
|
|
99
|
-
]
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
await adapter.saveState(stateData);
|
|
103
|
-
const loadedActions = await adapter.loadActions();
|
|
104
|
-
|
|
105
|
-
// Should only load the valid action
|
|
106
|
-
expect(loadedActions).toHaveLength(1);
|
|
107
|
-
expect(loadedActions[0].id).toBe('valid-action');
|
|
108
|
-
});
|
|
109
|
-
|
|
110
|
-
test('Should handle missing state gracefully', async () => {
|
|
111
|
-
const loadedActions = await adapter.loadActions();
|
|
112
|
-
|
|
113
|
-
expect(loadedActions).toHaveLength(0);
|
|
114
|
-
expect(Array.isArray(loadedActions)).toBe(true);
|
|
115
|
-
});
|
|
116
|
-
});
|
|
117
|
-
|
|
118
|
-
// Note: MongoDB tests would require a running MongoDB instance
|
|
119
|
-
// For now, we'll test the serialization logic only
|
|
120
|
-
describe('MongodbAdapter Serialization', () => {
|
|
121
|
-
test('Should serialize actions data correctly for MongoDB', () => {
|
|
122
|
-
const testActions = [
|
|
123
|
-
new TimeAction('1m', 'action1', 'contract1', 'method1', { data: 'test1' }),
|
|
124
|
-
new TimeAction('5m', 'action2', 'contract2', 'method2', { data: 'test2' })
|
|
125
|
-
];
|
|
126
|
-
|
|
127
|
-
const serializedActions = testActions.map(a => a.toJSON());
|
|
128
|
-
|
|
129
|
-
const stateData = {
|
|
130
|
-
lastBlockNumber: 12345,
|
|
131
|
-
actions: serializedActions
|
|
132
|
-
};
|
|
133
|
-
|
|
134
|
-
// Verify the data structure is valid for MongoDB
|
|
135
|
-
expect(stateData.actions).toHaveLength(2);
|
|
136
|
-
expect(stateData.actions[0]).toHaveProperty('timeValue');
|
|
137
|
-
expect(stateData.actions[0]).toHaveProperty('id');
|
|
138
|
-
expect(stateData.actions[0]).toHaveProperty('contractName');
|
|
139
|
-
expect(stateData.actions[0]).toHaveProperty('contractAction');
|
|
140
|
-
expect(stateData.actions[0]).toHaveProperty('date');
|
|
141
|
-
expect(typeof stateData.actions[0].date).toBe('string'); // Should be ISO string
|
|
142
|
-
});
|
|
143
|
-
});
|
|
144
|
-
});
|
|
@@ -1,127 +0,0 @@
|
|
|
1
|
-
import { PostgreSQLAdapter } from "../../src/adapters/postgresql.adapter";
|
|
2
|
-
|
|
3
|
-
describe('PostgreSQL Adapter', () => {
|
|
4
|
-
let sut: PostgreSQLAdapter;
|
|
5
|
-
|
|
6
|
-
beforeEach(() => {
|
|
7
|
-
sut = new PostgreSQLAdapter({
|
|
8
|
-
host: 'localhost',
|
|
9
|
-
port: 5432,
|
|
10
|
-
user: 'test',
|
|
11
|
-
password: 'test',
|
|
12
|
-
database: 'test_db'
|
|
13
|
-
});
|
|
14
|
-
});
|
|
15
|
-
|
|
16
|
-
test('constructor creates adapter with connection config', () => {
|
|
17
|
-
expect(sut).toBeInstanceOf(PostgreSQLAdapter);
|
|
18
|
-
expect(sut.getDb()).toBeDefined();
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
test('constructor works with connection string', () => {
|
|
22
|
-
const adapter = new PostgreSQLAdapter({
|
|
23
|
-
connectionString: 'postgresql://user:pass@localhost:5432/dbname',
|
|
24
|
-
ssl: true
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
expect(adapter).toBeInstanceOf(PostgreSQLAdapter);
|
|
28
|
-
expect(adapter.getDb()).toBeDefined();
|
|
29
|
-
});
|
|
30
|
-
|
|
31
|
-
test('constructor uses default values when not provided', () => {
|
|
32
|
-
const adapter = new PostgreSQLAdapter({
|
|
33
|
-
password: 'test'
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
expect(adapter).toBeInstanceOf(PostgreSQLAdapter);
|
|
37
|
-
expect(adapter.getDb()).toBeDefined();
|
|
38
|
-
});
|
|
39
|
-
|
|
40
|
-
test('processOperation sets internal state', async () => {
|
|
41
|
-
const testData = {
|
|
42
|
-
blockNumber: 12345,
|
|
43
|
-
blockId: 'test-block',
|
|
44
|
-
prevBlockId: 'prev-block',
|
|
45
|
-
trxId: 'test-tx',
|
|
46
|
-
blockTime: new Date()
|
|
47
|
-
};
|
|
48
|
-
|
|
49
|
-
await sut.processOperation({}, testData.blockNumber, testData.blockId, testData.prevBlockId, testData.trxId, testData.blockTime);
|
|
50
|
-
|
|
51
|
-
// Since these are private properties, we can't directly test them
|
|
52
|
-
// But we can verify the method doesn't throw
|
|
53
|
-
expect(true).toBe(true);
|
|
54
|
-
});
|
|
55
|
-
|
|
56
|
-
test('getDb returns knex instance', () => {
|
|
57
|
-
const db = sut.getDb();
|
|
58
|
-
expect(db).toBeDefined();
|
|
59
|
-
expect(typeof db).toBe('function'); // Knex instance is callable
|
|
60
|
-
});
|
|
61
|
-
|
|
62
|
-
test('destroy method is implemented', async () => {
|
|
63
|
-
// Create a mock knex instance
|
|
64
|
-
const mockKnex = {
|
|
65
|
-
destroy: jest.fn().mockResolvedValue(undefined)
|
|
66
|
-
};
|
|
67
|
-
(sut as any).db = mockKnex;
|
|
68
|
-
|
|
69
|
-
const result = await sut.destroy();
|
|
70
|
-
|
|
71
|
-
expect(result).toBe(true);
|
|
72
|
-
expect(mockKnex.destroy).toHaveBeenCalled();
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
test('query method handles raw SQL', async () => {
|
|
76
|
-
// Create a mock knex instance
|
|
77
|
-
const mockKnex = {
|
|
78
|
-
raw: jest.fn().mockResolvedValue({ rows: [{ id: 1, name: 'test' }] })
|
|
79
|
-
};
|
|
80
|
-
(sut as any).db = mockKnex;
|
|
81
|
-
|
|
82
|
-
const result = await sut.query('SELECT * FROM test WHERE id = $1', [1]);
|
|
83
|
-
|
|
84
|
-
expect(result).toEqual([{ id: 1, name: 'test' }]);
|
|
85
|
-
expect(mockKnex.raw).toHaveBeenCalledWith('SELECT * FROM test WHERE id = $1', [1]);
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
test('loadState handles missing state gracefully', async () => {
|
|
89
|
-
// Mock the query to return no results
|
|
90
|
-
const mockQuery = jest.fn().mockReturnValue({
|
|
91
|
-
select: jest.fn().mockReturnValue({
|
|
92
|
-
first: jest.fn().mockResolvedValue(null)
|
|
93
|
-
})
|
|
94
|
-
});
|
|
95
|
-
(sut as any).db = mockQuery;
|
|
96
|
-
|
|
97
|
-
const result = await sut.loadState();
|
|
98
|
-
|
|
99
|
-
expect(result).toBeNull();
|
|
100
|
-
});
|
|
101
|
-
|
|
102
|
-
test('saveState serializes actions correctly', async () => {
|
|
103
|
-
const testData = {
|
|
104
|
-
lastBlockNumber: 12345,
|
|
105
|
-
actions: [{ id: 'test', type: 'action' }]
|
|
106
|
-
};
|
|
107
|
-
|
|
108
|
-
// Mock the query chain
|
|
109
|
-
const mockMerge = jest.fn().mockResolvedValue(undefined);
|
|
110
|
-
const mockOnConflict = jest.fn().mockReturnValue({ merge: mockMerge });
|
|
111
|
-
const mockInsert = jest.fn().mockReturnValue({ onConflict: mockOnConflict });
|
|
112
|
-
const mockQuery = jest.fn().mockReturnValue({ insert: mockInsert });
|
|
113
|
-
(sut as any).db = mockQuery;
|
|
114
|
-
|
|
115
|
-
const result = await sut.saveState(testData);
|
|
116
|
-
|
|
117
|
-
expect(result).toBe(true);
|
|
118
|
-
expect(mockQuery).toHaveBeenCalledWith('params');
|
|
119
|
-
expect(mockInsert).toHaveBeenCalledWith({
|
|
120
|
-
id: 1,
|
|
121
|
-
actions: JSON.stringify(testData.actions),
|
|
122
|
-
lastBlockNumber: 12345
|
|
123
|
-
});
|
|
124
|
-
expect(mockOnConflict).toHaveBeenCalledWith('id');
|
|
125
|
-
expect(mockMerge).toHaveBeenCalled();
|
|
126
|
-
});
|
|
127
|
-
});
|