prelearning.spec.ts 14.4 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428
import { expect, test, type Page, type Route } from '@playwright/test';

const readGateConfigUrl = 'https://config-center-backend.glsx.com.cn/glsxAdmin/config?moduleUrl=single-pages-config';

type EntrySource = 'home_popup' | 'package_detail';
type ActionType = 'learn_next' | 'complete' | 'view_other';
type BuriedPointEventType = 'page_view' | 'intercept_back' | 'task_completed' | 'reroll_case';

interface PrelearningInitPayload {
  entrySource: EntrySource;
  activityId: string;
  activityProductId: string;
  staffName?: string;
  staffUuid: string;
  merchantId: string;
  caseId?: string;
}

interface ActionState {
  actionType: ActionType;
  buttonLabel: string;
  disabled: boolean;
  disabledReason?: string;
  toastMessage?: string;
}

interface PrelearningResponse {
  sessionId: string;
  productType: 0 | 1;
  activityProduct: {
    activityId: number;
    activityProductId: string;
    productType: 0 | 1;
    tabName: string;
    tabDescription: string;
    goodsCode: string;
    packageCode: string;
    goodsType: number | null;
    goodsTypeName: string | null;
    mainImageUrl: string;
    thumbImageUrl: string;
    detailImageUrl: string;
    currentPrice: string;
    minimumPrice: string;
    discountPrice: string;
    salesIncentive: string;
    isMainPush: boolean;
  };
  script: {
    caseId: string;
    title: string;
    packageInfo: {
      name: string;
      oldPrice: string;
      newPrice: string;
      includes: string[];
      imageUrl: string;
    };
    profile: {
      name: string;
      type: string;
      car: string;
      job: string;
      family: string;
      behavior: string;
      objections: string[];
      needs: string[];
      includes: string[];
    };
    gameSteps: Array<{
      kind: 'normal' | 'highlight';
      stageName: string;
      stageSummary: string;
      audioStartTime: string;
      audioEndTime: string;
      highlightTitle?: string;
      prompt?: string;
      dialogue?: string;
      salesPitch?: string;
      analysis?: string;
    }>;
  };
  actionState: ActionState;
  notice?: string;
}

interface ActionRequest {
  actionType: ActionType;
  payload: PrelearningInitPayload & { sessionId?: string };
}

interface BuriedPointRequest {
  payload: {
    entrySource: EntrySource;
    eventType: BuriedPointEventType;
    staffName?: string;
    staffUuid: string;
    merchantId: string;
    activityId: string;
    activityProductId: string;
    caseId: string;
    sessionId: string;
    durationSeconds: number;
    eventTime: string;
  };
}

const initialPayload: PrelearningInitPayload = {
  entrySource: 'home_popup',
  activityId: '100001',
  activityProductId: '200009',
  staffName: '张三',
  staffUuid: 'staff-001',
  merchantId: 'merchant-001',
};

const nextPayload: PrelearningInitPayload = {
  ...initialPayload,
  activityProductId: '200010',
  caseId: 'case-200010',
};

const prelearningQuery = (payload: PrelearningInitPayload) => new URLSearchParams({
  entrySource: payload.entrySource,
  activityId: payload.activityId,
  activityProductId: payload.activityProductId,
  staffName: payload.staffName ?? '',
  staffUuid: payload.staffUuid,
  merchantId: payload.merchantId,
  ...(payload.caseId ? { caseId: payload.caseId } : {}),
}).toString();

