Skip to content

AI, fully local on your computer.

16.4 KB
Raw Blame
// @vitest-environment jsdom
import { describe, it, expect, vi, afterEach } from 'vitest'
import { screen, cleanup, waitFor, act } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { startSkillRun, clearSkillRunSession } from '../../src/renderer/lib/skillruns'
import {
  DEFAULT_SETTINGS,
  type Conversation,
  type Message,
  type SkillInfo,
  type SkillRunState,
  type StartSkillRunRequest,
  type StartSkillRunResult
} from '../../src/shared/types'
import type { PreloadApi } from '../../src/preload'
import { assertNoUnexpectedApiCalls, stubApi } from '../helpers/renderer'
import { chatScreenStubs, renderChatScreen } from '../helpers/chat-screen'
import { makeSkillInfo, makeConversation, makeDocumentInfo, makeMessage, userMsg } from '../helpers/chat-fixtures'

// SKA-6/SKA-17 (skills audit 2026-07-03, U6) — the ChatScreen wiring of the per-run store. These
// exercise the invariants that had ZERO renderer coverage: the run bar is gated to the launching
// conversation (a run in chat A never renders in chat B), a "working in another chat" chip covers a
// run elsewhere, and the routed-run relay lands its answer in the RUN's conversation (C1), under the
// RUN's skill (C2), pinned to the RUN's document (ux-6) — resolving the pin BEFORE acknowledge.

function conv(over: Partial<Conversation> = {}): Conversation {
  return makeConversation({
    id: 'convA',
    title: 'Chat A',
    mode: 'documents',
    scopeDocumentIds: ['docA'],
    scope: { collectionIds: [], documentIds: ['docA'] },
    ...over
  })
}

function runState(over: Partial<SkillRunState>): SkillRunState {
  return {
    runHandle: 'h',
    skillInstallId: 'app:bank-statement',
    toolName: 'extract_transactions',
    documentCount: 1,
    state: 'done',
    progress: { done: 0, total: 0 },
    ...over
  }
}

afterEach(() => {
  try {
    assertNoUnexpectedApiCalls()
  } finally {
    // Unmount first: it unsubscribes the store listener, so the clear notifies nothing (no act() warning).
    cleanup()
    clearSkillRunSession()
    vi.restoreAllMocks()
    window.localStorage.clear()
  }
})

/** Stub the bridge for a two-conversation, documents-mode ChatScreen; return the spies a test asserts. */
function baseApi(runs: Record<string, SkillRunState>, extra: Partial<PreloadApi> = {}): { askDocuments: ReturnType<typeof vi.fn> } {
  const askDocuments = vi.fn(async (): Promise<Message> => makeMessage({ conversationId: 'convA', content: 'ok' }))
  stubApi(
    chatScreenStubs({
      listConversations: vi.fn(async () => [conv(), conv({ id: 'convB', title: 'Chat B', scopeDocumentIds: ['docB'], scope: { collectionIds: [], documentIds: ['docB'] } })]),
      listDocuments: vi.fn(async () => [makeDocumentInfo({ id: 'docA', title: 'a.pdf' }), makeDocumentInfo({ id: 'docB', title: 'b.pdf' })]),
      // The per-run store surface.
      startSkillRun: vi.fn(
        async (req: StartSkillRunRequest): Promise<StartSkillRunResult> => ({
          started: true,
          run: runs[req.documentId ?? '']
        })
      ),
      getSkillRun: vi.fn(async (h: string) => Object.values(runs).find((r) => r.runHandle === h) ?? null),
      clearSkillRun: vi.fn(async () => {}),
      // The routed relay's `stream()` sends through askDocuments; chatScreenStubs answers its stream subscriptions.
      askDocuments,
      ...extra
    })
  )
  return { askDocuments }
}

async function selectConversation(user: ReturnType<typeof userEvent.setup>, title: string): Promise<void> {
  await user.click(await screen.findByText(title))
}

