Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

33.0 KB
Raw Blame
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { act, fireEvent, render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';

vi.mock('../../services/api', () => ({ playLoomTurn: vi.fn() }));
vi.mock('../MediaImage', () => ({ default: (props) => <img alt="" {...props} /> }));
vi.mock('socket.io-client', () => ({
  io: vi.fn(() => ({ on: vi.fn(), emit: vi.fn(), disconnect: vi.fn() })),
}));
vi.mock('./LoomHostedSessionModal', () => ({
  default: ({ onSessionCreated }) => (
    <button type="button" onClick={() => onSessionCreated({ id: 'host-session' }, {
      id: 'host-session',
      token: 'audience-token',
      hostToken: 'the-real-host-token',
      joinUrl: 'https://example.test/fableloom/join#session=host-session&token=audience-token',
    })}>
      Start hosted session
    </button>
  ),
}));
vi.mock('../ui/Toast', () => ({ default: { error: vi.fn(), success: vi.fn() } }));

import { playLoomTurn } from '../../services/api';
import { io } from 'socket.io-client';
import toast from '../ui/Toast';
import LoomPlayPanel from './LoomPlayPanel';

const loom = { id: 'loom-1', name: 'The Hollow Crown', episodes: [] };
const episode = {
  id: 'ep-1',
  startNodeId: 'n1',
  nodes: [
    {
      id: 'n1',
      title: 'The Gate',
      prose: 'You stand before it.',
      transitions: [{ id: 't1', targetNodeId: 'n2', intent: 'enter the gate', triggers: [], description: '' }],
    },
    { id: 'n2', title: 'Inside', prose: 'Torchlight.', isEnding: false, transitions: [{ id: 't2', targetNodeId: 'n1', intent: 'retreat', triggers: [], description: '' }] },
  ],
};

const sendMessage = async (user, text) => {
  await user.type(screen.getByLabelText('Your action'), text);
  await user.click(screen.getByRole('button', { name: 'Send' }));
};

beforeEach(() => vi.clearAllMocks());

describe('LoomPlayPanel', () => {
  it('authenticates the host socket with the HOST token, never the audience join token (#8357)', async () => {
    const user = userEvent.setup();
    render(<LoomPlayPanel loom={loom} episode={episode} />);

    await user.click(screen.getByRole('button', { name: 'Host (QR)' }));
    await user.click(screen.getByRole('button', { name: 'Start hosted session' }));

    expect(io).toHaveBeenCalledWith('/fableloom-hosted', expect.objectContaining({
      auth: { sessionId: 'host-session', token: 'the-real-host-token', role: 'host' },
    }));
  });

  it('presents produced media as the main stage with the teleplay beside it', () => {
    const onClose = vi.fn();
    const producedEpisode = {
      ...episode,
      nodes: [{ ...episode.nodes[0], image: 'opening-still.png' }, episode.nodes[1]],
    };

    render(<LoomPlayPanel
      loom={{ ...loom, format: 'teleplay', episodes: [producedEpisode] }}
      episode={producedEpisode}
      onClose={onClose}
    />);

    expect(screen.getByRole('region', { name: 'Scene media' })).toBeInTheDocument();
    expect(screen.getByRole('complementary', { name: 'Teleplay' })).toHaveTextContent('You stand before it.');
    expect(screen.getByRole('img', { name: 'The Gate' })).toHaveClass('h-full', 'w-full', 'object-contain');
    expect(screen.getByLabelText('Preview stage')).toHaveValue('auto');

    fireEvent.click(screen.getByRole('button', { name: 'Close player' }));
    expect(onClose).toHaveBeenCalledTimes(1);
  });

  it('renders the opening scene with intent hint chips', () => {
    render(<LoomPlayPanel loom={loom} episode={episode} />);
    expect(screen.getByText('You stand before it.')).toBeInTheDocument();
    expect(screen.getByRole('button', { name: 'Take path: enter the gate' })).toBeInTheDocument();
  });

  it('keeps the scene description and dialogue visible in storyboard image mode', async () => {
    const user = userEvent.setup();
    render(<LoomPlayPanel loom={loom} episode={episode} />);

    await user.selectOptions(screen.getByLabelText('Preview stage'), 'image');

    expect(screen.getByText('Scene description & dialogue')).toBeInTheDocument();
    expect(screen.getByText('You stand before it.')).toBeInTheDocument();
  });

  it('uses the best available medium as playback moves between scenes', async () => {
    const user = userEvent.setup();
    const mixedMediaEpisode = {
      id: 'ep-mixed-media',
      startNodeId: 'opening-video',
      nodes: [{
        id: 'opening-video',
        title: 'Video opening',
        prose: 'The episode begins in motion.',
        playbackMode: 'cut',
        videoHistoryId: 'video-opening',
        transitions: [{ id: 'to-still', targetNodeId: 'still-scene', intent: 'Continue' }],
      }],
    };
    playLoomTurn
      .mockResolvedValueOnce({
        action: 'move',
        narration: '',
        ended: false,
        node: {
          id: 'still-scene',
          title: 'Storyboard scene',
          prose: 'The story holds on a painted frame.',
          image: 'storyboard-scene.png',
          choices: [{ id: 'to-video', intent: 'Continue onward' }],
        },
      })
      .mockResolvedValueOnce({
        action: 'move',
        narration: '',
        ended: true,
        node: {
          id: 'closing-video',
          title: 'Video closing',
          prose: 'Motion returns for the ending.',
          videoHistoryId: 'video-closing',
          isEnding: true,
          choices: [],
        },
      });

    render(<LoomPlayPanel
      loom={{ ...loom, episodes: [mixedMediaEpisode] }}
      episode={mixedMediaEpisode}
    />);

    expect(screen.getByLabelText('Video opening')).toHaveAttribute(
      'src',
      expect.stringContaining('video-opening'),
    );
    fireEvent.ended(screen.getByLabelText('Video opening'));

    expect(await screen.findByRole('img', { name: 'Storyboard scene' })).toHaveAttribute(
      'src',
      expect.stringContaining('storyboard-scene.png'),
    );
    expect(screen.queryByText('No video rendered for this cut yet.')).not.toBeInTheDocument();

    await user.click(screen.getByRole('button', { name: 'Take path: Continue onward' }));

    expect(await screen.findByLabelText('Video closing')).toHaveAttribute(
      'src',
      expect.stringContaining('video-closing'),
    );
    expect(screen.getByLabelText('Preview stage')).toHaveValue('auto');
  });

  it('keeps a helper audience passive and follows the first canon path until the channel connects', async () => {
    const user = userEvent.setup();
    const helperEpisode = {
      id: 'ep-helper', startNodeId: 'opening', nodes: [{
        id: 'opening', title: 'Opening', prose: 'The protagonist walks alone.',
        playbackMode: 'cut', audienceConnection: 'disconnected',
        transitions: [
          { id: 'continue', targetNodeId: 'radio', intent: 'Continue' },
          { id: 'legacy-choice', targetNodeId: 'other', intent: 'Legacy choice' },
        ],
      }, {
        id: 'radio', title: 'Radio', prose: 'The radio crackles.',
        playbackMode: 'decision', audienceConnection: 'connected',
        transitions: [{ id: 'answer', targetNodeId: 'end', intent: 'warn them' }],
      }],
    };
    playLoomTurn.mockResolvedValue({
      action: 'move', resolvedBy: 'graph', narration: '', ended: false,
      node: {
        id: 'radio', title: 'Radio', prose: 'The radio crackles.',
        playbackMode: 'decision', audienceConnection: 'connected', choices: [],
      },
    });
    render(<LoomPlayPanel
      loom={{
        ...loom,
        participationMode: 'helper',
        audienceCommunicationMedium: 'the crystal radio',
        episodes: [helperEpisode],
      }}
      episode={helperEpisode}
    />);

    expect(screen.getByText(/Connection unavailable/)).toHaveTextContent('the crystal radio');
    expect(screen.queryByLabelText('Your action')).not.toBeInTheDocument();
    expect(screen.queryByRole('button', { name: 'Take path: Continue' })).not.toBeInTheDocument();
    expect(screen.getByRole('button', { name: 'Next cut' })).toBeEnabled();
    await user.click(screen.getByRole('button', { name: 'Next cut' }));
    await waitFor(() => expect(playLoomTurn).toHaveBeenCalledWith(
      'loom-1', 'ep-helper', expect.objectContaining({ transitionId: 'continue' }), { silent: true },
    ));
  });

  it('sends a tapped path as a transition id, not as free text to match', async () => {
    const user = userEvent.setup();
    playLoomTurn.mockResolvedValue({
      action: 'move',
      resolvedBy: 'choice',
      narration: '',
      ended: false,
      node: { id: 'n2', title: 'Inside', prose: 'Torchlight.', isEnding: false, choices: [{ id: 't2', intent: 'retreat' }] },
    });
    render(<LoomPlayPanel loom={loom} episode={episode} />);
    await user.click(screen.getByRole('button', { name: 'Take path: enter the gate' }));

    await waitFor(() => expect(playLoomTurn).toHaveBeenCalledTimes(1));
    const [, , payload] = playLoomTurn.mock.calls[0];
    // No `message`: nothing for the play stage to match, so it never runs.
    expect(payload).toMatchObject({ nodeId: 'n1', transitionId: 't1' });
    expect(payload.message).toBeUndefined();
    // The reader's choice reads back in the transcript, and the scene advanced.
    expect(screen.getByText('enter the gate')).toBeInTheDocument();
    expect(screen.getByText('You stand before it.')).toBeInTheDocument();
    expect(screen.getByText('Torchlight.')).toBeInTheDocument();
    await waitFor(() => expect(screen.getByRole('button', { name: 'Take path: retreat' })).toBeInTheDocument());
  });

  it('sends typed text as a message for the play stage to match', async () => {
    const user = userEvent.setup();
    playLoomTurn.mockResolvedValue({ action: 'stay', narration: 'You hesitate.', ended: false });
    render(<LoomPlayPanel loom={loom} episode={episode} />);

    await sendMessage(user, 'look at the lock');
    await waitFor(() => expect(playLoomTurn).toHaveBeenCalledTimes(1));
    const [, , payload] = playLoomTurn.mock.calls[0];
    expect(payload).toMatchObject({ message: 'look at the lock' });
    expect(payload.transitionId).toBeUndefined();
  });

  it('renders teleplay scenes monospaced', () => {
    render(<LoomPlayPanel loom={{ ...loom, format: 'teleplay' }} episode={episode} />);
    expect(screen.getByText('You stand before it.').className).toContain('font-mono');
  });

  it('sends only reader/narrator turns in the transcript after a scene move', async () => {
    const user = userEvent.setup();
    playLoomTurn
      .mockResolvedValueOnce({
        action: 'move',
        narration: 'You step through.',
        ended: false,
        node: { id: 'n2', title: 'Inside', prose: 'Torchlight.', isEnding: false, choices: [{ id: 't2', intent: 'retreat' }] },
      })
      .mockResolvedValueOnce({ action: 'stay', narration: 'You hesitate.', ended: false });

    render(<LoomPlayPanel loom={loom} episode={episode} />);
    await sendMessage(user, 'go in');
    await waitFor(() => expect(screen.getByText('You step through.')).toBeInTheDocument());

    // Second turn: the transcript state now holds a scene card — the payload
    // must contain only reader/narrator text turns or the API rejects it.
    await sendMessage(user, 'look around');
    await waitFor(() => expect(playLoomTurn).toHaveBeenCalledTimes(2));
    const [, , payload] = playLoomTurn.mock.calls[1];
    expect(payload.nodeId).toBe('n2');
    // Length first: `every()` is vacuously true on an empty array, so a filter
    // that dropped EVERY turn would pass the role/text assertions below.
    expect(payload.transcript).toHaveLength(3);
    expect(payload.transcript).toEqual([
      { role: 'reader', text: 'go in' },
      { role: 'narrator', text: 'You step through.' },
      { role: 'reader', text: 'look around' },
    ]);
  });

  it('never leaves a turn silent when the server moves nowhere and says nothing', async () => {
    const user = userEvent.setup();
    // A path whose target scene the author deleted: the edge is deliberately
    // kept on the graph, and the server answers stay-with-no-narration.
    playLoomTurn.mockResolvedValue({ action: 'stay', narration: '', ended: false });
    render(<LoomPlayPanel loom={loom} episode={episode} />);
    await user.click(screen.getByRole('button', { name: 'Take path: enter the gate' }));

    await waitFor(() => expect(playLoomTurn).toHaveBeenCalledTimes(1));
    await waitFor(() => expect(screen.getByText('Nothing comes of it.')).toBeInTheDocument());
  });

  it('auto-advances a rendered cut when its video ends', async () => {
    const cutEpisode = {
      id: 'ep-cut', number: 1, title: 'Pilot', startNodeId: 'cut-1', nodes: [{
        id: 'cut-1', title: 'Setup', prose: 'A door opens.', playbackMode: 'cut', videoHistoryId: 'video-1',
        transitions: [{ id: 'continue-1', targetNodeId: 'decision-1', intent: 'Continue' }],
      }, {
        id: 'decision-1', title: 'Wait', prose: 'A guard paces.', playbackMode: 'decision', transitions: [],
      }],
    };
    playLoomTurn.mockResolvedValue({
      action: 'move', narration: '', ended: true,
      node: { id: 'decision-1', title: 'Wait', prose: 'A guard paces.', playbackMode: 'decision', choices: [] },
    });
    const user = userEvent.setup();
    render(<LoomPlayPanel loom={{ ...loom, episodes: [cutEpisode] }} episode={cutEpisode} />);
    await user.selectOptions(screen.getByLabelText('Preview stage'), 'video');

    expect(screen.getByRole('button', { name: 'Video advances automatically' })).toBeDisabled();

    fireEvent.ended(screen.getByLabelText('Setup'));

    await waitFor(() => expect(playLoomTurn).toHaveBeenCalledWith(
      'loom-1', 'ep-cut', expect.objectContaining({ nodeId: 'cut-1', transitionId: 'continue-1' }), { silent: true },
    ));
    await waitFor(() => expect(screen.getAllByText('Wait')).not.toHaveLength(0));
    expect(screen.getByText('A door opens.')).toBeInTheDocument();
    expect(screen.getByText('A guard paces.')).toBeInTheDocument();
  });

  it('loops rendered decision video while waiting for input', async () => {
    const decisionEpisode = {
      id: 'ep-loop', number: 1, title: 'Pilot', startNodeId: 'decision-1', nodes: [{
        id: 'decision-1', title: 'Guard patrol', prose: 'A guard paces.', playbackMode: 'decision', videoHistoryId: 'video-loop',
        transitions: [{ id: 'go', targetNodeId: 'end', intent: 'cross now' }],
      }, { id: 'end', title: 'Across', isEnding: true, transitions: [] }],
    };
    const user = userEvent.setup();
    render(<LoomPlayPanel loom={{ ...loom, episodes: [decisionEpisode] }} episode={decisionEpisode} />);
    await user.selectOptions(screen.getByLabelText('Preview stage'), 'video');

    expect(screen.getByLabelText('Guard patrol')).toHaveProperty('loop', true);
    expect(screen.getByRole('button', { name: 'Take path: cross now' })).toBeInTheDocument();
  });

  it('restores manual cut controls when a rendered video cannot load', async () => {
    const cutEpisode = {
      id: 'ep-cut', startNodeId: 'cut-1', nodes: [{
        id: 'cut-1', title: 'Setup', prose: 'A door opens.', playbackMode: 'cut', videoHistoryId: 'missing-video',
        transitions: [{ id: 'continue-1', targetNodeId: 'next', intent: 'Continue' }],
      }, { id: 'next', isEnding: true, transitions: [] }],
    };
    const user = userEvent.setup();
    render(<LoomPlayPanel loom={{ ...loom, episodes: [cutEpisode] }} episode={cutEpisode} />);
    await user.selectOptions(screen.getByLabelText('Preview stage'), 'video');

    fireEvent.error(screen.getByLabelText('Setup'));

    expect(await screen.findByText('The rendered video is unavailable; advance manually or retry after rendering.')).toBeInTheDocument();
    expect(screen.getByRole('button', { name: 'Next cut' })).toBeEnabled();
    expect(screen.getByRole('button', { name: 'Restart' })).toBeEnabled();
  });

  it('shows "not rendered yet" rather than "unavailable" for a scene that was never rendered', async () => {
    // Regression: failedVideoId starts at null and the server also sends
    // videoHistoryId: null for an unrendered scene, so a naive
    // `failedVideoId === scene.videoHistoryId` comparison was `null === null`
    // -> true, mislabeling every never-rendered scene as a failed render.
    const cutEpisode = {
      id: 'ep-cut-none', startNodeId: 'cut-1', nodes: [{
        id: 'cut-1', title: 'Setup', prose: 'A door opens.', playbackMode: 'cut', videoHistoryId: null,
        transitions: [{ id: 'continue-1', targetNodeId: 'next', intent: 'Continue' }],
      }, { id: 'next', isEnding: true, transitions: [] }],
    };
    const user = userEvent.setup();
    render(<LoomPlayPanel loom={{ ...loom, episodes: [cutEpisode] }} episode={cutEpisode} />);
    await user.selectOptions(screen.getByLabelText('Preview stage'), 'video');

    expect(screen.getByText('No video rendered for this cut yet.')).toBeInTheDocument();
    expect(screen.queryByText('The rendered video is unavailable; advance manually or retry after rendering.')).not.toBeInTheDocument();
  });

  it('continues to the next playable episode and resets the transcript', async () => {
    const first = {
      id: 'ep-1', number: 1, title: 'One', startNodeId: 'end-1',
      nodes: [{ id: 'end-1', title: 'First ending', prose: 'Episode one ends.', isEnding: true, transitions: [] }],
    };
    const second = {
      id: 'ep-2', number: 2, title: 'Two', startNodeId: 'start-2',
      nodes: [{ id: 'start-2', title: 'Second opening', prose: 'Episode two begins.', transitions: [{ id: 'wait', targetNodeId: 'start-2', intent: 'wait' }] }],
    };
    const user = userEvent.setup();
    render(<LoomPlayPanel loom={{ ...loom, episodes: [first, second] }} episode={first} />);

    await user.click(screen.getByRole('button', { name: 'Next: Episode 2' }));

    expect(await screen.findByText('Episode two begins.')).toBeInTheDocument();
    expect(screen.queryByText('Episode one ends.')).not.toBeInTheDocument();
  });

  // #8112: without this, the server-side hosted session keeps resolving
  // audience turns against episode 1's graph after the host advances — every
  // turn throws "Scene not found" and silently falls back to canned
  // narration while the UI still claims the session is connected.
  it('re-binds the hosted session before syncing playback when advancing episodes during a hosted session', async () => {
    const user = userEvent.setup();
    const first = {
      id: 'ep-1', number: 1, title: 'One', startNodeId: 'end-1',
      nodes: [{ id: 'end-1', title: 'First ending', prose: 'Episode one ends.', isEnding: true, transitions: [] }],
    };
    const second = {
      id: 'ep-2', number: 2, title: 'Two', startNodeId: 'start-2',
      nodes: [{ id: 'start-2', title: 'Second opening', prose: 'Episode two begins.', transitions: [] }],
    };
    render(<LoomPlayPanel loom={{ ...loom, episodes: [first, second] }} episode={first} />);

    await user.click(screen.getByRole('button', { name: 'Host (QR)' }));
    await user.click(screen.getByRole('button', { name: 'Start hosted session' }));

    const hostedSocket = io.mock.results.at(-1).value;
    hostedSocket.emit.mockClear();

    await user.click(screen.getByRole('button', { name: 'Next: Episode 2' }));

    await waitFor(() => expect(hostedSocket.emit).toHaveBeenCalledWith(
      'hosted:episode:switch', { episodeId: 'ep-2' },
    ));

    const calls = hostedSocket.emit.mock.calls;
    const switchIndex = calls.findIndex(([event]) => event === 'hosted:episode:switch');
    const playbackIndex = calls.findIndex(
      ([event, payload]) => event === 'hosted:playback:update' && payload?.nodeId === 'start-2',
    );
    expect(switchIndex).toBeGreaterThanOrEqual(0);
    expect(playbackIndex).toBeGreaterThan(switchIndex);
  });

  it('clears the hosted session and toasts naming the episode when the switch target fails preflight', async () => {
    const user = userEvent.setup();
    const first = {
      id: 'ep-1', number: 1, title: 'One', startNodeId: 'end-1',
      nodes: [{ id: 'end-1', title: 'First ending', prose: 'Episode one ends.', isEnding: true, transitions: [] }],
    };
    const second = {
      id: 'ep-2', number: 2, title: 'Two', startNodeId: 'start-2',
      nodes: [{ id: 'start-2', title: 'Second opening', prose: 'Episode two begins.', transitions: [] }],
    };
    render(<LoomPlayPanel loom={{ ...loom, episodes: [first, second] }} episode={first} />);

    await user.click(screen.getByRole('button', { name: 'Host (QR)' }));
    await user.click(screen.getByRole('button', { name: 'Start hosted session' }));

    await user.click(screen.getByRole('button', { name: 'Next: Episode 2' }));

    const hostedSocket = io.mock.results.at(-1).value;
    const endedHandler = hostedSocket.on.mock.calls.find(([event]) => event === 'hosted:session:ended')?.[1];
    act(() => { endedHandler({ sessionId: 'host-session', reason: 'episode_not_ready' }); });

    expect(await screen.findByRole('button', { name: 'Host (QR)' })).toBeInTheDocument();
    expect(toast.error).toHaveBeenCalledWith(expect.stringContaining('Episode 2'));
  });

  it('toasts hosted:error frames so the operator sees refused host actions', async () => {
    const user = userEvent.setup();
    render(<LoomPlayPanel loom={loom} episode={episode} />);

    await user.click(screen.getByRole('button', { name: 'Host (QR)' }));
    await user.click(screen.getByRole('button', { name: 'Start hosted session' }));

    const hostedSocket = io.mock.results.at(-1).value;
    const errorHandler = hostedSocket.on.mock.calls.find(([event]) => event === 'hosted:error')?.[1];
    act(() => { errorHandler({ code: 'NODE_NOT_IN_EPISODE', message: 'node is not in this episode' }); });

    expect(toast.error).toHaveBeenCalledWith('node is not in this episode');
  });

  // #8112: the server rejects a second hosted:episode:switch sent while an
  // earlier one is still in flight (409 EPISODE_SWITCH_IN_PROGRESS). A host
  // who advances past the in-flight target before it settles would otherwise
  // strand the hosted session on that earlier, now-stale episode with no
  // retry. Every hosted:session:sync — including the one the in-flight
  // switch itself broadcasts on commit — names the server's current
  // episode; a mismatch against what the host is showing now must trigger a
  // corrective re-request.
  it('re-requests the current episode when session:sync settles on a stale target', async () => {
    const user = userEvent.setup();
    const first = {
      id: 'ep-1', number: 1, title: 'One', startNodeId: 'end-1',
      nodes: [{ id: 'end-1', title: 'First ending', prose: 'Episode one ends.', isEnding: true, transitions: [] }],
    };
    const second = {
      id: 'ep-2', number: 2, title: 'Two', startNodeId: 'end-2',
      nodes: [{ id: 'end-2', title: 'Second ending', prose: 'Episode two ends too.', isEnding: true, transitions: [] }],
    };
    const third = {
      id: 'ep-3', number: 3, title: 'Three', startNodeId: 'start-3',
      nodes: [{ id: 'start-3', title: 'Third opening', prose: 'Episode three begins.', transitions: [] }],
    };
    render(<LoomPlayPanel loom={{ ...loom, episodes: [first, second, third] }} episode={first} />);

    await user.click(screen.getByRole('button', { name: 'Host (QR)' }));
    await user.click(screen.getByRole('button', { name: 'Start hosted session' }));

    await user.click(screen.getByRole('button', { name: 'Next: Episode 2' }));
    await user.click(screen.getByRole('button', { name: 'Next: Episode 3' }));

    const hostedSocket = io.mock.results.at(-1).value;
    hostedSocket.emit.mockClear();

    // The in-flight switch for episode 2 finally commits and broadcasts its
    // own sync — stale, because the host is already showing episode 3.
    const syncHandler = hostedSocket.on.mock.calls.find(([event]) => event === 'hosted:session:sync')?.[1];
    act(() => { syncHandler({ episodeId: 'ep-2' }); });

    expect(hostedSocket.emit).toHaveBeenCalledWith('hosted:episode:switch', { episodeId: 'ep-3' });
  });

  it('shows an authored overnight voicemail at an episode boundary', async () => {
    const first = {
      id: 'ep-1', number: 1, title: 'One', startNodeId: 'end-1',
      nodes: [{ id: 'end-1', title: 'First ending', prose: 'Episode one ends.', isEnding: true, transitions: [] }],
    };
    const second = {
      id: 'ep-2', number: 2, title: 'Two', startNodeId: 'start-2',
      nodes: [{ id: 'start-2', title: 'Second opening', prose: 'Episode two begins.', transitions: [] }],
    };
    render(<LoomPlayPanel
      loom={{
        ...loom,
        episodes: [first, second],
        seriesPlan: {
          deliveryOptions: { overnightVoicemails: true, nextSeasonTeaser: false },
          interEpisodeVoicemails: [{
            id: 'vm-1', fromEpisodeId: 'ep-1', toEpisodeId: 'ep-2',
            title: 'A voice in the dark', transcript: 'Don’t let the signal go cold.',
          }],
        },
      }}
      episode={first}
    />);

    expect(screen.getByRole('region', { name: 'Overnight voicemail · Episode 1 → Episode 2' })).toHaveTextContent(
      'Don’t let the signal go cold.',
    );
  });

  it('shows a configured next-season teaser after the final ending', () => {
    const finale = {
      id: 'ep-final', number: 3, title: 'Finale', startNodeId: 'end-final',
      nodes: [{ id: 'end-final', title: 'Final ending', prose: 'The season closes.', isEnding: true, transitions: [] }],
    };
    render(<LoomPlayPanel
      loom={{
        ...loom,
        episodes: [finale],
        seriesPlan: {
          deliveryOptions: { overnightVoicemails: false, nextSeasonTeaser: true },
          nextSeasonTeaser: {
            title: 'The answer beyond the relay',
            transcript: 'A second voice answers in her own voice.',
          },
        },
      }}
      episode={finale}
    />);

    expect(screen.getByRole('region', { name: 'Next-season teaser' })).toHaveTextContent(
      'A second voice answers in her own voice.',
    );
  });

  it('does not offer an unplayable empty episode', () => {
    const first = {
      id: 'ep-1', number: 1, startNodeId: 'end-1',
      nodes: [{ id: 'end-1', prose: 'Done.', isEnding: true, transitions: [] }],
    };
    const empty = { id: 'ep-2', number: 2, startNodeId: null, nodes: [] };

    render(<LoomPlayPanel loom={{ ...loom, episodes: [first, empty] }} episode={first} />);

    expect(screen.getByRole('button', { name: 'Play again' })).toBeInTheDocument();
    expect(screen.queryByRole('button', { name: 'Next: Episode 2' })).not.toBeInTheDocument();
  });

  it('rehearses entry clip, transitions to hold loop on ended, and displays live voice status', async () => {
    const user = userEvent.setup();
    const productionEpisode = {
      id: 'ep-prod', number: 1, title: 'Production Pilot', startNodeId: 'node-prod',
      nodes: [{
        id: 'node-prod',
        title: 'Courtyard',
        prose: 'You arrive at the courtyard.',
        playbackAssets: {
          entryVideoHistoryId: 'vid-entry-1',
          holdLoopVideoHistoryIds: ['vid-hold-1', 'vid-hold-2'],
          exitByTransition: { 'tr-gate': 'vid-exit-gate' },
          audioOccupancy: {
            'vid-hold-1': { durationMs: 6000, music: [{ startMs: 0, endMs: 6000 }] },
            'vid-hold-2': { durationMs: 6000, music: [{ startMs: 0, endMs: 6000 }] },
          },
        },
        interactionWindow: {
          enabled: true,
          protagonistCharacterId: 'char-maya',
          protagonistPresence: 'offscreen',
          ambientDuckDb: -10,
        },
        transitions: [{ id: 'tr-gate', targetNodeId: 'node-inside', intent: 'open the gate' }],
      }, {
        id: 'node-inside',
        title: 'Inside Sanctum',
        prose: 'Inside the quiet hall.',
        isEnding: true,
        transitions: [],
      }],
    };

    render(<LoomPlayPanel loom={{ ...loom, episodes: [productionEpisode] }} episode={productionEpisode} />);
    await user.selectOptions(screen.getByLabelText('Preview stage'), 'video');

    // Initially plays entry clip
    const video = screen.getByLabelText('Courtyard');
    expect(video.getAttribute('src')).toContain('vid-entry-1');

    // When entry video ends, advances to hold loop
    fireEvent.ended(video);

    // Now playing hold loop vid-hold-1 and live voice status is displayed
    await waitFor(() => {
      const updatedVideo = screen.getByLabelText('Courtyard');
      expect(updatedVideo.getAttribute('src')).toContain('vid-hold-1');
    });

    expect(screen.getByText('Off-screen voice window open')).toBeInTheDocument();
    expect(screen.getByText(/Ambience ducked -10 dB/)).toBeInTheDocument();

    // Loop ended again -> rotates to vid-hold-2
    fireEvent.ended(screen.getByLabelText('Courtyard'));
    await waitFor(() => {
      const rotatedVideo = screen.getByLabelText('Courtyard');
      expect(rotatedVideo.getAttribute('src')).toContain('vid-hold-2');
    });

    // Tap path 'open the gate' -> starts exit clip vid-exit-gate
    playLoomTurn.mockResolvedValue({
      action: 'move', narration: '', ended: true,
      node: { id: 'node-inside', title: 'Inside Sanctum', prose: 'Inside the quiet hall.', isEnding: true, choices: [] },
    });

    await user.click(screen.getByRole('button', { name: 'Take path: open the gate' }));

    // Rehearses exit clip before sending turn
    await waitFor(() => {
      const exitVideo = screen.getByLabelText('Courtyard');
      expect(exitVideo.getAttribute('src')).toContain('vid-exit-gate');
    });

    // Exit video ends -> finishes turn and enters next node
    fireEvent.ended(screen.getByLabelText('Courtyard'));

    await waitFor(() => expect(playLoomTurn).toHaveBeenCalledWith(
      'loom-1', 'ep-prod', expect.objectContaining({ transitionId: 'tr-gate' }), { silent: true },
    ));
    await waitFor(() => expect(screen.getAllByText('Ending')).not.toHaveLength(0));
  });

  it('shows rehearsal details with inspector drawer', async () => {
    const user = userEvent.setup();
    const episodeWithOccupancy = {
      id: 'ep-occ', number: 1, title: 'Occupancy', startNodeId: 'node-1',
      nodes: [{
        id: 'node-1',
        title: 'Hall',
        prose: 'A quiet hall.',
        playbackAssets: {
          entryVideoHistoryId: 'vid-entry-hall',
          audioOccupancy: {
            'vid-entry-hall': { durationMs: 4000, safeForLiveVoice: true },
          },
        },
        interactionWindow: { enabled: true, ambientDuckDb: -8 },
        transitions: [],
      }],
    };

    render(<LoomPlayPanel loom={{ ...loom, episodes: [episodeWithOccupancy] }} episode={episodeWithOccupancy} />);
    await user.click(screen.getByRole('button', { name: 'Rehearsal details' }));

    expect(screen.getByRole('region', { name: 'Playback rehearsal' })).toBeInTheDocument();
    expect(screen.getByText(/Duck level: -8 dB/)).toBeInTheDocument();
    expect(screen.getByText(/Asset: vid-entry-hall/)).toBeInTheDocument();
  });

  it('reports host playback completion and failure for the designated output only', async () => {
    const user = userEvent.setup();
    const listeners = {};
    const socket = {
      on: vi.fn((event, fn) => { listeners[event] = fn; }),
      emit: vi.fn(),
      disconnect: vi.fn(),
    };
    io.mockImplementation(() => socket);
    const play = vi.spyOn(HTMLMediaElement.prototype, 'play').mockResolvedValue(undefined);
    const { unmount } = render(<LoomPlayPanel loom={loom} episode={episode} />);

    await user.click(screen.getByRole('button', { name: 'Host (QR)' }));
    await user.click(screen.getByRole('button', { name: 'Start hosted session' }));
    const audio = document.querySelector('audio');
    const turnId = '22222222-2222-4222-8222-222222222222';

    await act(async () => {
      listeners['hosted:turn:tts']({
        target: 'audience', audio: 'QQ==', mimeType: 'audio/wav', turnId: '11111111-1111-4111-8111-111111111111',
      });
    });
    expect(socket.emit).not.toHaveBeenCalledWith('hosted:speech:done', expect.anything());

    await act(async () => {
      listeners['hosted:turn:tts']({ target: 'host', audio: 'QQ==', mimeType: 'audio/wav', turnId });
      await Promise.resolve();
    });
    expect(play).toHaveBeenCalled();
    await act(async () => { audio.onended(); });
    expect(socket.emit).toHaveBeenCalledWith('hosted:speech:done', { turnId, status: 'ended' });

    play.mockRejectedValueOnce(new Error('blocked'));
    const failedTurn = '33333333-3333-4333-8333-333333333333';
    await act(async () => {
      listeners['hosted:turn:tts']({ target: 'host', audio: 'QQ==', mimeType: 'audio/wav', turnId: failedTurn });
      await Promise.resolve();
    });
    expect(socket.emit).toHaveBeenCalledWith('hosted:speech:done', { turnId: failedTurn, status: 'failed' });

    const droppedTurn = '44444444-4444-4444-8444-444444444444';
    play.mockResolvedValue(undefined);
    const pause = vi.spyOn(HTMLMediaElement.prototype, 'pause').mockImplementation(() => {});
    await act(async () => {
      listeners['hosted:turn:tts']({ target: 'host', audio: 'QQ==', mimeType: 'audio/wav', turnId: droppedTurn });
    });
    await act(async () => {
      listeners['hosted:turn:phase']({ phase: 'idle', turnId, reason: 'episode_switch' });
    });
    expect(socket.emit).not.toHaveBeenCalledWith('hosted:speech:done', { turnId: droppedTurn, status: 'aborted' });
    await act(async () => {
      listeners['hosted:turn:phase']({ phase: 'idle', turnId: droppedTurn, reason: 'speech_timeout' });
    });
    expect(pause).toHaveBeenCalled();
    expect(socket.emit).toHaveBeenCalledWith('hosted:speech:done', { turnId: droppedTurn, status: 'aborted' });

    pause.mockRestore();
    play.mockRestore();
    unmount();
    io.mockImplementation(() => ({ on: vi.fn(), emit: vi.fn(), disconnect: vi.fn() }));
  });
});