import { createMemoryHistory, createRouter } from 'vue-router';

import { describe, expect, it, vi } from 'vitest';

import {
  buildForcePasswordRoute,
  isForcePasswordRoute,
  isPasswordResetRequired,
  resolveForcePasswordPath,
} from '#/utils/password-reset';

vi.mock('element-plus', () => ({
  ElMessage: vi.fn(),
}));

vi.mock('#/locales', () => ({
  $t: (key: string) => key,
}));

function createTestRouter() {
  return createRouter({
    history: createMemoryHistory(),
    routes: [
      {
        name: 'FallbackNotFound',
        path: '/:path(.*)*',
        component: { template: '<div />' },
      },
    ],
  });
}

describe('password reset route', () => {
  it('can resolve the forced password path before Profile is registered', () => {
    const router = createTestRouter();
    const route = router.resolve(buildForcePasswordRoute());

    expect(route.fullPath).toBe('/profile?force=1&tab=password');
    expect(isForcePasswordRoute(route)).toBe(true);
    expect(resolveForcePasswordPath(router)).toBe(
      '/profile?force=1&tab=password',
    );
  });

  it('matches Profile after the dynamic route is registered', () => {
    const router = createTestRouter();
    const targetPath = router.resolve(buildForcePasswordRoute()).fullPath;

    router.addRoute({
      name: 'Profile',
      path: '/profile',
      component: { template: '<div />' },
    });

    expect(router.resolve(targetPath).name).toBe('Profile');
  });

  it('uses the account state as the authoritative password reset signal', () => {
    expect(isPasswordResetRequired({ passwordResetRequired: true })).toBe(true);
    expect(isPasswordResetRequired({ passwordResetRequired: false })).toBe(
      false,
    );
    expect(isPasswordResetRequired()).toBe(false);
  });
});
