11cb0ef41Sopenharmony_ciconst t = require('tap')
21cb0ef41Sopenharmony_ciconst tmock = require('../../fixtures/tmock')
31cb0ef41Sopenharmony_ci
41cb0ef41Sopenharmony_cilet readOpts = null
51cb0ef41Sopenharmony_cilet readResult = null
61cb0ef41Sopenharmony_ciconst read = async (opts) => {
71cb0ef41Sopenharmony_ci  readOpts = opts
81cb0ef41Sopenharmony_ci  return readResult
91cb0ef41Sopenharmony_ci}
101cb0ef41Sopenharmony_ci
111cb0ef41Sopenharmony_ciconst npmUserValidate = {
121cb0ef41Sopenharmony_ci  username: (username) => {
131cb0ef41Sopenharmony_ci    if (username === 'invalid') {
141cb0ef41Sopenharmony_ci      return new Error('invalid username')
151cb0ef41Sopenharmony_ci    }
161cb0ef41Sopenharmony_ci
171cb0ef41Sopenharmony_ci    return null
181cb0ef41Sopenharmony_ci  },
191cb0ef41Sopenharmony_ci  email: (email) => {
201cb0ef41Sopenharmony_ci    if (email.startsWith('invalid')) {
211cb0ef41Sopenharmony_ci      return new Error('invalid email')
221cb0ef41Sopenharmony_ci    }
231cb0ef41Sopenharmony_ci
241cb0ef41Sopenharmony_ci    return null
251cb0ef41Sopenharmony_ci  },
261cb0ef41Sopenharmony_ci}
271cb0ef41Sopenharmony_ci
281cb0ef41Sopenharmony_cilet logMsg = null
291cb0ef41Sopenharmony_ciconst readUserInfo = tmock(t, '{LIB}/utils/read-user-info.js', {
301cb0ef41Sopenharmony_ci  read,
311cb0ef41Sopenharmony_ci  npmlog: {
321cb0ef41Sopenharmony_ci    clearProgress: () => {},
331cb0ef41Sopenharmony_ci    showProgress: () => {},
341cb0ef41Sopenharmony_ci  },
351cb0ef41Sopenharmony_ci  'proc-log': {
361cb0ef41Sopenharmony_ci    warn: (msg) => logMsg = msg,
371cb0ef41Sopenharmony_ci  },
381cb0ef41Sopenharmony_ci  'npm-user-validate': npmUserValidate,
391cb0ef41Sopenharmony_ci})
401cb0ef41Sopenharmony_ci
411cb0ef41Sopenharmony_cit.beforeEach(() => {
421cb0ef41Sopenharmony_ci  logMsg = null
431cb0ef41Sopenharmony_ci})
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_cit.test('otp', async (t) => {
461cb0ef41Sopenharmony_ci  readResult = '1234'
471cb0ef41Sopenharmony_ci  t.teardown(() => {
481cb0ef41Sopenharmony_ci    readResult = null
491cb0ef41Sopenharmony_ci    readOpts = null
501cb0ef41Sopenharmony_ci  })
511cb0ef41Sopenharmony_ci  const result = await readUserInfo.otp()
521cb0ef41Sopenharmony_ci  t.equal(result, '1234', 'received the otp')
531cb0ef41Sopenharmony_ci})
541cb0ef41Sopenharmony_ci
551cb0ef41Sopenharmony_cit.test('password', async (t) => {
561cb0ef41Sopenharmony_ci  readResult = 'password'
571cb0ef41Sopenharmony_ci  t.teardown(() => {
581cb0ef41Sopenharmony_ci    readResult = null
591cb0ef41Sopenharmony_ci    readOpts = null
601cb0ef41Sopenharmony_ci  })
611cb0ef41Sopenharmony_ci  const result = await readUserInfo.password()
621cb0ef41Sopenharmony_ci  t.equal(result, 'password', 'received the password')
631cb0ef41Sopenharmony_ci  t.match(readOpts, {
641cb0ef41Sopenharmony_ci    silent: true,
651cb0ef41Sopenharmony_ci  }, 'got the correct options')
661cb0ef41Sopenharmony_ci})
671cb0ef41Sopenharmony_ci
681cb0ef41Sopenharmony_cit.test('username', async (t) => {
691cb0ef41Sopenharmony_ci  readResult = 'username'
701cb0ef41Sopenharmony_ci  t.teardown(() => {
711cb0ef41Sopenharmony_ci    readResult = null
721cb0ef41Sopenharmony_ci    readOpts = null
731cb0ef41Sopenharmony_ci  })
741cb0ef41Sopenharmony_ci  const result = await readUserInfo.username()
751cb0ef41Sopenharmony_ci  t.equal(result, 'username', 'received the username')
761cb0ef41Sopenharmony_ci})
771cb0ef41Sopenharmony_ci
781cb0ef41Sopenharmony_cit.test('username - invalid warns and retries', async (t) => {
791cb0ef41Sopenharmony_ci  readResult = 'invalid'
801cb0ef41Sopenharmony_ci  t.teardown(() => {
811cb0ef41Sopenharmony_ci    readResult = null
821cb0ef41Sopenharmony_ci    readOpts = null
831cb0ef41Sopenharmony_ci  })
841cb0ef41Sopenharmony_ci
851cb0ef41Sopenharmony_ci  const pResult = readUserInfo.username(null, null)
861cb0ef41Sopenharmony_ci  // have to swap it to a valid username after execution starts
871cb0ef41Sopenharmony_ci  // or it will loop forever
881cb0ef41Sopenharmony_ci  readResult = 'valid'
891cb0ef41Sopenharmony_ci  const result = await pResult
901cb0ef41Sopenharmony_ci  t.equal(result, 'valid', 'received the username')
911cb0ef41Sopenharmony_ci  t.equal(logMsg, 'invalid username')
921cb0ef41Sopenharmony_ci})
931cb0ef41Sopenharmony_ci
941cb0ef41Sopenharmony_cit.test('email', async (t) => {
951cb0ef41Sopenharmony_ci  readResult = 'foo@bar.baz'
961cb0ef41Sopenharmony_ci  t.teardown(() => {
971cb0ef41Sopenharmony_ci    readResult = null
981cb0ef41Sopenharmony_ci    readOpts = null
991cb0ef41Sopenharmony_ci  })
1001cb0ef41Sopenharmony_ci  const result = await readUserInfo.email()
1011cb0ef41Sopenharmony_ci  t.equal(result, 'foo@bar.baz', 'received the email')
1021cb0ef41Sopenharmony_ci})
1031cb0ef41Sopenharmony_ci
1041cb0ef41Sopenharmony_cit.test('email - invalid warns and retries', async (t) => {
1051cb0ef41Sopenharmony_ci  readResult = 'invalid@bar.baz'
1061cb0ef41Sopenharmony_ci  t.teardown(() => {
1071cb0ef41Sopenharmony_ci    readResult = null
1081cb0ef41Sopenharmony_ci    readOpts = null
1091cb0ef41Sopenharmony_ci  })
1101cb0ef41Sopenharmony_ci
1111cb0ef41Sopenharmony_ci  const pResult = readUserInfo.email(null, null)
1121cb0ef41Sopenharmony_ci  readResult = 'foo@bar.baz'
1131cb0ef41Sopenharmony_ci  const result = await pResult
1141cb0ef41Sopenharmony_ci  t.equal(result, 'foo@bar.baz', 'received the email')
1151cb0ef41Sopenharmony_ci  t.equal(logMsg, 'invalid email')
1161cb0ef41Sopenharmony_ci})
117