Initial commit: add .gitignore and README
This commit is contained in:
70
tests/unit/guards/minHealthFactor.test.ts
Normal file
70
tests/unit/guards/minHealthFactor.test.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
import { describe, it, expect, vi, beforeEach } from "vitest";
|
||||
import { evaluateMinHealthFactor } from "../../../src/guards/minHealthFactor.js";
|
||||
import { AaveV3Adapter } from "../../../src/adapters/aaveV3.js";
|
||||
import { Guard } from "../../../src/strategy.schema.js";
|
||||
|
||||
describe("Min Health Factor Guard", () => {
|
||||
let mockAave: AaveV3Adapter;
|
||||
let mockProvider: any;
|
||||
|
||||
beforeEach(() => {
|
||||
mockProvider = {
|
||||
getNetwork: vi.fn().mockResolvedValue({ chainId: 1n }),
|
||||
};
|
||||
|
||||
mockAave = new AaveV3Adapter("mainnet");
|
||||
// @ts-ignore - access private property for testing
|
||||
mockAave.provider = mockProvider;
|
||||
});
|
||||
|
||||
it("should pass when health factor is above minimum", async () => {
|
||||
const guard: Guard = {
|
||||
type: "minHealthFactor",
|
||||
params: {
|
||||
minHF: 1.2,
|
||||
user: "0x1234567890123456789012345678901234567890",
|
||||
},
|
||||
};
|
||||
|
||||
// Mock health factor calculation
|
||||
vi.spyOn(mockAave, "getUserHealthFactor").mockResolvedValue(1.5);
|
||||
|
||||
const result = await evaluateMinHealthFactor(guard, mockAave, {});
|
||||
expect(result.passed).toBe(true);
|
||||
});
|
||||
|
||||
it("should fail when health factor is below minimum", async () => {
|
||||
const guard: Guard = {
|
||||
type: "minHealthFactor",
|
||||
params: {
|
||||
minHF: 1.2,
|
||||
user: "0x1234567890123456789012345678901234567890",
|
||||
},
|
||||
};
|
||||
|
||||
// Mock health factor below minimum
|
||||
vi.spyOn(mockAave, "getUserHealthFactor").mockResolvedValue(1.1);
|
||||
|
||||
const result = await evaluateMinHealthFactor(guard, mockAave, {});
|
||||
expect(result.passed).toBe(false);
|
||||
expect(result.reason).toContain("health factor");
|
||||
});
|
||||
|
||||
it("should handle missing user position", async () => {
|
||||
const guard: Guard = {
|
||||
type: "minHealthFactor",
|
||||
params: {
|
||||
minHF: 1.2,
|
||||
user: "0x0000000000000000000000000000000000000000",
|
||||
},
|
||||
};
|
||||
|
||||
// Mock no position
|
||||
vi.spyOn(mockAave, "getUserHealthFactor").mockResolvedValue(0);
|
||||
|
||||
const result = await evaluateMinHealthFactor(guard, mockAave, {});
|
||||
expect(result.passed).toBe(false);
|
||||
expect(result.reason).toBeDefined();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user