Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions backend/src/api/controllers/archived-vaults.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { describe, it, expect, beforeEach, vi } from "vitest";
import type { Request, Response, NextFunction } from "express";
import { getArchivedVaults } from "./admin.js";
import * as vault from "../../services/vault.js";

vi.mock("../../services/vault.js", () => ({
VaultService: vi.fn().mockImplementation(() => ({
Expand Down Expand Up @@ -82,17 +81,19 @@ describe("getArchivedVaults (#675)", () => {
},
];

const mockVaultService = new vault.VaultService();
vi.mocked(mockVaultService.listArchivedVaults).mockResolvedValue(mockVaults);
const { VaultService } = await import("../../services/vault.js");
const mockListArchivedVaults = vi.fn().mockResolvedValue(mockVaults);
vi.mocked(VaultService).mockReturnValue({ listArchivedVaults: mockListArchivedVaults });

await getArchivedVaults(mockReq as Request, mockRes as Response, mockNext);

expect(mockJson).toHaveBeenCalledWith(mockVaults);
});

it("returns empty array when no archived vaults exist", async () => {
const mockVaultService = new vault.VaultService();
vi.mocked(mockVaultService.listArchivedVaults).mockResolvedValue([]);
const { VaultService } = await import("../../services/vault.js");
const mockListArchivedVaults = vi.fn().mockResolvedValue([]);
vi.mocked(VaultService).mockReturnValue({ listArchivedVaults: mockListArchivedVaults });

await getArchivedVaults(mockReq as Request, mockRes as Response, mockNext);

Expand All @@ -101,8 +102,9 @@ describe("getArchivedVaults (#675)", () => {

it("calls next on error", async () => {
const error = new Error("Database error");
const mockVaultService = new vault.VaultService();
vi.mocked(mockVaultService.listArchivedVaults).mockRejectedValue(error);
const { VaultService } = await import("../../services/vault.js");
const mockListArchivedVaults = vi.fn().mockRejectedValue(error);
vi.mocked(VaultService).mockReturnValue({ listArchivedVaults: mockListArchivedVaults });

await getArchivedVaults(mockReq as Request, mockRes as Response, mockNext);

Expand Down
26 changes: 14 additions & 12 deletions backend/src/api/controllers/consistency-total-supply.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ describe("getTotalSupplyConsistency (#673)", () => {
const { VaultService } = await import("../../services/vault.js");
const { readTotalSupply } = await import("../../services/stellar.js");

const mockVaultService = new VaultService();
vi.mocked(mockVaultService.getVault).mockResolvedValue({
const mockGetVault = vi.fn().mockResolvedValue({
id: 1,
contractId: CONTRACT_ID,
factoryId: null,
Expand All @@ -72,6 +71,7 @@ describe("getTotalSupplyConsistency (#673)", () => {
createdAt: new Date(),
updatedAt: new Date(),
});
vi.mocked(VaultService).mockReturnValue({ getVault: mockGetVault });

vi.mocked(readTotalSupply).mockResolvedValue(5000n);

Expand All @@ -91,8 +91,7 @@ describe("getTotalSupplyConsistency (#673)", () => {
const { VaultService } = await import("../../services/vault.js");
const { readTotalSupply } = await import("../../services/stellar.js");

const mockVaultService = new VaultService();
vi.mocked(mockVaultService.getVault).mockResolvedValue({
const mockGetVault2 = vi.fn().mockResolvedValue({
id: 1,
contractId: CONTRACT_ID,
factoryId: null,
Expand All @@ -118,6 +117,7 @@ describe("getTotalSupplyConsistency (#673)", () => {
createdAt: new Date(),
updatedAt: new Date(),
});
vi.mocked(VaultService).mockReturnValue({ getVault: mockGetVault2 });

vi.mocked(readTotalSupply).mockResolvedValue(5000n);

Expand All @@ -137,8 +137,7 @@ describe("getTotalSupplyConsistency (#673)", () => {
const { VaultService } = await import("../../services/vault.js");
const { readTotalSupply } = await import("../../services/stellar.js");

const mockVaultService = new VaultService();
vi.mocked(mockVaultService.getVault).mockResolvedValue({
const mockGetVault3 = vi.fn().mockResolvedValue({
id: 1,
contractId: CONTRACT_ID,
factoryId: null,
Expand All @@ -165,6 +164,8 @@ describe("getTotalSupplyConsistency (#673)", () => {
updatedAt: new Date(),
});

vi.mocked(VaultService).mockReturnValue({ getVault: mockGetVault3 });

vi.mocked(readTotalSupply).mockResolvedValue(5000n);

await getTotalSupplyConsistency(mockReq as Request, mockRes as Response, mockNext);
Expand All @@ -183,8 +184,7 @@ describe("getTotalSupplyConsistency (#673)", () => {
const { VaultService } = await import("../../services/vault.js");
const { readTotalSupply } = await import("../../services/stellar.js");

const mockVaultService = new VaultService();
vi.mocked(mockVaultService.getVault).mockResolvedValue({
const mockGetVault4 = vi.fn().mockResolvedValue({
id: 1,
contractId: CONTRACT_ID,
factoryId: null,
Expand All @@ -211,6 +211,8 @@ describe("getTotalSupplyConsistency (#673)", () => {
updatedAt: new Date(),
});

vi.mocked(VaultService).mockReturnValue({ getVault: mockGetVault4 });

vi.mocked(readTotalSupply).mockResolvedValue(5100n);

await getTotalSupplyConsistency(mockReq as Request, mockRes as Response, mockNext);
Expand All @@ -236,8 +238,8 @@ describe("getTotalSupplyConsistency (#673)", () => {
mockReq.query = { contractId: CONTRACT_ID };

const { VaultService } = await import("../../services/vault.js");
const mockVaultService = new VaultService();
vi.mocked(mockVaultService.getVault).mockResolvedValue(null);
const mockGetVaultNull = vi.fn().mockResolvedValue(null);
vi.mocked(VaultService).mockReturnValue({ getVault: mockGetVaultNull });

await getTotalSupplyConsistency(mockReq as Request, mockRes as Response, mockNext);

Expand All @@ -254,8 +256,7 @@ describe("getTotalSupplyConsistency (#673)", () => {
const { VaultService } = await import("../../services/vault.js");
const { readTotalSupply } = await import("../../services/stellar.js");

const mockVaultService = new VaultService();
vi.mocked(mockVaultService.getVault).mockResolvedValue({
const mockGetVault5 = vi.fn().mockResolvedValue({
id: 1,
contractId: CONTRACT_ID,
factoryId: null,
Expand All @@ -282,6 +283,7 @@ describe("getTotalSupplyConsistency (#673)", () => {
updatedAt: new Date(),
});

vi.mocked(VaultService).mockReturnValue({ getVault: mockGetVault5 });
vi.mocked(readTotalSupply).mockRejectedValue(new Error("RPC timeout"));

await getTotalSupplyConsistency(mockReq as Request, mockRes as Response, mockNext);
Expand Down
5 changes: 5 additions & 0 deletions backend/src/api/controllers/webhooks.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { describe, it, expect, vi, beforeEach } from "vitest";

vi.mock("../../db/index.js", () => ({ query: vi.fn() }));
vi.mock("../../services/notifications.js", () => ({
validateWebhookUrl: vi.fn().mockResolvedValue(undefined),
NotificationService: vi.fn().mockImplementation(() => ({})),
}));

async function getTestContext() {
const { query } = await import("../../db/index.js");
Expand Down Expand Up @@ -42,6 +46,7 @@ describe("Webhook Controller", () => {
url: "https://example.com/hook",
events: ["deposit"],
active: true,
consecutiveFailures: 0,
createdAt: row.created_at,
});
});
Expand Down
2 changes: 1 addition & 1 deletion backend/src/deposit-indexing.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { describe, it, expect, vi, beforeEach } from "vitest";

vi.mock("./db/index.js", () => ({ query: vi.fn().mockResolvedValue([]) }));
vi.mock("./logger.js", () => ({
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn() },
logger: { info: vi.fn(), warn: vi.fn(), error: vi.fn(), debug: vi.fn(), child: vi.fn().mockReturnThis() },
}));
vi.mock("./services/stellar.js", () => ({ getSorobanRpc: vi.fn() }));
vi.mock("./services/vault.js", () => ({
Expand Down
12 changes: 6 additions & 6 deletions backend/src/services/indexer-lag-alert.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ describe("Indexer lag alert (#672)", () => {
getEvents: ReturnType<typeof vi.fn>;
};

beforeEach(() => {
beforeEach(async () => {
vi.clearAllMocks();
mockServer = {
getLatestLedger: vi.fn(),
getEvents: vi.fn().mockResolvedValue({ events: [], latestLedger: 1000 }),
};
const { getSorobanRpc } = require("./stellar.js");
const { getSorobanRpc } = await import("./stellar.js");
(getSorobanRpc as any).mockReturnValue(mockServer);
indexer = new Indexer();
indexer["lastLedger"] = 900;
Expand Down Expand Up @@ -75,11 +75,11 @@ describe("Indexer lag alert (#672)", () => {

it("logs error on every tick when lagging", async () => {
mockServer.getLatestLedger.mockResolvedValue({ sequence: 1050 });
indexer["lastLedger"] = 900;

await indexer.tick();
await indexer.tick();
await indexer.tick();
for (let i = 0; i < 3; i++) {
indexer["lastLedger"] = 900;
await indexer.tick();
}

expect(logger.error).toHaveBeenCalledTimes(3);
});
Expand Down
38 changes: 0 additions & 38 deletions backend/src/services/indexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1028,44 +1028,6 @@ export class Indexer {
);
}

private async handleOperatorAdded(
contractId: string,
ev: { operator: string; timestamp: bigint },
): Promise<void> {
const vaultRow = await query<{ id: number }>(
"SELECT id FROM vaults WHERE contract_id = $1",
[contractId],
);
if (vaultRow.length === 0) return;
const vaultId = vaultRow[0].id;
const assignedAt = new Date(Number(ev.timestamp) * 1000);
await query(
`INSERT INTO vault_operators (vault_id, address, active, assigned_at, updated_at)
VALUES ($1, $2, TRUE, $3, NOW())
ON CONFLICT (vault_id, address) DO UPDATE SET active = TRUE, updated_at = NOW()`,
[vaultId, ev.operator, assignedAt],
);
logger.info({ contractId, operator: ev.operator }, "Processed op_add event");
}

private async handleOperatorRemoved(
contractId: string,
ev: { operator: string },
): Promise<void> {
const vaultRow = await query<{ id: number }>(
"SELECT id FROM vaults WHERE contract_id = $1",
[contractId],
);
if (vaultRow.length === 0) return;
const vaultId = vaultRow[0].id;
await query(
`UPDATE vault_operators SET active = FALSE, updated_at = NOW()
WHERE vault_id = $1 AND address = $2`,
[vaultId, ev.operator],
);
logger.info({ contractId, operator: ev.operator }, "Processed op_rem event");
}

private async handleZkmeVerifierUpdated(
contractId: string,
ev: { newVerifier: string },
Expand Down
Loading
Loading