Skip to content

armgit / avibe

Public Imported from GitHub
0

Make your computer a home for your AI partner. Avibe is the local-first Agent OS: Vibey, its built-in AI partner, works out of the box, and Claude Code, Codex & OpenCode run unchanged beside it — reach them from your browser, phone, or any chat app.

45.4 KB
Raw Blame
/* @vitest-environment jsdom */

import { createInstance } from 'i18next';
import type { ReactElement } from 'react';
import { renderToStaticMarkup } from 'react-dom/server';
import { fireEvent, render as renderDom } from '@testing-library/react';
import { I18nextProvider, initReactI18next } from 'react-i18next';
import { MemoryRouter } from 'react-router-dom';
import { describe, expect, it } from 'vitest';

import en from '../../i18n/en.json';
import zh from '../../i18n/zh.json';
import type {
  ApiContextType,
  HarnessRun,
  HarnessSessionSummary,
  HarnessTask,
  HarnessWatch,
  VibeAgentBrief,
} from '../../context/ApiContext';
import {
  DetailSession,
  HealthBadge,
  ProcessingHealthBadge,
  RunDetail,
  RunTriggerChip,
  TaskDetail,
  TaskKindBadge,
  WatchDetail,
} from './HarnessPage';
import { agentDisplayName, loadHarnessAgentCatalog } from './harnessAgents';
import { harnessEmptyStateKey, harnessTabFromParam } from './harnessTabs';
import { RUN_TYPES, harnessSessionState, runRowTitle, runStatusLabel, runTypeLabel, runTypeOptions } from './harnessRuns';

const i18n = createInstance();
void i18n.use(initReactI18next).init({
  lng: 'en',
  fallbackLng: 'en',
  resources: { en: { translation: en } },
  interpolation: { escapeValue: false },
});

const render = (ui: ReactElement) =>
  renderToStaticMarkup(
    <I18nextProvider i18n={i18n}>
      <MemoryRouter>{ui}</MemoryRouter>
    </I18nextProvider>,
  );

const NO_SESSION: HarnessSessionSummary = {
  session_title: null,
  session_platform: null,
  session_scope_kind: null,
  session_label: null,
  session_is_workbench: false,
  session_openable: false,
};

const run = (overrides: Partial<HarnessRun>): HarnessRun =>
  ({
    id: 'run-1',
    status: 'succeeded',
    run_type: 'agent_run',
    message: null,
    prompt: null,
    definition_id: null,
    definition_name: null,
    definition_kind: null,
    definition_deleted: false,
    session_id: null,
    callback_session: null,
    ...NO_SESSION,
    ...overrides,
  }) as HarnessRun;

const watch = (overrides: Partial<HarnessWatch>): HarnessWatch => ({
  id: 'watch-1',
  name: 'CI watcher',
  agent_name: null,
  session_policy: null,
  session_id: null,
  session_key: '',
  command: [],
  shell_command: 'true',
  prefix: null,
  message: null,
  message_payload: null,
  cwd: null,
  mode: 'once',
  timeout_seconds: 0,
  lifetime_timeout_seconds: 0,
  retry_exit_codes: [],
  retry_delay_seconds: 0,
  post_to: null,
  deliver_key: null,
  enabled: false,
  created_at: null,
  updated_at: null,
  last_started_at: null,
  last_finished_at: null,
  retired_at: null,
  last_event_at: null,
  last_error: null,
  last_exit_code: null,
  metadata: {},
  lifecycle_state: 'paused',
  lifecycle_detail: null,
  next_run_at: null,
  waiting_since: null,
  running_since: null,
  runtime: { running: false },
  process_alive: null,
  ...NO_SESSION,
  ...overrides,
});

// A scheduled task as ``/api/harness/tasks`` serves it: the raw store row, so
// ``metadata`` arrives already decoded and the command columns are
// None-preserving. Defaults describe a *message* task, which is what almost
// every stored row is; a command case overrides shell_command/command.
const task = (overrides: Partial<HarnessTask>): HarnessTask =>
  ({
    id: 'task-1',
    name: null,
    agent_name: null,
    session_policy: null,
    session_id: null,
    session_key: '',
    prompt: '',
    message: '',
    message_payload: null,
    schedule_type: 'cron',
    cron: '17 3 * * *',
    run_at: null,
    timezone: 'UTC',
    post_to: null,
    deliver_key: null,
    enabled: true,
    created_at: null,
    updated_at: null,
    last_run_at: null,
    last_run_id: null,
    last_error: null,
    lifecycle_state: 'waiting',
    lifecycle_detail: null,
    next_run_at: null,
    waiting_since: null,
    running_since: null,
    health: null,
    consecutive_failures: 0,
    recent_failures: 0,
    ...NO_SESSION,
    ...overrides,
  }) as HarnessTask;

// Translation-free stand-in: proves the mappers pick the right key without
// depending on the copy.
const key = (k: string) => k;

describe('harnessTabFromParam', () => {
  it('opens the tab a link names', () => {
    expect(harnessTabFromParam('watches')).toBe('watches');
    expect(harnessTabFromParam('runs')).toBe('runs');
  });

  it('lands a link to a tab that no longer exists on Tasks', () => {
    // ?tab=webhooks is still in bookmarks and in old chat messages. It must
    // open a real tab, not a page with nothing lit.
    expect(harnessTabFromParam('webhooks')).toBe('tasks');
    expect(harnessTabFromParam('')).toBe('tasks');
    expect(harnessTabFromParam(null)).toBe('tasks');
  });
});

describe('harnessEmptyStateKey', () => {
  it.each([
    ['tasks', 'harness.emptyTasks', 'harness.noTaskMatches'],
    ['watches', 'harness.emptyWatches', 'harness.noWatchMatches'],
    ['runs', 'harness.emptyRuns', 'harness.noRunMatches'],
  ] as const)('distinguishes an empty store from an empty %s view', (kind, emptyKey, filteredKey) => {
    expect(harnessEmptyStateKey(kind, false)).toBe(emptyKey);
    expect(harnessEmptyStateKey(kind, true)).toBe(filteredKey);
  });
});

