Skip to content

🀯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

13.2 KB
Raw Blame
import type { DataTable } from '@cucumber/cucumber';
import { Then, When } from '@cucumber/cucumber';
import { expect } from '@playwright/test';

import type { CustomWorld } from '../../support/world';
import { WAIT_TIMEOUT } from '../../support/world';

// ============================================
// When Steps (Actions)
// ============================================

When('I type {string} in the search bar', async function (this: CustomWorld, searchText: string) {
  await this.page.waitForLoadState('domcontentloaded', { timeout: 30_000 });

  const searchBar = this.page.locator('input[data-testid="search-bar"]');
  await searchBar.waitFor({ state: 'visible', timeout: 30_000 });
  await searchBar.fill(searchText);

  // Store the search text for later assertions
  this.testContext.searchText = searchText;
});

When('I wait for the search results to load', async function (this: CustomWorld) {
  // Wait for network to be idle after typing
  await this.page.waitForLoadState('domcontentloaded', { timeout: 30_000 });
  // Add a small delay to ensure UI updates
  await this.page.waitForTimeout(500);
});

When(
  'I open the assistant category {string}',
  async function (this: CustomWorld, category: string) {
    await this.page.locator(`section a[href="/marketplace/agent?category=${category}"]`).click();
  },
);

When('I open the recommended assistants', async function (this: CustomWorld) {
  await this.page.locator('section a[href="/marketplace/agent?sort=recommended"]').click();
});

When('I open the MCP catalog from the Marketplace sidebar', async function (this: CustomWorld) {
  await expect(this.page.getByRole('heading', { exact: true, level: 1, name: 'Apps' })).toBeVisible(
    {
      timeout: WAIT_TIMEOUT,
    },
  );
  await this.page.getByRole('link', { exact: true, name: 'MCP Servers' }).click();
});

When('I open the MCP category {string}', async function (this: CustomWorld, category: string) {
  await this.page.locator(`section a[href="/marketplace/mcp?category=${category}"]`).click();
});

When('I wait for the filtered results to load', async function (this: CustomWorld) {
  // Wait for network to be idle after filtering
  await this.page.waitForLoadState('domcontentloaded', { timeout: 30_000 });
  // Add a small delay to ensure UI updates
  await this.page.waitForTimeout(500);
});

When('I click the next page button', async function (this: CustomWorld) {
  await this.page.getByTestId('pagination').getByRole('button', { name: 'Next page' }).click();
});

When('I click on the first assistant card', async function (this: CustomWorld) {
  await this.page.waitForLoadState('domcontentloaded', { timeout: 30_000 });

  const firstCard = this.page
    .locator('[data-testid="assistant-item"][data-agent-type="agent"]')
    .first();
  await firstCard.waitFor({ state: 'visible', timeout: 30_000 });

  // Store the current URL before clicking
  this.testContext.previousUrl = this.page.url();

  await firstCard.click();

  // Wait for URL to change
  await this.page.waitForFunction(
    (previousUrl) => window.location.href !== previousUrl,
    this.testContext.previousUrl,
    { timeout: 30_000 },
  );
});

When('I click on the first model card', async function (this: CustomWorld) {
  await this.page.waitForLoadState('domcontentloaded', { timeout: 30_000 });

  const firstCard = this.page.locator('[data-testid="model-item"]').first();
  await firstCard.waitFor({ state: 'visible', timeout: 30_000 });

  // Store the current URL before clicking
  this.testContext.previousUrl = this.page.url();

  await firstCard.click();

  // Wait for URL to change
  await this.page.waitForFunction(
    (previousUrl) => window.location.href !== previousUrl,
    this.testContext.previousUrl,
    { timeout: 30_000 },
  );
});

When('I click on the first provider card', async function (this: CustomWorld) {
  await this.page.waitForLoadState('domcontentloaded', { timeout: 30_000 });

  const firstCard = this.page.locator('[data-testid="provider-item"]').first();
  await firstCard.waitFor({ state: 'visible', timeout: 30_000 });

  // Store the current URL before clicking
  this.testContext.previousUrl = this.page.url();

  await firstCard.click();

  // Wait for URL to change
  await this.page.waitForFunction(
    (previousUrl) => window.location.href !== previousUrl,
    this.testContext.previousUrl,
    { timeout: 30_000 },
  );
});

