'use strict';

const test = require('node:test');
const assert = require('node:assert');
const { AuthStateMachine } = require('../src/bot/auth');

test('AuthStateMachine: register success via translate', () => {
  const sm = new AuthStateMachine({});
  sm.markRegisterSent();
  sm.markAwaitRegister();
  const r = sm.consume({ text: '', translate: 'commands.register.success' });
  assert.strictEqual(r, 'auth_done');
  assert.strictEqual(sm.isDone(), true);
});

test('AuthStateMachine: register success via phrase', () => {
  const sm = new AuthStateMachine({ register_success: ['successfully registered'] });
  sm.markRegisterSent();
  sm.markAwaitRegister();
  const r = sm.consume({ text: 'You have been successfully registered' });
  assert.strictEqual(r, 'auth_done');
});

test('AuthStateMachine: already registered → login', () => {
  const sm = new AuthStateMachine({});
  const r = sm.consume({ text: 'You are already registered' });
  assert.strictEqual(r, 'login_sent');
});

test('AuthStateMachine: login success', () => {
  const sm = new AuthStateMachine({});
  sm.markLoginSent();
  sm.markAwaitLogin();
  const r = sm.consume({ text: 'You have been logged in' });
  assert.strictEqual(r, 'auth_done');
});

test('AuthStateMachine: out-of-order consume is no-op', () => {
  const sm = new AuthStateMachine({});
  sm.markRegisterSent();
  sm.markAwaitRegister();
  sm.consume({ text: 'You have been logged in' });
  assert.strictEqual(sm.state, 'AWAIT_REGISTER');
});

test('AuthStateMachine: done consumes nothing', () => {
  const sm = new AuthStateMachine({});
  sm.markRegisterSent();
  sm.markAwaitRegister();
  sm.consume({ translate: 'commands.register.success' });
  const r = sm.consume({ translate: 'commands.register.success' });
  assert.strictEqual(r, null);
});