describe('loadHarnessAgentCatalog', () => {
  it('bypasses the read cache and indexes a newly archived Agent by its internal name', async () => {
    const archived = {
      id: 'agent-pm',
      name: '_pm-a1b2',
      display_name: 'pm',
      description: null,
      backend: 'codex',
      model: null,
      reasoning_effort: null,
      enabled: false,
      archived: true,
      archived_at: '2026-07-31T21:00:00Z',
      source: 'custom',
      updated_at: '2026-07-31T21:00:00Z',
    } satisfies VibeAgentBrief;
    let params: Parameters<ApiContextType['listVibeAgents']>[0] = undefined;

    const catalog = await loadHarnessAgentCatalog({
      listVibeAgents: async (nextParams) => {
        params = nextParams;
        return { ok: true, agents: [archived], default_agent_name: 'codex' };
      },
    });

    expect(params).toEqual({ includeDisabled: true, includeArchived: true, cache: false });
    expect(catalog).toEqual({ '_pm-a1b2': archived });
  });
});

describe('runRowTitle', () => {
  it("uses the message's first non-empty line, whitespace collapsed", () => {
    expect(
      runRowTitle({ message: '\n\n  Summarize   yesterday\'s PRs  \nmore detail', prompt: null, definition_name: 'Digest' }, 'Agent run'),
    ).toBe("Summarize yesterday's PRs");
  });

  it('falls back to prompt when the run carries no message', () => {
    expect(runRowTitle({ message: null, prompt: 'check CI', definition_name: 'Digest' }, 'Agent run')).toBe('check CI');
  });

  it('falls back to the originating definition name when there is no text', () => {
    expect(runRowTitle({ message: '   \n  ', prompt: null, definition_name: 'Nightly digest' }, 'Agent run')).toBe(
      'Nightly digest',
    );
  });

  it('falls back to the run-type label when nothing else names the run', () => {
    expect(runRowTitle({ message: null, prompt: null, definition_name: null }, 'Watcher heartbeat')).toBe(
      'Watcher heartbeat',
    );
  });

  it('never slices the title — truncation is the row layout\'s job', () => {
    const long = 'x'.repeat(400);
    expect(runRowTitle({ message: long, prompt: null, definition_name: null }, 'Agent run')).toBe(long);
  });
});

describe('RunDetail title', () => {
  it('bounds a pathological title while preserving the complete message', () => {
    const message = `Fix the parser regression.\\n\\n${'Preserve every trailing constraint. '.repeat(40)}`;
    const html = render(<RunDetail run={run({ message })} />);

    expect(html).toContain('line-clamp-2');
    expect(html).toContain('break-words');
    expect(html).toContain(`title="${message.trim()}"`);
    expect(html).toContain(message);
  });

  it('uses the archived Agent display name instead of its routing name', () => {
    const agent: VibeAgentBrief = {
      id: 'agent-pm',
      name: '_pm-8dd7',
      display_name: 'pm',
      description: null,
      backend: 'codex',
      model: null,
      reasoning_effort: null,
      enabled: false,
      archived: true,
      archived_at: '2026-07-31T00:00:00Z',
      source: 'user',
      updated_at: '2026-07-31T00:00:00Z',
    };
    const html = render(<RunDetail run={run({ agent_name: agent.name })} agent={agent} />);

    expect(agentDisplayName(agent.name, agent)).toBe('pm');
    expect(html).toContain('>pm<');
    expect(html).not.toContain('_pm-8dd7');
  });
});

describe('runTypeLabel / runStatusLabel', () => {
  it('maps known internal values to translation keys', () => {
    expect(runTypeLabel('hook_send', key)).toBe('harness.runType.hook_send');
    expect(runTypeLabel(null, key)).toBe('harness.runType.unknown');
    expect(runStatusLabel('canceled', key)).toBe('harness.runStatus.canceled');
  });

  it('shows an unknown value rather than blanking it out', () => {
    expect(runTypeLabel('brand_new_kind', key)).toBe('brand_new_kind');
    expect(runStatusLabel('weird', key)).toBe('weird');
  });

  it('never leaks a raw run_type into user copy for the types the store writes', () => {
    // Driven off RUN_TYPES rather than a copy of it, so declaring a type
    // without translating it fails here.
    for (const value of RUN_TYPES) {
      expect(i18n.t(`harness.runType.${value}`)).not.toBe(`harness.runType.${value}`);
    }
  });
});

describe('runTypeOptions', () => {
  it('offers a type the ledger holds that the UI has no name for', () => {
    // Otherwise the row shows under All and no filter can isolate it: search
    // skips run_type on purpose, so the selector is the only way in.
    expect(runTypeOptions(['agent_run', 'legacy_import'])).toContain('legacy_import');
    expect(runTypeLabel('legacy_import', key)).toBe('legacy_import');
  });

  it('keeps the declared types in their declared order, extras after', () => {
    const options = runTypeOptions(['zeta', 'alpha', 'agent_run']);
    expect(options.slice(0, RUN_TYPES.length)).toEqual([...RUN_TYPES]);
    expect(options.slice(RUN_TYPES.length)).toEqual(['alpha', 'zeta']);
  });

  it('never duplicates a type, and survives a server that omits the facet', () => {
    expect(runTypeOptions(['agent_run', 'dup', 'dup'])).toEqual([...RUN_TYPES, 'dup']);
    expect(runTypeOptions(undefined)).toEqual([...RUN_TYPES]);
  });

  it('lists webhook, which the scheduler writes and the importer preserves', () => {
    expect(runTypeOptions([])).toContain('webhook');
  });
});