function createPrelearningResponse(payload: PrelearningInitPayload, overrides: Partial<PrelearningResponse> = {}): PrelearningResponse {
  const base: PrelearningResponse = {
    sessionId: `e2e-${payload.activityId}-${payload.activityProductId}`,
    productType: 0,
    activityProduct: {
      activityId: Number(payload.activityId),
      activityProductId: payload.activityProductId,
      productType: 0,
      tabName: '推荐套餐',
      tabDescription: 'AITO 春醒悦享高端车衣套餐,突出守护车漆和长期省心。',
      goodsCode: '',
      packageCode: `package-${payload.activityProductId}`,
      goodsType: null,
      goodsTypeName: null,
      mainImageUrl: 'https://example.com/main.png',
      thumbImageUrl: 'https://example.com/thumb.png',
      detailImageUrl: 'https://example.com/detail.png',
      currentPrice: '10888.00',
      minimumPrice: '8888.00',
      discountPrice: '9888.00',
      salesIncentive: '400.00',
      isMainPush: true,
    },
    script: {
      caseId: payload.caseId ?? `${payload.activityProductId}0`,
      title: 'AITO 春醒悦享高端车衣套餐剧本',
      packageInfo: {
        name: '[套餐] AITO 春醒悦享高端车衣套餐',
        oldPrice: '12888.00',
        newPrice: '10888.00',
        includes: ['前挡保护', '车身全覆盖', '施工质保'],
        imageUrl: 'https://example.com/package.png',
      },
      profile: {
        name: '吴万宇',
        type: '同行竞争少',
        car: 'AITO 问界 M7',
        job: '企业主',
        family: '三口之家',
        behavior: '重视品质与长期价值',
        objections: ['价格有点高', '朋友推荐了其他品牌', '担心施工影响质保'],
        needs: ['刚提车需要整车保护', '关注施工品质和售后质保', '希望价格透明、服务省心'],
        includes: ['延长保修', '施工质保'],
      },
      gameSteps: [
        {
          kind: 'normal',
          stageName: '开场破冰与基础介绍',
          stageSummary: '核心动作:先确认客户关注点,说明车衣能保护新车漆面状态。',
          audioStartTime: '00:00',
          audioEndTime: '01:29',
        },
        {
          kind: 'highlight',
          stageName: '异议处理:正面回应“外包论”与熟人竞争',
          stageSummary: '引导客户把比较点从单纯价格转到施工标准、售后保障和长期用车风险。',
          audioStartTime: '01:30',
          audioEndTime: '02:18',
          highlightTitle: '话术高光',
          prompt: '客户对价格有了担心,也提到朋友那边可能便宜很多。',
          dialogue: '我不是看便宜,你们贵是不是外面贴完再拿回来,后面出问题是不是没人管?',
          salesPitch: '咱们这套车衣是门店标准施工,材料、施工和质保都能追溯。',
          analysis: '先承接客户对价格和熟人渠道的担忧,再把决策标准拉回服务与质保。',
        },
        {
          kind: 'normal',
          stageName: '合规促单与邀约交接',
          stageSummary: '总结套餐价值,确认客户对施工周期、质保和价格是否还有疑问。',
          audioStartTime: '02:19',
          audioEndTime: '03:47',
        },
      ],
    },
    actionState: {
      actionType: 'learn_next',
      buttonLabel: '继续学习下个套餐',
      disabled: false,
    },
  };

  return {
    ...base,
    ...overrides,
    activityProduct: {
      ...base.activityProduct,
      ...overrides.activityProduct,
    },
    script: {
      ...base.script,
      ...overrides.script,
      packageInfo: {
        ...base.script.packageInfo,
        ...overrides.script?.packageInfo,
      },
      profile: {
        ...base.script.profile,
        ...overrides.script?.profile,
      },
      gameSteps: overrides.script?.gameSteps ?? base.script.gameSteps,
    },
    actionState: {
      ...base.actionState,
      ...overrides.actionState,
    },
  };
}

const initialResponse = createPrelearningResponse(initialPayload);
const nextResponse = createPrelearningResponse(nextPayload, {
  sessionId: initialResponse.sessionId,
  activityProduct: {
    ...initialResponse.activityProduct,
    activityProductId: nextPayload.activityProductId,
    packageCode: `package-${nextPayload.activityProductId}`,
    discountPrice: '5888.00',
  },
  script: {
    ...initialResponse.script,
    caseId: nextPayload.caseId ?? 'case-200010',
    packageInfo: {
      ...initialResponse.script.packageInfo,
      name: '[套餐] 雨季智护玻璃膜套餐',
      oldPrice: '6888.00',
      newPrice: '5888.00',
      includes: ['玻璃隔热膜', '雨季镀膜', '施工质保'],
    },
    profile: {
      ...initialResponse.script.profile,
      name: '陈雨泽',
      type: '雨季通勤',
      objections: ['担心雨天视线', '想知道隔热效果'],
      needs: ['雨季通勤安全', '希望隔热更舒服'],
    },
  },
  actionState: {
    actionType: 'complete',
    buttonLabel: '完成学习',
    disabled: false,
  },
});

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

async function setupPrelearningRoutes(page: Page, options: {
  readonly initResponse?: PrelearningResponse;
  readonly actionResponse?: PrelearningResponse;
}) {
  const actionRequests: ActionRequest[] = [];
  const buriedPointRequests: BuriedPointRequest[] = [];

  await page.route(readGateConfigUrl, async (route) => {
    await fulfillJson(route, {
      data: {
        timeCount: 1,
        unit: '秒',
      },
    });
  });

  await page.route('**/api/prelearning/init', async (route) => {
    await fulfillJson(route, options.initResponse ?? initialResponse);
  });

  await page.route('**/api/prelearning/action', async (route) => {
    actionRequests.push(route.request().postDataJSON() as ActionRequest);
    await fulfillJson(route, options.actionResponse ?? nextResponse);
  });

  await page.route('**/api/prelearning/buried-point', async (route) => {
    buriedPointRequests.push(route.request().postDataJSON() as BuriedPointRequest);
    await fulfillJson(route, {
      stored: true,
      duplicate: false,
    });
  });

  return {
    actionRequests,
    buriedPointRequests,
  };
}

async function gotoPrelearning(page: Page, payload = initialPayload) {
  await page.goto(`/prelearning.html?${prelearningQuery(payload)}`);
}

