import { expect, test } from './authenticated-test.js';

/** File purpose (plain language): checks the enterprise WeChat page's create flow, external-number navigation, and phone filter. */
test('creates an enterprise WeChat asset and exposes the required form', async ({ page }) => {
  /** Code purpose (plain language): serves deterministic list and lookup API responses. Related files: WecomAccountView.js, wecom-api-client.js. Data flow: browser request -> route fixture -> rendered page. */
  await page.route('**/api/wecom-accounts**', async route => {
    const url = route.request().url();
    if (url.includes('/lookups/phone-assets')) return route.fulfill({ contentType: 'application/json', body: JSON.stringify({ code: 200, data: [{ id: 7, phoneNumber: '13812345678', numberType: 'SELF' }] }) });
    if (route.request().method() === 'POST') return route.fulfill({ contentType: 'application/json', body: JSON.stringify({ code: 200, data: { id: 1 } }) });
    return route.fulfill({ contentType: 'application/json', body: JSON.stringify({ code: 200, data: { records: [], total: 0, page: 1, size: 20 } }) });
  });
  await page.goto('/asset/#/reference/wecom');
  await expect.poll(() => page.evaluate(() => document.documentElement.scrollWidth <= document.documentElement.clientWidth)).toBe(true);
  await expect(page.getByRole('heading', { name: '企业微信资产' })).toBeVisible();
  await page.getByRole('button', { name: '新增企业微信资产' }).click();
  await page.getByLabel('企微名称').fill('测试企微');
  await page.getByLabel('注册手机号').fill('13812345678');
  await page.getByRole('button', { name: '保存' }).click();
  await expect(page.getByText('新增成功')).toBeVisible();
});

test('external number links to the related enterprise WeChat asset filter', async ({ page }) => {
  /** Code purpose (plain language): returns one external WeCom-created number. Related files: PhoneAssetView.js, WecomAccountView.js. Data flow: phone list fixture -> external link -> WeCom URL filter. */
  await page.route('**/api/phone-assets**', route => route.fulfill({ contentType: 'application/json', body: JSON.stringify({ code: 200, data: { records: [{ id: 9, phoneNumber: '13912345678', numberType: 'EXTERNAL', sourceAssetType: 'WECOM', cardType: '', iccid: '', realNameOwner: '', managementType: '', disposalStatus: '正常使用' }], total: 1, page: 1, size: 20 } }) }));
  await page.goto('/asset/#/phone-assets');
  await expect.poll(() => page.evaluate(() => document.documentElement.scrollWidth <= document.documentElement.clientWidth)).toBe(true);
  const link = page.getByRole('link', { name: '外部号码' });
  await expect(link).toHaveAttribute('href', '#/reference/wecom?phoneAssetId=9');
});