describe('harnessSessionState', () => {
  it('reports workbench, im, deleted, and none distinctly', () => {
    expect(harnessSessionState({ ...NO_SESSION, session_is_workbench: true }, 'sess-1')).toBe('workbench');
    expect(harnessSessionState({ ...NO_SESSION, session_platform: 'slack' }, 'sess-1')).toBe('im');
    expect(harnessSessionState(NO_SESSION, 'sess-gone')).toBe('deleted');
    expect(harnessSessionState(NO_SESSION, null)).toBe('none');
  });
});

describe('DetailSession', () => {
  it('links a workbench session to its chat', () => {
    const html = render(
      <DetailSession
        summary={{
          ...NO_SESSION,
          session_is_workbench: true,
          session_openable: true,
          session_title: 'Weekly digest',
        }}
        sessionId="sess-1"
      />,
    );

    expect(html).toContain('href="/chat/sess-1"');
    expect(html).toContain('Weekly digest');
  });

  it('renders a deleted session as a label, not a link', () => {
    const html = render(<DetailSession summary={NO_SESSION} sessionId="sess-gone" />);

    expect(html).toContain('Session deleted');
    expect(html).toContain('sess-gone');
    expect(html).not.toContain('<a ');
    expect(html).not.toContain('/chat/');
  });

  // Linkability is one predicate now (plan §4.5): /chat/<id> serves an
  // IM-bound session exactly as it serves a workbench one, so the row that
  // names it also opens it. What stays IM-specific is only how it *reads* —
  // platform icon and channel label instead of a session title.
  it('links an IM session while still showing it as IM', () => {
    const html = render(
      <DetailSession
        summary={{ ...NO_SESSION, session_platform: 'slack', session_label: '#ops', session_openable: true }}
        sessionId="sess-2"
      />,
    );

    expect(html).toContain('#ops');
    expect(html).toContain('slack');
    expect(html).toContain('href="/chat/sess-2"');
  });

  it('names a session it must not link without pretending it is gone', () => {
    // The one thing /chat/<id> genuinely refuses: the legacy private_agent_run
    // pseudo-scope. The server clears session_openable; the label stays.
    const html = render(
      <DetailSession
        summary={{ ...NO_SESSION, session_is_workbench: true, session_title: 'Spawned run' }}
        sessionId="sess-3"
      />,
    );

    expect(html).toContain('Spawned run');
    expect(html).not.toContain('<a ');
  });

  it('says so when nothing is bound', () => {
    const html = render(<DetailSession summary={NO_SESSION} sessionId={null} />);

    expect(html).toContain('No bound session');
    expect(html).not.toContain('<a ');
  });
});

describe('HealthBadge', () => {
  // The projection contract: ``unknown`` means health could not be computed, and
  // must not read as a clean bill of health. Rendering it as nothing gave the
  // operator a spotless Harness list at exactly the moment the failure signal
  // was unavailable.
  it('names an unknown health instead of dropping it', () => {
    const label = i18n.t('harness.health.unknown');
    expect(label).not.toBe('harness.health.unknown');

    const html = render(<HealthBadge row={watch({ health: 'unknown', consecutive_failures: 3, recent_failures: 7 })} />);

    expect(html).toContain(`>${label}<`);
    // Muted, not pink or amber: a fault in the reporting path is not a verdict
    // that the definition itself is failing.
    expect(html).toContain('text-muted');
    expect(html).not.toContain('text-pink-ink');
    expect(html).not.toContain('text-amber');
  });

  it('shows no count on an unknown row', () => {
    // The counters come from the same unreadable history, so printing them
    // would put a number on a row whose runs could not be read at all.
    const label = i18n.t('harness.health.unknown');
    const html = render(<HealthBadge row={watch({ health: 'unknown', consecutive_failures: 3, recent_failures: 7 })} />);

    expect(html).toContain(`>${label}<`);
    expect(html).not.toContain(`${label} 3`);
    expect(html).not.toContain(`${label} 7`);
  });

  it('renders nothing for a healthy row', () => {
    // A badge on every passing row is noise; silence here is what makes the
    // unknown badge above worth looking at.
    expect(render(<HealthBadge row={watch({ health: 'healthy', consecutive_failures: 0, recent_failures: 0 })} />)).toBe('');
  });

  it('still counts a failing row', () => {
    const html = render(<HealthBadge row={watch({ health: 'failing', consecutive_failures: 4, recent_failures: 4 })} />);

    expect(html).toContain(`>${i18n.t('harness.health.failing')} 4<`);
    expect(html).toContain('text-pink-ink');
  });

  it('never exposes raw stderr through the default badge tooltip', () => {
    const raw = 'database password rejected';
    const html = render(
      <HealthBadge
        row={watch({ health: 'failing', consecutive_failures: 1, recent_failures: 1, last_error: raw })}
      />,
    );

    expect(html).toContain(`title="${en.harness.failure.generic}"`);
    expect(html).not.toContain(raw);
  });

  it('does not turn a circuit-breaker pause into an ordinary failure tooltip', () => {
    const html = render(
      <HealthBadge
        row={watch({
          health: 'failing',
          consecutive_failures: 1,
          recent_failures: 1,
          lifecycle_state: 'paused',
          metadata: { watch_circuit_breaker: { status: 'tripped' } },
          last_exit_code: 0,
          last_error: 'localized circuit pause detail',
        })}
      />,
    );

    expect(html).not.toContain(`title="${en.harness.failure.generic}"`);
    expect(html).not.toContain('localized circuit pause detail');
  });
});

