Skip to content
Open
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
56 changes: 56 additions & 0 deletions .github/workflows/trpc-integration-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
name: tRPC Integration Tests

on:
pull_request:
branches:
- main
- preprod
push:
branches:
- main
workflow_dispatch:

jobs:
trpc-tests:
runs-on: ubuntu-latest
timeout-minutes: 15

services:
postgres:
image: postgres:14-alpine
env:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
POSTGRES_DB: multisig_test
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5

env:
NODE_ENV: test
SKIP_ENV_VALIDATION: "true"
DATABASE_URL: postgresql://postgres:postgres@localhost:5432/multisig_test
DIRECT_URL: postgresql://postgres:postgres@localhost:5432/multisig_test

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20
cache: npm

- name: Install dependencies
run: npm ci

- name: Run database migrations
run: npx prisma migrate deploy

- name: Run tRPC integration tests
run: npm run test:trpc
40 changes: 40 additions & 0 deletions .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
name: Unit Tests

on:
pull_request:
branches:
- main
- preprod
push:
branches:
- main
workflow_dispatch:

jobs:
unit-tests:
name: Transaction builder unit tests
runs-on: ubuntu-latest
timeout-minutes: 10

steps:
- name: Checkout repository
uses: actions/checkout@v4

- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20

- name: Install dependencies
run: npm ci

- name: Run transaction builder tests
run: npm run test:ci -- --testPathPatterns="src/__tests__/tx-builders"

- name: Upload coverage report
if: always()
uses: actions/upload-artifact@v4
with:
name: tx-builder-coverage
path: coverage/
retention-days: 7
4 changes: 4 additions & 0 deletions jest.config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export default {
coverageProvider: 'v8',
coverageDirectory: 'coverage',
coverageReporters: ['text', 'lcov', 'html'],
coverageThreshold: {
'src/utils/stakingCertificates.ts': { lines: 90 },
'src/lib/tx-builders/buildDRepCertTx.ts': { lines: 90 },
},
setupFilesAfterEnv: ['<rootDir>/src/__tests__/setup.ts'],
testTimeout: 10000,
verbose: true,
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"test:watch": "jest --watch",
"test:coverage": "jest --coverage",
"test:ci": "jest --ci --coverage --watchAll=false",
"test:trpc": "jest --testPathPatterns=\"src/__tests__/trpc\" --runInBand",
"analyze": "ANALYZE=true npm run build",
"apply-project": "node scripts/apply-project-to-github.mjs"
},
Expand Down
208 changes: 208 additions & 0 deletions src/__tests__/completeTxWithFreshCostModels.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
import { beforeEach, describe, expect, it, jest } from "@jest/globals";

const insertedLanguages: string[] = [];
const costModelValues: number[][] = [];
const setScriptDataHashMock = jest.fn();
const hashScriptDataMock = jest.fn(() => ({ hash: "fresh-script-data-hash" }));

class MockCostModel {
values: number[] = [];

static new() {
const model = new MockCostModel();
costModelValues.push(model.values);
return model;
}

set(index: number, cost: { value: number }) {
this.values[index] = cost.value;
return cost;
}
}

class MockCostmdls {
static new() {
return new MockCostmdls();
}

insert(language: { label: string }, _costModel: MockCostModel) {
insertedLanguages.push(language.label);
return undefined;
}
}

class MockLanguage {
static new_plutus_v1() {
return { label: "V1" };
}

static new_plutus_v2() {
return { label: "V2" };
}

static new_plutus_v3() {
return { label: "V3" };
}
}

class MockInt {
static new_i32(value: number) {
return { value };
}
}

class MockTransaction {
private static redeemerCount = 0;
private static updatedHex = "updated-tx-hex";

static configure(args: { redeemerCount: number; updatedHex?: string }) {
MockTransaction.redeemerCount = args.redeemerCount;
MockTransaction.updatedHex = args.updatedHex ?? "updated-tx-hex";
}

static from_hex(hex: string) {
return {
witness_set: () => ({
redeemers: () =>
MockTransaction.redeemerCount > 0
? { len: () => MockTransaction.redeemerCount }
: undefined,
plutus_data: () => ({ datum: true }),
}),
body: () => ({ set_script_data_hash: setScriptDataHashMock }),
auxiliary_data: () => ({ metadata: true }),
is_valid: () => true,
to_hex: () => hex,
};
}

static new() {
return {
set_is_valid: jest.fn(),
to_hex: () => MockTransaction.updatedHex,
};
}
}

jest.mock(
"@meshsdk/core-csl",
() => ({
__esModule: true,
csl: {
CostModel: MockCostModel,
Costmdls: MockCostmdls,
Int: MockInt,
Language: MockLanguage,
Transaction: MockTransaction,
hash_script_data: hashScriptDataMock,
},
}),
{ virtual: true },
);

jest.mock(
"@/env",
() => ({
__esModule: true,
env: {
NEXT_PUBLIC_BLOCKFROST_API_KEY_PREPROD: "preprod-key",
NEXT_PUBLIC_BLOCKFROST_API_KEY_MAINNET: "mainnet-key",
},
}),
{ virtual: true },
);