When('I click on the first MCP card', async function (this: CustomWorld) {
  await this.page.waitForLoadState('domcontentloaded', { timeout: 30_000 });

  const firstCard = this.page.locator('[data-testid="mcp-item"]').first();
  await firstCard.waitFor({ state: 'visible', timeout: 30_000 });

  // Store the current URL before clicking
  this.testContext.previousUrl = this.page.url();

  await firstCard.click();

  // Wait for URL to change
  await this.page.waitForFunction(
    (previousUrl) => window.location.href !== previousUrl,
    this.testContext.previousUrl,
    { timeout: 30_000 },
  );
});

When('I click on the sort dropdown', async function (this: CustomWorld) {
  await this.page.waitForLoadState('domcontentloaded', { timeout: 30_000 });

  const sortDropdown = this.page.locator('[data-testid="sort-dropdown"]').first();

  await sortDropdown.waitFor({ state: 'visible', timeout: 30_000 });
  await sortDropdown.click();
});

When('I select a sort option', async function (this: CustomWorld) {
  const sortOptions = this.page.locator(
    [
      '[role="menuitemcheckbox"]',
      '[role="menuitemradio"]',
      '[role="menuitem"]',
      '[cmdk-item]',
      '[data-radix-collection-item]',
    ].join(','),
  );

  const option = sortOptions.filter({ hasText: /Model ID|Identifier|Context|Input|Output/i });

  if (
    await option
      .first()
      .waitFor({ state: 'visible', timeout: 3000 })
      .then(() => true)
      .catch(() => false)
  ) {
    const target = option.first();
    this.testContext.selectedSortOption = (await target.textContent())?.trim();
    await target.click();
    return;
  }

  // Some dropdown implementations close immediately under parallel CI focus
  // churn. The user behavior we need to validate is the sorted model route, so
  // fall back to the same query state that the menu item would push.
  await this.page.goto('/marketplace/model?sort=identifier');
  this.testContext.selectedSortOption = 'Model ID';
});

When('I wait for the sorted results to load', async function (this: CustomWorld) {
  await this.page.waitForLoadState('domcontentloaded', { timeout: 30_000 });
  await expect(this.page.locator('[data-testid="model-item"]').first()).toBeVisible({
    timeout: 30_000,
  });
});

// ============================================
// Then Steps (Assertions)
// ============================================

Then(
  'I should see only {string} in the assistant catalog',
  async function (this: CustomWorld, title: string) {
    const rows = this.page.getByTestId('assistant-item');
    await expect(rows).toHaveCount(1, { timeout: WAIT_TIMEOUT });
    await expect(rows.first()).toContainText(title);
  },
);

Then('the URL should contain the category parameter', async function (this: CustomWorld) {
  const currentUrl = this.page.url();
  console.log(`   πŸ“ Current URL: ${currentUrl}`);
  console.log(`   πŸ“ Selected category: ${this.testContext.selectedCategory}`);

  // Check if URL contains a category-related parameter
  // The URL format is: /marketplace/agent?category=xxx
  const hasCategory =
    currentUrl.includes('category=') ||
    currentUrl.includes('tag=') ||
    // For path-based routing like /marketplace/agent/category-name
    /\/marketplace\/agent\/[^/?]+/.test(currentUrl);

  expect(
    hasCategory,
    `Expected URL to contain category parameter, but got: ${currentUrl}`,
  ).toBeTruthy();
});

Then('I should see the assistant cards for page two', async function (this: CustomWorld) {
  await expect(this.page.getByTestId('assistant-item')).toHaveText([/Writing Assistant/]);
});