describe('ProcessingHealthBadge', () => {
  it('names a failed Watch follow-up without reporting the waiter as failed', () => {
    const row = watch({
      health: 'healthy',
      consecutive_failures: 0,
      recent_failures: 0,
      processing_health: 'failing',
      processing_consecutive_failures: 2,
      processing_recent_failures: 2,
    });

    expect(render(<HealthBadge row={row} />)).toBe('');
    const html = render(<ProcessingHealthBadge row={row} />);
    expect(html).toContain(`>${i18n.t('harness.processingHealth.failing')} 2<`);
    expect(html).toContain('text-pink-ink');
  });

  it('stays quiet when event processing is healthy', () => {
    expect(render(<ProcessingHealthBadge row={watch({ processing_health: 'healthy' })} />)).toBe('');
  });

  it('surfaces an unreadable downstream verdict without inventing a count', () => {
    const html = render(<ProcessingHealthBadge row={watch({ processing_health: 'unknown' })} />);
    expect(html).toContain(`>${i18n.t('harness.processingHealth.unknown')}<`);
    expect(html).toContain('text-muted');
  });
});

describe('TaskKindBadge', () => {
  it('marks a command task, so the list distinguishes it from a message task', () => {
    const label = i18n.t('harness.taskKind.command');
    expect(label).not.toBe('harness.taskKind.command');

    const html = render(<TaskKindBadge row={task({ shell_command: 'make test' })} kind="task" />);

    expect(html).toContain(`>${label}<`);
    // Same chip anatomy as the schedule chip beside it.
    expect(html).toContain('font-mono text-[9px] uppercase');
    // The full command is one hover away even though the chip is two words.
    expect(html).toContain('title="make test"');
  });

  it('marks an argv command task too', () => {
    const html = render(<TaskKindBadge row={task({ command: ['python3', 'scripts/health.py'] })} kind="task" />);

    expect(html).toContain(`>${i18n.t('harness.taskKind.command')}<`);
    expect(html).toContain('title="python3 scripts/health.py"');
  });

  it('renders nothing for a message task', () => {
    // Silence is the signal: a chip on every one of the overwhelming majority
    // is noise, and it is what makes the command chip worth reading.
    expect(render(<TaskKindBadge row={task({ prompt: 'Summarize #ops', message: 'Summarize #ops' })} kind="task" />)).toBe('');
    expect(render(<TaskKindBadge row={task({ command: [] })} kind="task" />)).toBe('');
  });

  it('renders nothing for a watch, which always runs a command', () => {
    expect(render(<TaskKindBadge row={watch({ shell_command: 'wait_pr.py 1140' })} kind="watch" />)).toBe('');
  });
});

