AI, fully local on your computer.
TypeScript aiai-toolskiwixllama-cpplocal-ailocal-llmofflineoffline-aioffline-firstprivacy-firstragwikipediazim
// @vitest-environment jsdom
import { describe, it, expect, afterEach, vi } from 'vitest'
import { render, screen, cleanup, waitFor, act, within } from '@testing-library/react'
import userEvent from '@testing-library/user-event'
import { useState } from 'react'
import { PacksPanel } from '../../src/renderer/screens/documents/PacksPanel'
import { ArticleModal, type ArticleTarget } from '../../src/renderer/chat/ArticleModal'
import { PackOutcomesNotice } from '../../src/renderer/chat/PackOutcomesNotice'
import { ScopePopover } from '../../src/renderer/chat/ScopePopover'
import { SourcesDisclosure } from '../../src/renderer/chat/SourcesDisclosure'
import { ChatScreen } from '../../src/renderer/screens/ChatScreen'
import { I18nProvider, UI_LANGUAGE_STORAGE_KEY } from '../../src/renderer/i18n'
import { ToastProvider } from '../../src/renderer/components'
import { t as tr, tCount as trCount, type UiLanguage } from '../../src/shared/i18n'
import { PROJECT_ORDER_CASES } from '../helpers/project-order-fixtures'
import type {
Citation,
Collection,
Conversation,
DocumentInfo,
DocumentScope,
KnowledgePack,
KnowledgePackOutcome,
KnowledgePackOutcomeReason,
PackArticleSaveResult,
RuntimeStatus
} from '../../src/shared/types'
import { MAX_SELECTED_PACKS } from '../../src/shared/types'
import { stubApi, assertNoUnexpectedApiCalls } from '../helpers/renderer'
import type { PreloadApi } from '../../src/preload'
import { knowledgePack, packsEventEmitter } from '../helpers/knowledge-pack-fixtures'
import { testBudgetMs } from '../helpers/hang-budget'
import { chatScreenStubs } from '../helpers/chat-screen'
import { ENGINE_INSTALLED } from '../helpers/model-fixtures'
import { makePolicyStatus } from '../helpers/status'
// Knowledge packs (ZIM wave) renderer surfaces: the PacksPanel management list, the
// ScopePopover pack sources (incl. packIds preservation on unrelated toggles), and the
// offline ArticleModal's honest states. The tools install lives in KnowledgePackToolsInstall.test.tsx.
const pack = (o: Partial<KnowledgePack> = {}): KnowledgePack =>
knowledgePack({ description: 'Offline-Auszug', ...o })
/** The packs panel subscribes to pack changes at mount (#832); a row that emits supplies its own. `over` spreads last. */
function packsPanelStubs(over: Partial<PreloadApi>): Partial<PreloadApi> {
return { onKnowledgePacksChanged: vi.fn<PreloadApi['onKnowledgePacksChanged']>(() => () => {}), ...over }
}
afterEach(() => {
cleanup()
window.localStorage.clear()
assertNoUnexpectedApiCalls()
})
describe('PacksPanel', () => {
it('lists packs with state badges and meta; missing file shows the honest badge', async () => {
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: true, refreshing: false, revision: 0 }),
listKnowledgePacks: async () => [
pack(),
pack({ id: 'uuid-gone', title: 'Chemie von Wikipedia', available: false, enabled: true })
]
}))
render(
<I18nProvider>
<PacksPanel />
</I18nProvider>
)
expect(await screen.findByText('Klimawandel von Wikipedia')).toBeInTheDocument()
// §11.16: both packs are enabled, so both switches read "Enabled" — the missing file is a
// badge BESIDE the switch, not a replacement for its state.
expect(screen.getAllByText('Enabled')).toHaveLength(2)
expect(screen.getByText('File missing')).toBeInTheDocument()
expect(screen.getAllByText(/4102 articles/).length).toBeGreaterThan(0)
// #340 nit: the ISO 639-3 code is shown as a language NAME in the UI language, never raw.
expect(screen.getAllByText(/German · 4102 articles/).length).toBe(2)
expect(screen.queryByText(/\bdeu\b/)).toBeNull()
// #340 nit: an unavailable pack can be disabled, not only removed. §11.16: enablement is
// ONE switch per row (checked = enabled), named after the pack.
expect(screen.getByRole('switch', { name: 'Use Chemie von Wikipedia in chats' })).toBeEnabled()
expect(screen.getByRole('switch', { name: 'Use Chemie von Wikipedia in chats' })).toBeChecked()
})
// #340 nits: a row's buttons disable only while THAT row is busy — another row's toggle or
// remove is an independent operation — and the meta line's language name follows the UI
// language, with the raw code kept only when the platform cannot name it.
it('#340 nits: only the busy row disables, an unavailable pack can be disabled, the language is named in German too and an unknown code stays raw', async () => {
let release!: () => void
const parked = new Promise<void>((r) => (release = r))
const setKnowledgePackEnabled = vi.fn(async () => {
await parked
})
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: true, refreshing: false, revision: 0 }),
listKnowledgePacks: async () => [
pack(),
pack({ id: 'uuid-gone', title: 'Chemie von Wikipedia', available: false, enabled: true, language: 'zzz' })
],
setKnowledgePackEnabled
}))
const user = userEvent.setup()
const first = render(
<I18nProvider>
<PacksPanel />
</I18nProvider>
)
expect(await screen.findByText('Klimawandel von Wikipedia')).toBeInTheDocument()
// The unknown code echoes back as itself — no invented name.
expect(screen.getByText(/^zzz · 4102 articles/)).toBeInTheDocument()
await user.click(screen.getByRole('switch', { name: 'Use Klimawandel von Wikipedia in chats' }))
await waitFor(() => expect(setKnowledgePackEnabled).toHaveBeenCalledWith('uuid-climate', false))
// This row is working; the other row's controls are untouched.
expect(screen.getByRole('switch', { name: 'Use Klimawandel von Wikipedia in chats' })).toBeDisabled()
expect(screen.getByRole('button', { name: 'More actions for Klimawandel von Wikipedia' })).toBeDisabled()
expect(screen.getByRole('switch', { name: 'Use Chemie von Wikipedia in chats' })).toBeEnabled()
expect(screen.getByRole('button', { name: 'More actions for Chemie von Wikipedia' })).toBeEnabled()
release()
await waitFor(() =>
expect(screen.getByRole('switch', { name: 'Use Klimawandel von Wikipedia in chats' })).toBeEnabled()
)
// …and the unavailable pack's switch really reaches the bridge.
await user.click(screen.getByRole('switch', { name: 'Use Chemie von Wikipedia in chats' }))
await waitFor(() => expect(setKnowledgePackEnabled).toHaveBeenCalledWith('uuid-gone', false))
first.unmount()
window.localStorage.setItem(UI_LANGUAGE_STORAGE_KEY, 'de')
render(
<I18nProvider>
<PacksPanel />
</I18nProvider>
)
expect(await screen.findByText('Klimawandel von Wikipedia')).toBeInTheDocument()
expect(screen.getByText(/^Deutsch · 4102 Artikel/)).toBeInTheDocument()
})
// #340 (rag-design D-Z16): the served library's collision losers ride `packs:status.excluded`
// — a served-library fact, so it is a badge + a visible reason line on the LOSER's row that
// names the winner, never a row field; a status without the field (an older main) or with
// `null` (nothing computed yet) shows nothing.
it('a serving-name collision loser gets a "Not served" badge naming the winner; null / absent status shows none', async () => {
const winner = pack({ id: 'uuid-aaaa', title: 'Klimawandel von Wikipedia' })
const loser = pack({ id: 'uuid-bbbb', title: 'Klimawandel (Kopie)', leaf: 'wikipedia_de_climate.zim' })
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({
toolsInstalled: true,
refreshing: false,
revision: 0,
excluded: [{ packId: 'uuid-bbbb', collidesWith: 'uuid-aaaa' }]
}),
listKnowledgePacks: async () => [winner, loser]
}))
const { unmount } = render(
<I18nProvider>
<PacksPanel />
</I18nProvider>
)
expect(await screen.findByText('Klimawandel (Kopie)')).toBeInTheDocument()
// The loser's row: the badge and the visible line naming the winner (guidelines §7 — the
// reason is reachable without a mouse). The winner's row carries neither.
expect(screen.getByText('Not served')).toBeInTheDocument()
expect(
screen.getByText(/clashes with “Klimawandel von Wikipedia”, so only that pack is served/)
).toBeInTheDocument()
const loserCard = screen.getByText('Klimawandel (Kopie)').closest('li')
const winnerCard = screen.getByText('Klimawandel von Wikipedia').closest('li')
expect(loserCard).not.toBeNull()
expect(loserCard!.textContent).toContain('Not served')
expect(winnerCard!.textContent).not.toContain('Not served')
// Both stay Enabled: a collision is not a row state.
expect(screen.getAllByText('Enabled')).toHaveLength(2)
unmount()
// A winner the list no longer knows (a stale list): the generic line, still a badge.
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({
toolsInstalled: true,
refreshing: false,
revision: 0,
excluded: [{ packId: 'uuid-bbbb', collidesWith: 'uuid-gone' }]
}),
listKnowledgePacks: async () => [loser]
}))
const second = render(
<I18nProvider>
<PacksPanel />
</I18nProvider>
)
expect(await screen.findByText('Not served')).toBeInTheDocument()
expect(screen.getByText(/clashes with another pack’s/)).toBeInTheDocument()
second.unmount()
// `null` (nothing computed yet) and an ABSENT field (an older main): no badge at all.
for (const status of [
{ toolsInstalled: true, refreshing: false, revision: 0, excluded: null },
{ toolsInstalled: true, refreshing: false, revision: 0 }
]) {
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => status,
listKnowledgePacks: async () => [winner, loser]
}))
const r = render(
<I18nProvider>
<PacksPanel />
</I18nProvider>
)
expect(await screen.findByText('Klimawandel (Kopie)')).toBeInTheDocument()
expect(screen.queryByText('Not served')).toBeNull()
r.unmount()
}
})
it('shows the tools-missing hint and disables adding when kiwix-tools is absent', async () => {
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: false, refreshing: false, revision: 0 }),
listKnowledgePacks: async () => [],
// With the tools missing the panel reads the engine and the policy (the install offer).
getEngineStatus: async () => ENGINE_INSTALLED,
getPolicy: async () => makePolicyStatus()
}))
render(
<I18nProvider>
<PacksPanel />
</I18nProvider>
)
expect(await screen.findByText(/kiwix-tools binaries are not installed/)).toBeInTheDocument()
expect(screen.getByRole('button', { name: 'Add packs…' })).toBeDisabled()
})
it('#339 P8-2: never calls getEngineStatus or getPolicy while the tools are already installed', async () => {
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: true, refreshing: false, revision: 0 }),
listKnowledgePacks: async () => [pack()],
onKnowledgePacksChanged: () => () => {}
}))
render(
<I18nProvider>
<PacksPanel />
</I18nProvider>
)
expect(await screen.findByText('Klimawandel von Wikipedia')).toBeInTheDocument()
expect(screen.queryByText(/kiwix-tools binaries are not installed/)).toBeNull()
assertNoUnexpectedApiCalls()
})
it('add flow calls the main-side dialog channel and refreshes on success', async () => {
let added = false
const addKnowledgePacks = vi.fn(async () => {
added = true
return { outcome: 'success' as const, added: [pack()], failed: 0, failureReason: null }
})
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: true, refreshing: false, revision: 0 }),
listKnowledgePacks: async () => (added ? [pack()] : []),
addKnowledgePacks
}))
const user = userEvent.setup()
render(
<I18nProvider>
<ToastProvider>
<PacksPanel />
</ToastProvider>
</I18nProvider>
)
expect(await screen.findByText('No knowledge packs yet')).toBeInTheDocument()
await user.click(screen.getByRole('button', { name: 'Add packs…' }))
expect(await screen.findByText('Klimawandel von Wikipedia')).toBeInTheDocument()
expect(addKnowledgePacks).toHaveBeenCalledTimes(1)
expect(await screen.findByText('Knowledge pack added')).toBeInTheDocument()
})
// #301 P5, finding L1 (plan §9.19 (c)3): the typed add-result DTO's four outcomes, asserted
// through the RENDERED copy — the generic mixed-add string for 'partial', the mapped
// reason-specific string for 'failure', nothing for 'cancelled'.
it('add flow: cancelled shows nothing and never refreshes', async () => {
const addKnowledgePacks = vi.fn(async () => ({
outcome: 'cancelled' as const,
added: [],
failed: 0,
failureReason: null
}))
const listKnowledgePacks = vi.fn(async () => [])
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: true, refreshing: false, revision: 0 }),
listKnowledgePacks,
addKnowledgePacks
}))
const user = userEvent.setup()
render(
<I18nProvider>
<PacksPanel />
</I18nProvider>
)
expect(await screen.findByText('No knowledge packs yet')).toBeInTheDocument()
const callsBefore = listKnowledgePacks.mock.calls.length
await user.click(screen.getByRole('button', { name: 'Add packs…' }))
await waitFor(() => expect(addKnowledgePacks).toHaveBeenCalledTimes(1))
// No refresh, no toast, no banner — 'cancelled' does nothing (§9.19 (c)3).
expect(listKnowledgePacks.mock.calls.length).toBe(callsBefore)
expect(screen.queryByText('Knowledge pack added')).not.toBeInTheDocument()
})
it('add flow: partial shows the toast for the added count AND the generic mixed-add banner', async () => {
let added = false
const addKnowledgePacks = vi.fn(async () => {
added = true
return { outcome: 'partial' as const, added: [pack()], failed: 1, failureReason: 'manager' as const }
})
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: true, refreshing: false, revision: 0 }),
listKnowledgePacks: async () => (added ? [pack()] : []),
addKnowledgePacks
}))
const user = userEvent.setup()
render(
<I18nProvider>
<ToastProvider>
<PacksPanel />
</ToastProvider>
</I18nProvider>
)
expect(await screen.findByText('No knowledge packs yet')).toBeInTheDocument()
await user.click(screen.getByRole('button', { name: 'Add packs…' }))
expect(await screen.findByText('Knowledge pack added')).toBeInTheDocument()
// The generic banner — a `failureReason` never renders anything but this mapped copy.
expect(await screen.findByText('1 of 2 archives could not be added.')).toBeInTheDocument()
expect(await screen.findByText('Klimawandel von Wikipedia')).toBeInTheDocument()
})
it.each([
['not-a-zim', 'The chosen file is not a readable ZIM archive.'],
['tools-missing', /kiwix-tools binaries are not installed/],
['manager', 'The archive could not be read by kiwix-manage. Check that the file is complete and try again.'],
['path-unsupported', /kiwix-manage cannot read this archive because its file name or folder contains/],
['other', 'The archive could not be added.']
] as const)('add flow: failure (%s) shows the reason’s banner text, never a different one', async (reason, expected) => {
const addKnowledgePacks = vi.fn(async () => ({
outcome: 'failure' as const,
added: [],
failed: 1,
failureReason: reason
}))
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: true, refreshing: false, revision: 0 }),
listKnowledgePacks: async () => [],
addKnowledgePacks
}))
const user = userEvent.setup()
render(
<I18nProvider>
<PacksPanel />
</I18nProvider>
)
expect(await screen.findByText('No knowledge packs yet')).toBeInTheDocument()
await user.click(screen.getByRole('button', { name: 'Add packs…' }))
expect(await screen.findByText(expected)).toBeInTheDocument()
expect(screen.queryByText('Knowledge pack added')).not.toBeInTheDocument()
})
// #301 P6 (plan §9.23, task 6(e)): the same four failure reasons in German — the DE catalog
// carries its own mapped banner text, never a raw reason code or manager detail.
it.each([
['not-a-zim', 'Die gewählte Datei ist kein lesbares ZIM-Archiv.'],
['tools-missing', /kiwix-tools-Programme sind auf diesem Laufwerk nicht installiert/],
[
'manager',
'Das Archiv konnte nicht von kiwix-manage gelesen werden. Prüfe, ob die Datei vollständig ist, und versuch es noch einmal.'
],
['path-unsupported', /kiwix-manage dieses Archiv nicht lesen, weil sein Datei- oder Ordnername/],
['other', 'Das Archiv konnte nicht hinzugefügt werden.']
] as const)('add flow (DE): failure (%s) shows the German reason banner', async (reason, expected) => {
window.localStorage.setItem(UI_LANGUAGE_STORAGE_KEY, 'de')
const addKnowledgePacks = vi.fn(async () => ({
outcome: 'failure' as const,
added: [],
failed: 1,
failureReason: reason
}))
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: true, refreshing: false, revision: 0 }),
listKnowledgePacks: async () => [],
addKnowledgePacks
}))
const user = userEvent.setup()
render(
<I18nProvider>
<PacksPanel />
</I18nProvider>
)
expect(await screen.findByText('Noch keine Wissenspakete')).toBeInTheDocument()
await user.click(screen.getByRole('button', { name: 'Pakete hinzufügen…' }))
expect(await screen.findByText(expected)).toBeInTheDocument()
})
it('remove asks for confirmation and says the file is untouched', async () => {
const removeKnowledgePack = vi.fn(async () => undefined)
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: true, refreshing: false, revision: 0 }),
listKnowledgePacks: async () => [pack()],
removeKnowledgePack
}))
const user = userEvent.setup()
render(
<I18nProvider>
<PacksPanel />
</I18nProvider>
)
// §11.16: Remove lives behind the row's "⋯" menu (named after the pack), then the confirm.
await user.click(await screen.findByRole('button', { name: 'More actions for Klimawandel von Wikipedia' }))
await user.click(await screen.findByRole('menuitem', { name: 'Remove' }))
expect(await screen.findByText(/archive file on disk is not touched/)).toBeInTheDocument()
await user.click(screen.getByRole('button', { name: 'Remove pack' }))
await waitFor(() => expect(removeKnowledgePack).toHaveBeenCalledWith('uuid-climate'))
})
// #301 P3b, finding L7: DB-only list, live refresh state, and the pack-update event.
it('shows the refreshing line while status.refreshing, and a reconcile-end event clears it and refetches', async () => {
const emitter = packsEventEmitter()
let listCalls = 0
const listKnowledgePacks = vi.fn(async () => {
listCalls++
return listCalls === 1 ? [] : [pack()]
})
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: true, refreshing: true, revision: 0 }),
listKnowledgePacks,
onKnowledgePacksChanged: emitter.onKnowledgePacksChanged
}))
render(
<I18nProvider>
<PacksPanel />
</I18nProvider>
)
expect(await screen.findByText('Checking the drive for packs…')).toBeInTheDocument()
act(() => emitter.emit({ epoch: 1, revision: 1, refreshing: false, reason: 'reconcile-end' }))
await waitFor(() =>
expect(screen.queryByText('Checking the drive for packs…')).not.toBeInTheDocument()
)
expect(await screen.findByText('Klimawandel von Wikipedia')).toBeInTheDocument()
expect(listKnowledgePacks).toHaveBeenCalledTimes(2)
})
it('Refresh calls packs:refresh', async () => {
const refreshKnowledgePacks = vi.fn(async () => ({ started: true }))
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: true, refreshing: false, revision: 0 }),
listKnowledgePacks: async () => [pack()],
refreshKnowledgePacks
}))
const user = userEvent.setup()
render(
<I18nProvider>
<PacksPanel />
</I18nProvider>
)
await user.click(await screen.findByRole('button', { name: 'Refresh' }))
expect(refreshKnowledgePacks).toHaveBeenCalledTimes(1)
})
it('ignores a packs:changed event whose epoch is below the last one seen', async () => {
const emitter = packsEventEmitter()
const listKnowledgePacks = vi.fn(async () => [pack()])
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: true, refreshing: false, revision: 0 }),
listKnowledgePacks,
onKnowledgePacksChanged: emitter.onKnowledgePacksChanged
}))
render(
<I18nProvider>
<PacksPanel />
</I18nProvider>
)
await screen.findByText('Klimawandel von Wikipedia')
expect(listKnowledgePacks).toHaveBeenCalledTimes(1)
// A newer epoch first advances what counts as "last seen"…
act(() => emitter.emit({ epoch: 5, revision: 2, refreshing: false, reason: 'mutation' }))
await waitFor(() => expect(listKnowledgePacks).toHaveBeenCalledTimes(2))
// …then an OLDER epoch (an old session's late announcement) must change nothing: the
// refreshing line never appears from a stale reconcile-start (act() has flushed the handler,
// so a taken event would show it now), and a newer event behind it is the only refetch.
act(() => emitter.emit({ epoch: 3, revision: 3, refreshing: true, reason: 'reconcile-start' }))
expect(screen.queryByText('Checking the drive for packs…')).not.toBeInTheDocument()
act(() => emitter.emit({ epoch: 6, revision: 4, refreshing: false, reason: 'mutation' }))
await waitFor(() => expect(listKnowledgePacks).toHaveBeenCalledTimes(3))
expect(listKnowledgePacks).toHaveBeenCalledTimes(3)
expect(screen.queryByText('Checking the drive for packs…')).not.toBeInTheDocument()
})
// #301 P6 (plan §9.23 (a) rows 5/6, (c)5): the NEW no-full-text-index badge shows BESIDE
// the enabled/disabled badge (a pack can be both), never for unknown/yes/undefined.
it('shows the no-full-text-index badge and its reason line beside the enabled badge; absent for unknown/yes/undefined', async () => {
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: true, refreshing: false, revision: 0 }),
listKnowledgePacks: async () => [
pack({ id: 'p-no', title: 'No-index pack', searchable: 'no' }),
pack({ id: 'p-unknown', title: 'Unknown pack', searchable: 'unknown' }),
pack({ id: 'p-yes', title: 'Yes pack', searchable: 'yes' }),
pack({ id: 'p-undef', title: 'Undefined pack', searchable: undefined })
]
}))
render(
<I18nProvider>
<PacksPanel />
</I18nProvider>
)
expect(await screen.findByText('No-index pack')).toBeInTheDocument()
// Exactly ONE pack (searchable: 'no') carries the badge and its visible reason line.
expect(screen.getAllByText('No full-text index')).toHaveLength(1)
expect(
screen.getByText(
'This archive has no full-text search index. It is skipped when asking, but its articles stay readable.'
)
).toBeInTheDocument()
// Every pack still carries its enabled badge — the two badges sit BESIDE each other.
expect(screen.getAllByText('Enabled')).toHaveLength(4)
})
it('shows the no-full-text-index badge and reason line in German', async () => {
window.localStorage.setItem(UI_LANGUAGE_STORAGE_KEY, 'de')
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: true, refreshing: false, revision: 0 }),
listKnowledgePacks: async () => [pack({ id: 'p-no', title: 'Paket ohne Index', searchable: 'no' })]
}))
render(
<I18nProvider>
<PacksPanel />
</I18nProvider>
)
expect(await screen.findByText('Kein Volltextindex')).toBeInTheDocument()
expect(
screen.getByText(
'Dieses Archiv hat keinen Volltextindex. Beim Fragen wird es übersprungen, seine Artikel bleiben aber lesbar.'
)
).toBeInTheDocument()
})
// #301 P6 (plan §9.23 (b)3/7): the panel's reason text for missing / identity-mismatch is
// reachable WITHOUT a mouse — a visible line under the title row, not only a Badge tooltip.
// The German copy is the next test.
it('renders the missing/identity-mismatch reason text visibly (EN), not only in a Badge tooltip', async () => {
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: true, refreshing: false, revision: 0 }),
listKnowledgePacks: async () => [
pack({ id: 'p-missing', title: 'Missing pack', available: false, unavailableReason: 'missing' }),
pack({
id: 'p-mismatch',
title: 'Mismatch pack',
available: false,
unavailableReason: 'identity-mismatch'
})
]
}))
render(
<I18nProvider>
<PacksPanel />
</I18nProvider>
)
expect(await screen.findByText(/archive file could not be found/)).toBeInTheDocument()
expect(screen.getByText(/is a different archive/)).toBeInTheDocument()
// The replaced archive's badge sits beside its line.
expect(screen.getByText('Different archive')).toBeInTheDocument()
})
it('renders the missing/identity-mismatch reason text visibly in German', async () => {
window.localStorage.setItem(UI_LANGUAGE_STORAGE_KEY, 'de')
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: true, refreshing: false, revision: 0 }),
listKnowledgePacks: async () => [
pack({ id: 'p-missing', title: 'Fehlendes Paket', available: false, unavailableReason: 'missing' }),
pack({
id: 'p-mismatch',
title: 'Abweichendes Paket',
available: false,
unavailableReason: 'identity-mismatch'
})
]
}))
render(
<I18nProvider>
<PacksPanel />
</I18nProvider>
)
expect(await screen.findByText(/Archivdatei wurde nicht gefunden/)).toBeInTheDocument()
expect(screen.getByText(/ist ein anderes Archiv/)).toBeInTheDocument()
})
// #301 P6 (plan §9.23 (b)7): `.packs-list` is an ARIA list — one listitem per pack.
it('renders the packs list with list/listitem roles, one listitem per pack', async () => {
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: true, refreshing: false, revision: 0 }),
listKnowledgePacks: async () => [pack(), pack({ id: 'p2', title: 'Second pack' })]
}))
render(
<I18nProvider>
<PacksPanel />
</I18nProvider>
)
const list = await screen.findByRole('list', { name: 'Knowledge packs' })
expect(within(list).getAllByRole('listitem')).toHaveLength(2)
})
// #301 P6 (plan §9.23 (b)6) / §11.16: the per-row switch and "⋯" menu get an accessible
// name that includes the pack title — the VISIBLE text stays the plain state word.
it('names the per-row switch and menu with the pack title', async () => {
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: true, refreshing: false, revision: 0 }),
listKnowledgePacks: async () => [
pack({ enabled: true }),
pack({ id: 'p-off', title: 'Off pack', enabled: false })
]
}))
render(
<I18nProvider>
<PacksPanel />
</I18nProvider>
)
await screen.findByText('Klimawandel von Wikipedia')
expect(screen.getByRole('switch', { name: 'Use Klimawandel von Wikipedia in chats' })).toBeChecked()
expect(screen.getByRole('switch', { name: 'Use Off pack in chats' })).not.toBeChecked()
expect(screen.getByRole('button', { name: 'More actions for Klimawandel von Wikipedia' })).toBeInTheDocument()
expect(screen.getByRole('button', { name: 'More actions for Off pack' })).toBeInTheDocument()
// Visible text is unchanged — the plain state word, never the pack title inline.
expect(screen.getByText('Enabled')).toBeInTheDocument()
expect(screen.getByText('Disabled')).toBeInTheDocument()
})
it('names the per-row switch and menu with the pack title in German', async () => {
window.localStorage.setItem(UI_LANGUAGE_STORAGE_KEY, 'de')
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: true, refreshing: false, revision: 0 }),
listKnowledgePacks: async () => [
pack({ enabled: true }),
pack({ id: 'p-off', title: 'Anderes Paket', enabled: false })
]
}))
render(
<I18nProvider>
<PacksPanel />
</I18nProvider>
)
await screen.findByText('Klimawandel von Wikipedia')
expect(
screen.getByRole('switch', { name: 'Klimawandel von Wikipedia in Chats nutzen' })
).toBeChecked()
expect(screen.getByRole('switch', { name: 'Anderes Paket in Chats nutzen' })).not.toBeChecked()
expect(
screen.getByRole('button', { name: 'Weitere Aktionen für Klimawandel von Wikipedia' })
).toBeInTheDocument()
expect(screen.getByRole('button', { name: 'Weitere Aktionen für Anderes Paket' })).toBeInTheDocument()
})
// §11.16: the panel's summary line, and "Ask this pack" — offered only for a pack an ask would
// actually search (present, enabled, served, not a confirmed no-index archive).
it('shows the summary line and offers "Ask this pack" only for an askable pack', async () => {
const onAskPack = vi.fn()
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: true, refreshing: false, revision: 0 }),
listKnowledgePacks: async () => [
pack({ sizeBytes: 2_000_000_000 }),
pack({ id: 'p-off', title: 'Off pack', enabled: false, sizeBytes: 500_000_000 }),
pack({ id: 'p-noindex', title: 'No index', searchable: 'no', sizeBytes: null }),
pack({ id: 'p-gone', title: 'Gone pack', available: false, unavailableReason: 'missing', sizeBytes: 9_000_000_000 })
]
}))
const user = userEvent.setup()
render(
<I18nProvider>
<PacksPanel onAskPack={onAskPack} />
</I18nProvider>
)
await screen.findByText('Klimawandel von Wikipedia')
// 4 packs, 3 enabled; the size counts only the archives that are present (2 GB + 0.5 GB, decimal like every disk size, #768).
expect(screen.getByText('4 packs · 3 enabled · 2.5 GB on this drive')).toBeInTheDocument()
const asks = screen.getAllByRole('button', { name: 'Ask this pack' })
expect(asks).toHaveLength(4)
// Listed in title order (#958): Gone pack, Klimawandel, No index, Off pack.
expect(asks[0]).toBeDisabled()
expect(asks[1]).toBeEnabled()
expect(asks[2]).toBeDisabled()
expect(asks[3]).toBeDisabled()
await user.click(asks[1])
expect(onAskPack).toHaveBeenCalledWith('uuid-climate')
})
it('renders no "Ask this pack" without the callback, and the empty state carries Add + the library address', async () => {
const copyToClipboard = vi.fn(async (_text: string) => true)
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: true, refreshing: false, revision: 0 }),
listKnowledgePacks: async () => [],
copyToClipboard
}))
const user = userEvent.setup()
render(
<I18nProvider>
<ToastProvider>
<PacksPanel />
</ToastProvider>
</I18nProvider>
)
expect(await screen.findByText('No knowledge packs yet')).toBeInTheDocument()
expect(screen.queryByRole('button', { name: 'Ask this pack' })).toBeNull()
// ONE primary "Add packs…" — in the empty state, not the head as well.
expect(screen.getAllByRole('button', { name: 'Add packs…' })).toHaveLength(1)
// The copy goes through MAIN (`clipboard:write`), never `navigator.clipboard`: user-event
// installs a working stub of the latter, so asserting on it would pass in the test and
// still fail in the packaged renderer (the "Zwischenablage" error, preload/index.ts).
const writeText = vi.spyOn(navigator.clipboard, 'writeText')
await user.click(screen.getByRole('button', { name: 'Copy the library address' }))
expect(copyToClipboard).toHaveBeenCalledWith('https://library.kiwix.org')
expect(writeText).not.toHaveBeenCalled()
expect(await screen.findByText('Copied library.kiwix.org')).toBeInTheDocument()
})
it('a refused or failing clipboard write still names the library address', async () => {
for (const copyToClipboard of [async () => false, async () => Promise.reject(new Error('ipc'))]) {
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: true, refreshing: false, revision: 0 }),
listKnowledgePacks: async () => [],
copyToClipboard
}))
const user = userEvent.setup()
const view = render(
<I18nProvider>
<ToastProvider>
<PacksPanel />
</ToastProvider>
</I18nProvider>
)
await user.click(await screen.findByRole('button', { name: 'Copy the library address' }))
expect(await screen.findByText('Could not copy — the address is library.kiwix.org')).toBeInTheDocument()
expect(screen.queryByText('Copied library.kiwix.org')).toBeNull()
view.unmount()
}
})
})
describe('ScopePopover — knowledge packs', () => {
const collections: Collection[] = [
{
id: 'lib',
name: 'Library',
type: 'library',
description: null,
builtin: true,
color: null,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
archivedAt: null
} as Collection
]
const doc = {
id: 'd1',
title: 'contract.pdf',
status: 'indexed',
chunkCount: 1
} as DocumentInfo
it('renders the packs section, toggles a pack, and an unavailable pack is not tickable', async () => {
stubApi({})
const emitted: DocumentScope[] = []
const user = userEvent.setup()
render(
<I18nProvider>
<ScopePopover
docs={[doc]}
collections={collections}
packs={[pack(), pack({ id: 'uuid-gone', title: 'Chemie von Wikipedia', available: false })]}
scope={{ collectionIds: [], documentIds: [] }}
onChangeScope={(next) => emitted.push(next)}
/>
</I18nProvider>
)
await user.click(screen.getByRole('button'))
expect(await screen.findByText('Knowledge packs')).toBeInTheDocument()
const packBox = screen.getByRole('checkbox', { name: /Klimawandel von Wikipedia/ })
const goneBox = screen.getByRole('checkbox', { name: /Chemie von Wikipedia/ })
expect(goneBox).toBeDisabled()
await user.click(packBox)
expect(emitted.at(-1)).toEqual({
collectionIds: [],
documentIds: [],
packIds: ['uuid-climate']
})
})
it('keeps the picker for a pack-only corpus (no documents imported yet)', async () => {
// Regression: the empty-corpus early return used to collapse the picker into the
// "Add documents" jump, making the packs section unreachable in a fresh workspace
// with an offline Wikipedia added but nothing imported (found in the live demo).
stubApi({})
const emitted: DocumentScope[] = []
const user = userEvent.setup()
render(
<I18nProvider>
<ScopePopover
docs={[]}
collections={collections}
packs={[pack()]}
scope={{ collectionIds: [], documentIds: [] }}
onChangeScope={(next) => emitted.push(next)}
/>
</I18nProvider>
)
await user.click(screen.getByRole('button'))
await user.click(await screen.findByRole('checkbox', { name: /Klimawandel von Wikipedia/ }))
expect(emitted.at(-1)?.packIds).toEqual(['uuid-climate'])
})
// ---- Documents toggle (#301 P4, finding M10, ruling D4) --------------------------------
// The explicit "answer from the ticked packs, not from my documents" control. It exists only
// where the packs section exists, the flag is never derived from an empty selection, and the
// chip says what the ask will really do (the resolved-scope half is T10-a in
// tests/integration/zim-regressions.test.ts).
it('unticking Documents emits the flag with cleared document sources, keeping the ticked packs', async () => {
stubApi({})
const emitted: DocumentScope[] = []
const user = userEvent.setup()
render(
<I18nProvider>
<ScopePopover
docs={[doc]}
collections={collections}
packs={[pack()]}
scope={{ collectionIds: ['lib'], documentIds: ['d1'], packIds: ['uuid-climate'] }}
onChangeScope={(next) => emitted.push(next)}
/>
</I18nProvider>
)
await user.click(screen.getByRole('button'))
const documentsBox = await screen.findByRole('checkbox', { name: /Search my documents/ })
expect(documentsBox).toBeChecked()
await user.click(documentsBox)
expect(emitted.at(-1)).toEqual({
collectionIds: [],
documentIds: [],
packIds: ['uuid-climate'],
documentsOff: true
})
})
it('ticking a collection while documents are off clears the flag (an emit never carries it with a document source)', async () => {
stubApi({})
const emitted: DocumentScope[] = []
const user = userEvent.setup()
render(
<I18nProvider>
<ScopePopover
docs={[doc]}
collections={collections}
packs={[pack()]}
scope={{ collectionIds: [], documentIds: [], packIds: ['uuid-climate'], documentsOff: true }}
onChangeScope={(next) => emitted.push(next)}
/>
</I18nProvider>
)
await user.click(screen.getByRole('button'))
expect(await screen.findByRole('checkbox', { name: /Search my documents/ })).not.toBeChecked()
await user.click(screen.getByRole('checkbox', { name: /Library/ }))
expect(emitted.at(-1)).toEqual({
collectionIds: ['lib'],
documentIds: [],
packIds: ['uuid-climate']
})
// Ticking the Documents row itself is the other way back: the legacy empty scope.
await user.click(screen.getByRole('checkbox', { name: /Search my documents/ }))
expect(emitted.at(-1)).toEqual({ collectionIds: [], documentIds: [], packIds: ['uuid-climate'] })
// Toggling a PACK while documents are off preserves the flag (spread-preservation).
await user.click(screen.getByRole('checkbox', { name: /Klimawandel von Wikipedia/ }))
expect(emitted.at(-1)).toEqual({ collectionIds: [], documentIds: [], documentsOff: true })
})
// #340 nit (the T18-b observation): a ticked pack that was disabled or went missing afterwards
// is still named by the chip — WITH its state, in the popover row's own words — so the
// "Answering from:" readout never claims a source the ask will skip.
it('the chip names a ticked pack that is disabled or unavailable with its state', () => {
stubApi({})
const scope = { collectionIds: [], documentIds: [], packIds: ['uuid-climate'] }
const disabled = render(
<I18nProvider>
<ScopePopover
docs={[doc]}
collections={collections}
packs={[pack({ enabled: false })]}
scope={scope}
onChangeScope={() => {}}
/>
</I18nProvider>
)
expect(screen.getByRole('button')).toHaveTextContent(
tr('en', 'chat.scope.packNamedState', { name: pack().title, state: tr('en', 'chat.scope.packDisabled') })
)
disabled.unmount()
const missing = render(
<I18nProvider>
<ScopePopover
docs={[doc]}
collections={collections}
packs={[pack({ available: false, unavailableReason: 'missing' })]}
scope={scope}
onChangeScope={() => {}}
/>
</I18nProvider>
)
expect(screen.getByRole('button')).toHaveTextContent(
tr('en', 'chat.scope.packNamedState', { name: pack().title, state: tr('en', 'chat.scope.packUnavailable') })
)
missing.unmount()
render(
<I18nProvider>
<ScopePopover docs={[doc]} collections={collections} packs={[pack()]} scope={scope} onChangeScope={() => {}} />
</I18nProvider>
)
expect(screen.getByRole('button')).toHaveTextContent(tr('en', 'chat.scope.packNamed', { name: pack().title }))
expect(screen.getByRole('button')).not.toHaveTextContent('(')
})
it('the chip says "documents off" beside the packs phrase, the hint names what stays on, and the reset clears the flag', async () => {
stubApi({})
const emitted: DocumentScope[] = []
const user = userEvent.setup()
render(
<I18nProvider>
<ScopePopover
docs={[doc]}
collections={collections}
packs={[pack()]}
scope={{ collectionIds: [], documentIds: [], packIds: ['uuid-climate'], documentsOff: true }}
onChangeScope={(next) => emitted.push(next)}
/>
</I18nProvider>
)
const trigger = screen.getByRole('button')
expect(trigger).toHaveTextContent(
`${tr('en', 'chat.scope.packNamed', { name: pack().title })} · ${tr('en', 'chat.scope.documentsOffSuffix')}`
)
await user.click(trigger)
expect(await screen.findByText(/Files attached to this chat are still used/)).toBeInTheDocument()
await user.click(screen.getByRole('button', { name: /All documents/ }))
expect(emitted.at(-1)).toEqual({ collectionIds: [], documentIds: [] })
})
it('renders no Documents toggle when no pack is registered (the popover stays byte-identical)', async () => {
stubApi({})
const user = userEvent.setup()
render(
<I18nProvider>
<ScopePopover
docs={[doc]}
collections={collections}
packs={[]}
scope={{ collectionIds: [], documentIds: [] }}
onChangeScope={() => {}}
/>
</I18nProvider>
)
await user.click(screen.getByRole('button'))
expect(await screen.findByRole('checkbox', { name: /Library/ })).toBeInTheDocument()
expect(screen.queryByRole('checkbox', { name: /Search my documents/ })).toBeNull()
})
// §11.16: with no pack registered the picker offers the way in instead of omitting the section.
it('with no pack registered and an onAddPacks callback, the picker shows the way in', async () => {
const onAddPacks = vi.fn()
stubApi({})
const user = userEvent.setup()
render(
<I18nProvider>
<ScopePopover
docs={[doc]}
collections={collections}
packs={[]}
scope={{ collectionIds: [], documentIds: [] }}
onChangeScope={() => {}}
onAddPacks={onAddPacks}
/>
</I18nProvider>
)
await user.click(screen.getByRole('button'))
expect(await screen.findByText('No knowledge packs on this drive yet.')).toBeInTheDocument()
// Still no Documents toggle — there is nothing to turn documents off FOR.
expect(screen.queryByRole('checkbox', { name: /Search my documents/ })).toBeNull()
await user.click(screen.getByRole('button', { name: 'Add packs…' }))
expect(onAddPacks).toHaveBeenCalledTimes(1)
})
// ---- D6 + the selection cap (#301 P4, ruling §7; plan §9.21 (c)6 / (e)8) ------------------
// The rule these pin: a pack the user TICKED stays untickable-able whatever state it is in — a
// greyed box on a selection that is silently contributing nothing is a trap. Ineligibility is
// SHOWN, never hidden, and the 13th tick is refused where the user can see why.
it('refuses the 13th tick, disables every unticked pack at the cap, and shows the limit line once', async () => {
stubApi({})
const emitted: DocumentScope[] = []
const user = userEvent.setup()
const many = Array.from({ length: MAX_SELECTED_PACKS + 1 }, (_, i) =>
pack({ id: `uuid-${i}`, title: `Pack ${String(i).padStart(2, '0')}` })
)
const selected = many.slice(0, MAX_SELECTED_PACKS).map((k) => k.id)
render(
<I18nProvider>
<ScopePopover
docs={[doc]}
collections={collections}
packs={many}
scope={{ collectionIds: [], documentIds: [], packIds: selected }}
onChangeScope={(next) => emitted.push(next)}
/>
</I18nProvider>
)
await user.click(screen.getByRole('button'))
const limitLine = `Up to ${MAX_SELECTED_PACKS} knowledge packs per chat`
expect(await screen.findByText(limitLine)).toBeInTheDocument()
expect(screen.getAllByText(limitLine)).toHaveLength(1) // once under the title, not per row
const thirteenth = screen.getByRole('checkbox', { name: /Pack 12/ })
expect(thirteenth).toBeDisabled()
await user.click(thirteenth)
expect(emitted).toEqual([]) // REFUSED — not accepted and silently trimmed downstream
// …and a SELECTED pack is still deselectable, which is how the user gets back under the cap.
await user.click(screen.getByRole('checkbox', { name: /Pack 00/ }))
expect(emitted.at(-1)?.packIds).toEqual(selected.filter((id) => id !== 'uuid-0'))
})
it('keeps a SELECTED unavailable or disabled pack deselectable and shows its reason', async () => {
stubApi({})
const emitted: DocumentScope[] = []
const user = userEvent.setup()
render(
<I18nProvider>
<ScopePopover
docs={[doc]}
collections={collections}
packs={[
pack({ id: 'uuid-gone', title: 'Chemie von Wikipedia', available: false, unavailableReason: 'missing' }),
pack({ id: 'uuid-off', title: 'Biologie von Wikipedia', enabled: false })
]}
scope={{ collectionIds: [], documentIds: [], packIds: ['uuid-gone', 'uuid-off'] }}
onChangeScope={(next) => emitted.push(next)}
/>
</I18nProvider>
)
await user.click(screen.getByRole('button'))
const gone = await screen.findByRole('checkbox', { name: /Chemie von Wikipedia/ })
const off = screen.getByRole('checkbox', { name: /Biologie von Wikipedia/ })
expect(gone).toBeChecked()
expect(gone).toBeEnabled()
expect(off).toBeChecked()
expect(off).toBeEnabled()
// #301 P6 (plan §9.23 (c)5): the hint names the RECORDED reason, not a generic
// "not available" — the row now says what the per-answer outcome will say.
expect(screen.getByText('file missing')).toBeInTheDocument()
expect(screen.queryByText('not available')).not.toBeInTheDocument()
expect(screen.getByText('disabled')).toBeInTheDocument()
await user.click(gone)
expect(emitted.at(-1)?.packIds).toEqual(['uuid-off'])
})
it('renders a persisted id with no pack row as a ticked "Removed pack" that can be unticked', async () => {
stubApi({})
const emitted: DocumentScope[] = []
const user = userEvent.setup()
render(
<I18nProvider>
<ScopePopover
docs={[doc]}
collections={collections}
packs={[pack()]}
scope={{ collectionIds: [], documentIds: [], packIds: ['uuid-climate', 'uuid-vanished'] }}
onChangeScope={(next) => emitted.push(next)}
/>
</I18nProvider>
)
await user.click(screen.getByRole('button'))
const removed = await screen.findByRole('checkbox', { name: /Removed pack/ })
expect(removed).toBeChecked()
expect(removed).toBeEnabled()
await user.click(removed)
expect(emitted.at(-1)?.packIds).toEqual(['uuid-climate'])
})
})
describe('ArticleModal', () => {
it('renders the article sections from the main-resolved plain text, and the archive’s license line after them (#662)', async () => {
stubApi(packsPanelStubs({
getPackArticle: async () => ({
title: 'Treibhausgas',
sections: [
{ label: null, text: 'Treibhausgase sind Spurengase.' },
{ label: 'Landwirtschaft', text: 'Methan entsteht in der Landwirtschaft.' }
],
partial: false,
notice: 'Dieser Artikel wurde von Wikipedia herausgegeben. Der Text ist unter Creative Commons Attribution-Share Alike 4.0 verfügbar.'
})
}))
render(
<I18nProvider>
<ArticleModal
target={{ packId: 'uuid-climate', articlePath: 'Treibhausgas', archiveTitle: 'Klimawandel von Wikipedia' }}
onClose={() => {}}
/>
</I18nProvider>
)
expect(await screen.findByText('Treibhausgase sind Spurengase.')).toBeInTheDocument()
expect(screen.getByRole('heading', { name: 'Landwirtschaft' })).toBeInTheDocument()
expect(screen.getByText(/From Klimawandel von Wikipedia/)).toBeInTheDocument()
// The license line closes the article, outside its last section rather than as its text.
const notice = screen.getByText(/^Dieser Artikel wurde von Wikipedia herausgegeben\./)
expect(notice.closest('section')).toBeNull()
const lastParagraph = screen.getByText('Methan entsteht in der Landwirtschaft.')
expect(lastParagraph.compareDocumentPosition(notice) & Node.DOCUMENT_POSITION_FOLLOWING).toBeTruthy()
})
it('warns that only the first part is shown when the conversion was partial (H1 truncation)', async () => {
stubApi(packsPanelStubs({
getPackArticle: async () => ({
title: 'Treibhausgas',
sections: [{ label: null, text: 'Treibhausgase sind Spurengase.' }],
partial: true,
notice: null
})
}))
render(
<I18nProvider>
<ArticleModal
target={{ packId: 'uuid-climate', articlePath: 'Treibhausgas' }}
onClose={() => {}}
/>
</I18nProvider>
)
expect(await screen.findByText(/Only the first part of this article could be shown/)).toBeInTheDocument()
// The partial text itself is still shown — a partial extraction beats an empty viewer.
expect(screen.getByText('Treibhausgase sind Spurengase.')).toBeInTheDocument()
})
})
// =========================================================================================
// T18-a (#301 P6) — the AUTOMATED half of the T18 acceptance row, per plan §9.23. One test
// with lettered legs (the T16-a pattern), each asserting by ROLE / accessible NAME /
// announced STATE — never by class, never by snapshot. The visual half (T18-b: both themes,
// a 900 px window, 200 % zoom, real screenshots) is the owner's and is recorded separately;
// nothing below claims appearance a token test already holds.
// =========================================================================================
/** Regex-escape, so a 200-character non-ASCII title can be used inside a name matcher. */
function esc(s: string): string {
return s.replace(/[.*+?^${}()|[\]\\]/g, '\\$&')
}
/**
* The accessible name a pack row composes: the title, plus its short reason hint when it has
* one (§9.23 (b)4 — the pack's own reason deliberately stays INSIDE the label, so "…, checkbox,
* not checked, dimmed" is never announced without its cause).
*/
function rowName(title: string, hint?: string): RegExp {
return new RegExp(`^${esc(title)}\\s*${hint ? esc(hint) : ''}$`)
}
/** A 200-character non-ASCII title (§9.23 (d)): German compounds + CJK, deliberately no emoji. */
const LONG_TITLE = ((): string => {
const chunk = 'Überwachungsverordnungsdurchführungsbestimmung-気候変動に関する政府間パネルの報告書-'
let out = ''
while (out.length < 200) out += chunk
return out.slice(0, 200)
})()
/** Every reason code in `KnowledgePackOutcomeReason` — leg (g) renders EN copy for all 14. */
const ALL_REASONS: readonly KnowledgePackOutcomeReason[] = [
'selection-limit',
'removed',
'disabled',
'file-missing',
'identity-mismatch',
'not-served',
'not-searchable',
'tools-missing',
'mode',
'search-failed',
'read-failed',
'timeout',
'deadline',
'server-restarted'
]
const T18_COLLECTIONS: Collection[] = [
{
id: 'lib',
name: 'Library',
type: 'library',
description: null,
builtin: true,
color: null,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
archivedAt: null
} as Collection
]
function t18Doc(over: Partial<DocumentInfo> = {}): DocumentInfo {
return {
id: 'd1',
title: 'contract.pdf',
originalPath: null,
mimeType: 'application/pdf',
sizeBytes: 10,
status: 'indexed',
errorMessage: null,
chunkCount: 1,
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
...over
} as DocumentInfo
}
/** The archive citation the "Open article" legs open. */
const T18_CITATION: Citation = {
label: 'S1',
sourceTitle: 'Treibhausgas',
pageNumber: null,
section: 'Landwirtschaft',
snippet: 'Methan entsteht in der Landwirtschaft.',
sourceKind: 'archive',
packId: 'uuid-climate',
archiveTitle: 'Klimawandel von Wikipedia',
articlePath: 'A/Treibhausgas'
}
/**
* Citation card + the shared viewer, wired the way `ChatScreen` wires the OPEN path (leg (d)).
* The #418 save shortcut is deliberately NOT wired here: this harness pins the viewer route and
* its focus trap, and the card shortcut has its own harness + describe at the end of this file.
*/
function ArticleHarness({ citation = T18_CITATION }: { citation?: Citation }): JSX.Element {
const [target, setTarget] = useState<ArticleTarget | null>(null)
return (
<I18nProvider>
<SourcesDisclosure
citations={[citation]}
onOpenArticle={(c) =>
setTarget({ packId: c.packId!, articlePath: c.articlePath!, archiveTitle: c.archiveTitle })
}
/>
<ArticleModal target={target} onClose={() => setTarget(null)} />
</I18nProvider>
)
}
/** A live scope beside a PERSISTED outcome set (leg (g), inventory row 34). */
function NoticeHarness({ outcomes }: { outcomes: KnowledgePackOutcome[] }): JSX.Element {
const [scope, setScope] = useState<DocumentScope>({ collectionIds: [], documentIds: [] })
return (
<I18nProvider>
<ScopePopover
docs={[t18Doc()]}
collections={T18_COLLECTIONS}
packs={[pack()]}
scope={scope}
onChangeScope={setScope}
/>
<PackOutcomesNotice outcomes={outcomes} />
</I18nProvider>
)
}
function t18Conversation(): Conversation {
return {
id: 'c1',
title: 'Doc Q&A',
createdAt: '2026-01-01T00:00:00Z',
updatedAt: '2026-01-01T00:00:00Z',
modelId: null,
mode: 'documents',
scopeDocumentIds: null,
collectionId: null,
scope: { collectionIds: [], documentIds: [] }
} as Conversation
}
const T18_RUNTIME: RuntimeStatus = {
running: true,
modelId: 'm1',
port: 1234,
healthy: true,
message: 'ok'
}
describe('T18 — knowledge-pack UI acceptance (#301 P6, plan §9.23)', () => {
it(
'T18 every pack state in both languages, empty source set, long labels, keyboard-only modal / popover (focus trap, Escape, focus restoration), disabled controls not selectable, live refresh and per-answer notices',
async () => {
// ---- (a) every pack state, EN and DE (inventory rows 16, 20-24) ---------------------
for (const lang of ['en', 'de'] as UiLanguage[]) {
const T = (k: Parameters<typeof tr>[1]): string => tr(lang, k)
window.localStorage.setItem(UI_LANGUAGE_STORAGE_KEY, lang)
stubApi({})
const user = userEvent.setup()
render(
<I18nProvider>
<ScopePopover
docs={[t18Doc()]}
collections={T18_COLLECTIONS}
packs={[
pack({ id: 'p-ok', title: 'Pack tickable' }),
pack({ id: 'p-off', title: 'Pack disabled', enabled: false }),
pack({
id: 'p-missing',
title: 'Pack missing',
available: false,
unavailableReason: 'missing'
}),
pack({
id: 'p-mismatch',
title: 'Pack mismatch',
available: false,
unavailableReason: 'identity-mismatch'
}),
// The pre-P3b generic state: unavailable with NO recorded reason.
pack({
id: 'p-generic',
title: 'Pack generic',
available: false,
unavailableReason: null
}),
pack({ id: 'p-noindex', title: 'Pack noindex', searchable: 'no' }),
// Searchability the probe has not settled is searched like yes — no hint.
pack({ id: 'p-unknown', title: 'Pack unknown', searchable: 'unknown' }),
pack({ id: 'p-sel-off', title: 'Pack selected disabled', enabled: false })
]}
scope={{ collectionIds: [], documentIds: [], packIds: ['p-sel-off', 'p-vanished'] }}
onChangeScope={() => {}}
/>
</I18nProvider>
)
await user.click(screen.getByRole('button'))
// Group semantics (§9.23 (b)3): both blocks are labelled groups, not bare checkbox runs.
expect(
await screen.findByRole('group', { name: T('chat.scope.packsTitle') })
).toBeInTheDocument()
expect(screen.getByRole('group', { name: T('chat.scope.sourcesTitle') })).toBeInTheDocument()
// The Documents toggle: NAME is the short label, the caveat is the DESCRIPTION (b)4.
const docsBox = screen.getByRole('checkbox', { name: T('chat.scope.documentsToggle') })
expect(docsBox).toHaveAccessibleName(T('chat.scope.documentsToggle'))
expect(docsBox).toHaveAccessibleDescription(T('chat.scope.documentsToggleHint'))
expect(docsBox).toBeChecked()
// Tickable, and searchability-unknown is tickable too (absence of a hint is pinned).
for (const title of ['Pack tickable', 'Pack unknown']) {
const box = screen.getByRole('checkbox', { name: rowName(title) })
expect(box, title).toBeEnabled()
expect(box, title).not.toBeChecked()
}
// Every ineligible reason, announced in the name and disabled while unselected (D6).
const reasons: Array<[string, string]> = [
['Pack disabled', T('chat.scope.packDisabled')],
['Pack missing', T('chat.scope.packMissing')],
['Pack mismatch', T('chat.scope.packMismatch')],
['Pack generic', T('chat.scope.packUnavailable')],
['Pack noindex', T('chat.scope.packNotSearchable')]
]
for (const [title, hint] of reasons) {
const box = screen.getByRole('checkbox', { name: rowName(title, hint) })
expect(box, `${title} / ${lang}`).toBeDisabled()
expect(box, `${title} / ${lang}`).not.toBeChecked()
}
// A SELECTED ineligible pack keeps its reason AND stays deselectable (D6).
const selectedOff = screen.getByRole('checkbox', {
name: rowName('Pack selected disabled', T('chat.scope.packDisabled'))
})
expect(selectedOff).toBeChecked()
expect(selectedOff).toBeEnabled()
// A persisted id with no row: named, ticked, clearable — never silently dropped.
const removed = screen.getByRole('checkbox', { name: T('chat.scope.packRemoved') })
expect(removed).toBeChecked()
expect(removed).toBeEnabled()
// Nothing is at the cap here, so no limit line and no cap description anywhere.
expect(
screen.queryByText(trCount(lang, 'chat.scope.packLimit', MAX_SELECTED_PACKS))
).toBeNull()
cleanup()
// …and the cap state (row 24): the line shows ONCE and every cap-refused box is
// DESCRIBED by it, so the reason for the refusal is announced with the state (b)5.
const many = Array.from({ length: MAX_SELECTED_PACKS + 1 }, (_, i) =>
pack({ id: `cap-${i}`, title: `Cap pack ${String(i).padStart(2, '0')}` })
)
render(
<I18nProvider>
<ScopePopover
docs={[t18Doc()]}
collections={T18_COLLECTIONS}
packs={many}
scope={{
collectionIds: [],
documentIds: [],
packIds: many.slice(0, MAX_SELECTED_PACKS).map((k) => k.id)
}}
onChangeScope={() => {}}
/>
</I18nProvider>
)
await user.click(screen.getByRole('button'))
const limitLine = trCount(lang, 'chat.scope.packLimit', MAX_SELECTED_PACKS)
expect(await screen.findByText(limitLine)).toBeInTheDocument()
expect(screen.getAllByText(limitLine)).toHaveLength(1)
const capped = screen.getByRole('checkbox', { name: rowName('Cap pack 12') })
expect(capped).toBeDisabled()
expect(capped).toHaveAccessibleDescription(limitLine)
// A pack that IS selected is not refused, so it carries no cap description.
const inside = screen.getByRole('checkbox', { name: rowName('Cap pack 00') })
expect(inside).toBeEnabled()
expect(inside).not.toHaveAccessibleDescription(limitLine)
cleanup()
window.localStorage.clear()
}
// ---- (b) empty source sets (inventory rows 18, 19, 25, 26; §9.23 (e)) ---------------
stubApi({})
const userB = userEvent.setup()
// Documents off with nothing ticked: the chip NAMES the state — and IS the trigger.
render(
<I18nProvider>
<ScopePopover
docs={[t18Doc()]}
collections={T18_COLLECTIONS}
packs={[pack()]}
scope={{ collectionIds: [], documentIds: [], documentsOff: true }}
onChangeScope={() => {}}
/>
</I18nProvider>
)
const emptyChip = screen.getByRole('button')
expect(emptyChip).toHaveTextContent(tr('en', 'chat.scope.documentsOffNoPacks'))
await userB.click(emptyChip)
expect(
await screen.findByRole('group', { name: tr('en', 'chat.scope.packsTitle') })
).toBeInTheDocument()
cleanup()
// Documents off with ONLY attachments (row 19): the file is named, the tail is honest.
render(
<I18nProvider>
<ScopePopover
docs={[]}
collections={T18_COLLECTIONS}
packs={[pack()]}
attachments={[t18Doc({ id: 'a1', title: 'notes.pdf' })]}
scope={{ collectionIds: [], documentIds: [], documentsOff: true }}
onChangeScope={() => {}}
/>
</I18nProvider>
)
expect(screen.getByRole('button')).toHaveTextContent(
`notes.pdf · ${tr('en', 'chat.scope.documentsOffSuffix')}`
)
cleanup()
// A pack-only corpus keeps the picker (row 25) — the source control stays reachable.
render(
<I18nProvider>
<ScopePopover
docs={[]}
collections={T18_COLLECTIONS}
packs={[pack()]}
scope={{ collectionIds: [], documentIds: [] }}
onChangeScope={() => {}}
/>
</I18nProvider>
)
await userB.click(screen.getByRole('button'))
expect(
await screen.findByRole('checkbox', { name: rowName('Klimawandel von Wikipedia') })
).toBeEnabled()
cleanup()
// Nothing at all (row 26): the ONE state with no picker, because there is nothing to pick.
render(
<I18nProvider>
<ScopePopover
docs={[]}
collections={T18_COLLECTIONS}
packs={[]}
scope={null}
onChangeScope={() => {}}
/>
</I18nProvider>
)
const jump = screen.getByRole('button')
expect(jump).toHaveTextContent(tr('en', 'chat.scope.none'))
await userB.click(jump)
expect(screen.queryByRole('group', { name: tr('en', 'chat.scope.packsTitle') })).toBeNull()
cleanup()
// ---- (c) long labels: the FULL 200-character title is the accessible name -----------
expect(LONG_TITLE).toHaveLength(200)
stubApi({})
const userC = userEvent.setup()
render(
<I18nProvider>
<ScopePopover
docs={[]}
collections={T18_COLLECTIONS}
packs={[pack({ id: 'p-long', title: LONG_TITLE })]}
scope={{ collectionIds: [], documentIds: [] }}
onChangeScope={() => {}}
/>
</I18nProvider>
)
await userC.click(screen.getByRole('button'))
expect(await screen.findByRole('checkbox', { name: LONG_TITLE })).toHaveAccessibleName(
LONG_TITLE
)
cleanup()
stubApi(packsPanelStubs({
getPackArticle: async () => ({
title: LONG_TITLE,
sections: [{ label: null, text: 'Ein Absatz.' }],
partial: false,
notice: null
})
}))
render(
<I18nProvider>
<ArticleModal
target={{ packId: 'uuid-climate', articlePath: 'A/x', archiveTitle: 'Wikipedia' }}
onClose={() => {}}
/>
</I18nProvider>
)
expect(await screen.findByRole('dialog', { name: LONG_TITLE })).toHaveAccessibleName(
LONG_TITLE
)
cleanup()
render(
<NoticeHarness
outcomes={[
{
packId: 'p-long',
title: LONG_TITLE,
status: 'searched',
reason: null,
found: 1,
admitted: 1
}
]}
/>
)
await userC.click(screen.getByRole('button', { name: /Knowledge packs:/ }))
expect(screen.getByText(LONG_TITLE)).toBeInTheDocument()
cleanup()
// ---- (d) keyboard only: popover Escape/restoration, modal trap/Escape/restoration ----
stubApi({})
const userD = userEvent.setup()
render(
<I18nProvider>
<ScopePopover
docs={[t18Doc()]}
collections={T18_COLLECTIONS}
packs={[pack()]}
scope={{ collectionIds: [], documentIds: [] }}
onChangeScope={() => {}}
/>
</I18nProvider>
)
const trigger = screen.getByRole('button')
trigger.focus()
expect(document.activeElement).toBe(trigger)
await userD.keyboard('{Enter}')
await screen.findByRole('group', { name: tr('en', 'chat.scope.packsTitle') })
// Tab moves onto a row control INSIDE the popover…
await userD.tab()
expect(document.activeElement?.tagName).toBe('INPUT')
expect(document.activeElement).toHaveAttribute('type', 'checkbox')
// …Escape closes it and focus is BACK on the chip that opened it.
await userD.keyboard('{Escape}')
await waitFor(() =>
expect(screen.queryByRole('group', { name: tr('en', 'chat.scope.packsTitle') })).toBeNull()
)
expect(document.activeElement).toBe(trigger)
cleanup()
stubApi(packsPanelStubs({
getPackArticle: async () => ({
title: 'Treibhausgas',
sections: [{ label: null, text: 'Treibhausgase sind Spurengase.' }],
partial: false,
notice: null
})
}))
render(<ArticleHarness />)
await userD.click(screen.getByRole('button', { name: /Sources/ }))
const openBtn = screen.getByRole('button', {
name: tr('en', 'chat.sources.openArticleNamed', { title: 'Treibhausgas' })
})
// The visible text stays the bare action; the NAME carries the article (b)6.
expect(openBtn).toHaveTextContent(tr('en', 'chat.sources.openArticle'))
openBtn.focus()
await userD.keyboard('{Enter}')
const dialog = await screen.findByRole('dialog')
await waitFor(() => expect(dialog.contains(document.activeElement)).toBe(true))
// Focus TRAP: repeated Tab never leaves the dialog.
for (let i = 0; i < 6; i++) {
await userD.tab()
expect(dialog.contains(document.activeElement), `tab ${i}`).toBe(true)
}
await userD.keyboard('{Escape}')
await waitFor(() => expect(screen.queryByRole('dialog')).toBeNull())
expect(document.activeElement).toBe(openBtn)
cleanup()
// ---- (e) disabled controls are not selectable (§9.23 (b)9) --------------------------
stubApi({})
const userE = userEvent.setup()
const emitted: DocumentScope[] = []
const capPacks = [
...Array.from({ length: MAX_SELECTED_PACKS }, (_, i) =>
pack({ id: `e-${i}`, title: `Sel pack ${String(i).padStart(2, '0')}` })
),
pack({ id: 'e-extra', title: 'Cap refused pack' }),
pack({ id: 'e-noindex', title: 'Index-less pack', searchable: 'no' })
]
render(
<I18nProvider>
<ScopePopover
docs={[t18Doc()]}
collections={T18_COLLECTIONS}
packs={capPacks}
scope={{
collectionIds: [],
documentIds: [],
packIds: capPacks.slice(0, MAX_SELECTED_PACKS).map((k) => k.id)
}}
onChangeScope={(next) => emitted.push(next)}
/>
</I18nProvider>
)
await userE.click(screen.getByRole('button'))
const capRefused = await screen.findByRole('checkbox', { name: rowName('Cap refused pack') })
const ineligible = screen.getByRole('checkbox', {
name: rowName('Index-less pack', tr('en', 'chat.scope.packNotSearchable'))
})
expect(capRefused).toBeDisabled()
expect(ineligible).toBeDisabled()
await userE.click(capRefused)
await userE.click(ineligible)
// REFUSED at the source — not accepted here and trimmed somewhere downstream.
expect(emitted).toEqual([])
cleanup()
// ---- (f) live refresh through the real event (§9.23 (f); rows 5, 35) ----------------
// A searchability verdict arrives on the SAME `packs:changed` refetch as everything
// else, so a mounted popover must grey the row with its new reason — with no navigation.
const emitter = packsEventEmitter()
let listCalls = 0
const listKnowledgePacks = vi.fn(async () => {
listCalls++
return listCalls === 1 ? [pack()] : [pack({ searchable: 'no' })]
})
stubApi(chatScreenStubs({
listConversations: vi.fn(async () => [t18Conversation()]),
getRuntimeStatus: vi.fn(async () => T18_RUNTIME),
listMessages: vi.fn(async () => []),
listDocuments: vi.fn(async () => [t18Doc()]),
listCollections: vi.fn(async () => T18_COLLECTIONS),
listAttachments: vi.fn(async () => []),
listKnowledgePacks,
onKnowledgePacksChanged: emitter.onKnowledgePacksChanged
}))
const userF = userEvent.setup()
render(<ChatScreen onNavigate={() => {}} />)
await userF.click(await screen.findByText('Doc Q&A'))
await waitFor(() => expect(listKnowledgePacks).toHaveBeenCalledTimes(1))
await userF.click(await screen.findByRole('button', { name: /answering from/i }))
expect(
await screen.findByRole('checkbox', { name: rowName('Klimawandel von Wikipedia') })
).toBeEnabled()
act(() => emitter.emit({ epoch: 2, revision: 1, refreshing: false, reason: 'reconcile-end' }))
await waitFor(() => expect(listKnowledgePacks).toHaveBeenCalledTimes(2))
const greyedName = rowName(
'Klimawandel von Wikipedia',
tr('en', 'chat.scope.packNotSearchable')
)
expect(await screen.findByRole('checkbox', { name: greyedName })).toBeDisabled()
// An OLDER epoch (an old session's late announcement) refetches nothing. Proved without
// a sleep: a newer event follows it, and the total call count shows the old one was
// dropped rather than merely slow.
act(() => emitter.emit({ epoch: 1, revision: 9, refreshing: false, reason: 'reconcile-end' }))
act(() => emitter.emit({ epoch: 3, revision: 3, refreshing: false, reason: 'reconcile-end' }))
await waitFor(() => expect(listKnowledgePacks).toHaveBeenCalledTimes(3))
expect(listKnowledgePacks).toHaveBeenCalledTimes(3)
expect(screen.getByRole('checkbox', { name: greyedName })).toBeDisabled()
cleanup()
// ---- (g) per-answer notices (rows 29-34): every reason code, and row 34's independence -
stubApi({})
const userG = userEvent.setup()
const outcomes: KnowledgePackOutcome[] = [
{
packId: 'ok',
title: 'Klimawandel von Wikipedia',
status: 'searched',
reason: null,
found: 3,
admitted: 2
},
...ALL_REASONS.map((reason, i) => ({
packId: `p-${reason}`,
title: `Pack ${i}`,
status: (reason === 'search-failed' ||
reason === 'read-failed' ||
reason === 'timeout' ||
reason === 'server-restarted'
? 'failed'
: 'skipped') as KnowledgePackOutcome['status'],
reason,
found: 0,
admitted: 0
}))
]
render(<NoticeHarness outcomes={outcomes} />)
const summary = screen.getByRole('button', {
name: new RegExp(
esc(tr('en', 'chat.packs.outcome.summary', { searched: 1, other: ALL_REASONS.length }))
)
})
expect(summary).toHaveAttribute('aria-expanded', 'false')
await userG.click(summary)
expect(summary).toHaveAttribute('aria-expanded', 'true')
// One list, one item per selected pack — "list, 15 items" is the announced structure.
expect(screen.getByRole('list')).toBeInTheDocument()
expect(screen.getAllByRole('listitem')).toHaveLength(outcomes.length)
expect(screen.getByText(tr('en', 'chat.packs.outcome.searched'))).toBeInTheDocument()
expect(screen.getByText(trCount('en', 'chat.packs.outcome.passages', 2))).toBeInTheDocument()
for (const reason of ALL_REASONS) {
expect(
screen.getByText(tr('en', `chat.packs.outcome.${reason}` as 'chat.packs.outcome.searched')),
reason
).toBeInTheDocument()
}
// Row 34: the notice reads the PERSISTED message, never the live scope. Change the scope
// underneath it — tick a pack — and nothing about the answer's outcomes may move.
await userG.click(screen.getByRole('button', { name: /answering from|using/i }))
await userG.click(
await screen.findByRole('checkbox', { name: rowName('Klimawandel von Wikipedia') })
)
await userG.keyboard('{Escape}')
expect(summary).toHaveAttribute('aria-expanded', 'true')
expect(summary).toHaveTextContent(
tr('en', 'chat.packs.outcome.summary', { searched: 1, other: ALL_REASONS.length })
)
expect(screen.getAllByRole('listitem')).toHaveLength(outcomes.length)
expect(screen.getByText(tr('en', 'chat.packs.outcome.deadline'))).toBeInTheDocument()
},
// A ceiling on a hang, never a proof (the T16-a precedent): these legs mount the real
// ChatScreen and drive two portalled overlays with real userEvent timing.
testBudgetMs(120_000)
)
})
// ---- #340 Tier-2 (D-Z21): "Save to my documents" in the article viewer ---------------------
describe('ArticleModal — save to my documents (#340 Tier-2)', () => {
const article = {
title: 'Treibhausgas',
sections: [{ label: null, text: 'Treibhausgase sind Spurengase.' }],
partial: false,
notice: null
}
const target = { packId: 'uuid-climate', articlePath: 'Treibhausgas', archiveTitle: 'Klimawandel von Wikipedia' }
it('offers the save action once the article is ready, sends only the two ids, and reports the filed title', async () => {
const savePackArticle = vi.fn(async () => ({
documentId: 'doc-1',
title: 'Treibhausgas (Klimawandel von Wikipedia).md',
alreadySaved: false,
chunkCount: 3
}))
stubApi(packsPanelStubs({ getPackArticle: async () => article, savePackArticle }))
render(
<I18nProvider>
<ArticleModal target={target} onClose={() => {}} />
</I18nProvider>
)
const button = await screen.findByRole('button', { name: 'Save to my documents: Treibhausgas' })
await userEvent.click(button)
expect(savePackArticle).toHaveBeenCalledWith('uuid-climate', 'Treibhausgas')
expect(await screen.findByText(/Saved to your documents as “Treibhausgas \(Klimawandel von Wikipedia\)\.md”/)).toBeInTheDocument()
expect(screen.queryByRole('button', { name: /Save to my documents/ })).not.toBeInTheDocument()
})
it('says so when the article was already saved, naming the existing document', async () => {
stubApi(packsPanelStubs({
getPackArticle: async () => article,
savePackArticle: vi.fn(async () => ({ documentId: 'doc-1', title: 'Treibhausgas (Klimawandel von Wikipedia).md', alreadySaved: true, chunkCount: 0 }))
}))
render(
<I18nProvider>
<ArticleModal target={target} onClose={() => {}} />
</I18nProvider>
)
await userEvent.click(await screen.findByRole('button', { name: /Save to my documents/ }))
expect(await screen.findByText(/already in your documents as “Treibhausgas/)).toBeInTheDocument()
})
it('shows the friendly failure and keeps the action available when the save is refused', async () => {
stubApi(packsPanelStubs({
getPackArticle: async () => article,
savePackArticle: vi.fn(async () => {
throw new Error("Error invoking remote method 'packs:saveArticle': Error: This article is not available right now, so it could not be saved.")
})
}))
render(
<I18nProvider>
<ArticleModal target={target} onClose={() => {}} />
</I18nProvider>
)
await userEvent.click(await screen.findByRole('button', { name: /Save to my documents/ }))
// The main-side reason is a whole sentence already — shown alone, no stacked prefix.
expect(await screen.findByText(/This article is not available right now, so it could not be saved/)).toBeInTheDocument()
expect(screen.queryByText(/The article could not be saved\./)).not.toBeInTheDocument()
expect(screen.getByRole('button', { name: /Save to my documents/ })).toBeEnabled()
})
it('discards a save that resolves after the viewer moved on to another article', async () => {
type Saved = { documentId: string; title: string; alreadySaved: boolean; chunkCount: number }
let resolveSave: (r: Saved) => void = () => {}
stubApi(packsPanelStubs({
getPackArticle: async () => article,
savePackArticle: () => new Promise<Saved>((r) => (resolveSave = r))
}))
const { rerender } = render(
<I18nProvider>
<ArticleModal target={target} onClose={() => {}} />
</I18nProvider>
)
await userEvent.click(await screen.findByRole('button', { name: /Save to my documents/ }))
expect(screen.getByText(/Saving to your documents/)).toBeInTheDocument()
// The viewer moves to another article while the first save is still running.
rerender(
<I18nProvider>
<ArticleModal target={{ ...target, articlePath: 'Methan' }} onClose={() => {}} />
</I18nProvider>
)
expect(await screen.findByRole('button', { name: /Save to my documents/ })).toBeEnabled()
await act(async () => resolveSave({ documentId: 'doc-1', title: 'Treibhausgas (Klimawandel von Wikipedia).md', alreadySaved: false, chunkCount: 3 }))
// The stale result never lands on the new article: no "saved" line, the action still offered.
expect(screen.queryByText(/Saved to your documents/)).not.toBeInTheDocument()
expect(screen.getByRole('button', { name: /Save to my documents/ })).toBeEnabled()
})
it('has no save action while the article is loading or when it is unavailable', async () => {
let resolveArticle: (a: typeof article | null) => void = () => {}
stubApi(packsPanelStubs({
getPackArticle: () => new Promise<typeof article | null>((r) => (resolveArticle = r)),
savePackArticle: vi.fn()
}))
render(
<I18nProvider>
<ArticleModal target={target} onClose={() => {}} />
</I18nProvider>
)
expect(await screen.findByText(/Loading the article/)).toBeInTheDocument()
expect(screen.queryByRole('button', { name: /Save to my documents/ })).not.toBeInTheDocument()
await act(async () => resolveArticle(null))
expect(await screen.findByText(/not available right now/)).toBeInTheDocument()
expect(screen.queryByRole('button', { name: /Save to my documents/ })).not.toBeInTheDocument()
})
})
// ---- #418: "Save to my documents" on the citation card (ruling C3, second half) -------------
// The main-side machinery is D-Z21's and unchanged — these legs pin the ENTRY POINT: the five
// states the issue names (idle / saving / saved / already saved / unavailable), that only the
// two ids leave the card, that the copy is the viewer's own keys, and that the affordance is
// gated on the callback so a read-only surface can never grow it by accident.
describe('SourcesDisclosure — save an article from the citation card (#418)', () => {
const saveName = tr('en', 'chat.article.saveAria', { title: 'Treibhausgas' })
const filed = 'Treibhausgas (Klimawandel von Wikipedia).md'
const saved = (alreadySaved = false): PackArticleSaveResult => ({
documentId: 'doc-1',
title: filed,
alreadySaved,
chunkCount: alreadySaved ? 0 : 3
})
/** The card wired the way `ChatScreen` wires it, with an injectable save. */
function CardHarness({
onSaveArticle,
citations = [T18_CITATION]
}: {
onSaveArticle?: (packId: string, articlePath: string) => Promise<PackArticleSaveResult>
citations?: Citation[]
}): JSX.Element {
return (
<I18nProvider>
<SourcesDisclosure citations={citations} onOpenArticle={() => {}} onSaveArticle={onSaveArticle} />
</I18nProvider>
)
}
it('offers the shortcut beside "Open article", sends ONLY the two ids, and names the filed title', async () => {
const onSaveArticle = vi.fn(async () => saved())
render(<CardHarness onSaveArticle={onSaveArticle} />)
await userEvent.click(screen.getByRole('button', { name: /Sources/ }))
// Both affordances on the same card, the save one named for THIS article (§11.15 decision 3)
// with the visible label as the exact prefix of the name (WCAG 2.5.3, label in name).
expect(
screen.getByRole('button', {
name: tr('en', 'chat.sources.openArticleNamed', { title: 'Treibhausgas' })
})
).toBeInTheDocument()
const save = screen.getByRole('button', { name: saveName })
expect(save).toHaveTextContent(tr('en', 'chat.article.save'))
await userEvent.click(save)
// Only the pack id and the entry key cross the boundary — never the snippet or the title.
expect(onSaveArticle).toHaveBeenCalledTimes(1)
expect(onSaveArticle).toHaveBeenCalledWith('uuid-climate', 'A/Treibhausgas')
// Saved: the viewer's OWN sentence, and nothing left to click.
expect(await screen.findByText(tr('en', 'chat.article.saved', { title: filed }))).toBeInTheDocument()
expect(screen.queryByRole('button', { name: saveName })).not.toBeInTheDocument()
// The citation itself is untouched beside it.
expect(screen.getByText('Treibhausgas')).toBeInTheDocument()
})
it('announces the outcome: the saved line is a live region', async () => {
render(<CardHarness onSaveArticle={async () => saved()} />)
await userEvent.click(screen.getByRole('button', { name: /Sources/ }))
await userEvent.click(screen.getByRole('button', { name: saveName }))
const status = await screen.findByRole('status')
expect(status).toHaveTextContent(tr('en', 'chat.article.saved', { title: filed }))
})
it('disables the action and swaps the label while the save is in flight', async () => {
let resolveSave: (r: PackArticleSaveResult) => void = () => {}
render(
<CardHarness onSaveArticle={() => new Promise<PackArticleSaveResult>((r) => (resolveSave = r))} />
)
await userEvent.click(screen.getByRole('button', { name: /Sources/ }))
await userEvent.click(screen.getByRole('button', { name: saveName }))
const saving = screen.getByRole('button', { name: saveName })
expect(saving).toBeDisabled()
expect(saving).toHaveTextContent(tr('en', 'chat.article.saving'))
await act(async () => resolveSave(saved()))
expect(await screen.findByText(tr('en', 'chat.article.saved', { title: filed }))).toBeInTheDocument()
})
it('says the article is already in the corpus instead of appearing to do nothing', async () => {
render(<CardHarness onSaveArticle={async () => saved(true)} />)
await userEvent.click(screen.getByRole('button', { name: /Sources/ }))
await userEvent.click(screen.getByRole('button', { name: saveName }))
expect(
await screen.findByText(tr('en', 'chat.article.alreadySaved', { title: filed }))
).toBeInTheDocument()
expect(screen.queryByRole('button', { name: saveName })).not.toBeInTheDocument()
})
it('shows the friendly main-side reason and keeps the action retryable (unavailable / failed)', async () => {
let attempt = 0
const onSaveArticle = vi.fn(async () => {
attempt += 1
if (attempt === 1) {
throw new Error(
"Error invoking remote method 'packs:saveArticle': Error: This article is not available right now, so it could not be saved."
)
}
return saved()
})
render(<CardHarness onSaveArticle={onSaveArticle} />)
await userEvent.click(screen.getByRole('button', { name: /Sources/ }))
await userEvent.click(screen.getByRole('button', { name: saveName }))
// The main-side sentence alone — no stacked "could not be saved" prefix in front of it.
expect(
await screen.findByText(/This article is not available right now, so it could not be saved/)
).toBeInTheDocument()
const retry = screen.getByRole('button', { name: saveName })
expect(retry).toBeEnabled()
await userEvent.click(retry)
expect(await screen.findByText(tr('en', 'chat.article.saved', { title: filed }))).toBeInTheDocument()
})
it('two cards citing the SAME article each answer for themselves — the second is told it is already saved', async () => {
const seen = new Set<string>()
const onSaveArticle = vi.fn(async (packId: string, articlePath: string) => {
const key = `${packId}::${articlePath}`
const already = seen.has(key)
seen.add(key)
return saved(already)
})
const twoCards: Citation[] = [T18_CITATION, { ...T18_CITATION, label: 'S2', section: 'Industrie' }]
render(<CardHarness onSaveArticle={onSaveArticle} citations={twoCards} />)
await userEvent.click(screen.getByRole('button', { name: /Sources/ }))
const buttons = screen.getAllByRole('button', { name: saveName })
expect(buttons).toHaveLength(2)
await userEvent.click(buttons[0]!)
expect(await screen.findByText(tr('en', 'chat.article.saved', { title: filed }))).toBeInTheDocument()
// The second card still offers its own action and gets the main side's duplicate answer.
await userEvent.click(screen.getByRole('button', { name: saveName }))
expect(
await screen.findByText(tr('en', 'chat.article.alreadySaved', { title: filed }))
).toBeInTheDocument()
expect(onSaveArticle).toHaveBeenCalledTimes(2)
})
it('renders no shortcut without the callback — the gate that keeps it off read-only surfaces', async () => {
render(<CardHarness />)
await userEvent.click(screen.getByRole('button', { name: /Sources/ }))
// The open affordance is there; the save one is not.
expect(
screen.getByRole('button', {
name: tr('en', 'chat.sources.openArticleNamed', { title: 'Treibhausgas' })
})
).toBeInTheDocument()
expect(screen.queryByRole('button', { name: /Save to my documents/ })).not.toBeInTheDocument()
})
it('renders no shortcut on a DOCUMENT citation, or on an archive citation missing its ids', async () => {
const docCitation: Citation = { label: 'S1', sourceTitle: 'Bericht.pdf', pageNumber: 3, snippet: 'x' }
const idless: Citation = { ...T18_CITATION, label: 'S2', packId: null, articlePath: null }
render(<CardHarness onSaveArticle={async () => saved()} citations={[docCitation, idless]} />)
await userEvent.click(screen.getByRole('button', { name: /Sources/ }))
expect(screen.queryByRole('button', { name: /Save to my documents/ })).not.toBeInTheDocument()
})
it('speaks German too — the card reuses the viewer keys, so there is no second translation', async () => {
window.localStorage.setItem(UI_LANGUAGE_STORAGE_KEY, 'de')
render(<CardHarness onSaveArticle={async () => saved()} />)
// The exact accessible name (the disclosure label carries parentheses, so no regex here).
await userEvent.click(
screen.getByRole('button', { name: tr('de', 'chat.sources.toggle', { count: 1 }) })
)
const save = screen.getByRole('button', {
name: tr('de', 'chat.article.saveAria', { title: 'Treibhausgas' })
})
expect(save).toHaveTextContent(tr('de', 'chat.article.save'))
await userEvent.click(save)
expect(await screen.findByText(tr('de', 'chat.article.saved', { title: filed }))).toBeInTheDocument()
})
})
// #958: the management list shows packs in the interface language's order (main's SQL order is NOCASE, A-Z only).
describe('PacksPanel pack order (#958)', () => {
const C = String.fromCharCode
const packCases: Array<[UiLanguage, string[]]> = [
...PROJECT_ORDER_CASES,
['de', ['Wikip' + C(0xe9) + 'dia', 'Zebra', C(0xc4) + 'rzteblatt', C(0x412, 0x438, 0x43a, 0x438)]]
]
const renderListed = async (lang: UiLanguage, names: string[]): Promise<string[]> => {
window.localStorage.setItem(UI_LANGUAGE_STORAGE_KEY, lang)
stubApi(packsPanelStubs({
getKnowledgePackStatus: async () => ({ toolsInstalled: true, refreshing: false, revision: 0 }),
listKnowledgePacks: async () => names.map((n, i) => pack({ id: `p${i}`, title: n, description: null }))
}))
render(<PacksPanel />, { wrapper: I18nProvider })
await screen.findByText(names[0])
return screen
.getAllByRole('listitem')
.map((li) => names.find((n) => (li.textContent ?? '').includes(n)))
.filter((n): n is string => n !== undefined)
}
it.each(packCases)('lists the packs in the %s order, not the SQL order', async (lang, sqlOrder) => {
const expected = [...sqlOrder].sort(new Intl.Collator(lang).compare)
expect(expected).not.toEqual(sqlOrder)
expect(await renderListed(lang, sqlOrder)).toEqual(expected)
})
it('keeps packs with equal titles in their incoming (id) order', async () => {
const composed = 'Caf' + C(0xe9)
const decomposed = 'Cafe' + C(0x301)
expect(await renderListed('en', [composed, decomposed, 'Atlas'])).toEqual(['Atlas', composed, decomposed])
})
})