|
| 1 | +import { migrate } from "drizzle-orm/node-postgres/migrator"; |
| 2 | +import { beforeEach, describe, expect, it, vi } from "vitest"; |
| 3 | + |
| 4 | +import { |
| 5 | + deserializeCrossChainIndexingStatusSnapshot, |
| 6 | + serializeCrossChainIndexingStatusSnapshot, |
| 7 | + serializeEnsIndexerPublicConfig, |
| 8 | +} from "@ensnode/ensnode-sdk"; |
| 9 | + |
| 10 | +import * as ensNodeSchema from "../ensnode"; |
| 11 | +import { buildEnsDbDrizzleClient } from "../lib/drizzle"; |
| 12 | +import * as ensDbClientMock from "./ensdb-client.mock"; |
| 13 | +import { EnsDbWriter } from "./ensdb-writer"; |
| 14 | +import { EnsNodeMetadataKeys } from "./ensnode-metadata"; |
| 15 | + |
| 16 | +// Mock the config module to prevent it from trying to load actual environment variables during tests |
| 17 | +vi.mock("@/config", () => ({ default: {} })); |
| 18 | + |
| 19 | +// Mock the buildEnsDbDrizzleClient function to return a mock database instance |
| 20 | +vi.mock("../lib/drizzle", () => ({ buildEnsDbDrizzleClient: vi.fn() })); |
| 21 | + |
| 22 | +// Mock the drizzle-orm migrator |
| 23 | +vi.mock("drizzle-orm/node-postgres/migrator", () => ({ migrate: vi.fn() })); |
| 24 | + |
| 25 | +describe("EnsDbWriter", () => { |
| 26 | + // Mock database query results and methods |
| 27 | + const selectResult = { current: [] as Array<{ value: unknown }> }; |
| 28 | + const whereMock = vi.fn(async () => selectResult.current); |
| 29 | + const fromMock = vi.fn(() => ({ where: whereMock })); |
| 30 | + const selectMock = vi.fn(() => ({ from: fromMock })); |
| 31 | + const onConflictDoUpdateMock = vi.fn(async () => undefined); |
| 32 | + const valuesMock = vi.fn(() => ({ onConflictDoUpdate: onConflictDoUpdateMock })); |
| 33 | + const insertMock = vi.fn(() => ({ values: valuesMock })); |
| 34 | + const executeMock = vi.fn(async () => undefined); |
| 35 | + const txMock = { insert: insertMock, execute: executeMock }; |
| 36 | + const transactionMock = vi.fn(async (callback: (tx: typeof txMock) => Promise<void>) => |
| 37 | + callback(txMock), |
| 38 | + ); |
| 39 | + const dbMock = { select: selectMock, insert: insertMock, transaction: transactionMock }; |
| 40 | + |
| 41 | + beforeEach(() => { |
| 42 | + selectResult.current = []; |
| 43 | + whereMock.mockClear(); |
| 44 | + fromMock.mockClear(); |
| 45 | + selectMock.mockClear(); |
| 46 | + onConflictDoUpdateMock.mockClear(); |
| 47 | + valuesMock.mockClear(); |
| 48 | + insertMock.mockClear(); |
| 49 | + executeMock.mockClear(); |
| 50 | + transactionMock.mockClear(); |
| 51 | + vi.mocked(migrate).mockClear(); |
| 52 | + vi.mocked(buildEnsDbDrizzleClient).mockReturnValue( |
| 53 | + dbMock as unknown as ReturnType<typeof buildEnsDbDrizzleClient>, |
| 54 | + ); |
| 55 | + }); |
| 56 | + |
| 57 | + describe("upsertEnsDbVersion", () => { |
| 58 | + it("writes the database version metadata", async () => { |
| 59 | + // arrange |
| 60 | + const client = new EnsDbWriter( |
| 61 | + ensDbClientMock.ensDbUrl, |
| 62 | + ensDbClientMock.ensIndexerSchemaName, |
| 63 | + ); |
| 64 | + |
| 65 | + // act |
| 66 | + await client.upsertEnsDbVersion("0.2.0"); |
| 67 | + |
| 68 | + // assert |
| 69 | + expect(insertMock).toHaveBeenCalledWith(ensNodeSchema.metadata); |
| 70 | + expect(valuesMock).toHaveBeenCalledWith({ |
| 71 | + ensIndexerSchemaName: ensDbClientMock.ensIndexerSchemaName, |
| 72 | + key: EnsNodeMetadataKeys.EnsDbVersion, |
| 73 | + value: "0.2.0", |
| 74 | + }); |
| 75 | + expect(onConflictDoUpdateMock).toHaveBeenCalledWith({ |
| 76 | + target: [ensNodeSchema.metadata.ensIndexerSchemaName, ensNodeSchema.metadata.key], |
| 77 | + set: { value: "0.2.0" }, |
| 78 | + }); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe("upsertEnsIndexerPublicConfig", () => { |
| 83 | + it("serializes and writes the public config", async () => { |
| 84 | + // arrange |
| 85 | + const client = new EnsDbWriter( |
| 86 | + ensDbClientMock.ensDbUrl, |
| 87 | + ensDbClientMock.ensIndexerSchemaName, |
| 88 | + ); |
| 89 | + const expectedValue = serializeEnsIndexerPublicConfig(ensDbClientMock.publicConfig); |
| 90 | + |
| 91 | + // act |
| 92 | + await client.upsertEnsIndexerPublicConfig(ensDbClientMock.publicConfig); |
| 93 | + |
| 94 | + // assert |
| 95 | + expect(valuesMock).toHaveBeenCalledWith({ |
| 96 | + ensIndexerSchemaName: ensDbClientMock.ensIndexerSchemaName, |
| 97 | + key: EnsNodeMetadataKeys.EnsIndexerPublicConfig, |
| 98 | + value: expectedValue, |
| 99 | + }); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + describe("upsertIndexingStatusSnapshot", () => { |
| 104 | + it("serializes and writes the indexing status snapshot", async () => { |
| 105 | + // arrange |
| 106 | + const client = new EnsDbWriter( |
| 107 | + ensDbClientMock.ensDbUrl, |
| 108 | + ensDbClientMock.ensIndexerSchemaName, |
| 109 | + ); |
| 110 | + const snapshot = deserializeCrossChainIndexingStatusSnapshot( |
| 111 | + ensDbClientMock.serializedSnapshot, |
| 112 | + ); |
| 113 | + const expectedValue = serializeCrossChainIndexingStatusSnapshot(snapshot); |
| 114 | + |
| 115 | + // act |
| 116 | + await client.upsertIndexingStatusSnapshot(snapshot); |
| 117 | + |
| 118 | + // assert |
| 119 | + expect(valuesMock).toHaveBeenCalledWith({ |
| 120 | + ensIndexerSchemaName: ensDbClientMock.ensIndexerSchemaName, |
| 121 | + key: EnsNodeMetadataKeys.EnsIndexerIndexingStatus, |
| 122 | + value: expectedValue, |
| 123 | + }); |
| 124 | + }); |
| 125 | + }); |
| 126 | + |
| 127 | + describe("migrate", () => { |
| 128 | + it("calls drizzle-orm migrate with the correct parameters", async () => { |
| 129 | + // arrange |
| 130 | + const client = new EnsDbWriter( |
| 131 | + ensDbClientMock.ensDbUrl, |
| 132 | + ensDbClientMock.ensIndexerSchemaName, |
| 133 | + ); |
| 134 | + const migrationsDirPath = "/path/to/migrations"; |
| 135 | + |
| 136 | + // act |
| 137 | + await client.migrate(migrationsDirPath); |
| 138 | + |
| 139 | + // assert |
| 140 | + expect(vi.mocked(migrate)).toHaveBeenCalledWith(dbMock, { |
| 141 | + migrationsFolder: migrationsDirPath, |
| 142 | + migrationsSchema: "ensnode", |
| 143 | + }); |
| 144 | + }); |
| 145 | + |
| 146 | + it("propagates errors from the migrate function", async () => { |
| 147 | + // arrange |
| 148 | + const client = new EnsDbWriter( |
| 149 | + ensDbClientMock.ensDbUrl, |
| 150 | + ensDbClientMock.ensIndexerSchemaName, |
| 151 | + ); |
| 152 | + const migrationsDirPath = "/path/to/migrations"; |
| 153 | + const error = new Error("Migration failed"); |
| 154 | + vi.mocked(migrate).mockRejectedValueOnce(error); |
| 155 | + |
| 156 | + // act & assert |
| 157 | + await expect(client.migrate(migrationsDirPath)).rejects.toThrow("Migration failed"); |
| 158 | + }); |
| 159 | + }); |
| 160 | +}); |
0 commit comments