Files
strategic/tests/unit/guards/maxGas.test.ts
2026-02-09 21:51:54 -08:00

84 lines
2.1 KiB
TypeScript

import { describe, it, expect } from "vitest";
import { evaluateMaxGas } from "../../../src/guards/maxGas.js";
import { Guard } from "../../../src/strategy.schema.js";
import { GasEstimate } from "../../../src/utils/gas.js";
describe("Max Gas Guard", () => {
it("should pass when gas is below limit", () => {
const guard: Guard = {
type: "maxGas",
params: {
maxGasLimit: "5000000",
},
};
const gasEstimate: GasEstimate = {
gasLimit: 2000000n,
maxFeePerGas: 100000000000n,
maxPriorityFeePerGas: 2000000000n,
};
const result = evaluateMaxGas(guard, gasEstimate, "mainnet");
expect(result.passed).toBe(true);
});
it("should fail when gas exceeds limit", () => {
const guard: Guard = {
type: "maxGas",
params: {
maxGasLimit: "2000000",
},
};
const gasEstimate: GasEstimate = {
gasLimit: 3000000n,
maxFeePerGas: 100000000000n,
maxPriorityFeePerGas: 2000000000n,
};
const result = evaluateMaxGas(guard, gasEstimate, "mainnet");
expect(result.passed).toBe(false);
expect(result.reason).toContain("exceeds");
});
it("should handle gas price limits", () => {
const guard: Guard = {
type: "maxGas",
params: {
maxGasLimit: "5000000",
maxGasPrice: "100000000000", // 100 gwei
},
};
const gasEstimate: GasEstimate = {
gasLimit: 2000000n,
maxFeePerGas: 50000000000n, // 50 gwei
maxPriorityFeePerGas: 2000000000n,
};
const result = evaluateMaxGas(guard, gasEstimate, "mainnet");
expect(result.passed).toBe(true);
});
it("should fail when gas price exceeds limit", () => {
const guard: Guard = {
type: "maxGas",
params: {
maxGasLimit: "5000000",
maxGasPrice: "100000000000", // 100 gwei
},
};
const gasEstimate: GasEstimate = {
gasLimit: 2000000n,
maxFeePerGas: 150000000000n, // 150 gwei
maxPriorityFeePerGas: 2000000000n,
};
const result = evaluateMaxGas(guard, gasEstimate, "mainnet");
expect(result.passed).toBe(false);
expect(result.reason).toContain("gas price");
});
});