channel-code-mock.test.ts 7.09 KB
import { beforeEach, describe, expect, it } from 'vitest';

import {
  channelCodeMockFixtures,
  getChannelCodeListPage,
  getChannelCodeRecord,
  getEmployeeQRCodes,
  getPositionDerivedInfo,
  getPositionsWithChannelCode,
  saveChannelCodeRecord,
  updateChannelCodeStatus,
  resetChannelCodeMockState,
  type ChannelCodeMockItem,
} from './channel-code-mock';

describe('channel code mock contract', () => {
  beforeEach(() => {
    window.localStorage.clear();
    resetChannelCodeMockState();
  });

  it('provides the minimal page-facing fields and required fixtures', () => {
    const [code] = channelCodeMockFixtures.normal.items;

    expect(code).toMatchObject({
      code_id: expect.any(Number),
      code_name: expect.any(String),
      position_code: expect.any(String),
      position_name: expect.any(String),
      channel_code: expect.any(String),
      employee_name: expect.any(String),
      employee_role_text: expect.any(String),
      agent_names: expect.any(Array),
      status: expect.any(String),
      qrcode_url: expect.any(String),
      description: expect.any(String),
      welcome_message: expect.any(String),
      created_at: expect.any(String),
      updated_at: expect.any(String),
      operations: expect.any(Array),
    });

    expect(Object.keys(channelCodeMockFixtures).sort()).toEqual([
      'empty',
      'filteredEmpty',
      'loadError',
      'normal',
      'paginationBoundary',
      'qrcodeLoadError',
      'saveError',
      'statusBlockError',
    ]);
  });

  it('filters by code name, position and status', () => {
    const result1 = getChannelCodeListPage({ keyword: '售后', page: 1, pageSize: 10 });
    expect(result1.items).toHaveLength(1);
    expect(result1.items[0].code_name).toContain('售后');

    const result2 = getChannelCodeListPage({ positionCode: 'store_sales', page: 1, pageSize: 10 });
    expect(result2.items).toHaveLength(1);
    expect(result2.items[0].position_code).toBe('store_sales');

    const result3 = getChannelCodeListPage({ status: 'enable', page: 1, pageSize: 10 });
    expect(result3.items.every((item) => item.status === 'enable')).toBe(true);

    const result4 = getChannelCodeListPage({ status: 'disable', page: 1, pageSize: 10 });
    expect(result4.items.every((item) => item.status === 'disable')).toBe(true);
  });

  it('paginates from page-facing query fields', () => {
    const paged = getChannelCodeListPage({ page: 2, pageSize: 2 });
    expect(paged.total).toBe(5);
    expect(paged.items).toHaveLength(2);
    expect(paged.page).toBe(2);
    expect(paged.pageSize).toBe(2);
  });

  it('provides position derived info including channel code, employee and agents', () => {
    const info = getPositionDerivedInfo('store_sales');
    expect(info).toBeDefined();
    expect(info!).toMatchObject({
      position_code: 'store_sales',
      position_name: expect.any(String),
      channel_code: expect.any(String),
      employee_name: expect.any(String),
      employee_role_text: expect.any(String),
      agent_names: expect.any(Array),
    });
    expect(info!.agent_names.length).toBeGreaterThan(0);
  });

  it('returns undefined for position derived info when position does not exist', () => {
    const info = getPositionDerivedInfo('nonexistent_position');
    expect(info).toBeUndefined();
  });

  it('tracks positions that already have channel codes (one position one code)', () => {
    const positionsWithCodes = getPositionsWithChannelCode();
    expect(positionsWithCodes).toContain('store_sales');
    expect(positionsWithCodes).toContain('after_sales_service');
    expect(positionsWithCodes.length).toBeGreaterThan(0);
  });

  it('creates a new channel code with status disable by default', () => {
    const created = saveChannelCodeRecord({
      code_name: '测试渠道活码',
      position_code: 'live_ops',
      description: '测试描述',
      welcome_message: '欢迎添加',
      status: 'disable',
    });

    expect(created.code_id).toBeGreaterThan(0);
    expect(created.status).toBe('disable');
    expect(created.code_name).toBe('测试渠道活码');
    expect(getChannelCodeRecord(String(created.code_id))).toBeDefined();
    expect(getPositionsWithChannelCode()).toContain('live_ops');
  });

  it('updates existing channel code without changing position', () => {
    const existing = channelCodeMockFixtures.normal.items[0];
    const updated = saveChannelCodeRecord({
      codeId: existing.code_id,
      code_name: '更新后的活码名称',
      description: '更新后的描述',
      welcome_message: '更新后的欢迎语',
      status: 'enable',
    });

    expect(updated.code_id).toBe(existing.code_id);
    expect(updated.code_name).toBe('更新后的活码名称');
    expect(updated.position_code).toBe(existing.position_code);
    expect(updated.status).toBe('enable');
  });

  it('enables and disables channel code status', () => {
    const existing = channelCodeMockFixtures.normal.items.find((item) => item.status === 'disable');
    if (!existing) throw new Error('No disabled item found');

    const enabled = updateChannelCodeStatus(existing.code_id, 'enable', 'normal');
    expect(enabled.status).toBe('enable');
    expect(enabled.updated_at).not.toBe(existing.updated_at);
  });

  it('blocks disable when status update returns business blocking error', () => {
    const existing = channelCodeMockFixtures.normal.items.find((item) => item.status === 'enable');
    if (!existing) throw new Error('No enabled item found');

    const result = updateChannelCodeStatus(existing.code_id, 'disable', 'statusBlockError');
    expect(result.blocked).toBe(true);
    expect(result.message).toContain('官网');
    expect(result.status).toBe('enable');
  });

  it('handles qrcode image load error fixture', () => {
    const fixture = channelCodeMockFixtures.qrcodeLoadError;
    expect(fixture.items[0].qrcode_load_error).toBe(true);
  });

  it('returns undefined for non-existent channel code record', () => {
    const record = getChannelCodeRecord('99999');
    expect(record).toBeUndefined();
  });

  describe('getEmployeeQRCodes API', () => {
    it('returns employee QR codes info for valid employee_id', () => {
      // 岗位 store_sales 的员工 ID 是 1001
      const response = getEmployeeQRCodes(1001);
      expect(response).toBeDefined();
      expect(response!.employee_id).toBe(1001);
      expect(response!.employee_name).toBe('张小萌');
      expect(response!.employee_role_text).toBe('店销主管');
      expect(response!.qrcode_count).toBeGreaterThan(0);
      expect(response!.qrcodes.length).toBeGreaterThan(0);
      expect(response!.qrcodes[0].position_name).toBe('店销顾问');
    });

    it('returns undefined for non-existent employee_id', () => {
      const response = getEmployeeQRCodes(99999);
      expect(response).toBeUndefined();
    });

    it('includes employee derived info from API response', () => {
      const response = getEmployeeQRCodes(1002); // 售后客服员工
      expect(response).toBeDefined();
      expect(response!.employee_name).toBe('李小燕');
      expect(response!.employee_role_text).toBe('客服专员');
      expect(response!.qrcodes[0].channel_code).toBe('ASC-QR-002');
    });
  });
});