diff --git a/src/commands/app/create.ts b/src/commands/app/create.ts index 6f1b27e5..12a59ca1 100644 --- a/src/commands/app/create.ts +++ b/src/commands/app/create.ts @@ -163,7 +163,7 @@ export default class AppCreate extends Command { const planDetails = plans.projectBundlePlans[bundlePlan]; return Object.keys(planDetails) - .filter((key) => planDetails[key].available) + .filter((key) => key !== 'free' && planDetails[key].available) .map((key) => { const { displayPrice } = planDetails[key]; return { @@ -200,7 +200,7 @@ export default class AppCreate extends Command { ...Object.keys(plans.projects) .filter((plan) => { if ( - (plan === 'free' || plan.includes('g2')) && + plan.includes('g2') && plans.projects[plan].available && plans.projects[plan].region === 'iran' ) { @@ -216,18 +216,7 @@ export default class AppCreate extends Command { const storageClass = availablePlan.storageClass; return { value: plan, - name: `RAM: ${ram}${' '.repeat( - 5 - ram.toString().length, - )} GB, CPU: ${cpu}${' '.repeat( - 6 - cpu.toString().length, - )}Core, Disk: ${disk}${ - ' '.repeat(5 - disk.toString().length) + 'GB' - }${storageClass || 'SSD'}, Price: ${price.toLocaleString()}${ - price - ? ' '.repeat(7 - Math.floor(price).toString().length) + - 'Tomans/Month' - : '' - }`, + name: `RAM: ${String(ram).padEnd(5)} GB, CPU: ${String(cpu).padEnd(6)}Core, Disk: ${String(disk).padEnd(5)}GB${storageClass || 'SSD'}, Price: ${price.toLocaleString().padEnd(10)} Tomans/Month`, }; }), ], diff --git a/src/commands/db/create.ts b/src/commands/db/create.ts index d3c2277b..d88c97d2 100644 --- a/src/commands/db/create.ts +++ b/src/commands/db/create.ts @@ -260,20 +260,12 @@ export default class Create extends Command { const disk = availablePlan.volume; const price = availablePlan.price * 720; const storageClass = availablePlan.storageClass; + const priceLabel = price + ? `${price.toLocaleString().padEnd(10)} Tomans/Month` + : price.toLocaleString(); return { value: plan, - name: `RAM: ${ram}${' '.repeat( - 5 - ram.toString().length, - )} GB, CPU: ${cpu}${' '.repeat( - 6 - cpu.toString().length, - )}Core, Disk: ${disk}${ - ' '.repeat(5 - disk.toString().length) + 'GB' - }${storageClass || 'SSD'}, Price: ${price.toLocaleString()}${ - price - ? ' '.repeat(7 - Math.floor(price).toString().length) + - 'Tomans/Month' - : '' - }`, + name: `RAM: ${String(ram).padEnd(5)} GB, CPU: ${String(cpu).padEnd(6)}Core, Disk: ${String(disk).padEnd(5)}GB${storageClass || 'SSD'}, Price: ${priceLabel}`, }; }), ], diff --git a/src/commands/mail/create.ts b/src/commands/mail/create.ts index cfa09db8..ba64c2f2 100644 --- a/src/commands/mail/create.ts +++ b/src/commands/mail/create.ts @@ -121,7 +121,10 @@ export default class MailCreate extends Command { message: 'Please select a plan:', choices: [ ...Object.keys(plans.mail) - .filter((plan) => plans.mail[plan].available) + .filter( + (plan) => + MAIL_SERVICE_PLANS.includes(plan) && plans.mail[plan].available, + ) .map((plan) => { const availablePlan = plans.mail[plan]; const maxAccount = availablePlan.maxAccount; diff --git a/src/commands/plan/list.ts b/src/commands/plan/list.ts index 0deb745e..94bf4ead 100644 --- a/src/commands/plan/list.ts +++ b/src/commands/plan/list.ts @@ -22,21 +22,16 @@ export default class PlanList extends Command { const plansData = Object.keys(plans.projects) .filter( (plan) => - (plan === 'free' || plan.includes('g2')) && + plan.includes('g2') && plans.projects[plan].available && plans.projects[plan].region === 'iran', ) .map((plan) => { const Plan = plan; const BundlePlan = plans.projectBundlePlans[plan]; - const BundlePlanDetails = Object.keys(BundlePlan).map((key) => { - const { displayPrice } = BundlePlan[key]; - return { key, displayPrice }; - }); - - const Free = BundlePlanDetails[0].displayPrice.toLocaleString(); - const Standard = BundlePlanDetails[1].displayPrice.toLocaleString(); - const Pro = BundlePlanDetails[2].displayPrice.toLocaleString(); + const Basic = BundlePlan.basic.displayPrice.toLocaleString(); + const Standard = BundlePlan.standard.displayPrice.toLocaleString(); + const Pro = BundlePlan.pro.displayPrice.toLocaleString(); const availablePlan = plans.projects[plan]; const RAM = availablePlan.RAM.amount; @@ -50,7 +45,7 @@ export default class PlanList extends Command { CPU, Disk, Price, - Free, + Basic, Standard, Pro, }; @@ -64,7 +59,7 @@ export default class PlanList extends Command { CPU: { header: 'CPU(Core)' }, Disk: { header: 'Disk(GB SSD)' }, Price: { header: 'Price(Tomans/Month)' }, - Free: { header: 'Bronze' }, + Basic: { header: 'Bronze(Tomans/Month)' }, Standard: { header: 'Silver(Tomans/Month)' }, Pro: { header: 'Gold(Tomans/Month)' }, }, diff --git a/src/commands/vm/create.ts b/src/commands/vm/create.ts index 5b8bed9d..f8e9b926 100644 --- a/src/commands/vm/create.ts +++ b/src/commands/vm/create.ts @@ -167,28 +167,22 @@ export default class VmCreate extends Command { name: 'plan', type: 'list', message: 'Please select a plan:', - choices: Object.keys(plans).map((planKey) => { - const planDetails = plans[planKey]; - const { RAM, CPU, volume, monthlyPrice } = planDetails; - const ram = `RAM: ${RAM.amount}${' '.repeat( - 4 - RAM.amount.toString().length, - )} GB`; - const cpu = `CPU: ${CPU.amount}${' '.repeat( - 4 - CPU.amount.toString().length, - )} Core${CPU.amount > 1 ? 's' : ' '}`; - - const disk = `Disk: ${volume}${' '.repeat( - 4 - volume.toString().length, - )} GB`; - const price = `Price: ${monthlyPrice.toLocaleString()}${' '.repeat( - 10 - monthlyPrice.toLocaleString().length, - )} Tomans/Month`; - - return { - value: planKey, - name: `${ram.padEnd(8)} | ${cpu.padEnd(10)} | ${disk.padEnd(8)} | ${price}`, - }; - }), + choices: Object.keys(plans) + .filter((planKey) => !planKey.startsWith('free')) + .map((planKey) => { + const planDetails = plans[planKey]; + const { RAM, CPU, volume, monthlyPrice } = planDetails; + const ram = `RAM: ${String(RAM.amount).padEnd(4)} GB`; + const cpu = `CPU: ${String(CPU.amount).padEnd(4)} Core${CPU.amount > 1 ? 's' : ' '}`; + + const disk = `Disk: ${String(volume).padEnd(4)} GB`; + const price = `Price: ${monthlyPrice.toLocaleString().padEnd(10)} Tomans/Month`; + + return { + value: planKey, + name: `${ram.padEnd(8)} | ${cpu.padEnd(10)} | ${disk.padEnd(8)} | ${price}`, + }; + }), })) as { plan: string }; return plan; diff --git a/src/constants.ts b/src/constants.ts index e1e2545b..eb81c9be 100644 --- a/src/constants.ts +++ b/src/constants.ts @@ -46,16 +46,7 @@ export const AVAILABLE_PLATFORMS = [ ]; export const OBJ_PERMISSION = ['public', 'private']; -export const MAIL_SERVICE_PLANS = [ - 'm1', - 'm2', - 'm3', - 'm4', - 'm5', - 'm6', - 'm7', - 'm8', -]; +export const MAIL_SERVICE_PLANS = ['m2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8']; export const MAIL_SERVICE_MODES = ['DEV', 'LIVE']; diff --git a/test/app/create.unit.test.ts b/test/app/create.unit.test.ts index 1dbfe3f3..dafde4bc 100644 --- a/test/app/create.unit.test.ts +++ b/test/app/create.unit.test.ts @@ -1,6 +1,7 @@ import { runCommand } from '@oclif/test'; import nock from 'nock'; import sinon from 'sinon'; +import inquirer from 'inquirer'; import { expect } from 'chai'; import { networks } from '../fixtures/networks/fixture.ts'; import { stubGlobalConfig } from '../helpers/stubs.ts'; @@ -314,4 +315,101 @@ describe('app:create', function () { expect(stdout).to.equal('App test-app created.\n'); }); + + it('lists a plan with an 8-digit monthly price', async () => { + api + .get('/v1/me') + .query({ teamID: '' }) + .reply(200, { + plans: { + projects: { + 'small-g2': { + available: true, + region: 'iran', + RAM: { amount: 8 }, + CPU: { amount: 4 }, + volume: 100, + price: 20000, + }, + }, + }, + }); + api + .post('/v1/projects/', { + name: 'test-app', + planID: 'small-g2', + platform: 'laravel', + bundlePlanID: 'standard', + network: networks.networks[0]._id, + readOnlyRootFilesystem: true, + }) + .query({ teamID: '' }) + .reply(200); + const prompt = sinon + .stub(inquirer, 'prompt') + .resolves({ plan: 'small-g2' }); + + const { error } = await runCommand( + createFlags.filter((flag) => flag !== '--plan' && flag !== 'small-g2'), + ); + + expect(error).to.equal(undefined); + expect((prompt.firstCall.args[0] as any).choices[0].name).to.include( + `${(14_400_000).toLocaleString()} Tomans/Month`, + ); + }); + + it('does not offer the free plan or the free feature bundle', async () => { + const plan = { + available: true, + region: 'iran', + RAM: { amount: 1 }, + CPU: { amount: 1 }, + volume: 1, + }; + api + .get('/v1/me') + .twice() + .query({ teamID: '' }) + .reply(200, { + plans: { + projects: { + free: { ...plan, price: 0 }, + 'small-g2': { ...plan, price: 100 }, + }, + projectBundlePlans: { + 'small-g2': { + free: { available: true, displayPrice: 0 }, + standard: { available: true, displayPrice: 100 }, + }, + }, + }, + }); + api + .post('/v1/projects/', { + name: 'test-app', + planID: 'small-g2', + platform: 'laravel', + bundlePlanID: 'standard', + network: networks.networks[0]._id, + readOnlyRootFilesystem: true, + }) + .query({ teamID: '' }) + .reply(200); + const prompt = sinon.stub(inquirer, 'prompt'); + prompt.onFirstCall().resolves({ plan: 'small-g2' }); + prompt.onSecondCall().resolves({ bundlePlan: 'standard' }); + + await runCommand( + createFlags.filter( + (flag) => + !['--plan', 'small-g2', '--feature-plan', 'standard'].includes(flag), + ), + ); + + const offered = (call: sinon.SinonSpyCall) => + (call.args[0] as any).choices.map((choice: any) => choice.value); + expect(offered(prompt.firstCall)).to.deep.equal(['small-g2']); + expect(offered(prompt.secondCall)).to.deep.equal(['standard']); + }); }); diff --git a/test/db/create.unit.test.ts b/test/db/create.unit.test.ts index 646d5e58..3faf0566 100644 --- a/test/db/create.unit.test.ts +++ b/test/db/create.unit.test.ts @@ -1,6 +1,7 @@ import { runCommand } from '@oclif/test'; import nock from 'nock'; import sinon from 'sinon'; +import inquirer from 'inquirer'; import { expect } from 'chai'; import { networks } from '../fixtures/networks/fixture.ts'; import { stubGlobalConfig } from '../helpers/stubs.ts'; @@ -130,4 +131,35 @@ describe('db:create', function () { expect(error?.message).to.include(unableToCreate); }); + + it('lists a plan with an 8-digit monthly price', async () => { + api + .get('/v1/me') + .query({ teamID: '' }) + .reply(200, { + plans: { + databases: { + 'small-g2': { + available: true, + supports: ['postgres'], + RAM: { amount: 8 }, + CPU: { amount: 4 }, + volume: 100, + price: 20000, + }, + }, + }, + }); + expectCreate().reply(200); + const prompt = sinon.stub(inquirer, 'prompt').resolves({ plan: 'small' }); + + const { error } = await runCommand( + flags.filter((flag) => flag !== '--plan' && flag !== 'small'), + ); + + expect(error).to.equal(undefined); + expect((prompt.firstCall.args[0] as any).choices[0].name).to.include( + `${(14_400_000).toLocaleString()} Tomans/Month`, + ); + }); }); diff --git a/test/mail/create.unit.test.ts b/test/mail/create.unit.test.ts new file mode 100644 index 00000000..e6a39214 --- /dev/null +++ b/test/mail/create.unit.test.ts @@ -0,0 +1,49 @@ +import { runCommand } from '@oclif/test'; +import nock from 'nock'; +import sinon from 'sinon'; +import inquirer from 'inquirer'; +import { expect } from 'chai'; +import { stubGlobalConfig } from '../helpers/stubs.ts'; + +describe('mail:create', function () { + const api = nock('https://api.liara.ir'); + const mailApi = nock('https://mail-service.liara.ir'); + + beforeEach(() => { + nock.disableNetConnect(); + stubGlobalConfig(); + }); + + afterEach(() => { + api.done(); + mailApi.done(); + nock.cleanAll(); + nock.enableNetConnect(); + sinon.restore(); + }); + + it('does not offer the free plan', async () => { + api + .get('/v1/me') + .query({ teamID: '' }) + .reply(200, { + plans: { mail: { m1: { available: true }, m2: { available: true } } }, + }); + mailApi + .post('/api/v1/mails', { + domain: 'example.com', + plan: 'm2', + mode: 'DEV', + }) + .query({ teamID: '' }) + .reply(200); + const prompt = sinon.stub(inquirer, 'prompt'); + prompt.onFirstCall().resolves({ plan: 'm2' }); + prompt.onSecondCall().resolves({ mode: 'DEV' }); + + await runCommand(['mail:create', '--domain', 'example.com']); + + const { choices } = prompt.firstCall.args[0] as any; + expect(choices.map((choice: any) => choice.value)).to.deep.equal(['m2']); + }); +}); diff --git a/test/plan/list.unit.test.ts b/test/plan/list.unit.test.ts new file mode 100644 index 00000000..e6a05868 --- /dev/null +++ b/test/plan/list.unit.test.ts @@ -0,0 +1,60 @@ +import { runCommand } from '@oclif/test'; +import nock from 'nock'; +import sinon from 'sinon'; +import { expect } from 'chai'; +import { stubGlobalConfig } from '../helpers/stubs.ts'; + +const plan = { + available: true, + region: 'iran', + RAM: { amount: 1 }, + CPU: { amount: 1 }, + volume: 1, + price: 100, +}; + +// The API lists the deprecated free bundle first in every plan. +const bundles = { + free: { displayPrice: 0 }, + basic: { displayPrice: 175_000 }, + standard: { displayPrice: 275_000 }, + pro: { displayPrice: 475_000 }, +}; + +describe('plan:list', function () { + const api = nock('https://api.liara.ir'); + + beforeEach(() => { + nock.disableNetConnect(); + stubGlobalConfig(); + }); + + afterEach(() => { + api.done(); + nock.cleanAll(); + nock.enableNetConnect(); + sinon.restore(); + }); + + it('hides the free plan and bundle', async () => { + api + .get('/v1/me') + .query({ teamID: '' }) + .reply(200, { + plans: { + projects: { free: plan, 'mini-g2': plan }, + projectBundlePlans: { free: bundles, 'mini-g2': bundles }, + }, + }); + + const { stdout } = await runCommand(['plan:list', '--output', 'json']); + + const rows = JSON.parse(stdout); + expect(rows.map((row: any) => row.Plan)).to.deep.equal(['mini-g2']); + expect(rows[0]).to.include({ + Basic: (175_000).toLocaleString(), + Standard: (275_000).toLocaleString(), + Pro: (475_000).toLocaleString(), + }); + }); +}); diff --git a/test/vm/vm.unit.test.ts b/test/vm/vm.unit.test.ts index 2deace79..4b1fa890 100644 --- a/test/vm/vm.unit.test.ts +++ b/test/vm/vm.unit.test.ts @@ -1,6 +1,7 @@ import { runCommand } from '@oclif/test'; import nock from 'nock'; import sinon from 'sinon'; +import inquirer from 'inquirer'; import { expect } from 'chai'; import { stubGlobalConfig } from '../helpers/stubs.ts'; import VmCreate from '../../src/commands/vm/create.ts'; @@ -280,5 +281,54 @@ describe('vm commands', function () { 'A vm with this name already exists. Please choose a unique name.', ); }); + + it('lists a plan with a 9-digit monthly price', async () => { + (VmCreate.prototype.promptPlan as sinon.SinonStub).restore(); + api + .get('/plans') + .query({ teamID: '' }) + .reply(200, { + plans: { + g1: { + RAM: { amount: 8 }, + CPU: { amount: 4 }, + volume: 100, + monthlyPrice: 123_456_789, + }, + }, + }); + expectCreate().reply(200); + const prompt = sinon.stub(inquirer, 'prompt').resolves({ plan: 'g1' }); + + const { error } = await runCommand(['vm:create', '--detach']); + + expect(error).to.equal(undefined); + expect((prompt.firstCall.args[0] as any).choices[0].name).to.include( + `${(123_456_789).toLocaleString()} Tomans/Month`, + ); + }); + + it('does not offer the free plans', async () => { + (VmCreate.prototype.promptPlan as sinon.SinonStub).restore(); + const plan = { + RAM: { amount: 1 }, + CPU: { amount: 1 }, + volume: 1, + monthlyPrice: 0, + }; + api + .get('/plans') + .query({ teamID: '' }) + .reply(200, { + plans: { free: plan, 'free-windows': plan, g1: plan }, + }); + expectCreate().reply(200); + const prompt = sinon.stub(inquirer, 'prompt').resolves({ plan: 'g1' }); + + await runCommand(['vm:create', '--detach']); + + const { choices } = prompt.firstCall.args[0] as any; + expect(choices.map((choice: any) => choice.value)).to.deep.equal(['g1']); + }); }); });