Skip to content

OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.

Python
3.5 KB
Raw Blame
import path from "path";
import { ACTIVE_PROVIDER_CONFIG } from "../config/provider";
import { expect, test } from "../utils/fixtures";
import logger from "../utils/logger";
import { navigateToHome } from "../utils/navigation";

const TEST_DOCUMENT = "OpenRAG.Index.Test.Document.txt";
const TEST_DOCUMENT_PATH = path.join(__dirname, "../test-data", TEST_DOCUMENT);
const UNIQUE_SEARCH_TOKEN = "OPENSEARCH-7419-ZX";

test(`Opensearch Indexing - ${ACTIVE_PROVIDER_CONFIG.provider} @33219220`, async ({
  page,
  settings,
  knowledge,
  cleanupDocuments,
}) => {
  test.setTimeout(180000);

  // Navigate to the application
  await navigateToHome(page);

  logger.info(
    `\nπŸ§ͺ Testing Opensearch Indexing with ${ACTIVE_PROVIDER_CONFIG.provider}`,
  );

  logger.info(`  🧹 Cleaning up existing test document...`);
  try {
    await knowledge.deleteDocument(TEST_DOCUMENT);
    logger.info(`  βœ“ Test document cleaned up`);
  } catch (_error) {
    logger.info(`  ℹ️  No existing test document to clean up`);
  }

  // Step 2: Set embedding model for active provider
  logger.info(
    `  βš™οΈ  Setting embedding model for ${ACTIVE_PROVIDER_CONFIG.provider}...`,
  );
  await settings.clickTab("Ingestion");
  await settings.selectModel(
    "Embedding model",
    ACTIVE_PROVIDER_CONFIG.embedding,
  );
  logger.info(
    `  βœ“ Embedding model set to: ${ACTIVE_PROVIDER_CONFIG.embedding}`,
  );

  // Step 3: Ingest the test document
  logger.info(`  πŸ“„ Ingesting test document...`);
  const ingestedFileName = await knowledge.ingestFile(TEST_DOCUMENT_PATH);
  logger.info(`  βœ“ Document ingested: ${ingestedFileName}`);

  // Register for cleanup
  await cleanupDocuments([TEST_DOCUMENT]);

  // Step 4: Wait for document to be indexed (Active status)
  logger.info(`  ⏳ Waiting for document to be indexed...`);
  await knowledge.verifyDocumentActive(TEST_DOCUMENT);
  logger.info(`  βœ“ Document is indexed and active`);

  // Step 5: Search for the unique token
  // Note: Search works for content within documents, not document names
  logger.info(`  πŸ” Searching for unique token: ${UNIQUE_SEARCH_TOKEN}`);
  const searchResults = await knowledge.getSearchResults(UNIQUE_SEARCH_TOKEN);

  logger.info(`  πŸ“Š Search results: ${searchResults.length} document(s) found`);
  searchResults.forEach((doc, index) => {
    logger.info(`     ${index + 1}. ${doc}`);
  });

  // Step 6: Verify results
  // The test document must be in the results (other documents may also appear)
  const targetDocumentFound = searchResults.includes(TEST_DOCUMENT);

  if (!targetDocumentFound) {
    throw new Error(
      `❌ FAILED: Test document "${TEST_DOCUMENT}" not found in search results for token "${UNIQUE_SEARCH_TOKEN}"\n` +
        `   Search returned ${searchResults.length} document(s): ${searchResults.join(", ")}\n` +
        `   This means the document was not properly indexed or the search functionality is not working.`,
    );
  }

  // Success!
  if (searchResults.length === 1) {
    logger.info(`  βœ… SUCCESS: "${TEST_DOCUMENT}" found as the only result`);
  } else {
    const otherDocs = searchResults.filter((doc) => doc !== TEST_DOCUMENT);
    logger.info(`  βœ… SUCCESS: "${TEST_DOCUMENT}" found in results`);
    logger.info(
      `     ℹ️  Also found ${otherDocs.length} other document(s): ${otherDocs.join(", ")}`,
    );
  }
  logger.info(`  βœ“ Opensearch indexing verified for Azure OpenAI\n`);

  // Assertions for Playwright reporting
  expect(targetDocumentFound).toBe(true);
  expect(searchResults).toContain(TEST_DOCUMENT);
});