diff --git a/index.ts b/index.ts index 03e8e30adfdebbd7ec5f5817234aa8f38a0bcb1e..ffae5602f1105ad17ac01186e30c5d7539a9870b 100644 --- a/index.ts +++ b/index.ts @@ -69,7 +69,7 @@ program.command('main') }) break case 'CONNECTED': - questions[0].choices = ['Rejoindre une association', 'Créer un token', 'Voir les associations', 'Voir mon portefeuille'] + questions[0].choices = ['Rejoindre une association', 'Créer un token', 'Voir les associations', "Voir les détails d'une association", 'Voir mon portefeuille'] await inquirer.prompt(questions).then(async (answers: { choice: string }) => { await handleConnectedChoice(answers.choice, tezos) }) diff --git a/src/handlers/association/associationHandlers.ts b/src/handlers/association/associationHandlers.ts new file mode 100644 index 0000000000000000000000000000000000000000..159c9ac243fc7cd83107c3c6a72ca7cde5c5fff1 --- /dev/null +++ b/src/handlers/association/associationHandlers.ts @@ -0,0 +1,136 @@ +// IMPORT EXTERNAL LIBS +import chalk from 'chalk' +import { type TezosToolkit } from '@taquito/taquito' + +// IMPORT TYPES +import { type Association } from '../../types/Association.js' + +// IMPORT SERVICES +import { createAssociation, getAssociationDetails, getAssociations, joinAssociation } from '../../services/association.service.js' +import inquirer from 'inquirer' + +/** + * Handles the process of creating an association. + * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. + * @returns {Promise<void>} A promise resolved once the association creation process is complete. + */ +async function handleCreateAssociation (tezos: TezosToolkit): Promise<void> { + const questions = [ + { + type: 'input', + name: 'name', + message: 'Nom', + validate: function (input: string) { + const done = this.async() + + if (input.trim() === '') { + done('Vous devez remplir ce champ') + } else { + done(null, true) + } + } + }, + { + type: 'input', + name: 'description', + message: 'Description', + validate: function (input: string) { + const done = this.async() + + if (input.trim() === '') { + done('Vous devez remplir ce champ') + } else { + done(null, true) + } + } + } + ] + + let association: Association + + await inquirer.prompt(questions).then(async (answers: { name: string, description: string }) => { + association = { + name: answers.name, + description: answers.description + } + }) + + try { + await createAssociation(association, tezos) + } catch (error) { + console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`)) + } +} + +/** + * Handles the process of joining an association. + * @param {string[]} associations - A list of association name. + * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. + * @returns {Promise<void>} A promise resolved once the joining process is complete. + */ +async function handleJoinAssociation (associations: string[], tezos: TezosToolkit): Promise<void> { + const questions = [ + { + type: 'list', + name: 'choice', + message: 'Quelle association voulez-vous rejoindre ?', + choices: associations + } + ] + + let associationName: string + + await inquirer.prompt(questions).then(async (answers: { choice: string }) => { + associationName = answers.choice + }) + + try { + await joinAssociation(associationName, tezos) + } catch (error) { + console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`)) + } +} + +/** + * Handles the process of listing associations. + * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. + * @returns {Promise<string[]>} A promise with a string of association name. + */ +async function handleGetAssociations (tezos: TezosToolkit): Promise<string[]> { + try { + return await getAssociations(tezos) + } catch (error) { + console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`)) + } +} + +/** + * Handles the process of getting association details. + * @param {string[]} associations - A list of association name. + * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. + * @returns {Promise<Association>} A promise resolved with association details. + */ +async function handleGetAssociationDetails (associations: string[], tezos: TezosToolkit): Promise<Association> { + const questions = [ + { + type: 'list', + name: 'choice', + message: 'De quelle association voulez-vous voir les détails ?', + choices: associations + } + ] + + let associationName: string + + await inquirer.prompt(questions).then(async (answers: { choice: string }) => { + associationName = answers.choice + }) + + try { + return await getAssociationDetails(associationName, tezos) + } catch (error) { + console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`)) + } +} + +export { handleCreateAssociation, handleJoinAssociation, handleGetAssociations, handleGetAssociationDetails } diff --git a/src/handlers/balance/balanceHandlers.ts b/src/handlers/balance/balanceHandlers.ts new file mode 100644 index 0000000000000000000000000000000000000000..bef5dd10b82cc7e5274242c6b4f24184d860945f --- /dev/null +++ b/src/handlers/balance/balanceHandlers.ts @@ -0,0 +1,21 @@ +// IMPORT EXTERNAL LIBS +import chalk from 'chalk' +import { type TezosToolkit } from '@taquito/taquito' + +// IMPORT SERVICES +import { getBalance } from '../../services/balance.service.js' + +/** + * Handles the process of retrieving the balance of the wallet. + * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. + * @returns {Promise<void>} A promise resolved once the balance retrieval process is complete. + */ +async function handleGetBalance (tezos: TezosToolkit): Promise<number> { + try { + return await getBalance(tezos) + } catch (error) { + console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`)) + } +} + +export { handleGetBalance } diff --git a/src/handlers/handlers.ts b/src/handlers/handlers.ts deleted file mode 100644 index 2cabcc678a64422dcbc97a40ed2097da186111e8..0000000000000000000000000000000000000000 --- a/src/handlers/handlers.ts +++ /dev/null @@ -1,310 +0,0 @@ -// IMPORT EXTERNAL LIBS -import chalk from 'chalk' -import { type TezosToolkit } from '@taquito/taquito' - -// IMPORT TYPES -import { type Association } from '../types/Association.js' -import { type Proposal } from '../types/Proposal.js' - -// IMPORT SERVICES -import { getBalance } from '../services/balance.service.js' -import { createAssociation, getAssociationDetails, getAssociations, joinAssociation } from '../services/association.service.js' -import { createProposal } from '../services/proposal.service.js' -import { createFAToken } from '../services/contract.service.js' -import { burnToken } from '../services/token.service.js' -import inquirer from 'inquirer' - -/** - * Handles the process of creating an association. - * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. - * @returns {Promise<void>} A promise resolved once the association creation process is complete. - */ -async function handleCreateAssociation (tezos: TezosToolkit): Promise<void> { - const questions = [ - { - type: 'input', - name: 'name', - message: 'Nom', - validate: function (input: string) { - const done = this.async() - - if (input.trim() === '') { - done('Vous devez remplir ce champ') - } else { - done(null, true) - } - } - }, - { - type: 'input', - name: 'description', - message: 'Description', - validate: function (input: string) { - const done = this.async() - - if (input.trim() === '') { - done('Vous devez remplir ce champ') - } else { - done(null, true) - } - } - } - ] - - let association: Association - - await inquirer.prompt(questions).then(async (answers: { name: string, description: string }) => { - association = { - name: answers.name, - description: answers.description - } - }) - - try { - const op = await createAssociation(association, tezos) - console.log(op.hash) - console.log(chalk.bgGreenBright('\nVotre association a été créée !!\n')) - } catch (error) { - console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`)) - } -} - -/** - * Handles the process of creating a fungible token. - * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. - * @returns {Promise<void>} A promise resolved once the token creation process is complete. - */ -async function handleCreateToken (tezos: TezosToolkit): Promise<void> { - const questions = [ - { - type: 'input', - name: 'nbTokenFungible', - message: 'Nombre de token fongible', - validate: function (input: string) { - const done = this.async() - const parsedInput = parseFloat(input.trim()) - - if (isNaN(parsedInput) || !Number.isInteger(parsedInput)) { - done('Vous devez fournir un nombre entier') - } else { - done(null, true) - } - } - } - ] - - let nbTokenFungible: number - - await inquirer.prompt(questions).then(async (answers: { nbTokenFungible: number }) => { - nbTokenFungible = answers.nbTokenFungible - }) - - try { - const op = await createFAToken(nbTokenFungible, tezos) - console.log(op.hash) - console.log(chalk.bgGreenBright('\nVotre token a été créé !!\n')) - } catch (error) { - console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`)) - } -} - -/** - * Handles the process of retrieving the balance of the wallet. - * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. - * @returns {Promise<void>} A promise resolved once the balance retrieval process is complete. - */ -async function handleGetBalance (tezos: TezosToolkit): Promise<void> { - try { - const balance = await getBalance(tezos) - console.log(`\nSolde du portefeuille: ${balance} ꜩ\n`) - } catch (error) { - console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`)) - } -} - -/** - * Handles the process of creating a proposal. - * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. - * @returns {Promise<void>} A promise resolved once the proposal creation process is complete. - */ -async function handleCreateProposal (tezos: TezosToolkit): Promise<void> { - const questions = [ - { - type: 'input', - name: 'title', - message: 'Titre', - validate: function (input: string) { - const done = this.async() - - if (input.trim() === '') { - done('Vous devez remplir ce champ') - } else { - done(null, true) - } - } - }, - { - type: 'input', - name: 'description', - message: 'Description', - validate: function (input: string) { - const done = this.async() - - if (input.trim() === '') { - done('Vous devez remplir ce champ') - } else { - done(null, true) - } - } - } - ] - - let proposal: Proposal - - await inquirer.prompt(questions).then(async (answers: { title: string, description: string }) => { - proposal = { - title: answers.title, - description: answers.description - } - }) - - try { - const op = await createProposal(proposal, tezos) - console.log(op.hash) - console.log(chalk.bgGreenBright('\nVous avez soumis une proposition !!\n')) - } catch (error) { - console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`)) - } -} - -/** - * Handles the process of joining an association. - * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. - * @returns {Promise<void>} A promise resolved once the joining process is complete. - */ -async function handleJoinAssociation (tezos: TezosToolkit): Promise<void> { - const questions = [ - { - type: 'input', - name: 'associationName', - message: 'Nom', - validate: function (input: string) { - const done = this.async() - - if (input.trim() === '') { - done('Vous devez remplir ce champ') - } else { - done(null, true) - } - } - } - ] - - let associationName: string - - await inquirer.prompt(questions).then(async (answers: { associationName: string }) => { - associationName = answers.associationName - }) - - try { - const op = await joinAssociation(associationName, tezos) - console.log(op.hash) - console.log(chalk.bgGreenBright("\nVous avez rejoint l'association !!\n")) - } catch (error) { - console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`)) - } -} - -/** - * Handles the process of listing associations. - * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. - * @returns {Promise<void>} A promise resolved once the joining process is complete. - */ -async function handleGetAssociations (tezos: TezosToolkit): Promise<void> { - try { - const op = await getAssociations(tezos) - console.log(op.hash) - // LISTER LES ASSOCIATIONS - } catch (error) { - console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`)) - } -} - -/** - * Handles the process of getting association details. - * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. - * @returns {Promise<void>} A promise resolved once the joining process is complete. - */ -async function handleGetAssociationDetails (tezos: TezosToolkit): Promise<void> { - const questions = [ - { - type: 'input', - name: 'associationName', - message: 'Nom', - validate: function (input: string) { - const done = this.async() - - if (input.trim() === '') { - done('Vous devez remplir ce champ') - } else { - done(null, true) - } - } - } - ] - - let associationName: string - - await inquirer.prompt(questions).then(async (answers: { associationName: string }) => { - associationName = answers.associationName - }) - - try { - const op = await getAssociationDetails(associationName, tezos) - console.log(op.hash) - // RETURN ASSOCIATIONS - } catch (error) { - console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`)) - } -} - -/** - * Handles the process of creating a proposal. - * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. - * @returns {Promise<void>} A promise resolved once the proposal creation process is complete. - */ -async function handleBurnToken (tezos: TezosToolkit): Promise<void> { - const questions = [ - { - type: 'input', - name: 'nbTokenToBurn', - message: 'Nombre de token à bruler', - validate: function (input: string) { - const done = this.async() - const parsedInput = parseFloat(input.trim()) - - if (isNaN(parsedInput) || !Number.isInteger(parsedInput)) { - done('Vous devez fournir un nombre entier') - } else { - done(null, true) - } - } - } - ] - - let nbTokenToBurn: number - - await inquirer.prompt(questions).then(async (answers: { nbTokenToBurn: number }) => { - nbTokenToBurn = answers.nbTokenToBurn - }) - - try { - const op = await burnToken(nbTokenToBurn, tezos) - console.log(op.hash) - console.log(chalk.bgGreenBright('\nVous avez bruler vos tokens !!\n')) - } catch (error) { - console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`)) - } -} - -export { handleCreateAssociation, handleCreateToken, handleGetBalance, handleCreateProposal, handleJoinAssociation, handleGetAssociations, handleGetAssociationDetails, handleBurnToken } diff --git a/src/handlers/proposal/proposalHandlers.ts b/src/handlers/proposal/proposalHandlers.ts new file mode 100644 index 0000000000000000000000000000000000000000..feccb85175a01142775dd459f1d19d989a94e72d --- /dev/null +++ b/src/handlers/proposal/proposalHandlers.ts @@ -0,0 +1,65 @@ +// IMPORT EXTERNAL LIBS +import chalk from 'chalk' +import { type TezosToolkit } from '@taquito/taquito' + +// IMPORT TYPES +import { type Proposal } from '../../types/Proposal.js' + +// IMPORT SERVICES +import { createProposal } from '../../services/proposal.service.js' +import inquirer from 'inquirer' + +/** + * Handles the process of creating a proposal. + * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. + * @returns {Promise<void>} A promise resolved once the proposal creation process is complete. + */ +async function handleCreateProposal (tezos: TezosToolkit): Promise<void> { + const questions = [ + { + type: 'input', + name: 'title', + message: 'Titre', + validate: function (input: string) { + const done = this.async() + + if (input.trim() === '') { + done('Vous devez remplir ce champ') + } else { + done(null, true) + } + } + }, + { + type: 'input', + name: 'description', + message: 'Description', + validate: function (input: string) { + const done = this.async() + + if (input.trim() === '') { + done('Vous devez remplir ce champ') + } else { + done(null, true) + } + } + } + ] + + let proposal: Proposal + + await inquirer.prompt(questions).then(async (answers: { title: string, description: string }) => { + proposal = { + title: answers.title, + description: answers.description + } + }) + + try { + await createProposal(proposal, tezos) + } catch (error) { + console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`)) + } +} + +export { handleCreateProposal } diff --git a/src/handlers/roleHandlers.ts b/src/handlers/roleHandlers.ts index 83abf842c1db5c399f5242068fe5e9a6a1d1c238..8576476bf4107d40d846c13e49ff999d6eca6798 100644 --- a/src/handlers/roleHandlers.ts +++ b/src/handlers/roleHandlers.ts @@ -1,6 +1,10 @@ import { type TezosToolkit } from '@taquito/taquito' -import { handleBurnToken, handleCreateAssociation, handleCreateProposal, handleCreateToken, handleGetAssociations, handleGetBalance, handleJoinAssociation } from './handlers.js' import chalk from 'chalk' +import { handleCreateAssociation, handleGetAssociationDetails, handleGetAssociations, handleJoinAssociation } from './association/associationHandlers.js' +import { handleBurnToken, handleCreateToken } from './token/tokenHandlers.js' +import { handleGetBalance } from './balance/balanceHandlers.js' +import { handleCreateProposal } from './proposal/proposalHandlers.js' +import { type Association } from '../types/Association.js' /** * Handles fa token administrator actions based on the specified choice. @@ -9,18 +13,23 @@ import chalk from 'chalk' * @returns {Promise<void>} A promise resolved once the processing is complete. */ async function handleAdminFATokenChoice (choice: string, tezos: TezosToolkit): Promise<void> { + let balance: number switch (choice) { case 'Créer une association': await handleCreateAssociation(tezos) + console.log(chalk.bgGreenBright('\nVotre association a été créée !!\n')) break case 'Créer un token': await handleCreateToken(tezos) + console.log(chalk.bgGreenBright('\nVotre token a été créé !!\n')) break case 'Bruler des tokens': await handleBurnToken(tezos) + console.log(chalk.bgGreenBright('\nVous avez bruler vos tokens !!\n')) break case 'Voir mon portefeuille': - await handleGetBalance(tezos) + balance = await handleGetBalance(tezos) + console.log(`\nSolde du portefeuille: ${balance} ꜩ\n`) break default: console.log(chalk.bgRedBright('\nChoix invalide\n')) @@ -35,15 +44,19 @@ async function handleAdminFATokenChoice (choice: string, tezos: TezosToolkit): P * @returns {Promise<void>} A promise resolved once the processing is complete. */ async function handleAdminAssociationChoice (choice: string, tezos: TezosToolkit): Promise<void> { + let balance: number switch (choice) { case 'Créer un token': await handleCreateToken(tezos) + console.log(chalk.bgGreenBright('\nVotre token a été créé !!\n')) break case 'Bruler des tokens': await handleBurnToken(tezos) + console.log(chalk.bgGreenBright('\nVous avez bruler vos tokens !!\n')) break case 'Voir mon portefeuille': - await handleGetBalance(tezos) + balance = await handleGetBalance(tezos) + console.log(`\nSolde du portefeuille: ${balance} ꜩ\n`) break default: console.log(chalk.bgRedBright('\nChoix invalide\n')) @@ -58,15 +71,19 @@ async function handleAdminAssociationChoice (choice: string, tezos: TezosToolkit * @returns {Promise<void>} A promise resolved once the processing is complete. */ async function handleAdherentChoice (choice: string, tezos: TezosToolkit): Promise<void> { + let balance: number switch (choice) { case 'Faire une proposition': await handleCreateProposal(tezos) + console.log(chalk.bgGreenBright('\nVous avez soumis une proposition !!\n')) break case 'Créer un token': await handleCreateToken(tezos) + console.log(chalk.bgGreenBright('\nVotre token a été créé !!\n')) break case 'Voir mon portefeuille': - await handleGetBalance(tezos) + balance = await handleGetBalance(tezos) + console.log(`\nSolde du portefeuille: ${balance} ꜩ\n`) break default: console.log(chalk.bgRedBright('\nChoix invalide\n')) @@ -81,18 +98,34 @@ async function handleAdherentChoice (choice: string, tezos: TezosToolkit): Promi * @returns {Promise<void>} A promise resolved once the processing is complete. */ async function handleConnectedChoice (choice: string, tezos: TezosToolkit): Promise<void> { + let associationsByName: string[] + let associationDetails: Association + let balance: number switch (choice) { case 'Rejoindre une association': - await handleJoinAssociation(tezos) + associationsByName = await handleGetAssociations(tezos) + await handleJoinAssociation(associationsByName, tezos) + + console.log(chalk.bgGreenBright("\nVous avez rejoint l'association !!\n")) break case 'Créer un token': await handleCreateToken(tezos) + console.log(chalk.bgGreenBright('\nVotre token a été créé !!\n')) break case 'Voir les associations': - await handleGetAssociations(tezos) + associationsByName = await handleGetAssociations(tezos) + associationsByName.forEach(name => { console.log(chalk.yellow(name)) }) + break + case "Voir les détails d'une association": + associationsByName = await handleGetAssociations(tezos) + associationDetails = await handleGetAssociationDetails(associationsByName, tezos) + + console.log(`Nom de l'association: ${chalk.yellow(associationDetails.name)}`) + console.log(`Description de l'association: ${chalk.yellow(associationDetails.description)}`) break case 'Voir mon portefeuille': - await handleGetBalance(tezos) + balance = await handleGetBalance(tezos) + console.log(`\nSolde du portefeuille: ${balance} ꜩ\n`) break default: console.log(chalk.bgRedBright('\nChoix invalide\n')) diff --git a/src/handlers/token/tokenHandlers.ts b/src/handlers/token/tokenHandlers.ts new file mode 100644 index 0000000000000000000000000000000000000000..94d51e4485d9130e3c9e3dda76862da35d549c5f --- /dev/null +++ b/src/handlers/token/tokenHandlers.ts @@ -0,0 +1,83 @@ +// IMPORT EXTERNAL LIBS +import chalk from 'chalk' +import { type TezosToolkit } from '@taquito/taquito' + +// IMPORT SERVICE +import { burnToken, createFAToken } from '../../services/token.service.js' +import inquirer from 'inquirer' + +/** + * Handles the process of creating a fungible token. + * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. + * @returns {Promise<void>} A promise resolved once the token creation process is complete. + */ +async function handleCreateToken (tezos: TezosToolkit): Promise<void> { + const questions = [ + { + type: 'input', + name: 'nbTokenFungible', + message: 'Nombre de token fongible', + validate: function (input: string) { + const done = this.async() + const parsedInput = parseFloat(input.trim()) + + if (isNaN(parsedInput) || !Number.isInteger(parsedInput)) { + done('Vous devez fournir un nombre entier') + } else { + done(null, true) + } + } + } + ] + + let nbTokenFungible: number + + await inquirer.prompt(questions).then(async (answers: { nbTokenFungible: number }) => { + nbTokenFungible = answers.nbTokenFungible + }) + + try { + await createFAToken(nbTokenFungible, tezos) + } catch (error) { + console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`)) + } +} + +/** + * Handles the process of creating a proposal. + * @param {TezosToolkit} tezos - The TezosToolkit instance used for blockchain operations. + * @returns {Promise<void>} A promise resolved once the proposal creation process is complete. + */ +async function handleBurnToken (tezos: TezosToolkit): Promise<void> { + const questions = [ + { + type: 'input', + name: 'nbTokenToBurn', + message: 'Nombre de token à bruler', + validate: function (input: string) { + const done = this.async() + const parsedInput = parseFloat(input.trim()) + + if (isNaN(parsedInput) || !Number.isInteger(parsedInput)) { + done('Vous devez fournir un nombre entier') + } else { + done(null, true) + } + } + } + ] + + let nbTokenToBurn: number + + await inquirer.prompt(questions).then(async (answers: { nbTokenToBurn: number }) => { + nbTokenToBurn = answers.nbTokenToBurn + }) + + try { + await burnToken(nbTokenToBurn, tezos) + } catch (error) { + console.log(chalk.bgRed(`\n${error.lastError.with.string}\n`)) + } +} + +export { handleCreateToken, handleBurnToken } diff --git a/src/services/association.service.ts b/src/services/association.service.ts index bdd2c59d3d7c6e7c8672aca4c3cde72110836965..21ce166bb7dad2a36d95aaf5ad39eb02b020f58e 100644 --- a/src/services/association.service.ts +++ b/src/services/association.service.ts @@ -4,13 +4,23 @@ import { type Association } from '../types/Association' // NEED UPDATE ADDRESS !! (SMART CONTRACT 1: Registre des associations) const address = 'KT1NMZvpAEQmezU8kHKzgi9PjysoH4VTcB3P' +const mockAssociations: Association[] = [ + { + name: 'Association 1', + description: "Ceci est l'association 1" + }, + { + name: 'Association 2', + description: "Ceci est l'association 2" + } +] + // NEED UPDATE ENTRYPOINT !! -async function createAssociation (association: Association, tezos: TezosToolkit): Promise<Operation> { +async function createAssociation (association: Association, tezos: TezosToolkit): Promise<void> { const contract = await tezos.contract.at(address) const op: Operation = await contract.methodsObject.registerAssociation(association).send() await op.confirmation() - return op } // NEED UPDATE ENTRYPOINT !! @@ -23,21 +33,27 @@ async function joinAssociation (associationName: string, tezos: TezosToolkit): P } // NEED UPDATE ENTRYPOINT !! -async function getAssociations (tezos: TezosToolkit): Promise<Operation> { - const contract = await tezos.contract.at(address) - const op: Operation = await contract.methodsObject.getAssociations().send() +async function getAssociations (tezos: TezosToolkit): Promise<string[]> { + // const contract = await tezos.contract.at(address) + // const op: Operation = await contract.methodsObject.getAssociations().send() - await op.confirmation() - return op + // await op.confirmation() + // return op + + // MOCK + return mockAssociations.map(association => association.name) } // NEED UPDATE ENTRYPOINT !! -async function getAssociationDetails (associationName: string, tezos: TezosToolkit): Promise<Operation> { - const contract = await tezos.contract.at(address) - const op: Operation = await contract.methodsObject.getAssociationsDetails(associationName).send() +async function getAssociationDetails (associationName: string, tezos: TezosToolkit): Promise<Association> { + // const contract = await tezos.contract.at(address) + // const op: Operation = await contract.methodsObject.getAssociationsDetails(associationName).send() - await op.confirmation() - return op + // await op.confirmation() + // return op + + // MOCK + return mockAssociations.find(association => association.name === associationName) } export { createAssociation, joinAssociation, getAssociations, getAssociationDetails } diff --git a/src/services/contract.service.ts b/src/services/contract.service.ts deleted file mode 100644 index 20b576d9a547c45099ba7a6b8002a470774d83ce..0000000000000000000000000000000000000000 --- a/src/services/contract.service.ts +++ /dev/null @@ -1,33 +0,0 @@ -import { type Operation, type TezosToolkit } from '@taquito/taquito' - -// NEED UPDATE ADDRESS !! (SMART CONTRACT 3: Factory de Token) -const address = 'KT1QZJzhSPQ89K4eC59tmQYCt44qph7wXoJu' - -// NEED UPDATE ENTRYPOINT !! -async function createFAToken (nbTokenFongible: number, tezos: TezosToolkit): Promise<Operation> { - const contract = await tezos.contract.at(address) - const op: Operation = await contract.methodsObject.createFAToken(nbTokenFongible).send() - - await op.confirmation() - return op -} - -// NEED UPDATE ENTRYPOINT !! -async function createDAOContract (tezos: TezosToolkit): Promise<Operation> { - const contract = await tezos.contract.at(address) - const op: Operation = await contract.methodsObject.createDAOContract().send() - - await op.confirmation() - return op -} - -// NEED UPDATE ENTRYPOINT !! -async function deployDAOContract (contrat: string, tezos: TezosToolkit): Promise<Operation> { - const contract = await tezos.contract.at(address) - const op: Operation = await contract.methodsObject.deployDAOContract(contrat).send() - - await op.confirmation() - return op -} - -export { createFAToken, createDAOContract, deployDAOContract } diff --git a/src/services/token.service.ts b/src/services/token.service.ts index 5ee55140315f326c12ffed7654661977cbca2232..c4e2424802bac13203adfb5d2ad954e33527b53e 100644 --- a/src/services/token.service.ts +++ b/src/services/token.service.ts @@ -4,30 +4,19 @@ import { type Operation, type TezosToolkit } from '@taquito/taquito' const address = 'KT1QZJzhSPQ89K4eC59tmQYCt44qph7wXoJu' // NEED UPDATE ENTRYPOINT !! -async function createFAToken (tezos: TezosToolkit): Promise<Operation> { +async function createFAToken (nbTokenFongible: number, tezos: TezosToolkit): Promise<void> { const contract = await tezos.contract.at(address) - const op: Operation = await contract.methodsObject.destroyPlanet('Saturne').send() + const op: Operation = await contract.methodsObject.createFAToken(nbTokenFongible).send() await op.confirmation() - return op } // NEED UPDATE ENTRYPOINT !! -async function burnToken (amount: number, tezos: TezosToolkit): Promise<Operation> { +async function burnToken (amount: number, tezos: TezosToolkit): Promise<void> { const contract = await tezos.contract.at(address) const op: Operation = await contract.methodsObject.burnToken(amount).send() await op.confirmation() - return op } -// NEED UPDATE ENTRYPOINT !! -async function buyToken (amount: number, tezos: TezosToolkit): Promise<Operation> { - const contract = await tezos.contract.at(address) - const op: Operation = await contract.methodsObject.buyToken(amount).send() - - await op.confirmation() - return op -} - -export { createFAToken, burnToken, buyToken } +export { createFAToken, burnToken } diff --git a/src/utils/getRole.ts b/src/utils/getRole.ts index e21034dc158515d6afc37015b9999c909fb262e2..8b0c0067692b276f3c02611c5656f7561add1d4d 100644 --- a/src/utils/getRole.ts +++ b/src/utils/getRole.ts @@ -9,5 +9,5 @@ export function getRole (tezos: TezosToolkit): Role { // ELSE -> CONNECTED // TEMPORARY - return 'ADMIN_FA_TOKEN' + return 'ADHERENT' } diff --git a/test/handlers/association/associationHandlers.spec.ts b/test/handlers/association/associationHandlers.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..40cf29935a0990460cae95c9d93fa5d2a4f79d22 --- /dev/null +++ b/test/handlers/association/associationHandlers.spec.ts @@ -0,0 +1,227 @@ +import { handleCreateAssociation, handleJoinAssociation, handleGetAssociations, handleGetAssociationDetails } from '../../../src/handlers/association/associationHandlers' +import { type TezosToolkit } from '@taquito/taquito' +import chalk from 'chalk' +import { vi, describe, it, expect, beforeEach } from 'vitest' + +const { createAssociationSpy, promptSpy, joinAssociationSpy, getAssociationsSpy, getAssociationDetailsSpy } = + vi.hoisted(() => { + return { + createAssociationSpy: vi.fn().mockResolvedValue({}), + joinAssociationSpy: vi.fn().mockResolvedValue({}), + getAssociationsSpy: vi.fn().mockResolvedValue({}), + getAssociationDetailsSpy: vi.fn().mockResolvedValue({}), + promptSpy: vi.fn().mockResolvedValue({}) + } + }) + +vi.mock('inquirer', async (importOriginal) => { + const actual = await importOriginal() + + if (typeof actual === 'object' && actual !== null) { + return { + ...actual, + default: { + prompt: promptSpy + } + } + } else { + return actual + } +}) + +vi.mock('../../../src/services/association.service', () => ({ + createAssociation: createAssociationSpy, + joinAssociation: joinAssociationSpy, + getAssociations: getAssociationsSpy, + getAssociationDetails: getAssociationDetailsSpy +})) + +const mockedTezosToolkit = {} as unknown as TezosToolkit + +describe('associationHandlers', () => { + beforeEach(() => { + vi.clearAllMocks() + }) + + describe('handleCreateAssociation', () => { + describe('when createAssociation is called with success', () => { + it('should create an association with provided name and description', async () => { + const name = 'Association Name' + const description = 'Association Description' + + promptSpy.mockResolvedValueOnce({ name, description }) + + await handleCreateAssociation(mockedTezosToolkit) + + expect(createAssociationSpy).toBeCalledWith( + { name: 'Association Name', description: 'Association Description' }, + mockedTezosToolkit + ) + }) + }) + + describe('when createAssociation is called with error', () => { + it('should log error message', async () => { + const error = { + lastError: { + with: { + string: 'Custom Error' + } + } + } + createAssociationSpy.mockRejectedValueOnce(error) + + const name = 'Association Name' + const description = 'Association Description' + + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) + + promptSpy.mockResolvedValueOnce({ name, description }) + + await handleCreateAssociation(mockedTezosToolkit) + + expect(createAssociationSpy).toBeCalledWith( + { name: 'Association Name', description: 'Association Description' }, + mockedTezosToolkit + ) + expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n')) + }) + }) + }) + + describe('handleJoinAssociation', () => { + describe('when joinAssociation is called with success', () => { + it('should join an association with provided name', async () => { + const associationsByName = ['Association 1', 'Association 2'] + const associationChoose = 'Association 1' + + promptSpy.mockResolvedValueOnce({ choice: associationChoose }) + await handleJoinAssociation(associationsByName, mockedTezosToolkit) + + expect(joinAssociationSpy).toBeCalledWith(associationChoose, mockedTezosToolkit) + }) + }) + + describe('when joinAssociation is called with error', () => { + it('should log error message', async () => { + const error = { + lastError: { + with: { + string: 'Custom Error' + } + } + } + joinAssociationSpy.mockRejectedValueOnce(error) + const associationsByName = ['Association 1', 'Association 2'] + const associationChoose = 'Association 1' + + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) + + promptSpy.mockResolvedValueOnce({ choice: associationChoose }) + await handleJoinAssociation(associationsByName, mockedTezosToolkit) + + expect(joinAssociationSpy).toBeCalled() + expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n')) + }) + }) + }) + + describe('handleGetAssociations', () => { + describe('when getAssociations is called with success', () => { + it('should call getAssociations', async () => { + getAssociationsSpy.mockResolvedValueOnce([ + { + name: 'Association 1', + description: 'Association 1 description' + }, + { + name: 'Association 2', + description: 'Association 2 description' + } + ]) + const associations = await handleGetAssociations(mockedTezosToolkit) + + expect(getAssociationsSpy).toBeCalledWith(mockedTezosToolkit) + expect(associations).toStrictEqual([ + { + name: 'Association 1', + description: 'Association 1 description' + }, + { + name: 'Association 2', + description: 'Association 2 description' + } + ]) + }) + }) + + describe('when getAssociations is called with error', () => { + it('should log error message', async () => { + const error = { + lastError: { + with: { + string: 'Custom Error' + } + } + } + getAssociationsSpy.mockRejectedValueOnce(error) + + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) + + const associations = await handleGetAssociations(mockedTezosToolkit) + + expect(getAssociationsSpy).toBeCalled() + expect(associations).toBeUndefined() + expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n')) + }) + }) + }) + + describe('handleGetAssociationDetails', () => { + describe('when getAssociationDetails is called with success', () => { + it('should get association details with provided name', async () => { + const associationsByName = ['Association 1', 'Association 2'] + const associationChoose = 'Association 1' + getAssociationDetailsSpy.mockResolvedValueOnce( + { + name: 'Association 1', + description: 'Association 1 description' + } + ) + promptSpy.mockResolvedValueOnce({ choice: associationChoose }) + + const association = await handleGetAssociationDetails(associationsByName, mockedTezosToolkit) + + expect(getAssociationDetailsSpy).toBeCalledWith(associationChoose, mockedTezosToolkit) + expect(association).toStrictEqual({ + name: 'Association 1', + description: 'Association 1 description' + }) + }) + }) + + describe('when getAssociationDetails is called with error', () => { + it('should log error message', async () => { + const error = { + lastError: { + with: { + string: 'Custom Error' + } + } + } + getAssociationDetailsSpy.mockRejectedValueOnce(error) + + const associationsByName = ['Association 1', 'Association 2'] + const associationChoose = 'Association 1' + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) + + promptSpy.mockResolvedValueOnce({ choice: associationChoose }) + const association = await handleGetAssociationDetails(associationsByName, mockedTezosToolkit) + + expect(getAssociationDetailsSpy).toBeCalled() + expect(association).toBeUndefined() + expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n')) + }) + }) + }) +}) diff --git a/test/handlers/balance/balanceHandlers.spec.ts b/test/handlers/balance/balanceHandlers.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..66d21f90597e00c35f4ae28a25b9a669aabca5b5 --- /dev/null +++ b/test/handlers/balance/balanceHandlers.spec.ts @@ -0,0 +1,54 @@ +import { handleGetBalance } from '../../../src/handlers/balance/balanceHandlers' +import { type TezosToolkit } from '@taquito/taquito' +import chalk from 'chalk' +import { vi, describe, it, expect, beforeEach } from 'vitest' + +const { getBalanceSpy } = + vi.hoisted(() => { + return { + getBalanceSpy: vi.fn().mockResolvedValue({}), + promptSpy: vi.fn().mockResolvedValue({}) + } + }) + +vi.mock('../../../src/services/balance.service', () => ({ + getBalance: getBalanceSpy +})) + +const mockedTezosToolkit = {} as unknown as TezosToolkit + +describe('handlers', () => { + beforeEach(() => { + vi.clearAllMocks() + }) + + describe('handleGetBalance', () => { + describe('when getBalance is called with success', () => { + it('should call getBalance', async () => { + await handleGetBalance(mockedTezosToolkit) + + expect(getBalanceSpy).toBeCalledWith(mockedTezosToolkit) + }) + }) + + describe('when getBalance is called with error', () => { + it('should log error message', async () => { + const error = { + lastError: { + with: { + string: 'Custom Error' + } + } + } + getBalanceSpy.mockRejectedValueOnce(error) + + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) + + await handleGetBalance(mockedTezosToolkit) + + expect(getBalanceSpy).toBeCalled() + expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n')) + }) + }) + }) +}) diff --git a/test/handlers/handlers.spec.ts b/test/handlers/handlers.spec.ts deleted file mode 100644 index 07cb5db29069134a970082569a83c253a45ae2c3..0000000000000000000000000000000000000000 --- a/test/handlers/handlers.spec.ts +++ /dev/null @@ -1,360 +0,0 @@ -import { handleCreateAssociation, handleCreateProposal, handleCreateToken, handleGetBalance, handleJoinAssociation, handleGetAssociations, handleGetAssociationDetails, handleBurnToken } from '../../src/handlers/handlers' -import { type TezosToolkit } from '@taquito/taquito' -import chalk from 'chalk' -import { vi, describe, it, expect, beforeEach } from 'vitest' - -const { createAssociationSpy, promptSpy, createFATokenSpy, getBalanceSpy, createProposalSpy, joinAssociationSpy, getAssociationsSpy, getAssociationDetailsSpy, burnTokenSpy } = - vi.hoisted(() => { - return { - createAssociationSpy: vi.fn().mockResolvedValue({ hash: 'association-hash' }), - createFATokenSpy: vi.fn().mockResolvedValue({ hash: 'token-hash' }), - getBalanceSpy: vi.fn().mockResolvedValue({ hash: 'balance-hash' }), - createProposalSpy: vi.fn().mockResolvedValue({ hash: 'proposal-hash' }), - joinAssociationSpy: vi.fn().mockResolvedValue({ hash: 'association-hash' }), - getAssociationsSpy: vi.fn().mockResolvedValue({ hash: 'association-hash' }), - getAssociationDetailsSpy: vi.fn().mockResolvedValue({ hash: 'association-hash' }), - burnTokenSpy: vi.fn().mockResolvedValue({ hash: 'token-hash' }), - promptSpy: vi.fn().mockResolvedValue({}) - } - }) - -vi.mock('inquirer', async (importOriginal) => { - const actual = await importOriginal() - - if (typeof actual === 'object' && actual !== null) { - return { - ...actual, - default: { - prompt: promptSpy - } - } - } else { - return actual - } -}) - -vi.mock('../../src/services/association.service', () => ({ - createAssociation: createAssociationSpy, - joinAssociation: joinAssociationSpy, - getAssociations: getAssociationsSpy, - getAssociationDetails: getAssociationDetailsSpy -})) - -vi.mock('../../src/services/token.service', () => ({ - createFAToken: createFATokenSpy, - burnToken: burnTokenSpy -})) - -vi.mock('../../src/services/balance.service', () => ({ - getBalance: getBalanceSpy -})) - -vi.mock('../../src/services/proposal.service', () => ({ - createProposal: createProposalSpy -})) - -const mockedTezosToolkit = {} as unknown as TezosToolkit - -describe('handlers', () => { - beforeEach(() => { - vi.clearAllMocks() - }) - - describe('handleCreateAssociation', () => { - describe('when createAssociation is called with success', () => { - it('should create an association with provided name and description', async () => { - const name = 'Association Name' - const description = 'Association Description' - - const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) - - promptSpy.mockResolvedValueOnce({ name, description }) - - await handleCreateAssociation(mockedTezosToolkit) - - expect(createAssociationSpy).toBeCalledWith( - { name: 'Association Name', description: 'Association Description' }, - mockedTezosToolkit - ) - expect(consoleSpy).toBeCalledWith(chalk.bgGreenBright('\nVotre association a été créée !!\n')) - }) - }) - - describe('when createAssociation is called with error', () => { - it('should log error message', async () => { - const error = { - lastError: { - with: { - string: 'Custom Error' - } - } - } - createAssociationSpy.mockRejectedValueOnce(error) - - const name = 'Association Name' - const description = 'Association Description' - - const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) - - promptSpy.mockResolvedValueOnce({ name, description }) - - await handleCreateAssociation(mockedTezosToolkit) - - expect(createAssociationSpy).toBeCalledWith( - { name: 'Association Name', description: 'Association Description' }, - mockedTezosToolkit - ) - expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n')) - }) - }) - }) - - describe.skip('handleCreateToken', () => { - describe('when createFAToken is called with success', () => { - it('should create a token with provided nbTokenFungible', async () => { - const nbTokenFungible = 5 - const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) - - promptSpy.mockResolvedValueOnce({ nbTokenFungible }) - - await handleCreateToken(mockedTezosToolkit) - - expect(createFATokenSpy).toBeCalledWith(nbTokenFungible, mockedTezosToolkit) - expect(consoleSpy).toBeCalledWith(chalk.bgGreenBright('\nVotre token a été créé !!\n')) - }) - }) - - describe('when createFAToken is called with error', () => { - it('should log error message', async () => { - const error = { - lastError: { - with: { - string: 'Custom Error' - } - } - } - createAssociationSpy.mockRejectedValueOnce(error) - const nbTokenFungible = 5 - const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) - - promptSpy.mockResolvedValueOnce({ nbTokenFungible }) - - await handleCreateToken(mockedTezosToolkit) - - expect(createAssociationSpy).toBeCalledWith(nbTokenFungible, mockedTezosToolkit) - expect(consoleSpy).toBeCalledWith(chalk.red('\nCustom Error\n')) - }) - }) - }) - - describe('handleGetBalance', () => { - describe('when getBalance is called with success', () => { - it('should call getBalance', async () => { - await handleGetBalance(mockedTezosToolkit) - - expect(getBalanceSpy).toBeCalledWith(mockedTezosToolkit) - }) - }) - - describe('when getBalance is called with error', () => { - it('should log error message', async () => { - const error = { - lastError: { - with: { - string: 'Custom Error' - } - } - } - getBalanceSpy.mockRejectedValueOnce(error) - - const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) - - await handleGetBalance(mockedTezosToolkit) - - expect(getBalanceSpy).toBeCalled() - expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n')) - }) - }) - }) - - describe('handleCreateProposal', () => { - describe('when createProposal is called with success', () => { - it('should create a proposal with provided title and description', async () => { - const title = 'Proposal Title' - const description = 'Proposal Description' - - const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) - - promptSpy.mockResolvedValueOnce({ title, description }) - await handleCreateProposal(mockedTezosToolkit) - - expect(createProposalSpy).toBeCalledWith({ title: 'Proposal Title', description: 'Proposal Description' }, mockedTezosToolkit) - expect(consoleSpy).toBeCalledWith(chalk.bgGreenBright('\nVous avez soumis une proposition !!\n')) - }) - }) - - describe('when createProposal is called with error', () => { - it('should log error message', async () => { - const error = { - lastError: { - with: { - string: 'Custom Error' - } - } - } - createProposalSpy.mockRejectedValueOnce(error) - const title = 'Proposal Title' - const description = 'Proposal Description' - - const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) - - promptSpy.mockResolvedValueOnce({ title, description }) - await handleCreateProposal(mockedTezosToolkit) - - expect(createProposalSpy).toBeCalled() - expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n')) - }) - }) - }) - - describe('handleJoinAssociation', () => { - describe('when joinAssociation is called with success', () => { - it('should join an association with provided name', async () => { - const associationName = 'Association Name' - - const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) - - promptSpy.mockResolvedValueOnce({ associationName }) - await handleJoinAssociation(mockedTezosToolkit) - - expect(joinAssociationSpy).toBeCalledWith('Association Name', mockedTezosToolkit) - expect(consoleSpy).toBeCalledWith(chalk.bgGreenBright("\nVous avez rejoint l'association !!\n")) - }) - }) - - describe('when joinAssociation is called with error', () => { - it('should log error message', async () => { - const error = { - lastError: { - with: { - string: 'Custom Error' - } - } - } - joinAssociationSpy.mockRejectedValueOnce(error) - const name = 'Association Name' - - const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) - - promptSpy.mockResolvedValueOnce({ name }) - await handleJoinAssociation(mockedTezosToolkit) - - expect(joinAssociationSpy).toBeCalled() - expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n')) - }) - }) - }) - - describe('handleGetAssociations', () => { - describe('when getAssociations is called with success', () => { - it('should call getAssociations', async () => { - await handleGetAssociations(mockedTezosToolkit) - - expect(getAssociationsSpy).toBeCalledWith(mockedTezosToolkit) - }) - }) - - describe('when getAssociations is called with error', () => { - it('should log error message', async () => { - const error = { - lastError: { - with: { - string: 'Custom Error' - } - } - } - getAssociationsSpy.mockRejectedValueOnce(error) - - const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) - - await handleGetAssociations(mockedTezosToolkit) - - expect(getAssociationsSpy).toBeCalled() - expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n')) - }) - }) - }) - - describe('handleGetAssociationDetails', () => { - describe('when getAssociationDetails is called with success', () => { - it('should get association details with provided name', async () => { - const associationName = 'Association Name' - - promptSpy.mockResolvedValueOnce({ associationName }) - await handleGetAssociationDetails(mockedTezosToolkit) - - expect(getAssociationDetailsSpy).toBeCalledWith('Association Name', mockedTezosToolkit) - }) - }) - - describe('when getAssociationDetails is called with error', () => { - it('should log error message', async () => { - const error = { - lastError: { - with: { - string: 'Custom Error' - } - } - } - getAssociationDetailsSpy.mockRejectedValueOnce(error) - const associationName = 'Association Name' - - const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) - - promptSpy.mockResolvedValueOnce({ associationName }) - await handleGetAssociationDetails(mockedTezosToolkit) - - expect(getAssociationDetailsSpy).toBeCalled() - expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n')) - }) - }) - }) - - describe('handleBurnToken', () => { - describe('when burnToken is called with success', () => { - it('should create a token with provided nbTokenToBurn', async () => { - const nbTokenToBurn = 5 - const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) - - promptSpy.mockResolvedValueOnce({ nbTokenToBurn }) - - await handleBurnToken(mockedTezosToolkit) - - expect(burnTokenSpy).toBeCalledWith(nbTokenToBurn, mockedTezosToolkit) - expect(consoleSpy).toBeCalledWith(chalk.bgGreenBright('\nVous avez bruler vos tokens !!\n')) - }) - }) - - describe('when burnToken is called with error', () => { - it('should log error message', async () => { - const error = { - lastError: { - with: { - string: 'Custom Error' - } - } - } - burnTokenSpy.mockRejectedValueOnce(error) - const nbTokenToBurn = 5 - const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) - - promptSpy.mockResolvedValueOnce({ nbTokenToBurn }) - - await handleBurnToken(mockedTezosToolkit) - - expect(burnTokenSpy).toBeCalledWith(nbTokenToBurn, mockedTezosToolkit) - expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n')) - }) - }) - }) -}) diff --git a/test/handlers/proposal/proposalHandlers.spec.ts b/test/handlers/proposal/proposalHandlers.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..2cd18a416bfbad9eb396f46c67a314dfbf7a508a --- /dev/null +++ b/test/handlers/proposal/proposalHandlers.spec.ts @@ -0,0 +1,76 @@ +import { handleCreateProposal } from '../../../src/handlers/proposal/proposalHandlers' +import { type TezosToolkit } from '@taquito/taquito' +import chalk from 'chalk' +import { vi, describe, it, expect, beforeEach } from 'vitest' + +const { promptSpy, createProposalSpy } = + vi.hoisted(() => { + return { + createProposalSpy: vi.fn().mockResolvedValue({}), + promptSpy: vi.fn().mockResolvedValue({}) + } + }) + +vi.mock('inquirer', async (importOriginal) => { + const actual = await importOriginal() + + if (typeof actual === 'object' && actual !== null) { + return { + ...actual, + default: { + prompt: promptSpy + } + } + } else { + return actual + } +}) + +vi.mock('../../../src/services/proposal.service', () => ({ + createProposal: createProposalSpy +})) + +const mockedTezosToolkit = {} as unknown as TezosToolkit + +describe('handlers', () => { + beforeEach(() => { + vi.clearAllMocks() + }) + + describe('handleCreateProposal', () => { + describe('when createProposal is called with success', () => { + it('should create a proposal with provided title and description', async () => { + const title = 'Proposal Title' + const description = 'Proposal Description' + + promptSpy.mockResolvedValueOnce({ title, description }) + await handleCreateProposal(mockedTezosToolkit) + + expect(createProposalSpy).toBeCalledWith({ title: 'Proposal Title', description: 'Proposal Description' }, mockedTezosToolkit) + }) + }) + + describe('when createProposal is called with error', () => { + it('should log error message', async () => { + const error = { + lastError: { + with: { + string: 'Custom Error' + } + } + } + createProposalSpy.mockRejectedValueOnce(error) + const title = 'Proposal Title' + const description = 'Proposal Description' + + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) + + promptSpy.mockResolvedValueOnce({ title, description }) + await handleCreateProposal(mockedTezosToolkit) + + expect(createProposalSpy).toBeCalled() + expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n')) + }) + }) + }) +}) diff --git a/test/handlers/roleHandlers.spec.ts b/test/handlers/roleHandlers.spec.ts index 7c29272113b48c48132cbf212ba9796da2c0e9b4..747b7ae9fd5694962df83437df844fb32b1cefb0 100644 --- a/test/handlers/roleHandlers.spec.ts +++ b/test/handlers/roleHandlers.spec.ts @@ -1,17 +1,31 @@ import { type TezosToolkit } from '@taquito/taquito' import { handleAdminFATokenChoice, handleAdminAssociationChoice, handleAdherentChoice, handleConnectedChoice } from '../../src/handlers/roleHandlers' -import { handleCreateAssociation, handleCreateToken, handleBurnToken, handleGetBalance, handleCreateProposal, handleJoinAssociation, handleGetAssociations } from '../../src/handlers/handlers' +import { handleCreateProposal } from '../../src/handlers/proposal/proposalHandlers' +import { handleCreateAssociation, handleGetAssociations, handleJoinAssociation } from '../../src/handlers/association/associationHandlers' +import { handleCreateToken, handleBurnToken } from '../../src/handlers/token/tokenHandlers' +import { handleGetBalance } from '../../src/handlers/balance/balanceHandlers' + import { vi, describe, it, expect, beforeEach } from 'vitest' import chalk from 'chalk' -vi.mock('../../src/handlers/handlers', () => ({ +vi.mock('../../src/handlers/proposal/proposalHandlers', () => ({ + handleCreateProposal: vi.fn() +})) + +vi.mock('../../src/handlers/association/associationHandlers', () => ({ handleCreateAssociation: vi.fn(), - handleCreateToken: vi.fn(), - handleBurnToken: vi.fn(), - handleGetBalance: vi.fn(), - handleCreateProposal: vi.fn(), handleJoinAssociation: vi.fn(), - handleGetAssociations: vi.fn() + handleGetAssociations: vi.fn().mockResolvedValue([{}]), + handleGetAssociationDetails: vi.fn() +})) + +vi.mock('../../src/handlers/token/tokenHandlers', () => ({ + handleCreateToken: vi.fn(), + handleBurnToken: vi.fn() +})) + +vi.mock('../../src/handlers/balance/balanceHandlers', () => ({ + handleGetBalance: vi.fn() })) const mockedTezosToolkit = {} as unknown as TezosToolkit @@ -24,25 +38,34 @@ describe('roleHandlers', () => { describe('handleAdminFATokenChoice', () => { describe('when choice is "Créer une association"', () => { it('should call handleCreateAssociation', async () => { + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) + await handleAdminFATokenChoice('Créer une association', mockedTezosToolkit) expect(handleCreateAssociation).toBeCalled() + expect(consoleSpy).toBeCalledWith(chalk.bgGreenBright('\nVotre association a été créée !!\n')) }) }) describe('when choice is "Créer un token"', () => { it('should call handleCreateToken', async () => { + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) + await handleAdminFATokenChoice('Créer un token', mockedTezosToolkit) expect(handleCreateToken).toBeCalled() + expect(consoleSpy).toBeCalledWith(chalk.bgGreenBright('\nVotre token a été créé !!\n')) }) }) describe('when choice is "Bruler des tokens"', () => { it('should call handleBurnToken', async () => { + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) + await handleAdminFATokenChoice('Bruler des tokens', mockedTezosToolkit) expect(handleBurnToken).toBeCalled() + expect(consoleSpy).toBeCalledWith(chalk.bgGreenBright('\nVous avez bruler vos tokens !!\n')) }) }) @@ -67,17 +90,23 @@ describe('roleHandlers', () => { describe('handleAdminAssociationChoice', () => { describe('when choice is "Créer un token"', () => { it('should call handleCreateToken', async () => { + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) + await handleAdminAssociationChoice('Créer un token', mockedTezosToolkit) expect(handleCreateToken).toBeCalled() + expect(consoleSpy).toBeCalledWith(chalk.bgGreenBright('\nVotre token a été créé !!\n')) }) }) describe('when choice is "Bruler des tokens"', () => { it('should call handleBurnToken', async () => { + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) + await handleAdminAssociationChoice('Bruler des tokens', mockedTezosToolkit) expect(handleBurnToken).toBeCalled() + expect(consoleSpy).toBeCalledWith(chalk.bgGreenBright('\nVous avez bruler vos tokens !!\n')) }) }) @@ -92,6 +121,7 @@ describe('roleHandlers', () => { describe('when choice is invalid', () => { it('should log "Choix invalide"', async () => { const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) + await handleAdminAssociationChoice('invalid', mockedTezosToolkit) expect(consoleSpy).toHaveBeenCalledWith(chalk.bgRedBright('\nChoix invalide\n')) @@ -102,17 +132,23 @@ describe('roleHandlers', () => { describe('handleAdherentChoice', () => { describe('when choice is "Faire une proposition"', () => { it('should call handleCreateProposal', async () => { + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) + await handleAdherentChoice('Faire une proposition', mockedTezosToolkit) expect(handleCreateProposal).toBeCalled() + expect(consoleSpy).toHaveBeenCalledWith(chalk.bgGreenBright('\nVous avez soumis une proposition !!\n')) }) }) describe('when choice is "Créer un token"', () => { it('should call handleCreateToken', async () => { + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) + await handleAdherentChoice('Créer un token', mockedTezosToolkit) expect(handleCreateToken).toBeCalled() + expect(consoleSpy).toBeCalledWith(chalk.bgGreenBright('\nVotre token a été créé !!\n')) }) }) @@ -145,9 +181,12 @@ describe('roleHandlers', () => { describe('when choice is "Créer un token"', () => { it('should call handleCreateToken', async () => { + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) + await handleConnectedChoice('Créer un token', mockedTezosToolkit) expect(handleCreateToken).toBeCalled() + expect(consoleSpy).toBeCalledWith(chalk.bgGreenBright('\nVotre token a été créé !!\n')) }) }) diff --git a/test/handlers/token/tokenHandlers.spec.ts b/test/handlers/token/tokenHandlers.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..39e9999a0aa88b7ff9c2ce1506a20313e2f85381 --- /dev/null +++ b/test/handlers/token/tokenHandlers.spec.ts @@ -0,0 +1,113 @@ +import { handleCreateToken, handleBurnToken } from '../../../src/handlers/token/tokenHandlers' +import { type TezosToolkit } from '@taquito/taquito' +import chalk from 'chalk' +import { vi, describe, it, expect, beforeEach } from 'vitest' + +const { promptSpy, createFATokenSpy, burnTokenSpy } = + vi.hoisted(() => { + return { + createFATokenSpy: vi.fn().mockResolvedValue({}), + burnTokenSpy: vi.fn().mockResolvedValue({}), + promptSpy: vi.fn().mockResolvedValue({}) + } + }) + +vi.mock('inquirer', async (importOriginal) => { + const actual = await importOriginal() + + if (typeof actual === 'object' && actual !== null) { + return { + ...actual, + default: { + prompt: promptSpy + } + } + } else { + return actual + } +}) + +vi.mock('../../../src/services/token.service', () => ({ + createFAToken: createFATokenSpy, + burnToken: burnTokenSpy +})) + +const mockedTezosToolkit = {} as unknown as TezosToolkit + +describe('tokenHandlers', () => { + beforeEach(() => { + vi.clearAllMocks() + }) + + describe('handleCreateToken', () => { + describe('when createFAToken is called with success', () => { + it('should create a token with provided nbTokenFungible', async () => { + const nbTokenFungible = 5 + + promptSpy.mockResolvedValueOnce({ nbTokenFungible }) + + await handleCreateToken(mockedTezosToolkit) + + expect(createFATokenSpy).toBeCalledWith(nbTokenFungible, mockedTezosToolkit) + }) + }) + + describe('when createFAToken is called with error', () => { + it('should log error message', async () => { + const error = { + lastError: { + with: { + string: 'Custom Error' + } + } + } + createFATokenSpy.mockRejectedValueOnce(error) + const nbTokenFungible = 5 + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) + + promptSpy.mockResolvedValueOnce({ nbTokenFungible }) + + await handleCreateToken(mockedTezosToolkit) + + expect(createFATokenSpy).toBeCalledWith(nbTokenFungible, mockedTezosToolkit) + expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n')) + }) + }) + }) + + describe('handleBurnToken', () => { + describe('when burnToken is called with success', () => { + it('should create a token with provided nbTokenToBurn', async () => { + const nbTokenToBurn = 5 + + promptSpy.mockResolvedValueOnce({ nbTokenToBurn }) + + await handleBurnToken(mockedTezosToolkit) + + expect(burnTokenSpy).toBeCalledWith(nbTokenToBurn, mockedTezosToolkit) + }) + }) + + describe('when burnToken is called with error', () => { + it('should log error message', async () => { + const error = { + lastError: { + with: { + string: 'Custom Error' + } + } + } + burnTokenSpy.mockRejectedValueOnce(error) + const nbTokenToBurn = 5 + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) + + promptSpy.mockResolvedValueOnce({ nbTokenToBurn }) + + await handleBurnToken(mockedTezosToolkit) + + expect(burnTokenSpy).toBeCalledWith(nbTokenToBurn, mockedTezosToolkit) + expect(consoleSpy).toBeCalledWith(chalk.bgRed('\nCustom Error\n')) + }) + }) + }) +})