Changeset View
Changeset View
Standalone View
Standalone View
apps/alias-server/scripts/generateMocks.js
// Copyright (c) 2023 The Bitcoin developers | // Copyright (c) 2023 The Bitcoin developers | ||||
bytesofman: Note: This file was not updated in line with other refactor changes we made to the app, as it… | |||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
'use strict'; | 'use strict'; | ||||
const fs = require('fs'); | const fs = require('fs'); | ||||
const path = require('path'); | const path = require('path'); | ||||
const config = require('../config'); | const config = require('../config'); | ||||
const { | const { | ||||
getAliasTxs, | getAliasTxs, | ||||
sortAliasTxsByTxidAndBlockheight, | sortAliasTxsByTxidAndBlockheight, | ||||
getValidAliasRegistrations, | registerAliases, | ||||
} = require('../src/alias'); | } = require('../src/alias'); | ||||
const { getAllTxHistory } = require('../src/chronik'); | const { getAllTxHistory } = require('../src/chronik'); | ||||
const { getHexFromAlias, getAliasBytecount } = require('../src/utils'); | const { getHexFromAlias, getAliasBytecount } = require('../src/utils'); | ||||
const { ChronikClient } = require('chronik-client'); | const { ChronikClient } = require('chronik-client'); | ||||
const chronik = new ChronikClient(config.chronik); | const chronik = new ChronikClient(config.chronik); | ||||
// Use an in-memory db | |||||
// Mock mongodb | |||||
const { MongoClient } = require('mongodb'); | |||||
const { MongoMemoryServer } = require('mongodb-memory-server'); | |||||
const { initializeDb } = require('../src/db'); | |||||
async function generateMocks() { | async function generateMocks() { | ||||
// Initialize mock db | |||||
// Start mongo memory server before running this suite of unit tests | |||||
const mongoServer = await MongoMemoryServer.create(); | |||||
const mongoUri = mongoServer.getUri(); | |||||
const testMongoClient = new MongoClient(mongoUri); | |||||
const testDb = await initializeDb(testMongoClient); | |||||
// Directory for mocks. Relative to /scripts, ../test/mocks/generated/ | // Directory for mocks. Relative to /scripts, ../test/mocks/generated/ | ||||
const mocksDir = path.join(__dirname, '..', 'test', 'mocks', 'generated'); | const mocksDir = path.join(__dirname, '..', 'test', 'mocks', 'generated'); | ||||
// Create directory if it does not exist | // Create directory if it does not exist | ||||
if (!fs.existsSync(mocksDir)) { | if (!fs.existsSync(mocksDir)) { | ||||
fs.mkdirSync(mocksDir); | fs.mkdirSync(mocksDir); | ||||
} | } | ||||
Show All 27 Lines | async function generateMocks() { | ||||
const allAliasTxsSortedByTxidAndBlockheight = | const allAliasTxsSortedByTxidAndBlockheight = | ||||
sortAliasTxsByTxidAndBlockheight(allAliasTxs); | sortAliasTxsByTxidAndBlockheight(allAliasTxs); | ||||
fs.writeFileSync( | fs.writeFileSync( | ||||
`${mocksDir}/allAliasTxsSortedByTxidAndBlockheight.json`, | `${mocksDir}/allAliasTxsSortedByTxidAndBlockheight.json`, | ||||
JSON.stringify(allAliasTxsSortedByTxidAndBlockheight, null, 2), | JSON.stringify(allAliasTxsSortedByTxidAndBlockheight, null, 2), | ||||
'utf-8', | 'utf-8', | ||||
); | ); | ||||
/* | const validAliasRegistrations = await registerAliases(testDb, allAliasTxs); | ||||
validAliasRegistrations | |||||
validAliasRegistrations are registered aliases. | |||||
These will never change unless and until Phase 2 migration. | |||||
*/ | |||||
const validAliasRegistrations = getValidAliasRegistrations(allAliasTxs); | |||||
fs.writeFileSync( | fs.writeFileSync( | ||||
`${mocksDir}/validAliasTxs.json`, | `${mocksDir}/validAliasRegistrations.json`, | ||||
JSON.stringify(validAliasRegistrations, null, 2), | JSON.stringify(validAliasRegistrations, null, 2), | ||||
'utf-8', | 'utf-8', | ||||
); | ); | ||||
// Get an array of just valid aliases | // Get an array of just valid aliases | ||||
const aliasHexConversions = []; | const aliasHexConversions = []; | ||||
for (let i = 0; i < validAliasRegistrations.length; i += 1) { | for (let i = 0; i < validAliasRegistrations.length; i += 1) { | ||||
const { alias } = validAliasRegistrations[i]; | const { alias } = validAliasRegistrations[i]; | ||||
const aliasHex = getHexFromAlias(alias); | const aliasHex = getHexFromAlias(alias); | ||||
const aliasByteCount = getAliasBytecount(alias); | const aliasByteCount = getAliasBytecount(alias); | ||||
aliasHexConversions.push({ alias, aliasHex, aliasByteCount }); | aliasHexConversions.push({ alias, aliasHex, aliasByteCount }); | ||||
} | } | ||||
fs.writeFileSync( | fs.writeFileSync( | ||||
`${mocksDir}/aliasHexConversions.json`, | `${mocksDir}/aliasHexConversions.json`, | ||||
JSON.stringify(aliasHexConversions, null, 2), | JSON.stringify(aliasHexConversions, null, 2), | ||||
'utf-8', | 'utf-8', | ||||
); | ); | ||||
// Wipe the database after using the in-memory mock | |||||
await testDb.dropDatabase(); | |||||
// Shut down mongo memory server after running this suite of unit tests | |||||
await testMongoClient.close(); | |||||
await mongoServer.stop(); | |||||
// Exit script in success condition | // Exit script in success condition | ||||
process.exit(0); | process.exit(0); | ||||
} | } | ||||
generateMocks(); | generateMocks(); |
Note: This file was not updated in line with other refactor changes we made to the app, as it was also not used to generate new mocks and was overlooked.
These updates were required in this diff as I had to generate new mocks to use the new spec