Formatter Test Coverage
Relevant source files
The following files were used as context for generating this wiki page:
The beverage-utils project employs a robust testing suite to ensure the accuracy and reliability of its formatting utilities. These utilities are critical for converting technical slugs and classification data into human-readable strings used across the CellarNode platform. The test coverage specifically targets the formatBeverageLabel and formatBeverageType functions, ensuring they handle various edge cases, map lookups, and fallback scenarios correctly.
Tests are executed using the Vitest framework, as configured in the project's root. The suite validates the transformation of beverage categories and subtypes into standardized display formats like "Still Wine / Red Wine", ensuring that the disambiguation logic for overlapping subtype IDs functions as intended.
Sources: README.md:144-149, vitest.config.ts:1-7
Testing Infrastructure
The testing environment is configured to automatically include all files matching the __tests__/**/*.test.ts pattern. This ensures that any new formatting tests added to the directory are immediately integrated into the CI/CD pipeline.
Test Configuration
| Setting | Value | Description |
|---|---|---|
| Framework | Vitest | The primary test runner and assertion library. |
| Target Files | __tests__/**/*.test.ts | Pattern for locating test files. |
| Language | TypeScript | All tests are written in TS and compiled via tsc. |
Sources: vitest.config.ts:3-6, package.json:44-49
Core Formatting Logic Verification
The suite comprehensively tests two primary functions exported by src/format.ts. The logic flow involves checking for provided LabelMap data before falling back to manual string manipulation.
formatBeverageLabel Tests
This function is responsible for single-token conversion. The tests verify four distinct behaviors:
- Null Safety: Returns an empty string if input is
nullorundefined. - Map Lookup: Successfully retrieves a display name from a provided
LabelMap. - Snake Case Conversion: Converts slugs (e.g.,
sparkling_wine) to Title Case when no map entry exists. - Single Word Humanization: Ensures single words are capitalized.
The diagram shows the logical progression of the formatBeverageLabel function as verified by the test suite.
Sources: tests/format.test.ts:5-22, src/format.ts:3-10
formatBeverageType Tests
This function handles the combination of a category and an optional subtype. The test suite ensures that the logic correctly joins these components using a " / " separator.
- Category Only: If no subtype is provided, the function returns only the formatted category.
- Composite Key Disambiguation: This is a critical test case where the formatter must use a composite key (e.g.,
category:subtype) to distinguish between identical subtype IDs in different categories (e.g., "red" in "wine" vs "red" in "sparkling_wine"). - Fallback Logic: When no map is provided, it must humanize both the category and subtype slugs.
Sources: tests/format.test.ts:24-51, src/format.ts:12-21
Data Structures and Dependencies
The formatting tests rely on specific TypeScript interfaces and the buildLabelMap utility to simulate real-world data from the CellarNode API.
Classification Models
The tests utilize the BeverageClassification structure to generate test maps.
| Interface | Field | Type | Description |
|---|---|---|---|
BeverageCategory | id | string | Unique slug for the category. |
BeverageCategory | subtypes | BeverageSubtype[] | Array of child subtypes. |
BeverageSubtype | id | string | Slug that may be reused across categories. |
LabelMap | [key: string] | string | Flat key-value pair for lookups. |
Sources: src/types.ts:1-16, src/label-map.ts:3-15
Mapping Logic Flow
The following sequence diagram illustrates how the formatBeverageType test utilizes the LabelMap to resolve labels.
Conclusion
The formatter test coverage ensures that the beverage-utils library provides a predictable and robust interface for display logic. By validating null handling, humanization fallbacks, and the critical composite-key mapping system, the tests guarantee that developers can rely on the utility for accurate beverage classification rendering in React, Vue, and Angular applications.
Sources: README.md:37-65, tests/format.test.ts:1-53