Skip to content

Enterprise agent platform for building, deploying, and operating AI teammates.

14.3 KB
Raw Blame
import React from "react"
import { cleanup, fireEvent, render, screen, waitFor } from "@testing-library/react"
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"

const apiRequestMock = vi.hoisted(() => vi.fn())
const toastErrorMock = vi.hoisted(() => vi.fn())

vi.mock("@/lib/api-wrapper", () => ({
  apiRequest: apiRequestMock,
  uploadWithCollectionMigrationRetry: apiRequestMock,
  parseApiResponse: async (response: { json: () => Promise<unknown> }) => ({
    data: await response.json(),
    text: null,
    isHtml: false,
  }),
  // Mirrors api-wrapper.ts: detail wins over message. Getting this backwards
  // silently drops the backend sentence and makes assertions about it vacuous.
  getUploadErrorMessage: (
    _response: unknown,
    parsed: { data?: { detail?: string; message?: string } | null },
    messages: { generic: string }
  ) => parsed?.data?.detail || parsed?.data?.message || messages.generic,
  isJsonRecord: (value: unknown) => typeof value === "object" && value !== null && !Array.isArray(value),
  UPLOAD_ERROR_MESSAGES: {},
}))

vi.mock("@/lib/utils", () => ({
  getApiUrl: () => "http://api.local",
}))

vi.mock("@/contexts/i18n-context", () => ({
  useI18n: () => ({
    t: (key: string) => key,
  }),
}))

vi.mock("sonner", () => ({
  toast: {
    error: toastErrorMock,
    success: vi.fn(),
    warning: vi.fn(),
  },
}))

vi.mock("lucide-react", () => {
  const Icon = (props: React.SVGProps<SVGSVGElement>) => <svg {...props} />
  return {
    ArrowLeft: Icon,
    HardDrive: Icon,
    Search: Icon,
    Upload: Icon,
    Plus: Icon,
    Trash2: Icon,
    FileIcon: Icon,
    CheckCircle: Icon,
    XCircle: Icon,
    AlertCircle: Icon,
    Globe: Icon,
    Loader2: Icon,
  }
})

vi.mock("@radix-ui/react-tabs", () => ({
  Trigger: ({ children, ...props }: React.ButtonHTMLAttributes<HTMLButtonElement>) => <button {...props}>{children}</button>,
}))

vi.mock("@/components/ui/button", () => ({
  Button: ({ children, ...props }: React.ButtonHTMLAttributes<HTMLButtonElement>) => <button {...props}>{children}</button>,
}))

vi.mock("@/components/ui/input", () => ({
  Input: (props: React.InputHTMLAttributes<HTMLInputElement>) => <input {...props} />,
}))

vi.mock("@/components/ui/label", () => ({
  Label: ({ children, ...props }: React.LabelHTMLAttributes<HTMLLabelElement>) => <label {...props}>{children}</label>,
}))

vi.mock("@/components/ui/card", () => ({
  Card: ({ children, ...props }: React.HTMLAttributes<HTMLDivElement> & { children: React.ReactNode }) => <div {...props}>{children}</div>,
}))

vi.mock("@/components/ui/dialog", () => ({
  Dialog: ({
    children,
    open = true,
  }: {
    children: React.ReactNode
    open?: boolean
  }) => (open ? <div>{children}</div> : null),
  DialogContent: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
  DialogDescription: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
  DialogHeader: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
  DialogTitle: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
}))

vi.mock("@/components/ui/tabs", () => ({
  Tabs: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
  TabsContent: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
  TabsList: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
}))

vi.mock("@/components/ui/scroll-area", () => ({
  ScrollArea: ({ children }: { children: React.ReactNode }) => <div>{children}</div>,
}))

vi.mock("@/components/ui/select", () => ({
  Select: () => <div />,
}))

vi.mock("@/components/ui/badge", () => ({
  Badge: ({ children }: { children: React.ReactNode }) => <span>{children}</span>,
}))

vi.mock("@/components/ui/confirm-dialog", () => ({
  ConfirmDialog: () => null,
}))

vi.mock("./knowledge-base-document-list", () => ({
  KnowledgeBaseDocumentList: () => <div data-testid="kb-document-list" />,
}))

import { KnowledgeBaseDetailContent } from "./knowledge-base-detail"

function createJsonResponse(body: unknown, ok = true, status?: number) {
  return {
    ok,
    status: status ?? (ok ? 200 : 500),
    json: vi.fn().mockResolvedValue(body),
  }
}

function createSucceededJob(result: Record<string, unknown>) {
  return {
    id: "job-1",
    user_id: 1,
    job_type: "kb.ingest.web",
    queue: "kb",
    status: "succeeded",
    progress: { message: "Completed", completed: 1, total: 1 },
    result,
    error_message: null,
    celery_task_id: "task-1",
    attempts: 1,
    max_attempts: 3,
  }
}

