Initial commit: add .gitignore and README
This commit is contained in:
101
tests/integration/guards.test.ts
Normal file
101
tests/integration/guards.test.ts
Normal file
@@ -0,0 +1,101 @@
|
||||
import { describe, it, expect } from "vitest";
|
||||
import { evaluateGuard } from "../../src/planner/guards.js";
|
||||
import { GuardContext } from "../../src/planner/guards.js";
|
||||
import { Guard } from "../../src/strategy.schema.js";
|
||||
import { PriceOracle } from "../../src/pricing/index.js";
|
||||
import { AaveV3Adapter } from "../../src/adapters/aaveV3.js";
|
||||
|
||||
describe("Guard Integration", () => {
|
||||
it("should evaluate multiple guards in sequence", async () => {
|
||||
const guards: Guard[] = [
|
||||
{
|
||||
type: "maxGas",
|
||||
params: {
|
||||
maxGasLimit: "5000000",
|
||||
},
|
||||
},
|
||||
{
|
||||
type: "slippage",
|
||||
params: {
|
||||
maxBps: 50,
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const context: GuardContext = {
|
||||
chainName: "mainnet",
|
||||
gasEstimate: {
|
||||
total: 2000000n,
|
||||
perCall: [1000000n],
|
||||
},
|
||||
};
|
||||
|
||||
for (const guard of guards) {
|
||||
const result = await evaluateGuard(guard, context);
|
||||
expect(result).toBeDefined();
|
||||
expect(result.passed).toBeDefined();
|
||||
}
|
||||
});
|
||||
|
||||
it("should handle guard failure with revert action", async () => {
|
||||
const guard: Guard = {
|
||||
type: "maxGas",
|
||||
params: {
|
||||
maxGasLimit: "1000000", // Very low limit
|
||||
},
|
||||
onFailure: "revert",
|
||||
};
|
||||
|
||||
const context: GuardContext = {
|
||||
chainName: "mainnet",
|
||||
gasEstimate: {
|
||||
total: 2000000n, // Exceeds limit
|
||||
perCall: [2000000n],
|
||||
},
|
||||
};
|
||||
|
||||
const result = await evaluateGuard(guard, context);
|
||||
expect(result.passed).toBe(false);
|
||||
expect(result.guard.onFailure).toBe("revert");
|
||||
});
|
||||
|
||||
it("should handle guard failure with warn action", async () => {
|
||||
const guard: Guard = {
|
||||
type: "slippage",
|
||||
params: {
|
||||
maxBps: 10, // Very tight slippage
|
||||
},
|
||||
onFailure: "warn",
|
||||
};
|
||||
|
||||
const context: GuardContext = {
|
||||
chainName: "mainnet",
|
||||
amountIn: 1000000n,
|
||||
amountOut: 900000n, // 10% slippage
|
||||
};
|
||||
|
||||
const result = await evaluateGuard(guard, context);
|
||||
expect(result.passed).toBe(false);
|
||||
expect(result.guard.onFailure).toBe("warn");
|
||||
});
|
||||
|
||||
it("should handle missing context gracefully", async () => {
|
||||
const guard: Guard = {
|
||||
type: "oracleSanity",
|
||||
params: {
|
||||
token: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
|
||||
maxDeviationBps: 500,
|
||||
},
|
||||
};
|
||||
|
||||
const context: GuardContext = {
|
||||
chainName: "mainnet",
|
||||
// Missing oracle
|
||||
};
|
||||
|
||||
const result = await evaluateGuard(guard, context);
|
||||
expect(result.passed).toBe(false);
|
||||
expect(result.reason).toContain("not available");
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user