test.describe('prelearning E2E generated from the core journey rules', () => {
  test('P0 Happy Path: Given query payload, When the user opens prelearning, Then the core script is visible and page_view is recorded', async ({ page }) => {
    const { buriedPointRequests } = await setupPrelearningRoutes(page, {});

    await test.step('Given 可重复的 query 测试数据和隔离的接口拦截', async () => {
      expect(prelearningQuery(initialPayload)).toContain('activityProductId=200009');
    });

    await test.step('When 用户进入预学习页面', async () => {
      await gotoPrelearning(page);
    });

    await test.step('Then 页面展示套餐、客户画像、关键需求和阅读门禁', async () => {
      await expect(page.getByRole('heading', {
        name: '[套餐] AITO 春醒悦享高端车衣套餐实战案例解析',
      })).toBeVisible();
      await expect(page.getByLabel('预学习基础信息').getByText('吴万宇')).toBeVisible();
      await expect(page.getByText('刚提车需要整车保护')).toBeVisible();
      await expect(page.getByText('关注施工品质和售后质保')).toBeVisible();
      await expect(page.getByText('异议处理:正面回应“外包论”与熟人竞争')).toBeVisible();
      await expect(page.getByTestId('prelearning-read-gate')).toContainText('距离完成阅读剩余:');
      await expect(page.getByRole('button', { name: '继续学习下个套餐' })).toBeVisible();
    });

    await test.step('Then 非 UI 层记录首屏 page_view 埋点且关键字段一致', async () => {
      await expect.poll(() => buriedPointRequests).toHaveLength(1);
      expect(buriedPointRequests[0]).toMatchObject({
        payload: {
          entrySource: 'home_popup',
          eventType: 'page_view',
          staffName: '张三',
          staffUuid: 'staff-001',
          merchantId: 'merchant-001',
          activityId: '100001',
          activityProductId: '200009',
          caseId: '2000090',
          sessionId: 'e2e-100001-200009',
        },
      });
      expect(buriedPointRequests[0]?.payload.durationSeconds).toBeGreaterThanOrEqual(0);
    });
  });

  test('P1 Critical Branch: Given read gate is complete, When the user learns next, Then the next script loads and action state is persisted', async ({ page }) => {
    const { actionRequests, buriedPointRequests } = await setupPrelearningRoutes(page, {
      initResponse: initialResponse,
      actionResponse: nextResponse,
    });

    await test.step('Given 用户已进入首个预学习案例且阅读门禁可完成', async () => {
      await gotoPrelearning(page);
      await expect(page.getByRole('button', { name: '继续学习下个套餐' })).toBeVisible();
    });

    await test.step('When 用户点击继续学习下个套餐', async () => {
      await page.getByRole('button', { name: '继续学习下个套餐' }).click();
    });

    await test.step('Then 前端提交 learn_next 动作,payload 携带当前会话和案例字段', async () => {
      await expect.poll(() => actionRequests).toHaveLength(1);
      expect(actionRequests[0]).toMatchObject({
        actionType: 'learn_next',
        payload: {
          entrySource: 'home_popup',
          staffUuid: 'staff-001',
          merchantId: 'merchant-001',
          activityId: '100001',
          activityProductId: '200009',
          caseId: '2000090',
          sessionId: 'e2e-100001-200009',
        },
      });
    });

    await test.step('Then 页面切换到下一个案例并重置阅读门禁', async () => {
      await expect(page.getByRole('heading', {
        name: '[套餐] 雨季智护玻璃膜套餐实战案例解析',
      })).toBeVisible();
      await expect(page.getByLabel('预学习基础信息').getByText('陈雨泽')).toBeVisible();
      await expect(page.getByText('雨季通勤安全')).toBeVisible();
      await expect(page.getByText('希望隔热更舒服')).toBeVisible();
      await expect(page.getByLabel('预学习基础信息').getByText('吴万宇')).toHaveCount(0);
      await expect(page.getByTestId('prelearning-read-gate')).toContainText('距离完成阅读剩余:');
    });

    await test.step('Then 非 UI 层记录任务完成埋点,作为动作提交的业务状态证据', async () => {
      await expect.poll(() => buriedPointRequests.some((request) => request.payload.eventType === 'task_completed')).toBe(true);
      const taskCompleted = buriedPointRequests.find((request) => request.payload.eventType === 'task_completed');
      expect(taskCompleted).toMatchObject({
        payload: {
          entrySource: 'home_popup',
          eventType: 'task_completed',
          staffUuid: 'staff-001',
          merchantId: 'merchant-001',
          activityId: '100001',
          activityProductId: '200009',
          caseId: '2000090',
          sessionId: 'e2e-100001-200009',
        },
      });
      expect(taskCompleted?.payload.durationSeconds).toBeGreaterThanOrEqual(0);
    });
  });
});