describe('TaskDetail command task', () => {
  const commandTask = task({
    shell_command: 'pg_dump mydb | gzip > /backups/db.gz',
    session_id: null,
    last_exit_code: 7,
    timeout_seconds: 900,
    metadata: { on_failure: 'agent' },
  });
  const detail = (value: HarnessTask) =>
    render(<TaskDetail task={value} onToggleEnabled={() => undefined} pending={false} />);

  it('shows the command, the failure policy, the timeout and the last exit code', () => {
    const html = detail(commandTask);

    expect(html).toContain(`>${i18n.t('harness.detail.command')}<`);
    expect(html).toContain('pg_dump mydb | gzip &gt; /backups/db.gz');
    expect(html).toContain(`>${i18n.t('harness.detail.onFailure')}<`);
    expect(html).toContain(`>${i18n.t('harness.onFailure.agent')}<`);
    expect(html).toContain(`>${i18n.t('harness.detail.timeout')}<`);
    expect(html).toContain('900s');
    expect(html).toContain(`>${i18n.t('harness.detail.lastExitCode')}<`);
    expect(html).toContain('>7<');
  });

  it('names the command in the header, instead of the word "Scheduled task"', () => {
    // A command task has no name and no message to fall back to, so the
    // existing title chain landed on the kind label and every unnamed command
    // task in the list read as "Scheduled task".
    const html = detail(commandTask);

    expect(html).toContain('title="pg_dump mydb | gzip &gt; /backups/db.gz"');
    expect(html).not.toContain(`>${i18n.t('harness.kind.task')}<`);
  });

  it('renders a command task with no session without a broken chat link', () => {
    // ``--on-failure none`` stores no session at all. DetailSession already
    // owns the four session states, so this must say so rather than link to
    // /chat/null.
    const html = detail(commandTask);

    expect(html).toContain(i18n.t('harness.detail.sessionNone'));
    expect(html).not.toContain('/chat/');
  });

  it('localizes an orphan-owner pause reason stored as a machine code', () => {
    const html = detail(
      task({
        enabled: false,
        lifecycle_state: 'paused',
        resume_blocked: {
          code: 'task_owner_session_unavailable',
          owner_session_id: 'ses-removed',
        },
      }),
    );

    expect(html).toContain(`>${i18n.t('harness.detail.pauseReason')}<`);
    expect(html).toContain(i18n.t('harness.taskPauseReason.ownerSessionUnavailable'));
    expect(html).not.toContain('task_owner_session_unavailable');
  });

  it('drops the Agent routing a pure command task deliberately has none of', () => {
    // SCT-043. The label helpers resolve a null to a concrete answer, so a
    // command created with the CLI's default ``--on-failure none`` — no Agent,
    // no session, no delivery — was described as routing through an "Inherited
    // default" Agent with an "Existing" session policy delivering to "Session".
    // Every one of those was invented, and the on-failure field right below
    // already states that no Agent is involved.
    const html = detail(task({ shell_command: 'make test' }));

    expect(html).not.toContain(`>${i18n.t('harness.detail.agent')}<`);
    expect(html).not.toContain(i18n.t('harness.detail.agentInherit'));
    expect(html).not.toContain(`>${i18n.t('harness.detail.sessionPolicy')}<`);
    expect(html).not.toContain(`>${i18n.t('harness.detail.delivery')}<`);
    expect(html).toContain(`>${i18n.t('harness.onFailure.none')}<`);
  });

  it('keeps the routing fields wherever there is something to route', () => {
    // Not gated on the KIND: an escalation turn is routed by exactly these
    // fields, and a command bound to a conversation delivers its failure notice
    // there, so hiding them for every command would drop real answers.
    const escalates = detail(task({ shell_command: 'make test', metadata: { on_failure: 'agent' } }));
    expect(escalates).toContain(`>${i18n.t('harness.detail.agent')}<`);
    expect(escalates).toContain(`>${i18n.t('harness.detail.delivery')}<`);

    const bound = detail(task({ shell_command: 'make test', session_key: 'slack::channel::C123', post_to: 'thread' }));
    expect(bound).toContain(`>${i18n.t('harness.detail.delivery')}<`);
    expect(bound).toContain(`>${i18n.t('harness.delivery.thread')}<`);

    const pinned = detail(task({ shell_command: 'make test', agent_name: 'claude' }));
    expect(pinned).toContain(`>${i18n.t('harness.detail.agent')}<`);

    // And a message task is untouched: its null agent really is the inherited one.
    const message = detail(task({ prompt: 'Summarize #ops', message: 'Summarize #ops' }));
    expect(message).toContain(`>${i18n.t('harness.detail.agent')}<`);
    expect(message).toContain(i18n.t('harness.detail.agentInherit'));
    expect(message).toContain(`>${i18n.t('harness.detail.sessionPolicy')}<`);
  });

  it('shows where the command runs, and names the source when it follows a session', () => {
    // SCT-052. ``WatchDetail`` has shown the working directory since it shipped; the task pane
    // never did, so a scheduled command's directory was unanswerable from the UI even
    // once ``--cwd`` could store one.
    const pinned = detail(task({ shell_command: 'make test', cwd: '/srv/app' }));
    expect(pinned).toContain(`>${i18n.t('harness.detail.cwd')}<`);
    expect(pinned).toContain('/srv/app');

    // A null is not "nowhere": the fire walks a chain, which is a different answer
    // from the bare em-dash a missing field would print. Stated as the chain rather
    // than as an outcome — ``_bound_session_workdir`` answers None for a NULL workdir
    // or a failed read too, so "Session working directory" was a promise the pane
    // cannot keep.
    const inherited = detail(
      task({ shell_command: 'make test', session_id: 'ses1', session_platform: 'slack', cwd: null }),
    );
    expect(inherited).toContain(i18n.t('harness.detail.cwdFromSession'));
  });

  it('does not name a session the command cannot inherit a directory from', () => {
    // SCT-055. A binding whose Session row is gone resolves to None at fire time and falls
    // through to the runtime default — so naming the Session here would contradict the
    // "Session deleted" the Session field prints two rows up, and misstate where the
    // command runs.
    const deleted = detail(task({ shell_command: 'make test', session_id: 'ses-gone', cwd: null }));
    expect(deleted).toContain(i18n.t('harness.detail.cwdRuntimeDefault'));
    expect(deleted).not.toContain(i18n.t('harness.detail.cwdFromSession'));

    const unbound = detail(task({ shell_command: 'make test', session_id: null, cwd: null }));
    expect(unbound).toContain(i18n.t('harness.detail.cwdRuntimeDefault'));

    // Asserted on the copy itself because that is where the claim lives. The chain does
    // not end at ``runtime.default_cwd`` — it is unset on a fresh install and
    // revalidated on every fire, and ``_execute_command_task`` then falls through to the
    // Avibe state directory. Naming only the config key is the same overclaim as naming
    // the Session, one link further down.
    expect(en.harness.detail.cwdRuntimeDefault).toMatch(/Avibe/);
    expect(en.harness.detail.cwdFromSession).toMatch(/Avibe/);
    expect(zh.harness.detail.cwdRuntimeDefault).toMatch(/Avibe/);
    expect(zh.harness.detail.cwdFromSession).toMatch(/Avibe/);
  });

  it('states the failure policy before the routing it governs, not after it', () => {
    // SCT-052. The five routing fields describe a path that runs on no healthy day. Rendered
    // above the ``On failure`` that qualifies them, the pane read top-down as "this
    // job spends an Agent turn every morning" — the exact cost an agent-free command
    // task is chosen to avoid.
    const html = detail(task({ shell_command: 'make test', metadata: { on_failure: 'agent' } }));

    const onFailure = html.indexOf(`>${i18n.t('harness.detail.onFailure')}<`);
    const agent = html.indexOf(`>${i18n.t('harness.detail.agent')}<`);
    expect(onFailure).toBeGreaterThan(-1);
    expect(agent).toBeGreaterThan(-1);
    expect(onFailure).toBeLessThan(agent);
    expect(html).toContain(`>${i18n.t('harness.detail.escalation')}<`);
  });

  it('calls an escalating command task\'s message a triage prompt, not the job', () => {
    // SCT-052. Same label as a message task's payload, entirely different meaning: this text is
    // sent only when the command fails.
    const html = detail(
      task({ shell_command: 'make test', message: 'Diagnose it.', metadata: { on_failure: 'agent' } }),
    );

    expect(html).toContain(`>${i18n.t('harness.detail.triagePrompt')}<`);
    expect(html).not.toContain(`>${i18n.t('harness.detail.message')}<`);
    expect(html).toContain('Diagnose it.');
  });

  it('calls nothing an escalation on a command task that escalates to no one', () => {
    // SCT-054. The routing gate is SCT-043's: bindings, not kind. A command task with
    // ``--on-failure none`` delivered to a real conversation keeps these fields
    // legitimately — and heading them "On failure → escalation" would have the pane
    // say "Notice only (no Agent)" and "escalation" about the same task, one field
    // apart.
    const html = detail(
      task({
        shell_command: 'make test',
        session_key: 'slack::channel::C123',
        post_to: 'thread',
        message: 'Left over from an older edit.',
        metadata: { on_failure: 'none' },
      }),
    );

    expect(html).toContain(`>${i18n.t('harness.onFailure.none')}<`);
    expect(html).toContain(`>${i18n.t('harness.detail.delivery')}<`);
    expect(html).not.toContain(`>${i18n.t('harness.detail.escalation')}<`);
    expect(html).not.toContain(`>${i18n.t('harness.detail.triagePrompt')}<`);
    expect(html).toContain(`>${i18n.t('harness.detail.message')}<`);
  });

  it('leaves a message task reading as the job it is', () => {
    // SCT-054. A message task routes EVERY run through those fields, so grouping them under a
    // failure heading would be a lie in the other direction.
    const html = detail(task({ prompt: 'Summarize #ops', message: 'Summarize #ops' }));

    expect(html).toContain(`>${i18n.t('harness.detail.message')}<`);
    expect(html).not.toContain(`>${i18n.t('harness.detail.escalation')}<`);
    expect(html).not.toContain(`>${i18n.t('harness.detail.triagePrompt')}<`);
    expect(html).not.toContain(`>${i18n.t('harness.detail.onFailure')}<`);
  });

  it('drops the Message field a command task has nothing to put in', () => {
    const html = detail(commandTask);

    expect(html).not.toContain(`>${i18n.t('harness.detail.message')}<`);
  });

  it('keeps the Message field for a command task that escalates with one', () => {
    const html = detail(task({ shell_command: 'make test', message: 'Tests failed, investigate' }));

    expect(html).toContain(`>${i18n.t('harness.detail.message')}<`);
    expect(html).toContain('Tests failed, investigate');
  });

  it('says "no timeout" rather than "0s", which would read as an instant kill', () => {
    const html = detail(task({ shell_command: 'make test', timeout_seconds: 0 }));

    expect(html).toContain(i18n.t('harness.detail.timeoutNone'));
    expect(html).not.toContain('0s');
  });

  it('states the quiet default when no failure policy is stored', () => {
    const html = detail(task({ shell_command: 'make test' }));

    expect(html).toContain(`>${i18n.t('harness.onFailure.none')}<`);
  });

  it('states the timeout that actually applies when the row stores none', () => {
    // SCT-018. A null ``timeout_seconds`` is not "unlimited" and not "unknown": the
    // executor applies COMMAND_TASK_DEFAULT_TIMEOUT_SECONDS, so a six-hour limit is
    // in force. Hiding the field left the pane silent about the one number that
    // decides whether a long backup is killed mid-run, right next to a policy field
    // that DOES state its default — so the omission read as "no limit".
    const html = detail(task({ shell_command: 'make test' }));

    expect(html).toContain(`>${i18n.t('harness.detail.timeout')}<`);
    expect(html).toContain(i18n.t('harness.detail.timeoutDefault', { duration: '6h' }));
    expect(html).not.toContain(i18n.t('harness.detail.timeoutNone'));

    // A stored limit is the user's own number, and carries no default marker.
    const stored = detail(task({ shell_command: 'make test', timeout_seconds: 900 }));
    expect(stored).toContain('900s');
    expect(stored).not.toContain(i18n.t('harness.detail.timeoutDefault', { duration: '6h' }));
  });

  it('colours a zero exit code as neutral and a non-zero one as a failure', () => {
    expect(detail(task({ shell_command: 'make test', last_exit_code: 0 }))).toContain(
      '<span class="font-mono text-[11px] text-muted">0</span>',
    );
    expect(detail(task({ shell_command: 'make test', last_exit_code: 1 }))).toContain(
      '<span class="font-mono text-[11px] text-pink-ink">1</span>',
    );
  });

  it('shows a translated failure summary and keeps raw diagnostics collapsed', () => {
    const raw = 'pg_dump: connection refused';
    const html = detail(
      task({
        shell_command: 'pg_dump app',
        last_exit_code: 7,
        last_error: raw,
        lifecycle_detail: 'error',
      }),
    );

    expect(html).toContain(en.harness.failure.generic);
    expect(html).toContain(`>${en.harness.detail.lastExitCode}<`);
    expect(html).toContain('>7<');
    expect(html).toContain(`>${en.harness.detail.technicalDetails}<`);
    expect(html).toContain(raw);
    expect(html).toMatch(/<details(?![^>]*\bopen\b)[^>]*>/);
    expect(html.indexOf(en.harness.failure.generic)).toBeLessThan(html.indexOf(raw));
  });

  it('uses the generic summary when only an opaque technical error is available', () => {
    const raw = 'command not found: localized stderr';
    const html = detail(task({ shell_command: 'missing-tool', last_error: raw }));

    expect(html).toContain(en.harness.failure.generic);
    expect(html).toContain(`>${en.harness.detail.technicalDetails}<`);
    expect(html).toContain(raw);
    expect(html).toMatch(/<details(?![^>]*\bopen\b)[^>]*>/);
  });

  it('keeps a legacy Task timeout detail generic without the explicit timeout fact', () => {
    const raw = 'command returned status 124';
    const html = detail(
      task({
        shell_command: 'backup --self-timeout',
        lifecycle_detail: 'timeout',
        last_exit_code: 124,
        last_error: raw,
        metadata: {},
      }),
    );

    expect(html).toContain(en.harness.failure.generic);
    expect(html).not.toContain(en.harness.failure.timeout);
    expect(html).toContain(`>${en.harness.detail.technicalDetails}<`);
    expect(html).toContain(raw);
    expect(html).toMatch(/<details(?![^>]*\bopen\b)[^>]*>/);
  });

  it('keeps a canceled Task diagnostic collapsed without calling it a failure', () => {
    const raw = 'Run canceled by user';
    const html = detail(
      task({
        shell_command: 'make release',
        health: 'healthy',
        last_error: raw,
        metadata: { last_result_status: 'canceled' },
      }),
    );

    expect(html).not.toContain(en.harness.failure.generic);
    expect(html).not.toContain(en.harness.failure.timeout);
    expect(html).toContain(`>${en.harness.detail.technicalDetails}<`);
    expect(html).toContain(raw);
    expect(html).toMatch(/<details(?![^>]*\bopen\b)[^>]*>/);
  });

  it('leaves a message task rendering exactly as it does today', () => {
    const html = detail(task({ name: 'Nightly digest', prompt: 'Summarize #ops', message: 'Summarize #ops' }));

    expect(html).toContain(`>${i18n.t('harness.detail.message')}<`);
    expect(html).toContain('Summarize #ops');
    // None of the command-only fields appear.
    for (const label of ['harness.detail.command', 'harness.detail.onFailure', 'harness.detail.timeout', 'harness.detail.lastExitCode']) {
      expect(html).not.toContain(`>${i18n.t(label)}<`);
    }
    expect(html).not.toContain(i18n.t('harness.taskKind.command'));
  });
});

