import path from 'path' import { mkdtempSync, rmSync, existsSync } from 'fs' import { join } from 'path' import { tmpdir } from 'os' import { test, expect, _electron as electron, type Page } from '@playwright/test' const ROOT = path.join(import.meta.dirname, '..') async function launchApp(userDataDir: string) { return electron.launch({ args: [ROOT], env: { ...process.env, BILIN_E2E: '1', BILIN_E2E_USER_DATA: userDataDir } }) } async function skipOnboarding(page: Page): Promise { await page.waitForLoadState('domcontentloaded') await expect(page.locator('#app')).toBeVisible({ timeout: 20_000 }) await expect(page.getByText('欢迎使用笔临')).toBeVisible({ timeout: 15_000 }) await page.getByRole('button', { name: '跳过' }).click() await expect(page.getByText('欢迎使用笔临')).toBeHidden({ timeout: 10_000 }) } async function dismissCockpitIfOpen(page: Page): Promise { const cockpit = page.locator('[data-testid="cockpit-modal"]') try { await cockpit.waitFor({ state: 'visible', timeout: 15_000 }) await cockpit.locator('.modal-close').click() await expect(cockpit).toBeHidden({ timeout: 5000 }) } catch { /* cockpit did not open */ } } async function createAndOpenBook(page: Page, name: string): Promise { await page.getByRole('button', { name: /新建书籍/ }).first().click() await page.locator('.dialog-content input').first().fill(name) await page.getByRole('button', { name: '创建' }).click() await expect(page.locator('#editor-layout')).toBeVisible({ timeout: 15_000 }) await dismissCockpitIfOpen(page) } async function createCharacterSetting(page: Page, name: string): Promise { await page.getByTestId('sidebar-tab-setting').click() await page.getByTestId('setting-new').click() await page.getByTestId('setting-name-input').fill(name) await page.locator('.dialog-content select').selectOption('character') await page.getByRole('button', { name: '创建' }).click() await expect(page.getByTestId(/setting-item-/).filter({ hasText: name })).toBeVisible({ timeout: 10_000 }) } test.describe('Character graph P7', () => { let userDataDir: string test.beforeEach(() => { userDataDir = mkdtempSync(join(tmpdir(), 'bilin-e2e-graph-')) }) test.afterEach(async () => { if (userDataDir && existsSync(userDataDir)) { try { rmSync(userDataDir, { recursive: true, force: true, maxRetries: 3, retryDelay: 200 }) } catch { /* ignore */ } } }) test('E2E-GRAPH-01: relationship editor and graph modal', async () => { const app = await launchApp(userDataDir) const page = await app.firstWindow() await skipOnboarding(page) await createAndOpenBook(page, '图谱测试书') await createCharacterSetting(page, '角色甲') await createCharacterSetting(page, '角色乙') await page.getByTestId(/setting-item-/).filter({ hasText: '角色甲' }).click() await expect(page.getByTestId('setting-relationships')).toBeVisible({ timeout: 10_000 }) await page.getByTestId('setting-relationship-add').click() await expect(page.locator('.setting-relationship-form select')).toBeVisible({ timeout: 10_000 }) await page.locator('.setting-relationship-form select').selectOption({ index: 1 }) await expect(page.locator('.setting-relationship-form input.form-control')).toBeVisible({ timeout: 10_000 }) await page.locator('.setting-relationship-form input.form-control').fill('挚友') await page.locator('.setting-relationship-form .btn-primary').click() await expect(page.getByTestId('setting-relationships')).toContainText('挚友') await page.getByTestId('setting-open-graph').click() await expect(page.getByTestId('character-graph-cy')).toBeVisible({ timeout: 10_000 }) await page.getByTestId('graph-layout-circle').click() await expect(page.getByTestId('character-graph-sidebar')).toBeVisible() await app.close() }) test('E2E-GRAPH-02: tap node shows sidebar with character name', async () => { const app = await launchApp(userDataDir) const page = await app.firstWindow() await skipOnboarding(page) await createAndOpenBook(page, '图谱节点书') await createCharacterSetting(page, '节点A') await createCharacterSetting(page, '节点B') await page.getByTestId(/setting-item-/).filter({ hasText: '节点A' }).click() await page.getByTestId('setting-relationship-add').click() await expect(page.locator('.setting-relationship-form select')).toBeVisible({ timeout: 10_000 }) await page.locator('.setting-relationship-form select').selectOption({ index: 1 }) await expect(page.locator('.setting-relationship-form input.form-control')).toBeVisible({ timeout: 10_000 }) await page.locator('.setting-relationship-form input.form-control').fill('同门') await page.locator('.setting-relationship-form .btn-primary').click() await page.getByTestId('setting-open-graph').click() await expect(page.getByTestId('character-graph-cy')).toBeVisible({ timeout: 10_000 }) await page.evaluate(() => { const el = document.querySelector('[data-testid="character-graph-cy"]') as HTMLElement & { __bilinCy?: { nodes: () => { trigger: (e: string) => void }[] } } const cy = el?.__bilinCy if (!cy) throw new Error('cytoscape not ready') const nodes = cy.nodes() if (!nodes.length) throw new Error('no graph nodes') nodes[0]!.trigger('tap') }) await expect(page.getByTestId('character-graph-sidebar')).toContainText(/节点A|节点B/, { timeout: 10_000 }) await app.close() }) })