Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Brody/mocking setup #1726

Closed
wants to merge 15 commits into from
Closed
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
3 changes: 3 additions & 0 deletions e2e/mockServer.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { Server } from 'http';

export declare function startMockServer(): Promise<Server>;
124 changes: 124 additions & 0 deletions e2e/mockServer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
/* eslint-disable @typescript-eslint/no-var-requires */
const fs = require('fs').promises;
const path = require('path');

const express = require('express');

const app = express();
const PORT = 3001;

/**
* @typedef {import('express').Request} Request
* @typedef {import('express').Response} Response
* @typedef {import('http').Server} Server
*/

// Add request logging middleware
app.use((req, res, next) => {
console.log(`\n�incoming Request: ${req.method} ${req.path}`);
console.log('Query Parameters:', req.query);
next();
});

/**
* Middleware to serve JSON files based on request paths and query parameters.
* @param {Request} req - Express request object
* @param {Response} res - Express response object
*/
app.use(async (req, res) => {
try {
const filePath = req.path.slice(1).replace(/\//g, '_');
let filename = '_' + filePath;
for (const [key, value] of Object.entries(req.query)) {
filename += `_${key}_${value}`;
}
filename = filename.slice(0, 200) + '.json';

const dir = path.join(__dirname, 'responses');
const jsonPath = path.join(dir, filename);

console.log('\n📂 File Operation Details:');
console.log('Directory:', dir);
console.log('Generated Filename:', filename);
console.log('Full Path:', jsonPath);

// Check if directory exists
try {
await fs.access(dir);
console.log('✅ Responses directory found');
} catch (error) {
console.error('❌ Responses directory not found:', error.message);
return res.status(500).json({
error: 'Server configuration error',
details: 'Responses directory not found',
});
}

// Check if file exists before trying to read it
try {
await fs.access(jsonPath);
console.log('✅ Response file found');
} catch (error) {
console.error('❌ Response file not found:', error.message);
return res.status(404).json({
error: 'Response not found',
details: 'No matching response file found for this request',
});
}

// Read and parse the file
try {
const data = await fs.readFile(jsonPath, 'utf8');
console.log('✅ File successfully read');

const parsedData = JSON.parse(data);
console.log('✅ JSON successfully parsed');

res.json(parsedData);
console.log('✅ Response sent successfully');
} catch (error) {
console.error('❌ Error reading or parsing file:', error.message);
return res.status(500).json({
error: 'File processing error',
details: error.message,
});
}
} catch (error) {
console.error('\n❌ Unexpected error:', error.message);
res.status(500).json({
error: 'Server error',
details: error.message,
});
}
});

/**
* Starts the mock server.
* @returns {Promise<Server|null>} A promise that resolves to the HTTP server instance or null if the server is already running.
*/
const startMockServer = () => {
return new Promise((resolve) => {
const server = app.listen(PORT, () => {
console.log('\n🚀 Mock API server running on http://localhost:' + PORT);
resolve(server);
});

server.on('error', (error) => {
if (error.code === 'EADDRINUSE') {
console.error(
`\n❌ Port ${PORT} already in use, not starting a new server`,
);
resolve(null);
} else {
console.error('\n❌ Error starting server:', error);
resolve(null);
}
});
});
};

if (require.main === module) {
startMockServer();
}

module.exports = { startMockServer };
3 changes: 2 additions & 1 deletion e2e/parallel/vitest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export default mergeConfig(
defineConfig({
test: {
bail: 1,
threads: false,
isolate: false,
fileParallelism: false,
},
}) as UserConfig,
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
{
"sellTokenAddress": "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
"sellTokenAsset": {
"assetCode": "eth",
"decimals": 18,
"iconUrl": "https://rainbowme-res.cloudinary.com/image/upload/v1668565116/assets/ethereum/eth.png",
"name": "Ethereum",
"network": "mainnet",
"symbol": "ETH",
"networks": {
"1": {
"address": "eth",
"decimals": 18
},
"10": {
"address": "0x0000000000000000000000000000000000000000",
"decimals": 18
},
"42161": {
"address": "0x0000000000000000000000000000000000000000",
"decimals": 18
},
"56": {
"address": "0x2170ed0880ac9a755fd29b2688956bd959f933f8",
"decimals": 18
},
"7777777": {
"address": "0x0000000000000000000000000000000000000000",
"decimals": 18
},
"81457": {
"address": "0x0000000000000000000000000000000000000000",
"decimals": 18
},
"8453": {
"address": "0x0000000000000000000000000000000000000000",
"decimals": 18
}
},
"chainId": 1,
"price": {
"value": 2497.65,
"available": true
},
"totalPrice": {
"value": 2497.65,
"available": true
}
},
"buyTokenAddress": "0xff970a61a04b1ca14834a43f5de4533ebddb5cc8",
"buyTokenAsset": {
"assetCode": "0xff970a61a04b1ca14834a43f5de4533ebddb5cc8",
"decimals": 6,
"iconUrl": "https://rainbowme-res.cloudinary.com/image/upload/v1668633498/assets/ethereum/0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48.png",
"name": "USD Coin (Bridged)",
"network": "arbitrum",
"symbol": "USDCe",
"networks": {
"10": {
"address": "0x7f5c764cbc14f9669b88837ca1490cca17c31607",
"decimals": 6
},
"137": {
"address": "0x2791bca1f2de4661ed88a30c99a7a9449aa84174",
"decimals": 6
},
"42161": {
"address": "0xff970a61a04b1ca14834a43f5de4533ebddb5cc8",
"decimals": 6
},
"43114": {
"address": "0xa7d7079b0fead91f3e65f86e8915cb59c1a4c664",
"decimals": 6
}
},
"chainId": 42161,
"price": {
"value": 1.0002808658027897,
"available": true
},
"totalPrice": {
"value": 2469.4951229772973,
"available": true
}
},
"allowanceTarget": "0xa5f565650890fba1824ee0f21ebbbf660a179934",
"allowanceNeeded": false,
"to": "0xa5f565650890fba1824ee0f21ebbbf660a179934",
"data": "0x62df939ac48d9016703f662e03176448a557d06b39b4095d52f3fc718baab010",
"sellAmount": "1000000000000000000",
"sellAmountMinusFees": "989000000000000000",
"sellAmountDisplay": "1000000000000000000",
"sellAmountInEth": "1000000000000000000",
"buyAmount": "2468801721",
"buyAmountMinusFees": "2468801721",
"buyAmountDisplay": "2468801721",
"buyAmountDisplayMinimum": "2468801721",
"buyAmountInEth": "988727452996735908",
"tradeAmountUSD": 2497.65,
"tradeFeeAmountUSD": 27.47415,
"value": "1000000000000000000",
"gasPrice": "",
"source": "relay",
"protocols": [
{
"name": "relay",
"part": 100
}
],
"feeTokenAsset": {
"assetCode": "eth",
"decimals": 18,
"iconUrl": "https://rainbowme-res.cloudinary.com/image/upload/v1668565116/assets/ethereum/eth.png",
"name": "Ethereum",
"network": "mainnet",
"symbol": "ETH",
"networks": {
"1": {
"address": "eth",
"decimals": 18
},
"10": {
"address": "0x0000000000000000000000000000000000000000",
"decimals": 18
},
"42161": {
"address": "0x0000000000000000000000000000000000000000",
"decimals": 18
},
"56": {
"address": "0x2170ed0880ac9a755fd29b2688956bd959f933f8",
"decimals": 18
},
"7777777": {
"address": "0x0000000000000000000000000000000000000000",
"decimals": 18
},
"81457": {
"address": "0x0000000000000000000000000000000000000000",
"decimals": 18
},
"8453": {
"address": "0x0000000000000000000000000000000000000000",
"decimals": 18
}
},
"chainId": 1,
"price": {
"value": 2497.65,
"available": true
},
"totalPrice": {
"value": 27.47415,
"available": true
}
},
"fee": "11000000000000000",
"feeInEth": "11000000000000000",
"feePercentageBasisPoints": "11000000000000000",
"tradeType": "exact_input",
"from": "0x3c44cdddb6a900fa2b585dd299e03d12fa4293bc",
"defaultGasLimit": "70000",
"swapType": "cross-chain",
"txTarget": "0xa5f565650890fba1824ee0f21ebbbf660a179934",
"chainId": 1,
"routes": [],
"refuel": null,
"no_approval": true
}
Loading
Loading