Skip to content

An AI-powered search engine with a generative UI

8.5 KB
Raw Blame
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'

import { trackAccountDeleted } from '@/lib/analytics'
import { getCurrentUser } from '@/lib/auth/get-current-user'
import { db } from '@/lib/db'
import * as dbActions from '@/lib/db/actions'
import { deleteUserObjects } from '@/lib/storage/r2-client'
import { createAdminClient } from '@/lib/supabase/admin'

import { deleteAccount } from '../account'

vi.mock('@/lib/analytics')
vi.mock('@/lib/auth/get-current-user')
vi.mock('@/lib/db/actions')
vi.mock('@/lib/storage/r2-client')
vi.mock('@/lib/supabase/admin')
vi.mock('@/lib/db', () => {
  // deleteAccount wraps the flow in a transaction; the supabase provider
  // has no in-database guard, so the transaction body only runs mocked
  // cleanup and the mocked admin API call.
  const db: Record<string, unknown> = {}
  db.transaction = vi.fn((cb: (tx: unknown) => unknown) => cb(db))
  return { db }
})

const originalEnableAuth = process.env.ENABLE_AUTH

describe('Account Actions', () => {
  const user = { id: '550e8400-e29b-41d4-a716-446655440000' }
  const deleteUser = vi.fn()

  beforeEach(() => {
    vi.clearAllMocks()
    process.env.ENABLE_AUTH = 'true'

    vi.mocked(getCurrentUser).mockResolvedValue(user as any)
    vi.mocked(dbActions.deleteUserChats).mockResolvedValue({ success: true })
    vi.mocked(dbActions.deleteUserNotes).mockResolvedValue({ success: true })
    vi.mocked(dbActions.deleteUserLibraryFiles).mockResolvedValue({
      success: true
    })
    vi.mocked(dbActions.anonymizeUserFeedback).mockResolvedValue({
      success: true
    })
    vi.mocked(deleteUserObjects).mockResolvedValue({
      deletedCount: 0,
      skipped: true
    })
    vi.mocked(trackAccountDeleted).mockResolvedValue()
    deleteUser.mockResolvedValue({ data: { user: null }, error: null })
    vi.mocked(createAdminClient).mockReturnValue({
      auth: { admin: { deleteUser } }
    } as any)
  })

  afterEach(() => {
    process.env.ENABLE_AUTH = originalEnableAuth
  })

  it('returns an error in anonymous mode', async () => {
    process.env.ENABLE_AUTH = 'false'

    const result = await deleteAccount()

    expect(result).toEqual({
      success: false,
      error: 'Account deletion is unavailable in anonymous mode.'
    })
    expect(getCurrentUser).not.toHaveBeenCalled()
    expect(dbActions.deleteUserChats).not.toHaveBeenCalled()
  })

  it('returns an error when the user is not authenticated', async () => {
    vi.mocked(getCurrentUser).mockResolvedValue(null)

    const result = await deleteAccount()

    expect(result).toEqual({
      success: false,
      error: 'User not authenticated'
    })
    expect(createAdminClient).not.toHaveBeenCalled()
    expect(dbActions.deleteUserChats).not.toHaveBeenCalled()
  })

  it('returns an error when Supabase admin is not configured', async () => {
    vi.mocked(createAdminClient).mockImplementation(() => {
      throw new Error('Missing secret key')
    })

    const result = await deleteAccount()

    expect(result).toEqual({
      success: false,
      error: 'Account deletion is not configured. Set SUPABASE_SECRET_KEY.'
    })
    expect(dbActions.deleteUserChats).not.toHaveBeenCalled()
  })

  it('deletes app data, anonymizes feedback, uploaded files, and auth user', async () => {
    const result = await deleteAccount()

    expect(result).toEqual({ success: true })
    expect(dbActions.deleteUserChats).toHaveBeenCalledWith(user.id)
    expect(dbActions.deleteUserNotes).toHaveBeenCalledWith(user.id)
    expect(dbActions.deleteUserLibraryFiles).toHaveBeenCalledWith(user.id)
    expect(dbActions.anonymizeUserFeedback).toHaveBeenCalledWith(user.id)
    expect(deleteUserObjects).toHaveBeenCalledWith(user.id)
    expect(deleteUser).toHaveBeenCalledWith(user.id)
    expect(trackAccountDeleted).toHaveBeenCalledTimes(1)
    // supabase has no in-database guard, so no transaction is opened:
    // an outer transaction would hold a pooled connection while the
    // cleanup steps check out their own.
    expect(db.transaction).not.toHaveBeenCalled()
  })

  it('deletes the auth user only after all data cleanup succeeds', async () => {
    const calls: string[] = []
    deleteUser.mockImplementation(async () => {
      calls.push('auth')
      return { data: { user: null }, error: null }
    })
    vi.mocked(dbActions.deleteUserChats).mockImplementation(async () => {
      calls.push('chats')
      return { success: true }
    })
    vi.mocked(dbActions.deleteUserNotes).mockImplementation(async () => {
      calls.push('notes')
      return { success: true }
    })
    vi.mocked(dbActions.deleteUserLibraryFiles).mockImplementation(async () => {
      calls.push('files')
      return { success: true }
    })
    vi.mocked(dbActions.anonymizeUserFeedback).mockImplementation(async () => {
      calls.push('feedback')
      return { success: true }
    })
    vi.mocked(deleteUserObjects).mockImplementation(async () => {
      calls.push('objects')
      return { deletedCount: 0, skipped: true }
    })

    await deleteAccount()

    // The identity is removed last so that any cleanup failure leaves
    // the account in place and the user can retry the deletion.
    expect(calls).toEqual([
      'chats',
      'notes',
      'files',
      'feedback',
      'objects',
      'auth'
    ])
  })

  it('keeps the identity and stops before remaining cleanup when app data deletion fails', async () => {
    vi.mocked(dbActions.deleteUserChats).mockResolvedValue({
      success: false,
      error: 'Failed to delete user chats'
    })

    const result = await deleteAccount()

    expect(result).toEqual({
      success: false,
      error: 'Failed to delete user chats'
    })
    expect(deleteUser).not.toHaveBeenCalled()
    expect(dbActions.deleteUserNotes).not.toHaveBeenCalled()
    expect(deleteUserObjects).not.toHaveBeenCalled()
    expect(trackAccountDeleted).not.toHaveBeenCalled()
  })

  it('keeps the identity and stops before remaining cleanup when notes deletion fails', async () => {
    vi.mocked(dbActions.deleteUserNotes).mockResolvedValue({
      success: false,
      error: 'Failed to delete user notes'
    })

    const result = await deleteAccount()

    expect(result).toEqual({
      success: false,
      error: 'Failed to delete user notes'
    })
    expect(deleteUser).not.toHaveBeenCalled()
    expect(dbActions.deleteUserLibraryFiles).not.toHaveBeenCalled()
    expect(deleteUserObjects).not.toHaveBeenCalled()
    expect(trackAccountDeleted).not.toHaveBeenCalled()
  })

  it('keeps the identity and stops before remaining cleanup when feedback anonymization fails', async () => {
    vi.mocked(dbActions.anonymizeUserFeedback).mockResolvedValue({
      success: false,
      error: 'Failed to anonymize user feedback'
    })

    const result = await deleteAccount()

    expect(result).toEqual({
      success: false,
      error: 'Failed to anonymize user feedback'
    })
    expect(deleteUser).not.toHaveBeenCalled()
    expect(deleteUserObjects).not.toHaveBeenCalled()
    expect(trackAccountDeleted).not.toHaveBeenCalled()
  })

  it('keeps the identity and stops before remaining cleanup when library file deletion fails', async () => {
    vi.mocked(dbActions.deleteUserLibraryFiles).mockResolvedValue({
      success: false,
      error: 'Failed to delete user files'
    })

    const result = await deleteAccount()

    expect(result).toEqual({
      success: false,
      error: 'Failed to delete user files'
    })
    expect(deleteUser).not.toHaveBeenCalled()
    expect(deleteUserObjects).not.toHaveBeenCalled()
    expect(trackAccountDeleted).not.toHaveBeenCalled()
  })

  it('keeps the identity when uploaded file deletion fails', async () => {
    vi.mocked(deleteUserObjects).mockRejectedValue(new Error('Storage error'))

    const result = await deleteAccount()

    expect(result).toEqual({
      success: false,
      error: 'Storage error'
    })
    expect(dbActions.deleteUserChats).toHaveBeenCalledWith(user.id)
    expect(dbActions.deleteUserNotes).toHaveBeenCalledWith(user.id)
    expect(dbActions.deleteUserLibraryFiles).toHaveBeenCalledWith(user.id)
    expect(deleteUser).not.toHaveBeenCalled()
    expect(trackAccountDeleted).not.toHaveBeenCalled()
  })

  it('does not track account deletion when auth deletion fails', async () => {
    deleteUser.mockResolvedValue({
      data: { user: null },
      error: new Error('Auth deletion failed')
    })

    const result = await deleteAccount()

    expect(result).toEqual({
      success: false,
      error: 'Auth deletion failed'
    })
    expect(trackAccountDeleted).not.toHaveBeenCalled()
  })
})