describe("KnowledgeBaseDetailContent web ingest", () => {
  beforeEach(() => {
    apiRequestMock.mockReset()
    toastErrorMock.mockReset()

    apiRequestMock.mockImplementation((url: string) => {
      if (url === "http://api.local/api/kb/collections") {
        return Promise.resolve(
          createJsonResponse({
            collections: [
              {
                name: "demo",
                documents: 0,
                chunks: 0,
                embeddings: 0,
                parses: 0,
                document_names: [],
              },
            ],
          })
        )
      }
      if (url === "http://api.local/api/models/?category=embedding") {
        return Promise.resolve(createJsonResponse([]))
      }
      if (url === "http://api.local/api/models/user-default") {
        return Promise.resolve(createJsonResponse({}))
      }
      if (url === "http://api.local/api/jobs/capabilities") {
        return Promise.resolve(createJsonResponse({ kb_ingest_mode: "celery" }))
      }
      if (url === "http://api.local/api/kb/ingest-web/jobs") {
        return Promise.resolve(
          createJsonResponse(
            createSucceededJob({
              status: "partial",
              collection: "demo",
              total_urls_found: 1,
              pages_crawled: 1,
              pages_failed: 1,
              documents_created: 0,
              chunks_created: 0,
              embeddings_created: 0,
              crawled_urls: [],
              failed_urls: {
                "https://example.com/docs": "embedding missing",
              },
              message: "Web import partially failed",
              warnings: [],
              elapsed_time_ms: 0,
            })
          )
        )
      }

      throw new Error(`Unhandled apiRequest: ${url}`)
    })
  })

  afterEach(() => {
    cleanup()
  })

  it("keeps the add-source dialog open for partial web failures and surfaces the error", async () => {
    render(<KnowledgeBaseDetailContent collectionName="demo" />)

    await waitFor(() => {
      expect(screen.getByText("kb.detail.files.addSource")).toBeInTheDocument()
    })

    fireEvent.click(screen.getByText("kb.detail.files.addSource"))
    fireEvent.click(screen.getByText("kb.dialog.tabs.web"))
    fireEvent.change(screen.getByLabelText("kb.dialog.webImport.basic.startUrl *"), {
      target: { value: "https://example.com/docs" },
    })
    fireEvent.click(screen.getByText("kb.index.startImport"))

    await waitFor(() => {
      expect(toastErrorMock).toHaveBeenCalledWith(
        "kb.detail.errors.webImportFailed",
        expect.objectContaining({
          description: "Web import partially failed",
        })
      )
    })

    expect(await screen.findByText("kb.dialog.webImport.status.failed")).toBeInTheDocument()
    expect(await screen.findByText("Web import partially failed")).toBeInTheDocument()
    expect(screen.getByDisplayValue("https://example.com/docs")).toBeInTheDocument()

    const collectionCalls = apiRequestMock.mock.calls.filter(([url]) => url === "http://api.local/api/kb/collections")
    expect(collectionCalls).toHaveLength(1)
  })

  it("relays the backend verdict for a 409 instead of rename advice", async () => {
    // A collection can drop out of the caller's own listing while still existing
    // globally, and then this endpoint answers 409 even though the user never
    // typed a name here.
    const previous = apiRequestMock.getMockImplementation()!
    apiRequestMock.mockImplementation((url: string, options?: RequestInit) => {
      if (url === "http://api.local/api/kb/ingest-web/jobs") {
        return Promise.resolve(
          createJsonResponse(
            {
              detail:
                "Knowledge base name unavailable: demo.",
            },
            false,
            409
          )
        )
      }
      return previous(url, options)
    })

    render(<KnowledgeBaseDetailContent collectionName="demo" />)

    await waitFor(() => {
      expect(screen.getByText("kb.detail.files.addSource")).toBeInTheDocument()
    })

    fireEvent.click(screen.getByText("kb.detail.files.addSource"))
    fireEvent.click(screen.getByText("kb.dialog.tabs.web"))
    fireEvent.change(screen.getByLabelText("kb.dialog.webImport.basic.startUrl *"), {
      target: { value: "https://example.com/docs" },
    })
    fireEvent.click(screen.getByText("kb.index.startImport"))

    // This page has no name field. The backend states the fact without advising
    // a rename, and that sentence is what the user sees.
    await waitFor(() => {
      expect(toastErrorMock).toHaveBeenCalledWith(
        "kb.detail.errors.webImportFailed",
        expect.objectContaining({
          description:
            "Knowledge base name unavailable: demo.",
        })
      )
    })

    expect(toastErrorMock).not.toHaveBeenCalledWith(
      "kb.errors.nameUnavailable",
      expect.anything()
    )
  })

  it("clears the upgrading notice once a retried web import is accepted", async () => {
    const previous = apiRequestMock.getMockImplementation()!
    apiRequestMock.mockImplementation(
      (url: string, options?: RequestInit, onUpgrading?: () => void) => {
        if (url === "http://api.local/api/kb/ingest-web/jobs") {
          onUpgrading?.()
          return Promise.resolve(
            createJsonResponse({
              id: "job-2",
              user_id: 1,
              job_type: "kb.ingest.web",
              queue: "kb",
              status: "running",
              progress: { message: "Crawling", completed: 0, total: 1 },
              result: null,
              error_message: null,
              celery_task_id: "task-2",
              attempts: 1,
              max_attempts: 3,
            })
          )
        }
        if (url === "http://api.local/api/jobs/job-2") {
          return new Promise(() => {})
        }
        return previous(url, options)
      }
    )

    render(<KnowledgeBaseDetailContent collectionName="demo" />)

    await waitFor(() => {
      expect(screen.getByText("kb.detail.files.addSource")).toBeInTheDocument()
    })

    fireEvent.click(screen.getByText("kb.detail.files.addSource"))
    fireEvent.click(screen.getByText("kb.dialog.tabs.web"))
    fireEvent.change(screen.getByLabelText("kb.dialog.webImport.basic.startUrl *"), {
      target: { value: "https://example.com/docs" },
    })
    fireEvent.click(screen.getByText("kb.index.startImport"))

    await waitFor(() => {
      expect(
        apiRequestMock.mock.calls.some(
          ([url]) => url === "http://api.local/api/jobs/job-2"
        )
      ).toBe(true)
    }, { timeout: 5000 })
    expect(screen.queryByText(/kb\.migration\.upgrading/)).toBeNull()
  })

  it("aborts the pending upload retry when the page unmounts", async () => {
    const previous = apiRequestMock.getMockImplementation()!
    let signal: AbortSignal | undefined
    apiRequestMock.mockImplementation(
      (
        url: string,
        options?: RequestInit,
        _onUpgrading?: () => void,
        captured?: AbortSignal
      ) => {
        if (url === "http://api.local/api/kb/ingest-web/jobs") {
          signal = captured
          return new Promise(() => {})
        }
        return previous(url, options)
      }
    )

    const { unmount } = render(<KnowledgeBaseDetailContent collectionName="demo" />)

    await waitFor(() => {
      expect(screen.getByText("kb.detail.files.addSource")).toBeInTheDocument()
    })

    fireEvent.click(screen.getByText("kb.detail.files.addSource"))
    fireEvent.click(screen.getByText("kb.dialog.tabs.web"))
    fireEvent.change(screen.getByLabelText("kb.dialog.webImport.basic.startUrl *"), {
      target: { value: "https://example.com/docs" },
    })
    fireEvent.click(screen.getByText("kb.index.startImport"))

    await waitFor(() => expect(signal).toBeDefined())
    unmount()
    expect(signal?.aborted).toBe(true)
  })
})