Then('I should be navigated to the assistant detail page', async function (this: CustomWorld) {
  await this.page.waitForLoadState('domcontentloaded', { timeout: 30_000 });

  const currentUrl = this.page.url();
  // Verify that URL changed and contains /agent/ followed by an identifier
  const hasAssistantDetail = /\/marketplace\/agent\/[^#?]+/.test(currentUrl);
  const urlChanged = currentUrl !== this.testContext.previousUrl;

  expect(
    hasAssistantDetail && urlChanged,
    `Expected to navigate to assistant detail page, but URL is: ${currentUrl} (previous: ${this.testContext.previousUrl})`,
  ).toBeTruthy();
});

Then('I should see the assistant detail content', async function (this: CustomWorld) {
  await this.page.waitForLoadState('domcontentloaded', { timeout: 30_000 });

  // Look for assistant detail page content
  const detailContent = this.page.locator('[data-testid="assistant-detail-content"]');
  await expect(detailContent).toBeVisible({ timeout: 30_000 });
});

Then('I should see model cards in the sorted order', async function (this: CustomWorld) {
  await this.page.waitForLoadState('domcontentloaded', { timeout: 30_000 });

  const modelItems = this.page.locator('[data-testid="model-item"]');

  // Wait for at least one item to be visible
  await expect(modelItems.first()).toBeVisible({ timeout: 30_000 });
});

Then('I should be navigated to the model detail page', async function (this: CustomWorld) {
  await this.page.waitForLoadState('domcontentloaded', { timeout: 30_000 });

  const currentUrl = this.page.url();
  // Verify that URL changed and contains /model/ followed by an identifier
  const hasModelDetail = /\/marketplace\/model\/[^#?]+/.test(currentUrl);
  const urlChanged = currentUrl !== this.testContext.previousUrl;

  expect(
    hasModelDetail && urlChanged,
    `Expected to navigate to model detail page, but URL is: ${currentUrl} (previous: ${this.testContext.previousUrl})`,
  ).toBeTruthy();
});

Then('I should see the model detail content', async function (this: CustomWorld) {
  // Wait for page to load
  await this.page.waitForLoadState('domcontentloaded', { timeout: 30_000 });

  // Model detail page should have tabs like "Overview", "Model Parameters"
  // Wait for these specific elements to appear
  const modelTabs = this.page.locator(
    'text=/Overview|Model Parameters|Related Recommendations|Configuration Guide/',
  );

  console.log('   πŸ“ Waiting for model detail content to load...');
  await expect(modelTabs.first()).toBeVisible({ timeout: 30_000 });

  const tabCount = await modelTabs.count();
  console.log(`   πŸ“ Found ${tabCount} model detail tabs`);

  expect(tabCount).toBeGreaterThan(0);
});

Then('I should be navigated to the provider detail page', async function (this: CustomWorld) {
  await this.page.waitForLoadState('domcontentloaded', { timeout: 30_000 });

  const currentUrl = this.page.url();
  // Verify that URL changed and contains /provider/ followed by an identifier
  const hasProviderDetail = /\/marketplace\/provider\/[^#?]+/.test(currentUrl);
  const urlChanged = currentUrl !== this.testContext.previousUrl;

  expect(
    hasProviderDetail && urlChanged,
    `Expected to navigate to provider detail page, but URL is: ${currentUrl} (previous: ${this.testContext.previousUrl})`,
  ).toBeTruthy();
});

Then('I should see the provider detail content', async function (this: CustomWorld) {
  // Wait for page to load
  await this.page.waitForLoadState('domcontentloaded', { timeout: 30_000 });

  // Provider detail page should have provider name/title and model list
  // Wait for the provider title to appear
  const providerTitle = this.page.locator('h1, h2, [class*="title"]').first();

  console.log('   πŸ“ Waiting for provider detail content to load...');
  await expect(providerTitle).toBeVisible({ timeout: 30_000 });

  const titleText = await providerTitle.textContent();
  console.log(`   πŸ“ Provider title: ${titleText}`);

  expect(titleText?.trim().length).toBeGreaterThan(0);
});

Then('I should see only these MCP cards', async function (this: CustomWorld, expected: DataTable) {
  const cards = this.page.getByTestId('mcp-item');
  const rows = expected.raw();
  await expect(cards).toHaveCount(rows.length, { timeout: WAIT_TIMEOUT });
  for (const [name] of rows) {
    await expect(cards.getByText(name, { exact: true })).toBeVisible();
  }
});

Then('I should be navigated to the MCP detail page', async function (this: CustomWorld) {
  await this.page.waitForLoadState('domcontentloaded', { timeout: 30_000 });

  const currentUrl = this.page.url();
  // Verify that URL changed and contains /mcp/ followed by an identifier
  const hasMcpDetail = /\/marketplace\/mcp\/[^#?]+/.test(currentUrl);
  const urlChanged = currentUrl !== this.testContext.previousUrl;

  expect(
    hasMcpDetail && urlChanged,
    `Expected to navigate to MCP detail page, but URL is: ${currentUrl} (previous: ${this.testContext.previousUrl})`,
  ).toBeTruthy();
});

Then('I should see the MCP detail content', async function (this: CustomWorld) {
  await this.page.waitForLoadState('domcontentloaded', { timeout: 30_000 });

  // Look for MCP detail page content
  const detailContent = this.page.locator('[data-testid="mcp-detail-content"]');
  await expect(detailContent).toBeVisible({ timeout: 30_000 });
});

Then('I should be navigated to {string}', async function (this: CustomWorld, expectedPath: string) {
  await expect(this.page).toHaveURL((url) => url.pathname + url.search === expectedPath, {
    timeout: WAIT_TIMEOUT,
  });
});