Files

76 lines
6.2 KiB
Markdown

# Testing strategy
Framework: **Swift Testing** (`import Testing`, `@Test`, `#expect`) for all package tests. XCTest only where Swift Testing lacks support, e.g. performance `measure`.
## Layers
| Layer | Scope | Where | Runs in `make verify` |
|---|---|---|---|
| Unit | Pure logic: language mapping, chunking, token scanning, prompt building, option mapping, validation, settings, chrF/BLEU | `Packages/SprachmittlerKit/Tests/*Tests` | yes |
| Contract | Ollama client against recorded fixtures through `URLProtocol` stubs; request encoding | `SprachmittlerOllamaTests`, `SprachmittlerEnginesTests` | yes |
| Component | `TranslationSession` + `DoubleCopyDetector` + `InsertionCoordinator` with fakes, a manual clock and a fake pasteboard | `SprachmittlerCoreTests`, `SprachmittlerCaptureTests` | yes |
| Live integration | Real Ollama on localhost, opt-in via `SPRACHMITTLER_LIVE=1` | `SprachmittlerLiveTests` | no; `make live-test` |
| Evaluation | Model comparison on the private corpus | `sprachmittler-cli eval` | no; `/evaluate` |
| Manual | Double copy, panel focus, Accessibility replacement, clipboard restoration, Services, VoiceOver, real translation quality | checklist below | no |
## Rules
- Every bug fix starts with a failing test, unless it is only reproducible manually. In that case, add a manual checklist item.
- No test may depend on the network or a real model unless it is in `SprachmittlerLiveTests`.
- Time-dependent logic (double-copy window, timeouts) takes an injected clock. Tests use a manual clock; never `Task.sleep` with real time.
- Fakes live in `SprachmittlerTestSupport`: `FakePasteboard`, `FakeClock`, `FakeOllamaTransport`, `FakeTextInserter`, `FakeAccessibility`, `FakeSettingsStore`, `TemporaryDirectory`.
- Fixtures live inside their test target (SwiftPM resources must sit within the target) and are loaded with `Bundle.module`. All fixtures are **synthetic**; never add private text.
- Golden files for prompts, Markdown and JSON output live next to the tests (`Fixtures/golden/`); update them only with an explanation in the commit message.
## Required test cases (minimum)
Text:
- `Chunker` splits a 9000-character Latin text into paragraphs, never inside a grapheme; CJK text uses the 2000-character limit; join round-trips exactly.
- `ProtectedTokenScanner` finds URLs (with query and fragment), e-mails, `{{var}}`, `${var}`, `%s`, `%d`, `%1$@`, Markdown code spans, fenced blocks and HTML tags; does not flag ordinary punctuation.
- `LanguageDetector` identifies a German paragraph and an English paragraph; short/mixed text reports low confidence.
- `OutputValidator` flags a missing URL, a preamble, `done_reason: "length"`, a repetition loop and an empty output; clean output has no warnings.
Ollama client:
- NDJSON line split across chunks is decoded; `done_reason: "length"` reaches the caller; cancel mid-stream cancels the URL task; idle timeout fires with the manual clock; 404 missing model → `modelNotInstalled`; redirect is refused; `num_ctx` and option names are encoded exactly.
Engines:
- The default prompt equals the documented string byte for byte, including the blank line and the bold markers.
- The prompt prefix is identical for the same target language and differs only in the source text.
- `German`, `Chinese`, `Traditional Chinese`, `Portuguese`, `Norwegian`, `Norwegian Nynorsk`, `Arabic Kazakh` and `Cyrillic Kazakh` map correctly.
- Sampling merge: publisher values override model defaults; no unknown keys are sent.
- Structured Data 1 and Delimiters prompts are byte-exact fallbacks; no free-form prompt can be constructed.
Session and insertion:
- One translation in flight; a new trigger cancels the old task; late deltas from the old epoch are dropped.
- A blocking warning disables Replace; Copy stays enabled; a clean result enables Replace.
- Replace aborts when the source app changed, when the selected text changed, or when Accessibility is denied.
- AX direct replacement is preferred; paste fallback is used when AX fails; the clipboard snapshot is restored; own writes are suppressed.
- The double-copy detector requires identical text within the window and ignores empty/whitespace-only text.
Evaluation:
- chrF and BLEU match hand-computed values on a tiny pair.
- Protected-token retention is 1.0 for a clean pair and 0.0 for a lost URL.
- Numeric normalisation treats `1,5`, `1.5` and `1 5` as the same value only where documented.
## Manual release checklist
- [ ] First launch: onboarding shows correct states with Ollama stopped, model missing, Accessibility denied.
- [ ] Select a paragraph in Safari, Mail, TextEdit, Notes, Word, Pages, Slack, VS Code, Obsidian and Terminal; press ⌘C twice; the panel appears without stealing focus.
- [ ] Copy the translation; paste it into the source app; the source text is unchanged.
- [ ] Click Replace in TextEdit, Notes, Safari and Mail; the selection is replaced; ⌘Z restores it.
- [ ] Deny Accessibility: Copy and Services work; Replace explains what to enable and offers the right System Settings page.
- [ ] Replace with a rich-text or image clipboard: after the paste fallback, the previous clipboard is back.
- [ ] Copy two different things quickly: no translation is triggered.
- [ ] Copy the same text twice slowly: no translation is triggered. Change the window in Settings and repeat.
- [ ] Translate a long multi-paragraph text: chunks are joined with the original paragraph breaks; a failed chunk is visible.
- [ ] Translate text with a URL, an e-mail and `{{placeholder}}`: all survive or a warning appears.
- [ ] Translate Markdown and JSON: keys/formatting survive.
- [ ] Change the target language while streaming: the old request is cancelled and the new one starts.
- [ ] Services ▸ Translate with Sprachmittler works without Accessibility.
- [ ] VoiceOver can operate the panel and Settings; every action has a keyboard path.
- [ ] Console.app shows no selected text, translation, pasteboard content, app name or path.
- [ ] Quit during a translation: no orphaned request; `ollama ps` shows no hanging request.
- [ ] Reinstall the app and re-grant Accessibility if macOS requires it.
- [ ] Remote endpoint: `http://` remote refused; `https://` accepted only with the toggle.
- [ ] Launch at login mirrors the system setting.