/*
Copyright (C) 2023-2026 QuantumNous
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see .
For commercial licensing, please contact support@quantumnous.com
*/
import {
createMemoryHistory,
createRootRoute,
createRoute,
createRouter,
Outlet,
RouterProvider,
} from '@tanstack/react-router'
import { act, cleanup, render, screen, waitFor } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { afterEach, beforeEach, expect, it, vi } from 'vitest'
import { api } from '@/lib/api'
import { useAuthStore, type AuthBundle } from '@/stores/auth-store'
import { useAuthRedirect } from '../../hooks/use-auth-redirect'
import type { LoginResult } from '../../secure-verification/types'
import { OtpForm } from '../components/otp-form'
const bundle: AuthBundle = {
access_token: 'completed-login',
token_type: 'Bearer',
access_expires_at: 9999999999,
user: { id: 42, username: 'verified-user', role: 1 },
session: {
sid: 'verified-session',
current: true,
login_method: 'password',
ip: '',
user_agent: '',
created_at: 1,
last_active_at: 1,
expires_at: 9999999999,
},
}
function PrimaryLoginHarness(props: { result: LoginResult }) {
const { handleLoginResult } = useAuthRedirect()
return (
)
}
function renderLoginVerification(
hasChallenge = true,
primaryResult?: LoginResult
) {
useAuthStore.getState().auth.reset('complete')
if (hasChallenge) {
useAuthStore.getState().auth.setPendingLoginVerification({
challenge: {
require_verification: true,
flow_token: 'login-flow',
expires_at: Math.floor(Date.now() / 1000) + 300,
methods: [{ method: '2fa', available: true }],
},
redirectTo: '/pricing?view=grid',
})
}
const root = createRootRoute({ component: Outlet })
const routes = [
createRoute({
getParentRoute: () => root,
path: '/otp',
component: OtpForm,
}),
createRoute({
getParentRoute: () => root,
path: '/sign-in',
component: () =>