describe('ChatScreen — per-run store gating (SKA-6)', () => {
  it("a run in another conversation NEVER renders its result row in the active conversation", async () => {
    const runs = { docB: runState({ runHandle: 'hB', conversationId: 'convB', documentId: 'docB', state: 'done', transactionCount: 9 }) }
    baseApi(runs)
    const user = userEvent.setup()
    renderChatScreen()
    await selectConversation(user, 'Chat A') // active = convA
    // Seed a finished run in convB while convA is active.
    await act(async () => {
      await startSkillRun({ skillInstallId: 'app:bank-statement', toolName: 'extract_transactions', conversationId: 'convB', documentId: 'docB' })
    })
    // convA shows NO result row for convB's run (the foreign-conversation replay the audit describes).
    expect(screen.queryByText('Extracted 9 transactions.')).not.toBeInTheDocument()
    // Switching to convB surfaces its outcome.
    await selectConversation(user, 'Chat B')
    expect(await screen.findByText('Extracted 9 transactions.')).toBeInTheDocument()
  })

  it('shows a quiet "working in another chat" chip when a run is running elsewhere', async () => {
    const runs = { docB: runState({ runHandle: 'hB', conversationId: 'convB', documentId: 'docB', state: 'running' }) }
    baseApi(runs)
    const user = userEvent.setup()
    renderChatScreen()
    await selectConversation(user, 'Chat A')
    await act(async () => {
      await startSkillRun({ skillInstallId: 'app:bank-statement', toolName: 'extract_transactions', conversationId: 'convB', documentId: 'docB' })
    })
    expect(await screen.findByText('A skill is working in another chat.')).toBeInTheDocument()
    // …and no busy row for convA itself (it has no run).
    expect(screen.queryByText(/Running:/)).not.toBeInTheDocument()
  })

  it('re-adopts an in-flight run on a fresh mount and lands on its conversation, showing the outcome (SKA-17)', async () => {
    const term = runState({ runHandle: 'hB', conversationId: 'convB', documentId: 'docB', state: 'done', transactionCount: 5 })
    baseApi({ docB: term }, { listSkillRuns: vi.fn(async () => [term]) })
    renderChatScreen()
    // No manual selection — the mount re-adopts the run and lands on convB, showing its terminal outcome.
    expect(await screen.findByText('Extracted 5 transactions.')).toBeInTheDocument()
  })
})

describe('ChatScreen — routed-run relay invariants (C1/C2/ux-6)', () => {
  it('routes the summarize answer into the RUN’s conversation, under the RUN’s skill, pinned to the RUN’s document', async () => {
    const runs = {
      docA: runState({ runHandle: 'hA', skillInstallId: 'app:bank-statement', toolName: 'summarize_cashflow', conversationId: 'convA', documentId: 'docA', state: 'done', transactionCount: 4 })
    }
    const { askDocuments } = baseApi(runs)
    const user = userEvent.setup()
    renderChatScreen()
    await selectConversation(user, 'Chat A') // documents mode, no skill picked in the composer
    // A routed run (summarize_cashflow) finishes for convA — seed it via the same store ChatScreen reads.
    await act(async () => {
      await startSkillRun({ skillInstallId: 'app:bank-statement', toolName: 'summarize_cashflow', conversationId: 'convA', documentId: 'docA' })
    })
    // The relay streams the summarize question as a real chat answer:
    //  C1 — into convA (the launching conversation),
    //  C2 — under the RUN's skill (app:bank-statement), NOT the composer's current pick (none here),
    //  ux-6 — pinned to docA (the run's document, resolved from the store entry before acknowledge).
    await waitFor(() =>
      expect(askDocuments).toHaveBeenCalledWith('convA', 'Summarize my income and expenses.', 'app:bank-statement', false, 'docA', undefined)
    )
  })

  // #613: the relay's skill and pin are not in the composer, so "Send again" on a routed question that
  // got no answer (here the model crashed) must carry them itself, or the re-ask silently skips the
  // run's 0-model-call handler (C2) and spreads over the whole scope (ux-6).
  it('"Send again" on a routed question that got no answer keeps the RUN’s skill and document pin (#613)', async () => {
    const runs = {
      docA: runState({ runHandle: 'hA', skillInstallId: 'app:bank-statement', toolName: 'summarize_cashflow', conversationId: 'convA', documentId: 'docA', state: 'done', transactionCount: 4 })
    }
    let history: Message[] = []
    const { askDocuments } = baseApi(runs, { listMessages: vi.fn(async () => history) })
    askDocuments.mockImplementationOnce(async () => {
      history = [userMsg('q1', 'Summarize my income and expenses.', { conversationId: 'convA' })]
      throw new Error('The AI model stopped before the answer was finished.')
    })
    const user = userEvent.setup()
    renderChatScreen()
    await selectConversation(user, 'Chat A') // no skill picked in the composer
    await act(async () => {
      await startSkillRun({ skillInstallId: 'app:bank-statement', toolName: 'summarize_cashflow', conversationId: 'convA', documentId: 'docA' })
    })

    await user.click(await screen.findByRole('button', { name: /Send again/ }))
    await waitFor(() =>
      expect(askDocuments).toHaveBeenLastCalledWith('convA', '', 'app:bank-statement', false, 'docA', 'q1')
    )
  })

  it('does NOT relay when the run’s conversation is not the active one (C1)', async () => {
    const runs = {
      docB: runState({ runHandle: 'hB', skillInstallId: 'app:bank-statement', toolName: 'summarize_cashflow', conversationId: 'convB', documentId: 'docB', state: 'done', transactionCount: 4 })
    }
    const { askDocuments } = baseApi(runs)
    const user = userEvent.setup()
    renderChatScreen()
    await selectConversation(user, 'Chat A') // active = convA, but the run is convB's
    await act(async () => {
      await startSkillRun({ skillInstallId: 'app:bank-statement', toolName: 'summarize_cashflow', conversationId: 'convB', documentId: 'docB' })
    })
    // Deterministic negative gate (TS-1): a (wrongly) firing relay effect calls askDocuments
    // SYNCHRONOUSLY — `stream` has no await before the askDocuments call — and every effect for
    // the store update above already flushed inside `act`. One empty act drains any straggler
    // microtask cascade, so "not called by now" is conclusive; the answer waits for convB.
    await act(async () => {})
    expect(askDocuments).not.toHaveBeenCalled()
  })
})

