sop-material-preview.spec.ts 5.9 KB
import { expect, test, type Page, type Route } from '@playwright/test';

type MaterialItem = {
  type: string;
  url: string;
};

interface SopMaterialPreviewResponse {
  id: number;
  content: string;
  bgColor: string;
  intervalPx: number;
  materials: MaterialItem[];
}

interface SopMaterialPreviewInitMessage {
  type: 'SOP_MATERIAL_PREVIEW_INIT';
  payload: {
    id?: number;
    content: string;
    bgColor: string;
    intervalPx: number;
    materials: MaterialItem[];
  };
}

const transparentPng = Buffer.from(
  'iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==',
  'base64',
);

const queryPreviewResponse: SopMaterialPreviewResponse = {
  id: 321,
  content: 'AITO 精品保养 SOP 海报',
  bgColor: '#f5f1e8',
  intervalPx: 12,
  materials: [
    {
      type: 'image',
      url: 'https://e2e.local/sop-material-query.png',
    },
  ],
};

const iframePreviewMessage: SopMaterialPreviewInitMessage = {
  type: 'SOP_MATERIAL_PREVIEW_INIT',
  payload: {
    id: 654,
    content: 'iframe 直接预览 SOP 海报',
    bgColor: '#eef6ff',
    intervalPx: 8,
    materials: [
      {
        type: 'image',
        url: 'https://e2e.local/sop-material-iframe.png',
      },
    ],
  },
};

async function fulfillJson(route: Route, body: unknown) {
  await route.fulfill({
    status: 200,
    contentType: 'application/json',
    body: JSON.stringify(body),
  });
}

async function fulfillPng(route: Route) {
  await route.fulfill({
    status: 200,
    contentType: 'image/png',
    body: transparentPng,
  });
}

async function setupMaterialRoutes(page: Page, response = queryPreviewResponse) {
  const apiRequests: string[] = [];

  await page.route('https://e2e.local/**', fulfillPng);

  await page.route('**/api/sop-material-preview?**', async (route) => {
    apiRequests.push(route.request().url());
    await fulfillJson(route, response);
  });

  return {
    apiRequests,
  };
}

test.describe('sop-material-preview E2E generated from the core journey rules', () => {
  test('P0 Happy Path: Given actionId query, When the user opens the page, Then SOP material renders from the API model', async ({ page }) => {
    const { apiRequests } = await setupMaterialRoutes(page);

    await test.step('Given 可重复的 actionId query 和隔离的 API/material 拦截', async () => {
      expect(queryPreviewResponse.materials).toHaveLength(1);
    });

    await test.step('When 用户通过 actionId 打开 SOP 素材预览页', async () => {
      await page.goto('/sop-material-preview.html?actionId=321&taskId=88&customerInstanceId=9001');
    });

    await test.step('Then 前端请求素材接口并透传关键 query 参数', async () => {
      await expect.poll(() => apiRequests).toHaveLength(1);
      const apiUrl = new URL(apiRequests[0]);
      expect(apiUrl.pathname).toBe('/api/sop-material-preview');
      expect(apiUrl.searchParams.get('actionId')).toBe('321');
      expect(apiUrl.searchParams.get('taskId')).toBe('88');
      expect(apiUrl.searchParams.get('customerInstanceId')).toBe('9001');
    });

    await test.step('Then 页面渲染素材图片、标题和接口返回的背景色', async () => {
      const image = page.locator('img[src="https://e2e.local/sop-material-query.png"]');
      await expect(image).toBeVisible();
      await expect(page).toHaveTitle('AITO 精品保养 SOP 海报');
      await expect(page.locator('main')).toHaveCSS('background-color', 'rgb(245, 241, 232)');
      await expect(page.locator('body')).toHaveCSS('background-color', 'rgb(245, 241, 232)');
    });
  });

  test('P1 Critical Branch: Given iframe embedding, When parent sends init payload, Then direct preview renders without querying the API', async ({ page, baseURL }) => {
    const { apiRequests } = await setupMaterialRoutes(page);

    await test.step('Given SOP 素材页作为 iframe 被父页面嵌入', async () => {
      const iframeSrc = new URL('/sop-material-preview.html', baseURL).toString();
      await page.setContent(`
        <!doctype html>
        <html>
          <body>
            <iframe title="SOP material preview" src="${iframeSrc}"></iframe>
          </body>
        </html>
      `);
    });

    const frame = page.frameLocator('iframe[title="SOP material preview"]');

    await test.step('When 子页面 ready 后父页面发送 SOP_MATERIAL_PREVIEW_INIT', async () => {
      await expect(frame.locator('main')).toBeVisible();
      await frame.locator('body').evaluate((body, message) => {
        body.ownerDocument.defaultView?.dispatchEvent(new MessageEvent('message', {
          data: message,
          origin: 'https://dj.glsx.com.cn',
        }));
      }, iframePreviewMessage);
    });

    await test.step('Then iframe 内直接渲染素材图片、标题和背景色', async () => {
      const image = frame.locator('img[src="https://e2e.local/sop-material-iframe.png"]');
      await expect(image).toBeVisible();
      await expect(frame.locator('main')).toHaveCSS('background-color', 'rgb(238, 246, 255)');
      await expect.poll(() => apiRequests).toHaveLength(0);
    });
  });

  test('P1 Critical Error Path: Given no actionId and no init message, When the page opens, Then it stays blank and does not query the API', async ({ page }) => {
    const { apiRequests } = await setupMaterialRoutes(page);

    await test.step('Given URL 不包含 actionId 且没有 iframe 初始化消息', async () => {
      expect(apiRequests).toHaveLength(0);
    });

    await test.step('When 用户直接打开 SOP 素材预览页', async () => {
      await page.goto('/sop-material-preview.html');
    });

    await test.step('Then 页面保持空白承载态且不发起素材查询', async () => {
      await expect(page.locator('main img')).toHaveCount(0);
      await expect(page.locator('main video')).toHaveCount(0);
      await expect(page.locator('main')).toHaveCSS('background-color', 'rgb(255, 255, 255)');
      await expect.poll(() => apiRequests).toHaveLength(0);
    });
  });
});