fix(search): resolve unpadded hymn numbers falling back to placeholder lyrics in expert entry

Tried to use expert input of two songs, 40;44 and both pulled up lyrics for Amazing Grace AND O How I Love Jesus

Automated Issue Triage

Root Cause Analysis

When entering unpadded hymn numbers such as 40;44 in the Omnibox expert entry:

  1. ExpertEntryController.parse("40;44") (packages/sfw_desktop/lib/utils/expert_entry_parser.dart) parses two commands: ("40", null) and ("44", null), and OmniboxCommandDialog._submit() (packages/sfw_desktop/lib/widgets/omnibox_command_dialog.dart) invokes _executeScriptString("40;44").
  2. In _executeScriptString() (packages/sfw_desktop/lib/main.dart, lines 335–343), catalog lookup uses strict string equality:
    final hymn = catalog.firstWhere(
      (h) => h.number.trim().toLowerCase() == cmd.toLowerCase(),
      orElse: () => HymnMetadata(
        id: 'hymn_$cmd',
        number: cmd,
        hymnalId: 'rjs',
        title: 'Hymn $cmd',
      ),
    );
    
  3. Catalog numbers in packages/sfw_desktop/assets/metadata.json are zero-padded to 3 digits (e.g., "040" for God’s Love and Power, "044" for To God Be the Glory). Because "040" != "40" and "044" != "44", firstWhere falls back to orElse and synthesizes dummy HymnMetadata entries with IDs hymn_40 and hymn_44.
  4. fetchHymnPortionData() (packages/sfw_desktop/lib/services/hymn_service.dart, lines 270–339) fails to fetch FlatBuffers for those nonexistent IDs from CDN/API and falls back to generateFallbackFlatbuffer() (lines 149–191), which returns hardcoded placeholder lyrics (Verse 1: “Amazing grace! How sweet the sound…”, Chorus: “O how I love Jesus…”) and caches them in Drift (appDatabase.hymnsCache).

Relevant Codebase Files & Symbols

  • packages/sfw_desktop/lib/main.dart — _executeScriptString (strict h.number equality & orElse synthetic HymnMetadata fallback)
  • packages/sfw_desktop/lib/utils/hymn_search_utils.dart — searchHymns / _calculateMatchScore (already supports zero-padded & numeric number matching via int.tryParse and suffix extraction)
  • packages/sfw_desktop/lib/services/hymn_service.dart — fetchHymnPortionData, generateFallbackFlatbuffer
  • packages/sfw_desktop/lib/widgets/omnibox_command_dialog.dart — OmniboxCommandDialog._submit
  • packages/sfw_desktop/lib/utils/expert_entry_parser.dart — ExpertEntryController.parse
  • packages/sfw_desktop/assets/metadata.json — zero-padded hymn number catalog entries ("040", "044", etc.)

Implementation Notes

  • Update the hymn lookup in _executeScriptString() to match normalized/numeric hymn numbers (e.g., exact string match first, then numeric/unpadded match using the same logic as searchHymns() in hymn_search_utils.dart), rather than requiring leading zeros.
  • When a hymn number genuinely does not exist in the catalog, avoid silently generating and caching generateFallbackFlatbuffer() placeholder lyrics under hymn_<cmd> in appDatabase.hymnsCache; instead surface a warning or skip unresolved entries.

Metadata Updates Applied

  • Title: Updated to fix(search): resolve unpadded hymn numbers falling back to placeholder lyrics in expert entry
  • Labels: Confirmed bug (8) and added external user (54)