describe("KnowledgeBaseDetailContent config save", () => {
  beforeEach(() => {
    apiRequestMock.mockReset()
    toastErrorMock.mockReset()

    apiRequestMock.mockImplementation((url: string) => {
      if (url === "http://api.local/api/kb/collections") {
        return Promise.resolve(
          createJsonResponse({
            collections: [
              {
                name: "demo",
                documents: 0,
                chunks: 0,
                embeddings: 0,
                parses: 0,
                document_names: [],
              },
            ],
          })
        )
      }
      if (url === "http://api.local/api/kb/collections/demo/config") {
        return Promise.resolve(
          createJsonResponse(
            { detail: "Access denied for collection: demo" },
            false,
            403
          )
        )
      }
      if (url.startsWith("http://api.local/api/models/")) {
        return Promise.resolve(createJsonResponse([]))
      }
      if (url === "http://api.local/api/jobs/capabilities") {
        return Promise.resolve(createJsonResponse({ kb_ingest_mode: "celery" }))
      }

      throw new Error(`Unhandled apiRequest: ${url}`)
    })
  })

  afterEach(() => {
    cleanup()
  })

  it("relays the denial verdict verbatim", async () => {
    render(<KnowledgeBaseDetailContent collectionName="demo" />)

    await waitFor(() => {
      expect(screen.getByText("kb.index.saveConfig")).toBeInTheDocument()
    })

    fireEvent.click(screen.getByText("kb.index.saveConfig"))

    // The settings panel has no name field, so this endpoint answers a foreign
    // name with 403 rather than the ingest paths' "name taken" conflict.
    await waitFor(() => {
      expect(toastErrorMock).toHaveBeenCalledWith(
        "kb.detail.errors.saveConfigFailed",
        expect.objectContaining({
          description: "Access denied for collection: demo",
        })
      )
    })

    expect(toastErrorMock).not.toHaveBeenCalledWith(
      "kb.errors.nameUnavailable",
      expect.anything()
    )
  })
})