79 lines
2.3 KiB
TypeScript
79 lines
2.3 KiB
TypeScript
import { describe, it, expect } from "vitest";
|
|
import { loadStrategy, validateStrategy, substituteBlinds } from "../../src/strategy.js";
|
|
import { BlindValues } from "../../src/strategy.js";
|
|
|
|
describe("Strategy", () => {
|
|
const sampleStrategy = {
|
|
name: "Test Strategy",
|
|
chain: "mainnet",
|
|
steps: [
|
|
{
|
|
id: "step1",
|
|
action: {
|
|
type: "aaveV3.supply",
|
|
asset: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
|
|
amount: "1000000",
|
|
},
|
|
},
|
|
],
|
|
};
|
|
|
|
it("should load a valid strategy", () => {
|
|
// Write to temp file for testing
|
|
const fs = await import("fs");
|
|
const path = await import("path");
|
|
const tempFile = path.join(process.cwd(), "temp-strategy.json");
|
|
fs.writeFileSync(tempFile, JSON.stringify(sampleStrategy));
|
|
|
|
const strategy = loadStrategy(tempFile);
|
|
expect(strategy.name).toBe("Test Strategy");
|
|
expect(strategy.steps.length).toBe(1);
|
|
|
|
// Cleanup
|
|
fs.unlinkSync(tempFile);
|
|
});
|
|
|
|
it("should validate a strategy", () => {
|
|
const validation = validateStrategy(sampleStrategy as any);
|
|
expect(validation.valid).toBe(true);
|
|
expect(validation.errors.length).toBe(0);
|
|
});
|
|
|
|
it("should detect duplicate step IDs", () => {
|
|
const invalidStrategy = {
|
|
...sampleStrategy,
|
|
steps: [
|
|
{ id: "step1", action: sampleStrategy.steps[0].action },
|
|
{ id: "step1", action: sampleStrategy.steps[0].action },
|
|
],
|
|
};
|
|
const validation = validateStrategy(invalidStrategy as any);
|
|
expect(validation.valid).toBe(false);
|
|
expect(validation.errors).toContain("Duplicate step ID: step1");
|
|
});
|
|
|
|
it("should substitute blind values", () => {
|
|
const strategyWithBlinds = {
|
|
...sampleStrategy,
|
|
blinds: [
|
|
{ name: "amount", type: "uint256", description: "Amount to supply" },
|
|
],
|
|
steps: [
|
|
{
|
|
id: "step1",
|
|
action: {
|
|
type: "aaveV3.supply",
|
|
asset: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
|
|
amount: { blind: "amount" },
|
|
},
|
|
},
|
|
],
|
|
};
|
|
|
|
const blindValues: BlindValues = { amount: "1000000" };
|
|
const substituted = substituteBlinds(strategyWithBlinds as any, blindValues);
|
|
expect(substituted.steps[0].action.amount).toBe("1000000");
|
|
});
|
|
});
|
|
|