describe('RunDetail command run', () => {
  it('renders a command run with no session, and keeps its exit code visible', () => {
    // A command run keeps ``run_type: "scheduled"`` and carries exit_code /
    // stdout / stderr with ``session_id: null`` — nothing on this pane may
    // assume every run has a session.
    const html = render(
      <RunDetail
        run={run({
          run_type: 'scheduled',
          status: 'failed',
          session_id: null,
          exit_code: 7,
          stdout: 'dumping mydb',
          stderr: 'pg_dump: connection refused',
          definition_name: 'Nightly backup',
          definition_kind: 'task',
          definition_id: 'def-9',
          metadata: { command: { shell: null, argv: ['pg_dump', 'my db'] } },
        })}
      />,
    );

    expect(html).toContain(i18n.t('harness.detail.sessionNone'));
    expect(html).not.toContain('href="/chat/');
    expect(html).toContain('exit_code 7');
    expect(html).toContain('pg_dump: connection refused');
    // Command runs reuse the existing scheduled run-type label; no new key.
    expect(html).toContain(i18n.t('harness.runType.scheduled'));
    // SCT-019: what ran comes off the RUN's own snapshot, shell-joined, so a
    // definition edited or deleted after the fire cannot rewrite this pane's answer.
    expect(html).toContain(`>${i18n.t('harness.detail.command')}<`);
    expect(html).toContain("pg_dump &#x27;my db&#x27;");
  });
});