// SKA-18 (skills audit 2026-07-03, U6): a skill picked + kept on the 'new' composer must NOT resurrect
// on a later empty composer (a New chat / mode toggle) after it was carried onto the created
// conversation — else the next send persists a keep opt-in made for conversation 1 onto conversation 2.
describe('ChatScreen — the "new"-composer pick is cleared after being carried (SKA-18)', () => {
  function skill(): SkillInfo {
    return makeSkillInfo()
  }
  const pickerTrigger = (): HTMLElement => screen.getByRole('button', { name: /^skill:/i })

  it('does not resurrect the kept pick on New chat after the first send carried it to a conversation', async () => {
    const setConversationDefaultSkill = vi.fn(async () => {})
    const created: Conversation = { ...conv({ id: 'conv1', title: 'Created', mode: 'chat', scope: null, scopeDocumentIds: null }) }
    let convList: Conversation[] = []
    stubApi(
      chatScreenStubs({
        // Empty at first (start on the 'new' composer); the created conversation appears after send so the
        // mode toggle can see the active conversation's (chat) mode and deselect it.
        listConversations: vi.fn(async () => convList),
        listSkills: vi.fn(async () => [skill()]),
        // The skill's #46 info card was already seen, so the first pick opens no card (and writes no
        // settings): this row is about the carry, not the card.
        getSettings: vi.fn(async () => ({ ...DEFAULT_SETTINGS, skillInfoSeen: ['bank-statement'] })),
        createConversation: vi.fn(async () => {
          convList = [created] // the send's ensureConversation created it
          return created
        }),
        setConversationDefaultSkill,
        sendChatMessage: vi.fn(async (): Promise<Message> => makeMessage({ conversationId: 'conv1', content: 'ok' }))
      })
    )
    const user = userEvent.setup()
    renderChatScreen()
    // Pick the skill + keep it for the conversation, both on the 'new' composer (wait for the async
    // listSkills load so the picker is present).
    await user.click(await screen.findByRole('button', { name: /^skill:/i }))
    await user.click(await screen.findByRole('menuitemradio', { name: /bank statement helper/i }))
    await user.click(pickerTrigger())
    await user.click(await screen.findByRole('menuitemcheckbox', { name: /keep for this conversation/i }))
    await user.keyboard('{Escape}') // close the menu (Radix aria-hides the rest of the app while open)
    // Send → ensureConversation creates conv1 and persists the keep for it.
    await user.type(await screen.findByRole('textbox'), 'hello')
    await user.click(screen.getByRole('button', { name: /^send$/i }))
    await waitFor(() => expect(setConversationDefaultSkill).toHaveBeenCalledWith('conv1', 'app:bank-statement'))
    setConversationDefaultSkill.mockClear()
    // Toggle to the OTHER mode → the active (chat) conversation is deselected, giving a fresh 'new'
    // composer. It must be CLEAN: the carried pick was DELETED from the 'new' keys, not resurrected.
    await user.click(screen.getByRole('radio', { name: /ask my documents/i }))
    await waitFor(() => expect(pickerTrigger()).toHaveTextContent('No skill'))
    // …and it was never re-persisted for any conversation.
    expect(setConversationDefaultSkill).not.toHaveBeenCalledWith(expect.anything(), 'app:bank-statement')
  })

  // full-audit 2026-07-11 CODE-30: "+ New chat" used to BYPASS the SKA-18 carry+delete block (it
  // only ran inside ensureConversation, i.e. on a SEND) — a composer pick silently vanished from
  // the created chat and then resurrected on the next empty composer.
  it('"+ New chat" carries the composer pick onto the created conversation and cleans the "new" keys (CODE-30)', async () => {
    const setConversationDefaultSkill = vi.fn(async () => {})
    const created: Conversation = { ...conv({ id: 'conv1', title: 'Created', mode: 'chat', scope: null, scopeDocumentIds: null }) }
    let convList: Conversation[] = []
    stubApi(
      chatScreenStubs({
        listConversations: vi.fn(async () => convList),
        listSkills: vi.fn(async () => [skill()]),
        // As in the row above: the info card was already seen.
        getSettings: vi.fn(async () => ({ ...DEFAULT_SETTINGS, skillInfoSeen: ['bank-statement'] })),
        createConversation: vi.fn(async () => {
          convList = [created]
          return created
        }),
        setConversationDefaultSkill
      })
    )
    const user = userEvent.setup()
    renderChatScreen()
    // Pick + keep on the 'new' composer (no send).
    await user.click(await screen.findByRole('button', { name: /^skill:/i }))
    await user.click(await screen.findByRole('menuitemradio', { name: /bank statement helper/i }))
    await user.click(pickerTrigger())
    await user.click(await screen.findByRole('menuitemcheckbox', { name: /keep for this conversation/i }))
    await user.keyboard('{Escape}')
    // "+ New chat" — the CODE-30 entry point. Pre-fix: no carry ran, so the pick vanished (the
    // picker read "No skill" for the created conversation) and the kept default was never persisted.
    await user.click(screen.getByRole('button', { name: '+ New chat' }))
    await waitFor(() => expect(setConversationDefaultSkill).toHaveBeenCalledWith('conv1', 'app:bank-statement'))
    expect(pickerTrigger()).toHaveTextContent('Bank statement helper')
    setConversationDefaultSkill.mockClear()
    // …and the 'new' keys were DELETED: deselect via the mode toggle → the fresh composer is clean
    // (pre-fix the leftover 'new' key resurrected the pick here).
    await user.click(screen.getByRole('radio', { name: /ask my documents/i }))
    await waitFor(() => expect(pickerTrigger()).toHaveTextContent('No skill'))
    expect(setConversationDefaultSkill).not.toHaveBeenCalledWith(expect.anything(), 'app:bank-statement')
  })

  // CODE-30 rider: ConversationList's `onClick={onNew}` discards the promise — a failed
  // createConversation used to be an unhandled rejection with zero feedback.
  it('a failed "+ New chat" surfaces on the error banner (CODE-30)', async () => {
    stubApi(
      chatScreenStubs({
        createConversation: vi.fn(async () => {
          throw new Error(
            "Error invoking remote method 'chat:createConversation': Error: The workspace is locked. Unlock it to continue."
          )
        })
      })
    )
    const user = userEvent.setup()
    renderChatScreen()
    await user.click(await screen.findByRole('button', { name: '+ New chat' }))
    // Pre-fix: an unhandled rejection (vitest fails on it) and no banner.
    expect(await screen.findByText(/workspace is locked/i)).toBeInTheDocument()
  })
})