docling-studio/frontend/src/features/feature-flags/store.test.ts
Pier-Jean Malandrino 8ae9dcdc04 refactor(audit): remediate 0.5.0 audit findings — clean architecture, security, DRY, SOLID, perf
Closes the 12 MAJ raised by the release/0.5.0 audit pipeline (cf.
docs/audit/reports/release-0.5.0/summary.md → summary-reaudit.md).

Volet 1 — Reasoning architecture (audits 01/02/06/07 strengthening)
  * Domain ports: LLMProvider, ReasoningRunner, ReasoningParseError
  * Domain DTOs: LLMProviderType, ReasoningResult, ReasoningIteration
  * infra/llm/ollama_provider.py — OllamaProvider with health_check
  * infra/docling_agent_reasoning.py — runner adapter, encapsulates the
    private _rag_loop call (tracked at docling-project/docling-agent#26),
    commits OLLAMA_HOST once at boot (eliminates the per-request env race),
    translates upstream IndexError into ReasoningParseError
  * api/reasoning.py — zero coupling to docling-agent / mellea / docling-core,
    consumes app.state.reasoning_runner via the port
  * main.py — DI wires OllamaProvider + DoclingAgentReasoningRunner at boot
    when REASONING_ENABLED=true and deps are importable
  * Rename RAG_* env vars → REASONING_*, endpoint /rag → /reasoning,
    type RAGResult → ReasoningResult, frontend feature flag wiring,
    i18n strings, tests, docs (BREAKING — pre-1.0 surface, no external
    consumers in production)
  * 17 new tests: adapter unit tests with sys.modules stubs, OllamaProvider
    httpx tests, R3 concurrent-host isolation, R6 multi-iteration trace
    serialization, R13 Protocol conformance via isinstance
  * E2E Karate scenario: nav-reasoning hidden when REASONING_ENABLED=false
  * README — Live Reasoning section (env vars, archi, link to issue #26)

Bloc B — Security (audit 08, dev-only context)
  * docker-compose.yml — DEV DEFAULTS header, OpenSearch DISABLE_SECURITY_PLUGIN
    flagged as dev-only with link to OpenSearch security docs
  * main.py — boot warning if NEO4J_URI is set with the default 'changeme'
    password, so prod operators can't silently inherit it

Bloc C — DRY frontend (audit 05)
  * shared/storage/keys.ts — STORAGE_KEYS centralised (theme, locale)
  * features/settings/store.ts — dead apiUrl ref + orphan i18n keys removed
  * api/schemas.py — DOCUMENT_STATUS_UPLOADED constant

Bloc D — Quality (audits 02/06/07/09/10/12)
  * domain/ports.py — DocumentConverter.supports_page_batching property
    (LSP fix, replaces isinstance(ServeConverter) check)
  * domain/ports.py — VectorStore.ping() (encapsulation, replaces
    _vector_store._client.info() reach-around)
  * api/analyses.py + api/ingestion.py — path params {job_id} → {analysis_id}
    aligned with the user-facing terminology (URLs unchanged)
  * api/documents.py — Path.read_bytes() + generate_preview() wrapped in
    asyncio.to_thread, unblocks the FastAPI event loop on /preview
  * infra/docling_tree.py — PEP 604 union for isinstance (Ruff UP038)
  * src/__tests__/integration/ — cross-feature integration test relocated
    out of features/history/ so feature folders stay self-contained
  * Tightened terminal `assert X is not None` checks (isinstance(.., datetime),
    exact value comparisons)

Validation
  * 446 backend pytest, 202 frontend vitest — all green
  * ruff + ruff format + ESLint + Prettier + vue-tsc clean
  * Re-audit verdict: 0 CRIT / 0 MAJ, score ~94/100, GO

Closes #200
2026-04-29 09:23:09 +02:00

158 lines
5.1 KiB
TypeScript

import { describe, it, expect, vi, beforeEach } from 'vitest'
import { setActivePinia, createPinia } from 'pinia'
import { useFeatureFlagStore } from './store'
const mockApiFetch = vi.fn()
vi.mock('../../shared/api/http', () => ({
apiFetch: (...args: unknown[]) => mockApiFetch(...args),
}))
describe('useFeatureFlagStore', () => {
beforeEach(() => {
setActivePinia(createPinia())
mockApiFetch.mockReset()
})
it('starts unloaded with flags disabled', () => {
const store = useFeatureFlagStore()
expect(store.loaded).toBe(false)
expect(store.isEnabled('chunking')).toBe(false)
expect(store.isEnabled('disclaimer')).toBe(false)
})
it('enables chunking when engine is local', async () => {
mockApiFetch.mockResolvedValue({ status: 'ok', engine: 'local' })
const store = useFeatureFlagStore()
await store.load()
expect(store.engine).toBe('local')
expect(store.loaded).toBe(true)
expect(store.isEnabled('chunking')).toBe(true)
})
it('enables chunking when engine is remote', async () => {
mockApiFetch.mockResolvedValue({ status: 'ok', engine: 'remote' })
const store = useFeatureFlagStore()
await store.load()
expect(store.engine).toBe('remote')
expect(store.isEnabled('chunking')).toBe(true)
})
it('enables disclaimer when deploymentMode is huggingface', async () => {
mockApiFetch.mockResolvedValue({
status: 'ok',
engine: 'local',
deploymentMode: 'huggingface',
})
const store = useFeatureFlagStore()
await store.load()
expect(store.deploymentMode).toBe('huggingface')
expect(store.isEnabled('disclaimer')).toBe(true)
})
it('disables disclaimer when deploymentMode is self-hosted', async () => {
mockApiFetch.mockResolvedValue({
status: 'ok',
engine: 'local',
deploymentMode: 'self-hosted',
})
const store = useFeatureFlagStore()
await store.load()
expect(store.isEnabled('disclaimer')).toBe(false)
})
it('defaults deploymentMode to self-hosted when missing', async () => {
mockApiFetch.mockResolvedValue({ status: 'ok', engine: 'local' })
const store = useFeatureFlagStore()
await store.load()
expect(store.deploymentMode).toBe('self-hosted')
expect(store.isEnabled('disclaimer')).toBe(false)
})
it('reads maxFileSizeMb from health response', async () => {
mockApiFetch.mockResolvedValue({ status: 'ok', engine: 'local', maxFileSizeMb: 100 })
const store = useFeatureFlagStore()
await store.load()
expect(store.maxFileSizeMb).toBe(100)
})
it('defaults maxFileSizeMb to 0 when missing', async () => {
mockApiFetch.mockResolvedValue({ status: 'ok', engine: 'local' })
const store = useFeatureFlagStore()
await store.load()
expect(store.maxFileSizeMb).toBe(0)
})
it('enables ingestion when ingestionAvailable is true', async () => {
mockApiFetch.mockResolvedValue({
status: 'ok',
engine: 'local',
ingestionAvailable: true,
})
const store = useFeatureFlagStore()
await store.load()
expect(store.ingestionAvailable).toBe(true)
expect(store.isEnabled('ingestion')).toBe(true)
})
it('disables ingestion when ingestionAvailable is false', async () => {
mockApiFetch.mockResolvedValue({
status: 'ok',
engine: 'local',
ingestionAvailable: false,
})
const store = useFeatureFlagStore()
await store.load()
expect(store.ingestionAvailable).toBe(false)
expect(store.isEnabled('ingestion')).toBe(false)
})
it('defaults ingestionAvailable to false when missing', async () => {
mockApiFetch.mockResolvedValue({ status: 'ok', engine: 'local' })
const store = useFeatureFlagStore()
await store.load()
expect(store.ingestionAvailable).toBe(false)
expect(store.isEnabled('ingestion')).toBe(false)
})
it('enables reasoning when reasoningAvailable is true', async () => {
mockApiFetch.mockResolvedValue({
status: 'ok',
engine: 'local',
reasoningAvailable: true,
})
const store = useFeatureFlagStore()
await store.load()
expect(store.reasoningAvailable).toBe(true)
expect(store.isEnabled('reasoning')).toBe(true)
})
it('disables reasoning when reasoningAvailable is false', async () => {
mockApiFetch.mockResolvedValue({
status: 'ok',
engine: 'local',
reasoningAvailable: false,
})
const store = useFeatureFlagStore()
await store.load()
expect(store.reasoningAvailable).toBe(false)
expect(store.isEnabled('reasoning')).toBe(false)
})
it('defaults reasoningAvailable to false when missing', async () => {
mockApiFetch.mockResolvedValue({ status: 'ok', engine: 'local' })
const store = useFeatureFlagStore()
await store.load()
expect(store.reasoningAvailable).toBe(false)
expect(store.isEnabled('reasoning')).toBe(false)
})
it('handles health endpoint failure gracefully', async () => {
mockApiFetch.mockRejectedValue(new Error('Network error'))
const store = useFeatureFlagStore()
await store.load()
expect(store.loaded).toBe(true)
expect(store.error).toBe('Network error')
expect(store.isEnabled('chunking')).toBe(false)
expect(store.isEnabled('disclaimer')).toBe(false)
})
})