const { test } = require('node:test'); const assert = require('node:assert'); const { parseArgs, dispatch } = require('../scripts/lightsail-cli'); test('parseArgs parses "list" with no args', () => { const result = parseArgs(['list']); assert.deepStrictEqual(result, { command: 'list', subcommand: null, args: [], options: {} }); }); test('parseArgs parses "status "', () => { const result = parseArgs(['status', 'xwiki-01']); assert.deepStrictEqual(result, { command: 'status', subcommand: null, args: ['xwiki-01'], options: {} }); }); test('parseArgs parses "create --bundle --blueprint --az "', () => { const result = parseArgs(['create', 'xwiki-02', '--bundle', 'small_2_0', '--blueprint', 'ubuntu_22_04', '--az', 'ap-northeast-1a']); assert.deepStrictEqual(result, { command: 'create', subcommand: null, args: ['xwiki-02'], options: { bundle: 'small_2_0', blueprint: 'ubuntu_22_04', az: 'ap-northeast-1a' }, }); }); test('parseArgs parses "snapshot create " as subcommand', () => { const result = parseArgs(['snapshot', 'create', 'xwiki-01', 'xwiki-01-backup']); assert.deepStrictEqual(result, { command: 'snapshot', subcommand: 'create', args: ['xwiki-01', 'xwiki-01-backup'], options: {}, }); }); test('dispatch("list") calls clientModule.listInstances', async () => { const calls = []; const clientModule = { listInstances: async () => { calls.push('listInstances'); return [{ name: 'xwiki-01' }]; } }; const result = await dispatch({ command: 'list', subcommand: null, args: [], options: {} }, clientModule); assert.deepStrictEqual(result, [{ name: 'xwiki-01' }]); assert.deepStrictEqual(calls, ['listInstances']); }); test('dispatch("status", ["xwiki-01"]) calls clientModule.getInstance with name', async () => { const calls = []; const clientModule = { getInstance: async (name) => { calls.push(name); return { name: 'xwiki-01', state: { name: 'running' } }; } }; const result = await dispatch({ command: 'status', subcommand: null, args: ['xwiki-01'], options: {} }, clientModule); assert.deepStrictEqual(result, { name: 'xwiki-01', state: { name: 'running' } }); assert.deepStrictEqual(calls, ['xwiki-01']); }); test('dispatch("create", ...) calls clientModule.createInstance with parsed options', async () => { const calls = []; const clientModule = { createInstance: async (opts) => { calls.push(opts); return [{ status: 'Started' }]; } }; const parsed = { command: 'create', subcommand: null, args: ['xwiki-02'], options: { bundle: 'small_2_0', blueprint: 'ubuntu_22_04', az: 'ap-northeast-1a' }, }; const result = await dispatch(parsed, clientModule); assert.deepStrictEqual(result, [{ status: 'Started' }]); assert.deepStrictEqual(calls, [{ instanceName: 'xwiki-02', bundleId: 'small_2_0', blueprintId: 'ubuntu_22_04', availabilityZone: 'ap-northeast-1a', }]); }); test('dispatch("delete", ["xwiki-02"]) calls clientModule.deleteInstance', async () => { const calls = []; const clientModule = { deleteInstance: async (name) => { calls.push(name); return [{ status: 'Started' }]; } }; const result = await dispatch({ command: 'delete', subcommand: null, args: ['xwiki-02'], options: {} }, clientModule); assert.deepStrictEqual(result, [{ status: 'Started' }]); assert.deepStrictEqual(calls, ['xwiki-02']); }); test('dispatch("start"/"stop"/"reboot") call the matching clientModule function', async () => { const calls = []; const clientModule = { startInstance: async (name) => { calls.push(['start', name]); return [{ status: 'Started' }]; }, stopInstance: async (name) => { calls.push(['stop', name]); return [{ status: 'Started' }]; }, rebootInstance: async (name) => { calls.push(['reboot', name]); return [{ status: 'Started' }]; }, }; await dispatch({ command: 'start', subcommand: null, args: ['xwiki-01'], options: {} }, clientModule); await dispatch({ command: 'stop', subcommand: null, args: ['xwiki-01'], options: {} }, clientModule); await dispatch({ command: 'reboot', subcommand: null, args: ['xwiki-01'], options: {} }, clientModule); assert.deepStrictEqual(calls, [['start', 'xwiki-01'], ['stop', 'xwiki-01'], ['reboot', 'xwiki-01']]); }); test('dispatch("snapshot", "create", [...]) calls clientModule.createSnapshot', async () => { const calls = []; const clientModule = { createSnapshot: async (instanceName, snapshotName) => { calls.push([instanceName, snapshotName]); return [{ status: 'Started' }]; } }; const parsed = { command: 'snapshot', subcommand: 'create', args: ['xwiki-01', 'xwiki-01-backup'], options: {} }; const result = await dispatch(parsed, clientModule); assert.deepStrictEqual(result, [{ status: 'Started' }]); assert.deepStrictEqual(calls, [['xwiki-01', 'xwiki-01-backup']]); }); test('dispatch("snapshot", "list", []) calls clientModule.listSnapshots', async () => { const clientModule = { listSnapshots: async () => [{ name: 'xwiki-01-backup' }] }; const parsed = { command: 'snapshot', subcommand: 'list', args: [], options: {} }; const result = await dispatch(parsed, clientModule); assert.deepStrictEqual(result, [{ name: 'xwiki-01-backup' }]); }); test('dispatch("snapshot", "delete", [name]) calls clientModule.deleteSnapshot', async () => { const calls = []; const clientModule = { deleteSnapshot: async (name) => { calls.push(name); return [{ status: 'Started' }]; } }; const parsed = { command: 'snapshot', subcommand: 'delete', args: ['xwiki-01-backup'], options: {} }; const result = await dispatch(parsed, clientModule); assert.deepStrictEqual(result, [{ status: 'Started' }]); assert.deepStrictEqual(calls, ['xwiki-01-backup']); }); test('dispatch throws on unknown command', async () => { await assert.rejects( () => dispatch({ command: 'bogus', subcommand: null, args: [], options: {} }, {}), /Unknown command: bogus/ ); });