﻿/* @vitest-environment jsdom */

import { render, screen } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { beforeEach, expect, test, vi } from 'vitest';

import type { BigFishSessionSnapshotResponse } from '../../../packages/shared/src/contracts/bigFish';
import { BigFishAgentWorkspace } from './BigFishAgentWorkspace';

const baseSession: BigFishSessionSnapshotResponse = {
  sessionId: 'big-fish-session-1',
  currentTurn: 3,
  progressPercent: 64,
  stage: 'collecting_anchors',
  anchorPack: {
    gameplayPromise: {
      key: 'gameplayPromise',
      label: '玩法承诺',
      value: '从微光小鱼一路吞噬成长为深海巨兽',
      status: 'confirmed',
    },
    ecologyVisualTheme: {
      key: 'ecologyVisualTheme',
      label: '生态视觉主题',
      value: '幽蓝珊瑚海沟',
      status: 'confirmed',
    },
    growthLadder: {
      key: 'growthLadder',
      label: '成长阶梯',
      value: '',
      status: 'missing',
    },
    riskTempo: {
      key: 'riskTempo',
      label: '风险节奏',
      value: '',
      status: 'missing',
    },
  },
  draft: null,
  assetSlots: [],
  assetCoverage: {
    levelMainImageReadyCount: 0,
    levelMotionReadyCount: 0,
    backgroundReady: false,
    requiredLevelCount: 8,
    publishReady: false,
    blockers: [],
  },
  messages: [
    {
      id: 'message-1',
      role: 'assistant',
      kind: 'chat',
      text: '爽点和生态已经清楚，继续补剩余关键词。',
      createdAt: '2026-04-24T10:00:00.000Z',
    },
  ],
  lastAssistantReply: '爽点和生态已经清楚，继续补剩余关键词。',
  publishReady: false,
  updatedAt: '2026-04-24T10:00:00.000Z',
};

beforeEach(() => {
  if (!Element.prototype.scrollIntoView) {
    Element.prototype.scrollIntoView = () => {};
  }
});

test('big fish workspace submits quick keyword fill request after two turns', async () => {
  const user = userEvent.setup();
  const onSubmitMessage = vi.fn();

  render(
    <BigFishAgentWorkspace
      session={baseSession}
      onBack={() => {}}
      onSubmitMessage={onSubmitMessage}
      onExecuteAction={() => {}}
    />,
  );

  await user.click(screen.getByRole('button', { name: '补充剩余设定' }));

  expect(onSubmitMessage).toHaveBeenCalledWith(
    expect.objectContaining({
      text: '请补充剩余设定。',
      quickFillRequested: true,
    }),
  );
});

test('big fish workspace hides keyword fill before two turns', () => {
  render(
    <BigFishAgentWorkspace
      session={{ ...baseSession, currentTurn: 1 }}
      onBack={() => {}}
      onSubmitMessage={() => {}}
      onExecuteAction={() => {}}
    />,
  );

  expect(screen.queryByRole('button', { name: '补充剩余设定' })).toBeNull();
});

test('big fish workspace does not render progress action messages as chat bubbles', () => {
  render(
    <BigFishAgentWorkspace
      session={{
        ...baseSession,
        messages: [
          ...baseSession.messages,
          {
            id: 'message-action-result-1',
            role: 'assistant',
            kind: 'action_result',
            text: '本级主图已正式生成，可在结果页继续预览。',
            createdAt: '2026-04-24T10:01:00.000Z',
          },
        ],
      }}
      onBack={() => {}}
      onSubmitMessage={() => {}}
      onExecuteAction={() => {}}
    />,
  );

  expect(screen.getByText('爽点和生态已经清楚，继续补剩余关键词。')).toBeTruthy();
  expect(
    screen.queryByText('本级主图已正式生成，可在结果页继续预览。'),
  ).toBeNull();
});