describe("refreshScriptDataHash", () => {
beforeEach(() => {
insertedLanguages.length = 0;
costModelValues.length = 0;
setScriptDataHashMock.mockClear();
hashScriptDataMock.mockClear();
MockTransaction.configure({ redeemerCount: 0 });
});

it("leaves transactions without redeemers unchanged", async () => {
const { refreshScriptDataHash } = await import("@/lib/completeTxWithFreshCostModels");

expect(refreshScriptDataHash("unsigned-tx-hex", {}, {})).toBe("unsigned-tx-hex");
expect(hashScriptDataMock).not.toHaveBeenCalled();
expect(setScriptDataHashMock).not.toHaveBeenCalled();
});

it("recomputes script data hash with Plutus V3 cost_models_raw arrays", async () => {
MockTransaction.configure({ redeemerCount: 1, updatedHex: "fresh-tx-hex" });
const { refreshScriptDataHash } = await import("@/lib/completeTxWithFreshCostModels");

const refreshed = refreshScriptDataHash(
"unsigned-tx-hex",
{
PlutusV3: [10, 20],
},
{
mints: [
{
type: "Plutus",
scriptSource: { script: { version: "V3" } },
},
],
},
);

expect(refreshed).toBe("fresh-tx-hex");
expect(insertedLanguages).toEqual(["V3"]);
expect(costModelValues).toEqual([[10, 20]]);
expect(hashScriptDataMock).toHaveBeenCalled();
expect(setScriptDataHashMock).toHaveBeenCalledWith({ hash: "fresh-script-data-hash" });
});

it("orders indexed cost model objects by numeric key", async () => {
MockTransaction.configure({ redeemerCount: 1, updatedHex: "fresh-tx-hex" });
const { refreshScriptDataHash } = await import("@/lib/completeTxWithFreshCostModels");

refreshScriptDataHash(
"unsigned-tx-hex",
{
PlutusV3: {
"2": 30,
"0": 10,
"1": 20,
},
},
{
mints: [
{
type: "Plutus",
scriptSource: { script: { version: "V3" } },
},
],
},
);

expect(costModelValues).toEqual([[10, 20, 30]]);
});

it("rejects named cost_models objects that are not in ledger order", async () => {
MockTransaction.configure({ redeemerCount: 1 });
const { refreshScriptDataHash } = await import("@/lib/completeTxWithFreshCostModels");

expect(() =>
refreshScriptDataHash(
"unsigned-tx-hex",
{
PlutusV3: {
"addInteger-cpu-arguments-intercept": 10,
"addInteger-cpu-arguments-slope": 20,
},
},
{
mints: [
{
type: "Plutus",
scriptSource: { script: { version: "V3" } },
},
],
},
),
).toThrow(/cost_models_raw/);
});
});
13 changes: 13 additions & 0 deletions src/__tests__/proxyCleanup.bot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const buildProxyCleanupSweepTxMock: jest.Mock = jest.fn();
const buildProxyCleanupTxMock: jest.Mock = jest.fn();
const deriveProxyScriptsMock: jest.Mock = jest.fn();
const createPendingMultisigTransactionMock: jest.Mock = jest.fn();
const completeTxWithFreshCostModelsMock: jest.Mock = jest.fn();
const completeMock: jest.Mock = jest.fn();
const getTxBuilderMock: jest.Mock = jest.fn();
const fetchAddressUTxOsMock: jest.Mock = jest.fn();
Expand Down Expand Up @@ -93,6 +94,11 @@ jest.mock("@/lib/server/createPendingMultisigTransaction", () => ({
createPendingMultisigTransaction: createPendingMultisigTransactionMock,
}), { virtual: true });

jest.mock("@/lib/server/completeTxWithFreshCostModels", () => ({
__esModule: true,
completeTxWithFreshCostModels: completeTxWithFreshCostModelsMock,
}), { virtual: true });

jest.mock("@/utils/get-provider", () => ({
__esModule: true,
getProvider: () => ({ fetchAddressUTxOs: fetchAddressUTxOsMock }),
Expand Down Expand Up @@ -140,6 +146,7 @@ beforeEach(() => {
buildProxyCleanupTxMock.mockReturnValue({ burnedAuthTokens: "10" });
(completeMock as any).mockResolvedValue("tx-cbor");
getTxBuilderMock.mockReturnValue({ complete: completeMock, meshTxBuilderBody: {} });
(completeTxWithFreshCostModelsMock as any).mockResolvedValue("fresh-tx-cbor");
(createPendingMultisigTransactionMock as any).mockResolvedValue({ id: "tx-1" });
});

Expand Down Expand Up @@ -178,9 +185,15 @@ describe("proxyCleanup bot API", () => {
expect.anything(),
expect.objectContaining({
proposerAddress: makeBotJwtPayload().address,
txCbor: "fresh-tx-cbor",
initialSignedAddresses: [],
}),
);
expect(completeTxWithFreshCostModelsMock).toHaveBeenCalledWith(
getTxBuilderMock.mock.results[0]?.value,
0,
);
expect(completeMock).not.toHaveBeenCalled();
expect(buildProxyCleanupTxMock).not.toHaveBeenCalled();
expect(res.status).toHaveBeenCalledWith(201);
expect(res.json).toHaveBeenCalledWith({
Expand Down
Loading
Loading