@kstasi/jest-tolk
Version:
<p align="center"> <picture> <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/tonkite/tonkite/main/assets/logo-dark.svg"> <img alt="tonkite logo" src="https://raw.githubusercontent.com/tonkite/tonkite/main/a
70 lines (69 loc) • 2.69 kB
JavaScript
;
/**
* Copyright 2024 Scaleton Labs
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateSlice = generateSlice;
const core_1 = require("@ton/core");
const node_crypto_1 = require("node:crypto");
function generateSlice(type, fixtures) {
if (fixtures && fixtures.length > 0) {
fixtures.forEach((item) => {
if (item.type !== 'slice') {
throw new Error(`All fixtures must be of type slice, but got ${item.type}`);
}
});
return selectSlice(fixtures);
}
if (type === 'address') {
const EDGE_WEIGHT = 10;
const RANDOM_WEIGHT = 90;
const totalWeight = EDGE_WEIGHT + RANDOM_WEIGHT;
const bias = Math.floor(Math.random() * totalWeight);
if (bias < EDGE_WEIGHT)
return generateEdgeAddress();
return generateRandomAddress();
}
return generateRandomSlice();
}
function generateRandomAddress() {
const workchain = Math.random() < 0.5 ? 0 : -1;
return (0, core_1.beginCell)()
.storeAddress(new core_1.Address(workchain, (0, node_crypto_1.randomBytes)(32)))
.endCell();
}
function generateEdgeAddress() {
const EDGE_CASES = [
core_1.Address.parseRaw('0:0000000000000000000000000000000000000000000000000000000000000000'),
core_1.Address.parseRaw('-1:0000000000000000000000000000000000000000000000000000000000000000'),
];
return (0, core_1.beginCell)()
.storeAddress(EDGE_CASES[Math.floor(Math.random() * EDGE_CASES.length)])
.endCell();
}
function selectSlice(fixtures) {
const fixture = fixtures[Math.floor(Math.random() * fixtures.length)];
return fixture.cell;
}
function generateRandomSlice() {
const builder = (0, core_1.beginCell)();
const size = Math.floor(Math.random() * 127);
const refs = Math.floor(Math.random() * 4);
builder.storeBuffer((0, node_crypto_1.randomBytes)(size));
for (let i = 0; i < refs; i++) {
builder.storeRef((0, core_1.beginCell)().storeBuffer((0, node_crypto_1.randomBytes)(size)));
}
return builder.endCell();
}