describe('WatchDetail runtime', () => {
  it.each([
    [
      'disabled and stopped',
      watch({ enabled: false, process_alive: false }),
      { runtime: false, line: null, pid: null },
    ],
    [
      'enabled and stopped',
      watch({ enabled: true, process_alive: false }),
      { runtime: true, line: '<span class="text-[12px] text-pink-ink">process exited</span>', pid: null },
    ],
    [
      'disabled and alive',
      watch({ enabled: false, process_alive: true, runtime: { running: true, pid: 4321 } }),
      { runtime: true, line: '<span class="text-[12px] text-foreground">process running</span>', pid: 'pid 4321' },
    ],
  ] as const)('renders %s consistently with the row', (_name, value, expected) => {
    const html = render(<WatchDetail watch={value} onToggleEnabled={() => undefined} pending={false} />);

    expect(html.includes('>Runtime<')).toBe(expected.runtime);
    if (expected.line) expect(html).toContain(expected.line);
    else expect(html).not.toContain('process exited');
    if (expected.pid) expect(html).toContain(expected.pid);
  });

  it('keeps unknown event-processing health visible in the detail pane', () => {
    const html = render(
      <WatchDetail
        watch={watch({ processing_health: 'unknown' })}
        onToggleEnabled={() => undefined}
        pending={false}
      />,
    );

    expect(html).toContain(`>${i18n.t('harness.detail.eventProcessing')}<`);
    expect(html).toContain(`>${i18n.t('harness.processingHealth.unknown')}<`);
  });

  it('keeps an insufficiently evidenced Watch timeout generic and technical details collapsed', () => {
    const raw = 'localized-looking stderr must stay technical';
    const html = render(
      <WatchDetail
        watch={watch({
          lifecycle_state: 'finished',
          lifecycle_detail: 'timeout',
          last_exit_code: 124,
          last_error: raw,
        })}
        onToggleEnabled={() => undefined}
        pending={false}
      />,
    );

    expect(html).toContain(en.harness.failure.generic);
    expect(html).not.toContain(en.harness.failure.timeout);
    expect(html).toContain(`>${en.harness.detail.lastExitCode}<`);
    expect(html).toContain('>124<');
    expect(html).toContain(`>${en.harness.detail.technicalDetails}<`);
    expect(html).toContain(raw);
    expect(html).toMatch(/<details(?![^>]*\bopen\b)[^>]*>/);
  });

  it('keeps configured retry and no-event exits neutral', () => {
    for (const last_exit_code of [64, 75]) {
      const html = render(
        <WatchDetail
          watch={watch({
            enabled: true,
            retry_exit_codes: [75],
            health: 'healthy',
            lifecycle_state: 'waiting',
            lifecycle_detail: null,
            last_exit_code,
          })}
          onToggleEnabled={() => undefined}
          pending={false}
        />,
      );
      expect(html).toContain(`<span class="font-mono text-[11px] text-muted">${last_exit_code}</span>`);
    }
  });

  it('keeps a healthy retry diagnostic without presenting it as a failure', () => {
    const raw = 'avibe-watch: still waiting';
    const html = render(
      <WatchDetail
        watch={watch({
          enabled: true,
          retry_exit_codes: [75],
          health: 'healthy',
          lifecycle_state: 'waiting',
          lifecycle_detail: null,
          last_exit_code: 75,
          last_error: raw,
        })}
        onToggleEnabled={() => undefined}
        pending={false}
      />,
    );

    expect(html).not.toContain(en.harness.failure.generic);
    expect(html).toContain(`<span class="font-mono text-[11px] text-muted">75</span>`);
    expect(html).toContain(`>${en.harness.detail.technicalDetails}<`);
    expect(html).toContain(raw);
    expect(html).toMatch(/<details(?![^>]*\bopen\b)[^>]*>/);
  });

  it('keeps retry history neutral after the user manually pauses a Watch', () => {
    const raw = 'avibe-watch: still waiting';
    const html = render(
      <WatchDetail
        watch={watch({
          enabled: false,
          retry_exit_codes: [75],
          health: 'failing',
          lifecycle_state: 'paused',
          lifecycle_detail: null,
          retired_at: null,
          last_exit_code: 75,
          last_error: raw,
        })}
        onToggleEnabled={() => undefined}
        pending={false}
      />,
    );

    expect(html).not.toContain(en.harness.failure.generic);
    expect(html).toContain('<span class="font-mono text-[11px] text-muted">75</span>');
    expect(html).toContain(raw);
    expect(html).toMatch(/<details(?![^>]*\bopen\b)[^>]*>/);
  });

  it('keeps a terminal missing-cwd retry exit as a failure', () => {
    const raw = 'working directory is unavailable';
    const html = render(
      <WatchDetail
        watch={watch({
          enabled: false,
          retry_exit_codes: [1],
          health: 'failing',
          lifecycle_state: 'finished',
          lifecycle_detail: 'error',
          retired_at: '2026-08-12T00:00:00Z',
          last_exit_code: 1,
          last_error: raw,
        })}
        onToggleEnabled={() => undefined}
        pending={false}
      />,
    );

    expect(html).toContain(en.harness.failure.generic);
    expect(html).toContain('<span class="font-mono text-[11px] text-pink-ink">1</span>');
    expect(html).toContain(raw);
    expect(html).toMatch(/<details(?![^>]*\bopen\b)[^>]*>/);
  });

  it('does not call a Watch lifetime expiry a run timeout', () => {
    const html = render(
      <WatchDetail
        watch={watch({
          enabled: false,
          health: 'failing',
          lifecycle_state: 'finished',
          lifecycle_detail: 'timeout',
          last_exit_code: 124,
          last_error: 'watch lifetime expired before the next cycle',
        })}
        onToggleEnabled={() => undefined}
        pending={false}
      />,
    );

    expect(html).toContain(en.harness.failure.generic);
    expect(html).not.toContain(en.harness.failure.timeout);
  });

  it('shows a stable circuit-pause summary and keeps the reason technical', () => {
    const raw = 'Watch paused after rapid event burst';
    const html = render(
      <WatchDetail
        watch={watch({
          enabled: false,
          health: 'failing',
          lifecycle_state: 'paused',
          lifecycle_detail: null,
          last_exit_code: 0,
          last_error: raw,
          metadata: { watch_circuit_breaker: { status: 'tripped', exit_code: 0 } },
        })}
        onToggleEnabled={() => undefined}
        pending={false}
      />,
    );

    expect(html).toContain(en.harness.failure.circuitPaused);
    expect(html).not.toContain(en.harness.failure.generic);
    expect(html).not.toContain(en.harness.failure.timeout);
    expect(html).toContain('text-amber');
    expect(html).toContain(`>${en.harness.detail.technicalDetails}<`);
    expect(html).toContain(raw);
    expect(html).toMatch(/<details(?![^>]*\bopen\b)[^>]*>/);
  });

  it('resets technical details when selecting another definition', () => {
    const renderInteractive = (ui: ReactElement) =>
      renderDom(
        <I18nextProvider i18n={i18n}>
          <MemoryRouter>{ui}</MemoryRouter>
        </I18nextProvider>,
      );
    const view = renderInteractive(
      <TaskDetail
        task={task({ id: 'task-a', shell_command: 'first', last_error: 'first technical error' })}
        onToggleEnabled={() => undefined}
        pending={false}
      />,
    );
    const details = view.container.querySelector('details');
    expect(details?.open).toBe(false);
    fireEvent.click(details?.querySelector('summary') as HTMLElement);
    expect(view.container.querySelector('details')?.open).toBe(true);

    view.rerender(
      <I18nextProvider i18n={i18n}>
        <MemoryRouter>
          <TaskDetail
            task={task({ id: 'task-b', shell_command: 'second', last_error: 'second technical error' })}
            onToggleEnabled={() => undefined}
            pending={false}
          />
        </MemoryRouter>
      </I18nextProvider>,
    );
    expect(view.container.querySelector('details')?.open).toBe(false);
    view.unmount();
  });
});

