Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

4.8 KB
Raw Blame
import type { DifyWorld } from '../../support/world.ts'
import { Given, Then, When } from '@cucumber/cucumber'
import { expect } from '@playwright/test'
import { createTestApp } from '../../../support/api/apps.ts'
import { getAppSiteURL } from '../../../support/api/web-apps.ts'
import { syncRunnableWorkflowDraft } from '../../../support/api/workflows.ts'
import { createE2EResourceName } from '../../../support/naming.ts'
import { baseURL, defaultLocale } from '../../../test-env.ts'

Given('a new runnable workflow app has been published', async function (this: DifyWorld) {
  const client = this.getConsoleClient()
  const app = await createTestApp(client, createE2EResourceName('App', 'WebApp'), 'workflow')
  this.createdAppIds.push(app.id)
  this.lastCreatedAppName = app.name
  await syncRunnableWorkflowDraft(client, app.id)
  await client.apps.byAppId.workflows.publish.post({
    body: { marked_comment: '', marked_name: '' },
    params: { app_id: app.id },
  })

  const appDetail = await client.apps.byAppId.get({ params: { app_id: app.id } })
  expect(appDetail.enable_site).toBe(true)
  this.shareURL = getAppSiteURL(appDetail)
})

const getWebAppSwitch = (world: DifyWorld) => {
  const webAppCard = world.getPage().getByRole('region', { name: 'Web App' })
  return webAppCard.getByRole('switch', { name: 'Web App' })
}

async function waitForPersistedWebAppState(world: DifyWorld, enabled: boolean) {
  const appId = world.createdAppIds.at(-1)
  if (!appId) throw new Error('No workflow app has been created.')
  const client = world.getConsoleClient()
  // The status label updates optimistically. A different browser must wait for
  // the saved state before reloading the published app.
  await expect
    .poll(
      async () => {
        const app = await client.apps.byAppId.get({ params: { app_id: appId } })
        return app.enable_site
      },
      { timeout: 10_000 },
    )
    .toBe(enabled)
}

When('an anonymous visitor opens the Web App', async function (this: DifyWorld) {
  if (!this.shareURL) throw new Error('No Web App URL is available.')
  if (!this.context) throw new Error('Playwright browser context has not been initialized.')

  const browser = this.context.browser()
  if (!browser) throw new Error('Playwright browser has not been initialized.')

  const anonymousContext = await browser.newContext({ baseURL, locale: defaultLocale })
  this.registerCleanup(() => anonymousContext.close())
  this.sharedAppPage = await anonymousContext.newPage()
  await this.sharedAppPage.goto(this.shareURL, { timeout: 20_000 })
})

When('the anonymous visitor reloads the Web App', async function (this: DifyWorld) {
  if (!this.sharedAppPage) throw new Error('The anonymous visitor has not opened the Web App.')
  await this.sharedAppPage.reload({ timeout: 20_000 })
})

When('I disable the Web App', async function (this: DifyWorld) {
  const webAppSwitch = getWebAppSwitch(this)

  await expect(webAppSwitch).not.toHaveAttribute('aria-disabled', 'true', { timeout: 15_000 })
  await expect(webAppSwitch).toHaveAttribute('aria-checked', 'true')
  await webAppSwitch.click()
})

When('I enable the Web App', async function (this: DifyWorld) {
  const webAppSwitch = getWebAppSwitch(this)

  await expect(webAppSwitch).not.toHaveAttribute('aria-disabled', 'true', { timeout: 15_000 })
  await expect(webAppSwitch).toHaveAttribute('aria-checked', 'false')
  await webAppSwitch.click()
})

Then('the Web App should be in service', async function (this: DifyWorld) {
  const webAppCard = this.getPage().getByRole('region', { name: 'Web App' })
  await expect(webAppCard.getByText(/^In service$/i)).toBeVisible({
    timeout: 10_000,
  })
  await waitForPersistedWebAppState(this, true)
})

Then('the Web App should be disabled', async function (this: DifyWorld) {
  const webAppCard = this.getPage().getByRole('region', { name: 'Web App' })
  await expect(webAppCard.getByText('Disabled', { exact: true })).toBeVisible({
    timeout: 10_000,
  })
  await waitForPersistedWebAppState(this, false)
})

Then('the published workflow Web App should be accessible', async function (this: DifyWorld) {
  if (!this.sharedAppPage) throw new Error('The anonymous visitor has not opened the Web App.')
  await expect(this.sharedAppPage.getByRole('button', { name: 'Execute' })).toBeVisible({
    timeout: 15_000,
  })
})

Then('the published workflow Web App should be unavailable', async function (this: DifyWorld) {
  if (!this.sharedAppPage) throw new Error('The anonymous visitor has not opened the Web App.')
  await expect(
    this.sharedAppPage.getByRole('heading', {
      name: 'This app doesn’t exist or isn’t accessible from your network.',
    }),
  ).toBeVisible({
    timeout: 15_000,
  })
  await expect(
    this.sharedAppPage.getByText('Contact the app administrator if you need help.', {
      exact: true,
    }),
  ).toBeVisible()
})