diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 0bcca24c8c5674fb95a106a3ea7e527d21597cd0..7cb2ded8c972b008c8dd77f6093dfbdcd4d9bb98 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -10,6 +10,6 @@ module.exports = { }, "rules": { "@typescript-eslint/prefer-nullish-coalescing": "off", - "@typescript-eslint/strict-boolean-expressions": "off" + "@typescript-eslint/strict-boolean-expressions": "off", } } diff --git a/fakeWallet.json b/fakeWallet.json new file mode 100644 index 0000000000000000000000000000000000000000..2765aff0147140141eceb7f7f21f686969093d1e --- /dev/null +++ b/fakeWallet.json @@ -0,0 +1,3 @@ +{ + "privateKey": "fakePrivateKey" +} \ No newline at end of file diff --git a/index.ts b/index.ts index ec78eea40714c684519a79f54c2e415e61b32e49..03e8e30adfdebbd7ec5f5817234aa8f38a0bcb1e 100644 --- a/index.ts +++ b/index.ts @@ -4,9 +4,9 @@ import chalk from 'chalk' import fs from 'fs' import { TezosToolkit } from '@taquito/taquito' import { InMemorySigner } from '@taquito/signer' +import inquirer from 'inquirer' // IMPORT UTILS -import { askMultipleQuestion } from './src/utils/askQuestion.js' import { getAsciiArtText } from './src/utils/getAsciiArtText.js' import { getRole } from './src/utils/getRole.js' @@ -38,28 +38,41 @@ program.command('main') console.log('\n') console.log(chalk.bgBlue(`Vous êtes connecté en tant que ${getRole(tezos)}`)) console.log('\n') - console.log(chalk.yellow('Que souhaitez-vous faire ?')) - console.log('\n') // START MAIN APPLICATION while (true) { - let choice: string + const questions = [ + { + type: 'list', + name: 'choice', + message: 'Que souhaitez-vous faire ?', + choices: [] + } + ] switch (getRole(tezos)) { case 'ADMIN_FA_TOKEN': - choice = await askMultipleQuestion(['Créer une association', 'Créer un token', 'Bruler mes tokens', 'Voir mon portefeuille', 'Quitter']) - await handleAdminFATokenChoice(choice, tezos) + questions[0].choices = ['Créer une association', 'Créer un token', 'Bruler des tokens', 'Voir mon portefeuille'] + await inquirer.prompt(questions).then(async (answers: { choice: string }) => { + await handleAdminFATokenChoice(answers.choice, tezos) + }) break case 'ADMIN_ASSOCIATION': - choice = await askMultipleQuestion(['Créer un token', 'Bruler mes tokens', 'Voir mon portefeuille', 'Quitter']) - await handleAdminAssociationChoice(choice, tezos) + questions[0].choices = ['Créer un token', 'Bruler des tokens', 'Voir mon portefeuille'] + await inquirer.prompt(questions).then(async (answers: { choice: string }) => { + await handleAdminAssociationChoice(answers.choice, tezos) + }) break case 'ADHERENT': - choice = await askMultipleQuestion(['Faire une proposition', 'Créer un token FA', 'Voir mon portefeuille', 'Quitter']) - await handleAdherentChoice(choice, tezos) + questions[0].choices = ['Faire une proposition', 'Créer un token', 'Voir mon portefeuille'] + await inquirer.prompt(questions).then(async (answers: { choice: string }) => { + await handleAdherentChoice(answers.choice, tezos) + }) break case 'CONNECTED': - choice = await askMultipleQuestion(['Rejoindre une association', 'Créer un token FA', 'Voir les associations', 'Voir mon portefeuille', 'Quitter']) - await handleConnectedChoice(choice, tezos) + questions[0].choices = ['Rejoindre une association', 'Créer un token', 'Voir les associations', 'Voir mon portefeuille'] + await inquirer.prompt(questions).then(async (answers: { choice: string }) => { + await handleConnectedChoice(answers.choice, tezos) + }) break } } diff --git a/package-lock.json b/package-lock.json index 7ebbfaf3114113476727ac665cf5df79347b3b49..cef7a34124fb0a5ff521c3c2a76d42767a0f87e2 100644 --- a/package-lock.json +++ b/package-lock.json @@ -15,12 +15,14 @@ "chalk": "^5.3.0", "commander": "^12.0.0", "figlet": "^1.7.0", + "inquirer": "^9.2.16", "ts-node": "^10.9.2", "tsc": "^2.0.4" }, "devDependencies": { "@types/commander": "^2.12.2", "@types/figlet": "^1.5.8", + "@types/inquirer": "^9.0.7", "@types/node": "^20.11.19", "@typescript-eslint/eslint-plugin": "^6.21.0", "eslint": "^8.57.0", @@ -587,6 +589,17 @@ "@jridgewell/sourcemap-codec": "^1.4.10" } }, + "node_modules/@ljharb/through": { + "version": "2.3.13", + "resolved": "https://registry.npmjs.org/@ljharb/through/-/through-2.3.13.tgz", + "integrity": "sha512-/gKJun8NNiWGZJkGzI/Ragc53cOdcLNdzjLaIa+GEjguQs0ulsurx8WN0jijdK9yPqDvziX995sMRLyLt1uZMQ==", + "dependencies": { + "call-bind": "^1.0.7" + }, + "engines": { + "node": ">= 0.4" + } + }, "node_modules/@noble/hashes": { "version": "1.3.3", "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.3.tgz", @@ -1167,6 +1180,16 @@ "integrity": "sha512-G22AUvy4Tl95XLE7jmUM8s8mKcoz+Hr+Xm9W90gJsppJq9f9tHvOGkrpn4gRX0q/cLtBdNkWtWCKDg2UDZoZvQ==", "dev": true }, + "node_modules/@types/inquirer": { + "version": "9.0.7", + "resolved": "https://registry.npmjs.org/@types/inquirer/-/inquirer-9.0.7.tgz", + "integrity": "sha512-Q0zyBupO6NxGRZut/JdmqYKOnN95Eg5V8Csg3PGKkP+FnvsUZx1jAyK7fztIszxxMuoBA6E3KXWvdZVXIpx60g==", + "dev": true, + "dependencies": { + "@types/through": "*", + "rxjs": "^7.2.0" + } + }, "node_modules/@types/json-schema": { "version": "7.0.15", "resolved": "https://registry.npmjs.org/@types/json-schema/-/json-schema-7.0.15.tgz", @@ -1193,6 +1216,15 @@ "integrity": "sha512-I8EUhyrgfLrcTkzV3TSsGyl1tSuPrEDzr0yd5m90UgNxQkyDXULk3b6MlQqTCpZpNtWe1K0hzclnZkTcLBe2UQ==", "dev": true }, + "node_modules/@types/through": { + "version": "0.0.33", + "resolved": "https://registry.npmjs.org/@types/through/-/through-0.0.33.tgz", + "integrity": "sha512-HsJ+z3QuETzP3cswwtzt2vEIiHBk/dCcHGhbmG5X3ecnwFD/lPrMpliGXxSCg03L9AhrdwA4Oz/qfspkDW+xGQ==", + "dev": true, + "dependencies": { + "@types/node": "*" + } + }, "node_modules/@typescript-eslint/eslint-plugin": { "version": "6.21.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-6.21.0.tgz", @@ -1502,11 +1534,35 @@ "url": "https://github.com/sponsors/epoberezkin" } }, + "node_modules/ansi-escapes": { + "version": "4.3.2", + "resolved": "https://registry.npmjs.org/ansi-escapes/-/ansi-escapes-4.3.2.tgz", + "integrity": "sha512-gKXj5ALrKWQLsYG9jlTRmR/xKluxHV+Z9QEwNIgCfM1/uwPMCuzVVnh5mwTd+OuBZcwSIMbqssNWRm1lE51QaQ==", + "dependencies": { + "type-fest": "^0.21.3" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/ansi-escapes/node_modules/type-fest": { + "version": "0.21.3", + "resolved": "https://registry.npmjs.org/type-fest/-/type-fest-0.21.3.tgz", + "integrity": "sha512-t0rzBq87m3fVcduHDUFhKmyyX+9eo6WQjZvf51Ea/M0Q7+T374Jp1aUiyUl0GKxp8M/OETVHSDvmkyPgvX+X2w==", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/ansi-regex": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-5.0.1.tgz", "integrity": "sha512-quJQXlTSUGL2LH9SUXo8VwsY4soanhgo6LNSm84E1LBcE8s3O0wpdiRzyR9z/ZZJMlMWv37qOOb9pdJlMUEKFQ==", - "dev": true, "engines": { "node": ">=8" } @@ -1727,6 +1783,39 @@ "@noble/hashes": "^1.2.0" } }, + "node_modules/bl": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/bl/-/bl-4.1.0.tgz", + "integrity": "sha512-1W07cM9gS6DcLperZfFSj+bWLtaPGSOHWhPiGzXmvVJbRLdG82sH/Kn8EtW1VqWVA54AKf2h5k5BbnIbwF3h6w==", + "dependencies": { + "buffer": "^5.5.0", + "inherits": "^2.0.4", + "readable-stream": "^3.4.0" + } + }, + "node_modules/bl/node_modules/buffer": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz", + "integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "dependencies": { + "base64-js": "^1.3.1", + "ieee754": "^1.1.13" + } + }, "node_modules/blakejs": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/blakejs/-/blakejs-1.2.1.tgz", @@ -1837,7 +1926,6 @@ "version": "1.0.7", "resolved": "https://registry.npmjs.org/call-bind/-/call-bind-1.0.7.tgz", "integrity": "sha512-GHTSNSYICQ7scH7sZ+M2rFopRoLh8t2bLSW6BbgrtLsahOIB5iyAVJf9GjWK3cYTDaMj4XdBpM1cA6pIS0Kv2w==", - "dev": true, "dependencies": { "es-define-property": "^1.0.0", "es-errors": "^1.3.0", @@ -1890,6 +1978,11 @@ "url": "https://github.com/chalk/chalk?sponsor=1" } }, + "node_modules/chardet": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/chardet/-/chardet-0.7.0.tgz", + "integrity": "sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==" + }, "node_modules/check-error": { "version": "1.0.3", "resolved": "https://registry.npmjs.org/check-error/-/check-error-1.0.3.tgz", @@ -1911,11 +2004,48 @@ "safe-buffer": "^5.0.1" } }, + "node_modules/cli-cursor": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/cli-cursor/-/cli-cursor-3.1.0.tgz", + "integrity": "sha512-I/zHAwsKf9FqGoXM4WWRACob9+SNukZTd94DWF57E4toouRulbCxcUh6RKUEOQlYTHJnzkPMySvPNaaSLNfLZw==", + "dependencies": { + "restore-cursor": "^3.1.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/cli-spinners": { + "version": "2.9.2", + "resolved": "https://registry.npmjs.org/cli-spinners/-/cli-spinners-2.9.2.tgz", + "integrity": "sha512-ywqV+5MmyL4E7ybXgKys4DugZbX0FC6LnwrhjuykIjnK9k8OQacQ7axGKnjDXWNhns0xot3bZI5h55H8yo9cJg==", + "engines": { + "node": ">=6" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/cli-width": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/cli-width/-/cli-width-4.1.0.tgz", + "integrity": "sha512-ouuZd4/dm2Sw5Gmqy6bGyNNNe1qt9RpmxveLSO7KcgsTnU7RXfsw+/bukWGo1abgBiMAic068rclZsO4IWmmxQ==", + "engines": { + "node": ">= 12" + } + }, + "node_modules/clone": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/clone/-/clone-1.0.4.tgz", + "integrity": "sha512-JQHZ2QMW6l3aH/j6xCqQThY/9OH4D/9ls34cgkUBiEeocRTU04tHfKPBsUK1PqZCUQM7GiA0IIXJSuXHI64Kbg==", + "engines": { + "node": ">=0.8" + } + }, "node_modules/color-convert": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/color-convert/-/color-convert-2.0.1.tgz", "integrity": "sha512-RRECPsj7iu/xb5oKYcsFHSppFNnsj/52OVTRKb4zP5onXwVF3zVmmToNcOfGC+CRDpfK/U584fMg38ZHCaElKQ==", - "dev": true, "dependencies": { "color-name": "~1.1.4" }, @@ -1926,8 +2056,7 @@ "node_modules/color-name": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/color-name/-/color-name-1.1.4.tgz", - "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==", - "dev": true + "integrity": "sha512-dOy+3AuW3a2wNbZHIuMZpTcgjGuLU/uBL/ubcZF9OXbDo8ff4O8yVp5Bf0efS8uEoYo5q4Fx7dY9OgQGXgAsQA==" }, "node_modules/commander": { "version": "12.0.0", @@ -2073,11 +2202,21 @@ "integrity": "sha512-oIPzksmTg4/MriiaYGO+okXDT7ztn/w3Eptv/+gSIdMdKsJo0u4CfYNFJPy+4SKMuCqGw2wxnA+URMg3t8a/bQ==", "dev": true }, + "node_modules/defaults": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/defaults/-/defaults-1.0.4.tgz", + "integrity": "sha512-eFuaLoy/Rxalv2kr+lqMlUnrDWV+3j4pljOIJgLIhI058IQfWJ7vXhyEIHu+HtC738klGALYxOKDO0bQP3tg8A==", + "dependencies": { + "clone": "^1.0.2" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/define-data-property": { "version": "1.1.4", "resolved": "https://registry.npmjs.org/define-data-property/-/define-data-property-1.1.4.tgz", "integrity": "sha512-rBMvIzlpA8v6E+SJZoo++HAYqsLrkg7MSfIinMPFhmkorw7X+dOXVJQs+QT69zGkzMyfDnIMN2Wid1+NbL3T+A==", - "dev": true, "dependencies": { "es-define-property": "^1.0.0", "es-errors": "^1.3.0", @@ -2162,6 +2301,11 @@ "minimalistic-crypto-utils": "^1.0.1" } }, + "node_modules/emoji-regex": { + "version": "8.0.0", + "resolved": "https://registry.npmjs.org/emoji-regex/-/emoji-regex-8.0.0.tgz", + "integrity": "sha512-MSjYzcWNOA0ewAHpz0MxpYFvwg6yjy1NG3xteoqz644VCo/RPgnr1/GGt+ic3iJTzQ8Eu3TdM14SawnVUmGE6A==" + }, "node_modules/es-abstract": { "version": "1.23.2", "resolved": "https://registry.npmjs.org/es-abstract/-/es-abstract-1.23.2.tgz", @@ -2226,7 +2370,6 @@ "version": "1.0.0", "resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.0.tgz", "integrity": "sha512-jxayLKShrEqqzJ0eumQbVhTYQM27CfT1T35+gCgDFoL82JLsXqTJ76zv6A0YLOgEnLUMvLzsDsGIrl8NFpT2gQ==", - "dev": true, "dependencies": { "get-intrinsic": "^1.2.4" }, @@ -2238,7 +2381,6 @@ "version": "1.3.0", "resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz", "integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==", - "dev": true, "engines": { "node": ">= 0.4" } @@ -2846,6 +2988,19 @@ "url": "https://github.com/sindresorhus/execa?sponsor=1" } }, + "node_modules/external-editor": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/external-editor/-/external-editor-3.1.0.tgz", + "integrity": "sha512-hMQ4CX1p1izmuLYyZqLMO/qGNw10wSv9QDCPfzXfyFrOaCSSoRfqE1Kf1s5an66J5JZC62NewG+mK49jOCtQew==", + "dependencies": { + "chardet": "^0.7.0", + "iconv-lite": "^0.4.24", + "tmp": "^0.0.33" + }, + "engines": { + "node": ">=4" + } + }, "node_modules/fast-deep-equal": { "version": "3.1.3", "resolved": "https://registry.npmjs.org/fast-deep-equal/-/fast-deep-equal-3.1.3.tgz", @@ -2911,6 +3066,28 @@ "node": ">= 0.4.0" } }, + "node_modules/figures": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/figures/-/figures-3.2.0.tgz", + "integrity": "sha512-yaduQFRKLXYOGgEn6AZau90j3ggSOyiqXU0F9JZfeXYhNa+Jk4X+s45A2zg5jns87GAFa34BBm2kXw4XpNcbdg==", + "dependencies": { + "escape-string-regexp": "^1.0.5" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/figures/node_modules/escape-string-regexp": { + "version": "1.0.5", + "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", + "integrity": "sha512-vbRorB5FUQWvla16U8R/qgaFIya2qGzwDrNmCZuYKrbdSUMG6I1ZCGQRefkRVhuOkIGVne7BQ35DSfo1qvJqFg==", + "engines": { + "node": ">=0.8.0" + } + }, "node_modules/file-entry-cache": { "version": "6.0.1", "resolved": "https://registry.npmjs.org/file-entry-cache/-/file-entry-cache-6.0.1.tgz", @@ -3004,7 +3181,6 @@ "version": "1.1.2", "resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz", "integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==", - "dev": true, "funding": { "url": "https://github.com/sponsors/ljharb" } @@ -3049,7 +3225,6 @@ "version": "1.2.4", "resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.2.4.tgz", "integrity": "sha512-5uYhsJH8VJBTv7oslg4BznJYhDoRI6waYCxMmCdnTrcCrHA/fCFKoTFz2JKKE0HdDFUF7/oQuhzumXJK7paBRQ==", - "dev": true, "dependencies": { "es-errors": "^1.3.0", "function-bind": "^1.1.2", @@ -3213,7 +3388,6 @@ "version": "1.0.1", "resolved": "https://registry.npmjs.org/gopd/-/gopd-1.0.1.tgz", "integrity": "sha512-d65bNlIadxvpb/A2abVdlqKqV563juRnZ1Wtk6s1sIR8uNsXR70xqIzVqxVf1eTqDunwT2MkczEeaezCKTZhwA==", - "dev": true, "dependencies": { "get-intrinsic": "^1.1.3" }, @@ -3240,7 +3414,6 @@ "version": "4.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-4.0.0.tgz", "integrity": "sha512-EykJT/Q1KjTWctppgIAgfSO0tKVuZUjhgMr17kqTumMl6Afv3EISleU7qZUzoXDFTAHTDC4NOoG/ZxU3EvlMPQ==", - "dev": true, "engines": { "node": ">=8" } @@ -3249,7 +3422,6 @@ "version": "1.0.2", "resolved": "https://registry.npmjs.org/has-property-descriptors/-/has-property-descriptors-1.0.2.tgz", "integrity": "sha512-55JNKuIW+vq4Ke1BjOTjM2YctQIvCT7GFzHwmfZPGo5wnrgkid0YQtnAleFSqumZm4az3n2BS+erby5ipJdgrg==", - "dev": true, "dependencies": { "es-define-property": "^1.0.0" }, @@ -3261,7 +3433,6 @@ "version": "1.0.3", "resolved": "https://registry.npmjs.org/has-proto/-/has-proto-1.0.3.tgz", "integrity": "sha512-SJ1amZAJUiZS+PhsVLf5tGydlaVB8EdFpaSO4gmiUKUOxk8qzn5AIy4ZeJUmh22znIdk/uMAUT2pl3FxzVUH+Q==", - "dev": true, "engines": { "node": ">= 0.4" }, @@ -3273,7 +3444,6 @@ "version": "1.0.3", "resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.0.3.tgz", "integrity": "sha512-l3LCuF6MgDNwTDKkdYGEihYjt5pRPbEg46rtlmnSPlUbgmB8LOIrKJbYYFBSbnPaJexMKtiPO8hmeRjRz2Td+A==", - "dev": true, "engines": { "node": ">= 0.4" }, @@ -3322,7 +3492,6 @@ "version": "2.0.2", "resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.2.tgz", "integrity": "sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==", - "dev": true, "dependencies": { "function-bind": "^1.1.2" }, @@ -3349,6 +3518,17 @@ "node": ">=16.17.0" } }, + "node_modules/iconv-lite": { + "version": "0.4.24", + "resolved": "https://registry.npmjs.org/iconv-lite/-/iconv-lite-0.4.24.tgz", + "integrity": "sha512-v3MXnZAcvnywkTUEZomIActle7RXXeedOR31wwl7VlyoXO4Qi9arvSenNQWne1TcRwhCL1HwLI21bEqdpj8/rA==", + "dependencies": { + "safer-buffer": ">= 2.1.2 < 3" + }, + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/ieee754": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", @@ -3417,6 +3597,31 @@ "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==" }, + "node_modules/inquirer": { + "version": "9.2.16", + "resolved": "https://registry.npmjs.org/inquirer/-/inquirer-9.2.16.tgz", + "integrity": "sha512-qzgbB+yNjgSzk2omeqMDtO9IgJet/UL67luT1MaaggRpGK73DBQct5Q4pipwFQcIKK1GbMODYd4UfsRCkSP1DA==", + "dependencies": { + "@ljharb/through": "^2.3.13", + "ansi-escapes": "^4.3.2", + "chalk": "^5.3.0", + "cli-cursor": "^3.1.0", + "cli-width": "^4.1.0", + "external-editor": "^3.1.0", + "figures": "^3.2.0", + "lodash": "^4.17.21", + "mute-stream": "1.0.0", + "ora": "^5.4.1", + "run-async": "^3.0.0", + "rxjs": "^7.8.1", + "string-width": "^4.2.3", + "strip-ansi": "^6.0.1", + "wrap-ansi": "^6.2.0" + }, + "engines": { + "node": ">=18" + } + }, "node_modules/internal-slot": { "version": "1.0.7", "resolved": "https://registry.npmjs.org/internal-slot/-/internal-slot-1.0.7.tgz", @@ -3553,6 +3758,14 @@ "node": ">=0.10.0" } }, + "node_modules/is-fullwidth-code-point": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-3.0.0.tgz", + "integrity": "sha512-zymm5+u+sCsSWyD9qNaejV3DFvhCKclKdizYaJUuHA83RLjb7nSuGnddCHGv0hk+KY7BMAlsWeK4Ueg6EV6XQg==", + "engines": { + "node": ">=8" + } + }, "node_modules/is-glob": { "version": "4.0.3", "resolved": "https://registry.npmjs.org/is-glob/-/is-glob-4.0.3.tgz", @@ -3565,6 +3778,14 @@ "node": ">=0.10.0" } }, + "node_modules/is-interactive": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/is-interactive/-/is-interactive-1.0.0.tgz", + "integrity": "sha512-2HvIEKRoqS62guEC+qBjpvRubdX910WCMuJTZ+I9yvqKU2/12eSL549HMwtabb4oupdj2sMP50k+XJfB/8JE6w==", + "engines": { + "node": ">=8" + } + }, "node_modules/is-negative-zero": { "version": "2.0.3", "resolved": "https://registry.npmjs.org/is-negative-zero/-/is-negative-zero-2.0.3.tgz", @@ -3698,6 +3919,17 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/is-unicode-supported": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/is-unicode-supported/-/is-unicode-supported-0.1.0.tgz", + "integrity": "sha512-knxG2q4UC3u8stRGyAVJCOdxFmv5DZiRcdlIaAQXAbSfJya+OhopNotLQrstBhququ4ZpuKbDc/8S6mgXgPFPw==", + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, "node_modules/is-weakref": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/is-weakref/-/is-weakref-1.0.2.tgz", @@ -3834,12 +4066,61 @@ "url": "https://github.com/sponsors/sindresorhus" } }, + "node_modules/lodash": { + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz", + "integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg==" + }, "node_modules/lodash.merge": { "version": "4.6.2", "resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz", "integrity": "sha512-0KpjqXRVvrYyCsX1swR/XTK0va6VQkQM6MNo7PqW77ByjAhoARA8EfrP1N4+KlKj8YS0ZUCtRT/YUuhyYDujIQ==", "dev": true }, + "node_modules/log-symbols": { + "version": "4.1.0", + "resolved": "https://registry.npmjs.org/log-symbols/-/log-symbols-4.1.0.tgz", + "integrity": "sha512-8XPvpAA8uyhfteu8pIvQxpJZ7SYYdpUivZpGy6sFsBuKRY/7rQGavedeB8aK+Zkyq6upMFVL/9AW6vOYzfRyLg==", + "dependencies": { + "chalk": "^4.1.0", + "is-unicode-supported": "^0.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/log-symbols/node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/log-symbols/node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, "node_modules/loupe": { "version": "2.3.7", "resolved": "https://registry.npmjs.org/loupe/-/loupe-2.3.7.tgz", @@ -3980,6 +4261,14 @@ "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", "dev": true }, + "node_modules/mute-stream": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/mute-stream/-/mute-stream-1.0.0.tgz", + "integrity": "sha512-avsJQhyd+680gKXyG/sQc0nXaC6rBkPOfyHYcFb9+hdkqQkR9bdnkJ0AMZhke0oesPqIO+mFFJ+IdBc7mst4IA==", + "engines": { + "node": "^14.17.0 || ^16.13.0 || >=18.0.0" + } + }, "node_modules/nanoid": { "version": "3.3.7", "resolved": "https://registry.npmjs.org/nanoid/-/nanoid-3.3.7.tgz", @@ -4176,6 +4465,65 @@ "node": ">= 0.8.0" } }, + "node_modules/ora": { + "version": "5.4.1", + "resolved": "https://registry.npmjs.org/ora/-/ora-5.4.1.tgz", + "integrity": "sha512-5b6Y85tPxZZ7QytO+BQzysW31HJku27cRIlkbAXaNx+BdcVi+LlRFmVXzeF6a7JCwJpyw5c4b+YSVImQIrBpuQ==", + "dependencies": { + "bl": "^4.1.0", + "chalk": "^4.1.0", + "cli-cursor": "^3.1.0", + "cli-spinners": "^2.5.0", + "is-interactive": "^1.0.0", + "is-unicode-supported": "^0.1.0", + "log-symbols": "^4.1.0", + "strip-ansi": "^6.0.0", + "wcwidth": "^1.0.1" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/ora/node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, + "node_modules/ora/node_modules/chalk": { + "version": "4.1.2", + "resolved": "https://registry.npmjs.org/chalk/-/chalk-4.1.2.tgz", + "integrity": "sha512-oKnbhFyRIXpUuez8iBMmyEa4nbj4IOQyuhc/wy9kY7/WVPcwIO9VA668Pu8RkO7+0G76SLROeyw9CpQ061i4mA==", + "dependencies": { + "ansi-styles": "^4.1.0", + "supports-color": "^7.1.0" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "url": "https://github.com/chalk/chalk?sponsor=1" + } + }, + "node_modules/os-tmpdir": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/os-tmpdir/-/os-tmpdir-1.0.2.tgz", + "integrity": "sha512-D2FR03Vir7FIu45XBY20mTb+/ZSWB00sjU9jdQXt83gDrI4Ztz5Fs7/yy74g2N5SVQY4xY1qDr4rNddwYRVX0g==", + "engines": { + "node": ">=0.10.0" + } + }, "node_modules/p-limit": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/p-limit/-/p-limit-5.0.0.tgz", @@ -4507,6 +4855,45 @@ "url": "https://github.com/privatenumber/resolve-pkg-maps?sponsor=1" } }, + "node_modules/restore-cursor": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/restore-cursor/-/restore-cursor-3.1.0.tgz", + "integrity": "sha512-l+sSefzHpj5qimhFSE5a8nufZYAM3sBSVMAPtYkmC+4EH2anSGaEMXSD0izRQbu9nfyQ9y5JrVmp7E8oZrUjvA==", + "dependencies": { + "onetime": "^5.1.0", + "signal-exit": "^3.0.2" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/restore-cursor/node_modules/mimic-fn": { + "version": "2.1.0", + "resolved": "https://registry.npmjs.org/mimic-fn/-/mimic-fn-2.1.0.tgz", + "integrity": "sha512-OqbOk5oEQeAZ8WXWydlu9HJjz9WVdEIvamMCcXmuqUYjTknH/sqsWvhQ3vgwKFRR1HpjvNBKQ37nbJgYzGqGcg==", + "engines": { + "node": ">=6" + } + }, + "node_modules/restore-cursor/node_modules/onetime": { + "version": "5.1.2", + "resolved": "https://registry.npmjs.org/onetime/-/onetime-5.1.2.tgz", + "integrity": "sha512-kbpaSSGJTWdAY5KPVeMOKXSrPtr8C8C7wodJbcsd51jRnmD+GZu8Y0VoU6Dm5Z4vWr0Ig/1NKuWRKf7j5aaYSg==", + "dependencies": { + "mimic-fn": "^2.1.0" + }, + "engines": { + "node": ">=6" + }, + "funding": { + "url": "https://github.com/sponsors/sindresorhus" + } + }, + "node_modules/restore-cursor/node_modules/signal-exit": { + "version": "3.0.7", + "resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.7.tgz", + "integrity": "sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==" + }, "node_modules/reusify": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/reusify/-/reusify-1.0.4.tgz", @@ -4573,6 +4960,14 @@ "fsevents": "~2.3.2" } }, + "node_modules/run-async": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/run-async/-/run-async-3.0.0.tgz", + "integrity": "sha512-540WwVDOMxA6dN6We19EcT9sc3hkXPw5mzRNGM3FkdN/vtE9NFvj5lFAPNwUDmJjXidm3v7TC1cTE7t17Ulm1Q==", + "engines": { + "node": ">=0.12.0" + } + }, "node_modules/run-parallel": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/run-parallel/-/run-parallel-1.2.0.tgz", @@ -4658,6 +5053,11 @@ "url": "https://github.com/sponsors/ljharb" } }, + "node_modules/safer-buffer": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/safer-buffer/-/safer-buffer-2.1.2.tgz", + "integrity": "sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==" + }, "node_modules/semver": { "version": "7.6.0", "resolved": "https://registry.npmjs.org/semver/-/semver-7.6.0.tgz", @@ -4677,7 +5077,6 @@ "version": "1.2.2", "resolved": "https://registry.npmjs.org/set-function-length/-/set-function-length-1.2.2.tgz", "integrity": "sha512-pgRc4hJ4/sNjWCSS9AmnS40x3bNMDTknHgL5UaMBTMyJnU90EgWh1Rz+MC9eFu4BuN/UwZjKQuY/1v3rM7HMfg==", - "dev": true, "dependencies": { "define-data-property": "^1.1.4", "es-errors": "^1.3.0", @@ -4812,6 +5211,19 @@ "safe-buffer": "~5.2.0" } }, + "node_modules/string-width": { + "version": "4.2.3", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-4.2.3.tgz", + "integrity": "sha512-wKyQRQpjJ0sIp62ErSZdGsjMJWsap5oRNihHhu6G7JVO/9jIB6UyevL+tXuOqrng8j/cxKTWyWUwvSTriiZz/g==", + "dependencies": { + "emoji-regex": "^8.0.0", + "is-fullwidth-code-point": "^3.0.0", + "strip-ansi": "^6.0.1" + }, + "engines": { + "node": ">=8" + } + }, "node_modules/string.prototype.trim": { "version": "1.2.9", "resolved": "https://registry.npmjs.org/string.prototype.trim/-/string.prototype.trim-1.2.9.tgz", @@ -4865,7 +5277,6 @@ "version": "6.0.1", "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-6.0.1.tgz", "integrity": "sha512-Y38VPSHcqkFrCpFnQ9vuSXmquuv5oXOKpGeT6aGrr3o3Gc9AlVa6JBfUSOCnbxGGZF+/0ooI7KrPuUSztUdU5A==", - "dev": true, "dependencies": { "ansi-regex": "^5.0.1" }, @@ -4922,7 +5333,6 @@ "version": "7.2.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-7.2.0.tgz", "integrity": "sha512-qpCAvRl9stuOHveKsn7HncJRvv501qIacKzQlO/+Lwxc9+0q2wLyv4Dfvt80/DPn2pqOBsJdDiogXGR9+OvwRw==", - "dev": true, "dependencies": { "has-flag": "^4.0.0" }, @@ -4972,6 +5382,17 @@ "node": ">=14.0.0" } }, + "node_modules/tmp": { + "version": "0.0.33", + "resolved": "https://registry.npmjs.org/tmp/-/tmp-0.0.33.tgz", + "integrity": "sha512-jRCJlojKnZ3addtTOjdIqoRuPEKBvNXcGYqzO6zWZX8KfKEpnGY5jfggJQ3EjKuu8D4bJRr0y+cYJFmYbImXGw==", + "dependencies": { + "os-tmpdir": "~1.0.2" + }, + "engines": { + "node": ">=0.6.0" + } + }, "node_modules/to-regex-range": { "version": "5.0.1", "resolved": "https://registry.npmjs.org/to-regex-range/-/to-regex-range-5.0.1.tgz", @@ -5392,6 +5813,14 @@ } } }, + "node_modules/wcwidth": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/wcwidth/-/wcwidth-1.0.1.tgz", + "integrity": "sha512-XHPEwS0q6TaxcvG85+8EYkbiCux2XtWG2mkc47Ng2A77BQu9+DqIOJldST4HgPkuea7dvKSj5VgX3P1d4rW8Tg==", + "dependencies": { + "defaults": "^1.0.3" + } + }, "node_modules/webidl-conversions": { "version": "3.0.1", "resolved": "https://registry.npmjs.org/webidl-conversions/-/webidl-conversions-3.0.1.tgz", @@ -5472,6 +5901,33 @@ "node": ">=8" } }, + "node_modules/wrap-ansi": { + "version": "6.2.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-6.2.0.tgz", + "integrity": "sha512-r6lPcBGxZXlIcymEu7InxDMhdW0KDxpLgoFLcguasxCaJ/SOIZwINatK9KY/tf+ZrlywOKU0UDj3ATXUBfxJXA==", + "dependencies": { + "ansi-styles": "^4.0.0", + "string-width": "^4.1.0", + "strip-ansi": "^6.0.0" + }, + "engines": { + "node": ">=8" + } + }, + "node_modules/wrap-ansi/node_modules/ansi-styles": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/ansi-styles/-/ansi-styles-4.3.0.tgz", + "integrity": "sha512-zbB9rCJAT1rbjiVDb2hqKFHNYLxgtk8NURxZ3IZwD3F6NtxbXZQCnnSi1Lkx+IDohdPlFp222wVALIheZJQSEg==", + "dependencies": { + "color-convert": "^2.0.1" + }, + "engines": { + "node": ">=8" + }, + "funding": { + "url": "https://github.com/chalk/ansi-styles?sponsor=1" + } + }, "node_modules/wrappy": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", diff --git a/package.json b/package.json index 59ecc5b62b38c3fba7f2ebd48dd451cf748d19c8..91c154be471453aad5b95bb0fdb2efa3a0e7fcc3 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "start": "ts-node-esm index.ts", "test": "vitest", "lint": "eslint . --ext .ts --ignore-pattern **/*spec.ts", - "lint:fix": "eslint . --ext .ts --fix --ignore-pattern **/*spec.ts" + "lint:fix": "eslint . --ext .ts --ignore-pattern **/*spec.ts --fix" }, "author": "M2 Miage", "license": "MIT", @@ -19,12 +19,14 @@ "chalk": "^5.3.0", "commander": "^12.0.0", "figlet": "^1.7.0", + "inquirer": "^9.2.16", "ts-node": "^10.9.2", "tsc": "^2.0.4" }, "devDependencies": { "@types/commander": "^2.12.2", "@types/figlet": "^1.5.8", + "@types/inquirer": "^9.0.7", "@types/node": "^20.11.19", "@typescript-eslint/eslint-plugin": "^6.21.0", "eslint": "^8.57.0", diff --git a/test.json b/realWallet.json similarity index 100% rename from test.json rename to realWallet.json diff --git a/src/handlers/handlers.ts b/src/handlers/handlers.ts index 00a29722234b2c5ee9c5a01dab2b07a214846d1c..7cd96419d5619b3fea8da04999d6b14f3dcc9905 100644 --- a/src/handlers/handlers.ts +++ b/src/handlers/handlers.ts @@ -2,9 +2,6 @@ import chalk from 'chalk' import { type TezosToolkit } from '@taquito/taquito' -// IMPORT UTILS -import { askOneQuestion } from '../utils/askQuestion.js' - // IMPORT TYPES import { type Association } from '../types/Association.js' import { type Proposal } from '../types/Proposal.js' @@ -15,6 +12,7 @@ import { createAssociation, getAssociationDetails, getAssociations, joinAssociat 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. @@ -22,18 +20,51 @@ import { burnToken } from '../services/token.service.js' * @returns {Promise<void>} A promise resolved once the association creation process is complete. */ async function handleCreateAssociation (tezos: TezosToolkit): Promise<void> { - const name = await askOneQuestion('Nom: ') - const description = await askOneQuestion('Description: ') - const association: Association = { - name, - description - } + 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 + } + }) console.log("Création de l'association en cours...") try { const op = await createAssociation(association, tezos) console.log(op.hash) - console.log(chalk.green('\nVotre association a été crée !!\n')) + console.log(chalk.green('\nVotre association a été créée !!\n')) } catch { console.log(chalk.bgRedBright("\nErreur lors de la création de l'association\n")) } @@ -45,7 +76,29 @@ async function handleCreateAssociation (tezos: TezosToolkit): Promise<void> { * @returns {Promise<void>} A promise resolved once the token creation process is complete. */ async function handleCreateToken (tezos: TezosToolkit): Promise<void> { - const nbTokenFungible: string = await askOneQuestion('Nombre de token fongible: ') + 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) @@ -76,12 +129,45 @@ async function handleGetBalance (tezos: TezosToolkit): Promise<void> { * @returns {Promise<void>} A promise resolved once the proposal creation process is complete. */ async function handleCreateProposal (tezos: TezosToolkit): Promise<void> { - const title = await askOneQuestion('Titre: ') - const description = await askOneQuestion('Description: ') - const proposal: Proposal = { - title, - description - } + 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 { console.log('Création de la proposition en cours...') @@ -99,19 +185,39 @@ async function handleCreateProposal (tezos: TezosToolkit): Promise<void> { * @returns {Promise<void>} A promise resolved once the joining process is complete. */ async function handleJoinAssociation (tezos: TezosToolkit): Promise<void> { - const name = await askOneQuestion("Nom de l'association: ") + 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 { console.log("Inscription à l'association en cours...") - const op = await joinAssociation(name, tezos) + const op = await joinAssociation(associationName, tezos) console.log(op.hash) - console.log(chalk.green('\nVous avez soumis une proposition !!\n')) + console.log(chalk.green("\nVous avez rejoint l'association !!\n")) } catch { // ERREUR -> PAS ASSEZ DE TOKEN // ERREUR -> ASSOCIATION NON EXISTANTE console.log(chalk.bgRedBright("\nErreur lors de l'inscription à l'association\n")) } - console.log(chalk.green("\nVous avez rejoint l'association !!\n")) } /** @@ -136,11 +242,32 @@ async function handleGetAssociations (tezos: TezosToolkit): Promise<void> { * @returns {Promise<void>} A promise resolved once the joining process is complete. */ async function handleGetAssociationDetails (tezos: TezosToolkit): Promise<void> { - const name = await askOneQuestion("Nom de l'association: ") + 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 { console.log("Récupération des détails de l'association en cours...") - const op = await getAssociationDetails(name, tezos) + const op = await getAssociationDetails(associationName, tezos) console.log(op.hash) // RETURN ASSOCIATIONS } catch { @@ -154,11 +281,33 @@ async function handleGetAssociationDetails (tezos: TezosToolkit): Promise<void> * @returns {Promise<void>} A promise resolved once the proposal creation process is complete. */ async function handleBurnToken (tezos: TezosToolkit): Promise<void> { - const nbTokenToBurn = await askOneQuestion('Nombre de token à bruler: ') + 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 { console.log('Brulure des tokens en cours...') - const op = await burnToken(Number(nbTokenToBurn), tezos) + const op = await burnToken(nbTokenToBurn, tezos) console.log(op.hash) console.log(chalk.green('\nVous avez bruler vos tokens !!\n')) } catch { diff --git a/src/handlers/roleHandlers.ts b/src/handlers/roleHandlers.ts index 4fe367867439792e02375ee41f79f42fd16c4d55..83abf842c1db5c399f5242068fe5e9a6a1d1c238 100644 --- a/src/handlers/roleHandlers.ts +++ b/src/handlers/roleHandlers.ts @@ -10,20 +10,18 @@ import chalk from 'chalk' */ async function handleAdminFATokenChoice (choice: string, tezos: TezosToolkit): Promise<void> { switch (choice) { - case '1': + case 'Créer une association': await handleCreateAssociation(tezos) break - case '2': + case 'Créer un token': await handleCreateToken(tezos) break - case '3': + case 'Bruler des tokens': await handleBurnToken(tezos) break - case '4': + case 'Voir mon portefeuille': await handleGetBalance(tezos) break - case '5': - break default: console.log(chalk.bgRedBright('\nChoix invalide\n')) break @@ -38,17 +36,15 @@ async function handleAdminFATokenChoice (choice: string, tezos: TezosToolkit): P */ async function handleAdminAssociationChoice (choice: string, tezos: TezosToolkit): Promise<void> { switch (choice) { - case '1': + case 'Créer un token': await handleCreateToken(tezos) break - case '2': + case 'Bruler des tokens': await handleBurnToken(tezos) break - case '3': + case 'Voir mon portefeuille': await handleGetBalance(tezos) break - case '5': - break default: console.log(chalk.bgRedBright('\nChoix invalide\n')) break @@ -63,17 +59,15 @@ async function handleAdminAssociationChoice (choice: string, tezos: TezosToolkit */ async function handleAdherentChoice (choice: string, tezos: TezosToolkit): Promise<void> { switch (choice) { - case '1': + case 'Faire une proposition': await handleCreateProposal(tezos) break - case '2': + case 'Créer un token': await handleCreateToken(tezos) break - case '3': + case 'Voir mon portefeuille': await handleGetBalance(tezos) break - case '5': - break default: console.log(chalk.bgRedBright('\nChoix invalide\n')) break @@ -88,20 +82,18 @@ async function handleAdherentChoice (choice: string, tezos: TezosToolkit): Promi */ async function handleConnectedChoice (choice: string, tezos: TezosToolkit): Promise<void> { switch (choice) { - case '1': + case 'Rejoindre une association': await handleJoinAssociation(tezos) break - case '2': + case 'Créer un token': await handleCreateToken(tezos) break - case '3': + case 'Voir les associations': await handleGetAssociations(tezos) break - case '4': + case 'Voir mon portefeuille': await handleGetBalance(tezos) break - case '5': - break default: console.log(chalk.bgRedBright('\nChoix invalide\n')) break diff --git a/src/services/contract.service.ts b/src/services/contract.service.ts index 5acfa45e78582f69a14d7caf4bfdec3ba5ec229b..20b576d9a547c45099ba7a6b8002a470774d83ce 100644 --- a/src/services/contract.service.ts +++ b/src/services/contract.service.ts @@ -4,7 +4,7 @@ import { type Operation, type TezosToolkit } from '@taquito/taquito' const address = 'KT1QZJzhSPQ89K4eC59tmQYCt44qph7wXoJu' // NEED UPDATE ENTRYPOINT !! -async function createFAToken (nbTokenFongible: string, tezos: TezosToolkit): Promise<Operation> { +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() diff --git a/src/utils/askQuestion.ts b/src/utils/askQuestion.ts deleted file mode 100644 index 1f9227e92a018648a8cceef471103fe4cc8f485f..0000000000000000000000000000000000000000 --- a/src/utils/askQuestion.ts +++ /dev/null @@ -1,36 +0,0 @@ -import chalk from 'chalk' -import { createInterface } from 'readline' - -async function askMultipleQuestion (options: string[]): Promise<string> { - const readline = createInterface({ - input: process.stdin, - output: process.stdout - }) - - return await new Promise((resolve) => { - options.forEach((option, index) => { - console.log(`${index + 1}: ${option}`) - }) - - readline.question('\nVotre choix: ', (answer: string) => { - readline.close() - resolve(answer.trim()) - }) - }) -} - -async function askOneQuestion (question: string): Promise<string> { - const readline = createInterface({ - input: process.stdin, - output: process.stdout - }) - - return await new Promise(resolve => { - readline.question(chalk.yellow(question), (answer: string) => { - resolve(answer.trim()) - readline.close() - }) - }) -} - -export { askMultipleQuestion, askOneQuestion } diff --git a/test/handlers/handlers.spec.ts b/test/handlers/handlers.spec.ts new file mode 100644 index 0000000000000000000000000000000000000000..9d8a3b213949c11317c0be59bfc35f21d022ebb6 --- /dev/null +++ b/test/handlers/handlers.spec.ts @@ -0,0 +1,295 @@ +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: 'association-hash' }), + getBalanceSpy: vi.fn().mockResolvedValue({ hash: 'association-hash' }), + createProposalSpy: vi.fn().mockResolvedValue({ hash: 'association-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: 'association-hash' }), + promptSpy: vi.fn().mockResolvedValue({}) + } + }) + +vi.mock('inquirer', async (importOriginal) => { + const actual = await importOriginal() + actual.default.prompt = promptSpy + 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.green('\nVotre association a été créée !!\n')) + }) + }) + + describe('when createAssociation is called with error', () => { + it('should log error message', async () => { + createAssociationSpy.mockRejectedValueOnce({}) + + 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.bgRedBright("\nErreur lors de la création de l'association\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.green('\nVotre token a été créé !!\n')) + }) + }) + + describe('when createFAToken is called with error', () => { + it('should log error message', async () => { + createAssociationSpy.mockRejectedValueOnce({}) + 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.green('\nErreur lors de la création du token\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 () => { + getBalanceSpy.mockRejectedValueOnce({}) + + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) + + await handleGetBalance(mockedTezosToolkit) + + expect(getBalanceSpy).toBeCalled() + expect(consoleSpy).toBeCalledWith(chalk.bgRedBright('\nErreur lors de la récupération de votre portefeuille\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.green('\nVous avez soumis une proposition !!\n')) + }) + }) + + describe('when createProposal is called with error', () => { + it('should log error message', async () => { + createProposalSpy.mockRejectedValueOnce({}) + 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.bgRedBright('\nErreur lors de la création de la proposition\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.green("\nVous avez rejoint l'association !!\n")) + }) + }) + + describe('when joinAssociation is called with error', () => { + it('should log error message', async () => { + joinAssociationSpy.mockRejectedValueOnce({}) + const name = 'Association Name' + + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) + + promptSpy.mockResolvedValueOnce({ name }) + await handleJoinAssociation(mockedTezosToolkit) + + expect(joinAssociationSpy).toBeCalled() + expect(consoleSpy).toBeCalledWith(chalk.bgRedBright("\nErreur lors de l'inscription à l'association\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 () => { + getAssociationsSpy.mockRejectedValueOnce({}) + + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) + + await handleGetAssociations(mockedTezosToolkit) + + expect(getAssociationsSpy).toBeCalled() + expect(consoleSpy).toBeCalledWith(chalk.bgRedBright('\nErreur lors de la récupération des associations\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 () => { + getAssociationDetailsSpy.mockRejectedValueOnce({}) + const associationName = 'Association Name' + + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) + + promptSpy.mockResolvedValueOnce({ associationName }) + await handleGetAssociationDetails(mockedTezosToolkit) + + expect(getAssociationDetailsSpy).toBeCalled() + expect(consoleSpy).toBeCalledWith(chalk.bgRedBright("\nErreur lors de la récupération des détails de l'association\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.green('\nVous avez bruler vos tokens !!\n')) + }) + }) + + describe('when burnToken is called with error', () => { + it('should log error message', async () => { + burnTokenSpy.mockRejectedValueOnce({}) + 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.bgRedBright('\nErreur lors de la brulure de vos tokens\n')) + }) + }) + }) +}) diff --git a/test/handlers/roleHandlers.spec.ts b/test/handlers/roleHandlers.spec.ts index a01f14c0d64d89f31cbf0e4ad1407960fb6bfdcd..f83e3250af80ce949217c51e5b93167494ff2dcb 100644 --- a/test/handlers/roleHandlers.spec.ts +++ b/test/handlers/roleHandlers.spec.ts @@ -1,7 +1,7 @@ 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 { vi, describe, it, expect, afterEach } from 'vitest' +import { vi, describe, it, expect, beforeEach } from 'vitest' import chalk from 'chalk' vi.mock('../../src/handlers/handlers', () => ({ @@ -17,205 +17,163 @@ vi.mock('../../src/handlers/handlers', () => ({ const mockedTezosToolkit = {} as unknown as TezosToolkit describe('roleHandlers', () => { - afterEach(() => { - vi.restoreAllMocks() - }) + beforeEach(() => { + vi.clearAllMocks() + }); describe('handleAdminFATokenChoice', () => { - describe('when choice is "5"', () => { - it('should not call any handler', async () => { - await handleAdminFATokenChoice('5', mockedTezosToolkit) - - expect(handleCreateAssociation).not.toHaveBeenCalled() - expect(handleCreateToken).not.toHaveBeenCalled() - expect(handleBurnToken).not.toHaveBeenCalled() - expect(handleGetBalance).not.toHaveBeenCalled() - }) - }) - describe('when choice is "1"', () => { it('should call handleCreateAssociation', async () => { - await handleAdminFATokenChoice('1', mockedTezosToolkit) + await handleAdminFATokenChoice('Créer une association', mockedTezosToolkit) expect(handleCreateAssociation).toBeCalled() - }) + }); }) describe('when choice is "2"', () => { it('should call handleCreateToken', async () => { - await handleAdminFATokenChoice('2', mockedTezosToolkit) + await handleAdminFATokenChoice('Créer un token', mockedTezosToolkit) expect(handleCreateToken).toBeCalled() - }) + }); }) describe('when choice is "3"', () => { it('should call handleBurnToken', async () => { - await handleAdminFATokenChoice('3', mockedTezosToolkit) + await handleAdminFATokenChoice('Bruler des tokens', mockedTezosToolkit) expect(handleBurnToken).toBeCalled() - }) + }); }) describe('when choice is "4"', () => { it('should call handleGetBalance', async () => { - await handleAdminFATokenChoice('4', mockedTezosToolkit) + await handleAdminFATokenChoice('Voir mon portefeuille', mockedTezosToolkit) expect(handleGetBalance).toBeCalled() - }) + }); }) describe('when choice is invalid', () => { it('should log "Choix invalide"', async () => { - const consoleSpy = vi.spyOn(console, 'log') + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) await handleAdminFATokenChoice('invalid', mockedTezosToolkit) expect(consoleSpy).toHaveBeenCalledWith(chalk.bgRedBright('\nChoix invalide\n')) - }) + }); }) - }) + }); describe('handleAdminAssociationChoice', () => { - describe('when choice is "5"', () => { - it('should not call any handler', async () => { - await handleAdminAssociationChoice('5', mockedTezosToolkit) - - expect(handleCreateToken).not.toHaveBeenCalled() - expect(handleBurnToken).not.toHaveBeenCalled() - expect(handleGetBalance).not.toHaveBeenCalled() - }) - }) - describe('when choice is "1"', () => { it('should call handleCreateToken', async () => { - await handleAdminAssociationChoice('1', mockedTezosToolkit) + await handleAdminAssociationChoice('Créer un token', mockedTezosToolkit) expect(handleCreateToken).toBeCalled() - }) + }); }) describe('when choice is "2"', () => { it('should call handleBurnToken', async () => { - await handleAdminAssociationChoice('2', mockedTezosToolkit) + await handleAdminAssociationChoice('Bruler des tokens', mockedTezosToolkit) expect(handleBurnToken).toBeCalled() - }) + }); }) describe('when choice is "3"', () => { it('should call handleGetBalance', async () => { - await handleAdminAssociationChoice('3', mockedTezosToolkit) + await handleAdminAssociationChoice('Voir mon portefeuille', mockedTezosToolkit) expect(handleGetBalance).toBeCalled() - }) + }); }) describe('when choice is invalid', () => { it('should log "Choix invalide"', async () => { - const consoleSpy = vi.spyOn(console, 'log') + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) await handleAdminAssociationChoice('invalid', mockedTezosToolkit) expect(consoleSpy).toHaveBeenCalledWith(chalk.bgRedBright('\nChoix invalide\n')) - }) + }); }) - }) + }); describe('handleAdherentChoice', () => { - describe('when choice is "5"', () => { - it('should not call any handler', async () => { - await handleAdherentChoice('5', mockedTezosToolkit) - - expect(handleCreateProposal).not.toHaveBeenCalled() - expect(handleCreateToken).not.toHaveBeenCalled() - expect(handleGetBalance).not.toHaveBeenCalled() - }) - }) - describe('when choice is "1"', () => { it('should call handleCreateProposal', async () => { - await handleAdherentChoice('1', mockedTezosToolkit) + await handleAdherentChoice('Faire une proposition', mockedTezosToolkit) expect(handleCreateProposal).toBeCalled() - }) + }); }) describe('when choice is "2"', () => { it('should call handleCreateToken', async () => { - await handleAdherentChoice('2', mockedTezosToolkit) + await handleAdherentChoice('Créer un token', mockedTezosToolkit) expect(handleCreateToken).toBeCalled() - }) + }); }) describe('when choice is "3"', () => { it('should call handleGetBalance', async () => { - await handleAdherentChoice('3', mockedTezosToolkit) + await handleAdherentChoice('Voir mon portefeuille', mockedTezosToolkit) expect(handleGetBalance).toBeCalled() - }) + }); }) describe('when choice is invalid', () => { it('should log "Choix invalide"', async () => { - const consoleSpy = vi.spyOn(console, 'log') + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) await handleAdherentChoice('invalid', mockedTezosToolkit) expect(consoleSpy).toHaveBeenCalledWith(chalk.bgRedBright('\nChoix invalide\n')) - }) + }); }) - }) + }); describe('handleConnectedChoice', () => { - describe('when choice is "5"', () => { - it('should not call any handler', async () => { - await handleConnectedChoice('5', mockedTezosToolkit) - - expect(handleJoinAssociation).not.toHaveBeenCalled() - expect(handleCreateToken).not.toHaveBeenCalled() - expect(handleGetAssociations).not.toHaveBeenCalled() - expect(handleGetBalance).not.toHaveBeenCalled() - }) - }) - describe('when choice is "1"', () => { it('should call handleJoinAssociation', async () => { - await handleConnectedChoice('1', mockedTezosToolkit) + await handleConnectedChoice('Rejoindre une association', mockedTezosToolkit) expect(handleJoinAssociation).toBeCalled() - }) + }); }) describe('when choice is "2"', () => { it('should call handleCreateToken', async () => { - await handleConnectedChoice('2', mockedTezosToolkit) + await handleConnectedChoice('Créer un token', mockedTezosToolkit) expect(handleCreateToken).toBeCalled() - }) + }); }) describe('when choice is "3"', () => { it('should call handleGetAssociations', async () => { - await handleConnectedChoice('3', mockedTezosToolkit) + await handleConnectedChoice('Voir les associations', mockedTezosToolkit) expect(handleGetAssociations).toBeCalled() - }) + }); }) describe('when choice is "4"', () => { it('should call handleGetBalance', async () => { - await handleConnectedChoice('4', mockedTezosToolkit) + await handleConnectedChoice('Voir mon portefeuille', mockedTezosToolkit) expect(handleGetBalance).toBeCalled() - }) + }); }) describe('when choice is invalid', () => { it('should log "Choix invalide"', async () => { - const consoleSpy = vi.spyOn(console, 'log') + const consoleSpy = vi.spyOn(console, 'log').mockImplementation(() => {}) await handleConnectedChoice('invalid', mockedTezosToolkit) expect(consoleSpy).toHaveBeenCalledWith(chalk.bgRedBright('\nChoix invalide\n')) - }) + }); }) - }) + }); }) diff --git a/test/services/balance.service.spec.ts b/test/services/balance.service.spec.ts deleted file mode 100644 index 3cd59d3635f0205cf75188eca9352835694dac26..0000000000000000000000000000000000000000 --- a/test/services/balance.service.spec.ts +++ /dev/null @@ -1,37 +0,0 @@ -import { type TezosToolkit } from '@taquito/taquito' -import { getBalance } from '../../src/services/balance.service' -import { vi, describe, it, expect } from 'vitest' - -const FAKE_PUBLIC_HASH_KEY = 'tz1abc123def456' - -describe('getBalance', () => { - it('should return the balance', async () => { - const mockedTezosToolkit = { - tz: { - getBalance: vi.fn().mockResolvedValue({ toNumber: () => 1000000000 }) - }, - signer: { - publicKeyHash: vi.fn().mockResolvedValue(FAKE_PUBLIC_HASH_KEY) - } - } as unknown as TezosToolkit - - const balance = await getBalance(mockedTezosToolkit) - - expect(balance).toEqual(1000) // 1000000000 / 1000000 = 1000 tez - expect(mockedTezosToolkit.tz.getBalance).toHaveBeenCalledWith(FAKE_PUBLIC_HASH_KEY) - }) - - it('should throw an error if getting the balance fails', async () => { - const mockedTezosToolkit = { - tz: { - getBalance: vi.fn().mockRejectedValue(new Error('Failed to get balance')) - }, - signer: { - publicKeyHash: vi.fn().mockResolvedValue(FAKE_PUBLIC_HASH_KEY) - } - } as unknown as TezosToolkit - - await expect(getBalance(mockedTezosToolkit)).rejects.toThrowError('Failed to get balance') - expect(mockedTezosToolkit.tz.getBalance).toHaveBeenCalledWith(FAKE_PUBLIC_HASH_KEY) - }) -}) diff --git a/test/services/token.service.spec.ts b/test/services/token.service.spec.ts deleted file mode 100644 index 6e0b456e7590ec90c63b5d855104c489915a0da0..0000000000000000000000000000000000000000 --- a/test/services/token.service.spec.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { type TezosToolkit } from '@taquito/taquito' -import { createFAToken } from '../../src/services/token.service' -import { vi, describe, it, expect, type MockedClass } from 'vitest' - -const mockedTezosToolkit = { - contract: { - at: vi.fn().mockResolvedValue({ - methodsObject: { - destroyPlanet: vi.fn().mockReturnValue({ - send: vi.fn().mockResolvedValue({ - confirmation: vi.fn().mockResolvedValue({}) - }) - }) - } - }) - } -} as unknown as MockedClass<typeof TezosToolkit> - -describe('createFAToken', () => { - it('should create a token successfully', async () => { - await createFAToken(mockedTezosToolkit as unknown as TezosToolkit) - - expect(mockedTezosToolkit.contract.at).toHaveBeenCalledWith('KT1QZJzhSPQ89K4eC59tmQYCt44qph7wXoJu') - }) - - it('should handle failure to create a token', async () => { - const mockedRejectedTezosToolkit = { - contract: { - at: vi.fn().mockRejectedValue(new Error('Contract not found')) - } - } as unknown as MockedClass<typeof TezosToolkit> - - let error: Error | undefined - try { - await createFAToken(mockedRejectedTezosToolkit as unknown as TezosToolkit) - } catch (err) { - error = err - } - - expect(error).toBeDefined() - expect(error?.message).toBe('Contract not found') - }) -})