describe('RunTriggerChip', () => {
  it('links a live watch back to its definition row', () => {
    const html = render(
      <RunTriggerChip run={run({ definition_id: 'def-1', definition_name: 'CI watcher', definition_kind: 'watch' })} />,
    );

    expect(html).toContain('href="/harness?tab=watches&amp;definition=def-1"');
    expect(html).toContain('CI watcher');
  });

  it('routes a scheduled definition to the tasks tab', () => {
    const html = render(
      <RunTriggerChip run={run({ definition_id: 'def-2', definition_name: 'Nightly', definition_kind: 'task' })} />,
    );

    expect(html).toContain('href="/harness?tab=tasks&amp;definition=def-2"');
  });

  it('keeps naming a deleted definition but stops linking to it', () => {
    const html = render(
      <RunTriggerChip
        run={run({ definition_id: 'def-3', definition_name: 'Old digest', definition_kind: 'task', definition_deleted: true })}
      />,
    );

    expect(html).toContain('Old digest');
    expect(html).toContain('(deleted)');
    expect(html).not.toContain('<a ');
  });

  it('renders nothing for a run with no originating definition', () => {
    expect(render(<RunTriggerChip run={run({})} />)).toBe('');
  });

  it('drops the run anchor so the destination list is not hidden behind a detail panel', () => {
    // The chip means "show me this definition in the list". Carrying ?run
    // forward would re-select the run on arrival, and below `lg` an open
    // selection hides the list — the link would show everything except the
    // thing it promised. (The clicked-row case has no ?run to drop; the
    // HarnessPage effect that consumes ?definition clears the selection.)
    const html = render(
      <RunTriggerChip run={run({ definition_id: 'def-4', definition_name: 'Hourly sync', definition_kind: 'task' })} />,
    );

    expect(html).toContain('definition=def-4');
    expect(html).not.toContain('run=');
  });
});