Skip to content
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,8 @@ Just for clarification, an item can never have its Quality increase
above 50, however "Sulfuras" is a legendary item and as such its
Quality is 80 and it never alters.

### Cellar-Aged Mead

"Cellar-Aged Mead" is a premium maturing product recognised only by this
exact name. Its Quality increases by 1 per day, and by 2 per day once its
SellIn is 0 or lower, up to a maximum Quality of 50.
69 changes: 69 additions & 0 deletions specs/KAN-5-introduce-cellar-aged-mead-as-a-new/plan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# Implementation plan: Introduce Cellar-Aged Mead as a new maturing product (KAN-5)

## Technical context
- **Language and framework:** C# (latest `LangVersion`), `net8.0` set in `Directory.Build.props`. Nullable and implicit usings are on. CI (`.github/workflows/dotnet.yml` → `build.yml`) uses .NET SDK 9.0.x.
- **Tests:** xunit 2.9.3 with FluentAssertions 8.x in `tests/Core.Tests`. The tests use `[Theory]`/`[InlineData]` and are named `Method_WhenCondition_Expected`.
- **Analyzers:** Roslynator and SonarAnalyzer run in Debug. Warnings are not treated as errors.
- **Conventions:**
- Each file starts with the copyright header block.
- Members are accessed with `this.`.
- Namespace is `GildedRoseKata.ProposedCode`.
- Test namespace is `GildedRoseKata.ProposedCode.Tests`.
- **Architecture of the update path (`src/Core/ProposedCode`):**
- `ItemMaturationService.Mature(Item)` calls `ItemKind.FromName(name)`.
- It then builds an `ItemState` (`Quality` clamped to 0–50, `SellIn`) and calls `IMaturingPolicy.Mature(kind, state)`.
- `ItemMaturingPolicy` switches on the kind and has one private method per kind.
- The service returns a new `Item`, so each item is processed independently and without side effects.
- **Legacy path:** `OriginalCode/UpdateQualityService` is the original kata code. Nothing references it. `Item` is in `OriginalCode` and must not change (out of scope).

## Approach
1. **Product identity (FR-001).** Add a sealed record `ItemKind.CellarAgedMead` and one exact-match arm in `ItemKind.FromName`: `"Cellar-Aged Mead" => new CellarAgedMead()`.
- The switch uses exact, case-sensitive string patterns, like the existing names. This resolves the first open question.
- There is no substring or `Contains` matching, so names such as "Mead" or "Cellar-Aged Mead Reserve" stay `Standard` (AC-012).
2. **Maturation rule (FR-002, FR-003, FR-004, FR-005, FR-006).** Add an arm `ItemKind.CellarAgedMead => this.MatureCellarAgedMead(state)` in `ItemMaturingPolicy.Mature`. Add a private method that follows the existing per-kind pattern:
- `newQuality = state.Quality.Increase()`, `newSellIn = state.SellIn.NextDay()`.
- If `newSellIn.IsExpired`, then `newQuality = newQuality.Increase()`.
- Return `new(newQuality, newSellIn)`.
- `IsExpired` is `Value < 0` on the new sell-in. That equals the spec's "old sell-in ≤ 0 → +2, otherwise +1".
- The cap at 50 and the floor at 0 come from `Quality` (clamping in its constructor). Nothing is duplicated.
- Quality never decreases, so an expired mead keeps gaining.
- The method is deliberately separate from `MatureAgedBrie`, even though the arithmetic is the same today. That keeps the new behaviour isolated, as the spec requires, and lets the two products change independently later.
3. **Independence and determinism (FR-008, FR-009).** No change is needed. The policy is a pure function of `(kind, state)` and the service builds a new `Item`.
4. **Existing products (FR-007).** The existing arms and methods are untouched. The new switch arm sits before the `_` default.
5. **Legacy `UpdateQualityService` (open question 2).** Leave it unchanged. It is the kata's legacy implementation, nothing calls it, and the spec says to avoid changing existing logic. `ProposedCode` is the update path that gets the feature. If the approver wants the legacy path covered too, that is a separate decision, because the nested-`if` code is risky to extend.
6. **Docs.** Add a short note on the mead rule to `README.md` under the kata description. This covers the "update documentation" subtask and is optional.

## Files
- `src/Core/ProposedCode/Domain/ItemKind.cs` (change): add the `CellarAgedMead` record and the exact-name mapping.
- `src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs` (change): add the switch arm and `MatureCellarAgedMead`.
- `tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs` (change): add the name mapping, plus negative cases for a partial name, different case and trailing whitespace (all `Standard`).
- `tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs` (change): add a theory for the mead rules and boundaries, plus repeated-update, determinism and negative sell-in tests.
- `tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs` (change): add mead rows to the existing theory. Add tests for several mead entries mixed with other products, and a regression test over all existing kinds.
- `README.md` (change, optional): document the new product.
- Not changed: `Item.cs`, `UpdateQualityService.cs`, `IMaturingPolicy.cs`, `ItemMaturationService.cs`, `Quality.cs`, `SellIn.cs`, `ItemState.cs`.

## Risks
- **Shared switch in `ItemMaturingPolicy`.** A mistake in the arm order could reroute other kinds. The new arm goes before `_` and is a distinct type, so the risk is low. The regression tests cover it.
- **Public interface.** `ItemKind` is a public abstract record. Adding a nested sealed record is additive. A caller with an exhaustive switch over kinds would hit the default branch, and none exists in the repo.
- **`Quality` clamping.** A mead with an initial quality outside 0–50 is clamped on `ItemState.Create`. This is existing behaviour and satisfies FR-010 for 0–50. Quality above 50 is not a supported mead input.
- **Name matching.** Exact matching means any variant spelling (case or whitespace) is a Standard item that loses quality. This is intended per FR-001, and the tests pin it.
- **Legacy path.** The mead is not handled by `OriginalCode.UpdateQualityService`. This is documented as an assumption.
- **Migrations and config:** none.

## Test plan
Run all tests with `dotnet test` from the repo root. CI runs `dotnet restore`, `dotnet build --no-restore` and `dotnet test --no-build`. To run a subset: `dotnet test --filter "FullyQualifiedName~GildedRoseKata.ProposedCode.Tests"`.

| Requirement | Proving tests |
|---|---|
| FR-001 (AC-012) | `ItemKindTests`: "Cellar-Aged Mead" → `CellarAgedMead`. "Cellar-Aged", "Mead", "Cellar-Aged Mead Reserve", "cellar-aged mead" and "Cellar-Aged Mead " → `Standard`. `ItemMaturationServiceTests`: a mead row. |
| FR-002 (AC-001, AC-006) | Policy and service theory rows: (20, 5) → (21, 4); (20, 1) → (21, 0). |
| FR-003 (AC-002, AC-003) | Rows: (20, 0) → (22, -1); (30, -5) → (32, -6); (20, -1) → (22, -2). |
| FR-004 (AC-004, AC-005, AC-008) | Rows: (49, 5) → 50; (50, 0) → 50 with sell-in -1; (49, 0) → 50; (48, 0) → 50; (48, 5) → 49; (50, 5) → 50. |
| FR-005 | Every row asserts sell-in decreases by exactly 1, including negative sell-in and quality 50. |
| FR-006 (AC-013) | Negative sell-in rows assert quality ≥ the start value, never lower. |
| FR-007 (AC-007) | The existing policy and service theories stay unchanged. Add a mixed-inventory test with Standard, Aged Brie, Backstage, Conjured and Sulfuras items, asserting the known pre-change outputs. |
| FR-008 (AC-009) | A service test with several mead entries at different sell-in and quality values plus other products. Each result must equal the result of updating that item alone, and the input items must not be mutated. |
| FR-009 (AC-010, SC-002) | Repeated updates: (20, 2) over 3 days → (24, -1). Cumulative runs from 0 and from 48 are capped at 50. Running twice from the same values gives equal results. |
| FR-010 (AC-011, SC-003) | Rows with quality 0 → 1 (sell-in > 0) and 2 (sell-in ≤ 0), and 48 → 49 and 50. A long loop asserts quality stays within 0–50. |

The SC-004 existing-suite check is `dotnet test` with the existing expected values unchanged.
83 changes: 83 additions & 0 deletions specs/KAN-5-introduce-cellar-aged-mead-as-a-new/spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Feature specification: Introduce Cellar-Aged Mead as a new maturing product

Issue: KAN-5

UI work: no

## User scenarios

### Scenario 1: The innkeeper stocks a maturing premium product
The innkeeper adds "Cellar-Aged Mead" to the inventory. The daily update recognises it as its own product and makes it more valuable as it ages, so it can be sold at a premium.

Acceptance scenarios:
1. Given a Cellar-Aged Mead with quality 20 and sell-in 5, when the daily inventory update executes, then quality becomes 21 and sell-in becomes 4.
2. Given a Cellar-Aged Mead with quality 20 and sell-in 1, when the daily inventory update executes, then quality becomes 21 and sell-in becomes 0.

### Scenario 2: The mead matures faster once it has reached its sell-by date
The mead keeps gaining value after its sell-by date, and gains it faster. It never loses quality because it is expired.

Acceptance scenarios:
1. Given a Cellar-Aged Mead with quality 20 and sell-in 0, when the daily inventory update executes, then quality becomes 22 and sell-in becomes -1.
2. Given a Cellar-Aged Mead with quality 30 and sell-in -5, when the daily inventory update executes, then quality becomes 32 and sell-in becomes -6.

### Scenario 3: Quality never goes above 50
Acceptance scenarios:
1. Given a Cellar-Aged Mead with quality 49 and sell-in 5, when the daily inventory update executes, then quality becomes 50 and never exceeds 50.
2. Given a Cellar-Aged Mead with quality 50 and sell-in 0, when the daily inventory update executes, then quality remains 50 and sell-in becomes -1.
3. Given a Cellar-Aged Mead with quality 49 and sell-in 0, when the daily inventory update executes, then quality becomes 50 and not 51.

### Scenario 4: Existing products behave as before
Acceptance scenarios:
1. Given an inventory containing normal items, Aged Brie, Backstage passes, Sulfuras and Conjured items, when the daily inventory update executes, then every one of them follows the rules it followed before this change.
2. Given an inventory with several Cellar-Aged Mead entries with different sell-in and quality values plus other products, when the daily inventory update executes, then each entry is updated only from its own values and no other item is affected.

## Requirements

- FR-001: The system shall recognise "Cellar-Aged Mead" as a supported product, identified by its explicit product name and not by a broad substring match. [NEEDS CLARIFICATION: Should the name match be exact and case-sensitive, like the existing product names? The code matches the other names exactly.]
- FR-002: The system shall increase the quality of Cellar-Aged Mead by 1 for each daily inventory update while the product is within its sell-by period (sell-in greater than 0).
- FR-003: The system shall increase the quality of Cellar-Aged Mead by 2 for each daily inventory update when the product has reached or passed its sell-by date (sell-in 0 or lower).
- FR-004: The system shall never let the quality of Cellar-Aged Mead exceed 50, including when an increase would take it past 50, and shall keep quality at 50 when it is already 50.
- FR-005: The system shall decrease the sell-in of Cellar-Aged Mead by exactly 1 on every daily inventory update, including when it is already negative or when quality is already 50.
- FR-006: The system shall not reduce the quality of an expired Cellar-Aged Mead because its sell-by date has passed.
- FR-007: The system shall preserve the existing rules for normal products, Aged Brie, Backstage passes, Sulfuras, Conjured and all other existing product types.
- FR-008: The system shall process each inventory entry independently, so that several Cellar-Aged Mead entries can coexist, each with its own sell-in and quality, and updating one affects no other item.
- FR-009: The system shall give the same result every time the same Cellar-Aged Mead values are updated, and repeated daily updates shall apply the rules cumulatively and deterministically.
- FR-010: The system shall keep the minimum quality of Cellar-Aged Mead at 0 and shall accept any configured initial quality within 0 to 50. [NEEDS CLARIFICATION: Must the legacy `OriginalCode/UpdateQualityService` also handle Cellar-Aged Mead, or only the `ProposedCode` maturation service (`ItemMaturationService`)? The repository has both update paths.]

## Success criteria

- SC-001: All seven acceptance scenarios from the issue (AC-01 to AC-07) pass as automated tests.
- SC-002: A Cellar-Aged Mead starting at quality 20 and sell-in 2, updated three days in a row, ends at quality 24 and sell-in -1 (+1, +1, +2).
- SC-003: A Cellar-Aged Mead never shows quality above 50 or below 0 after any number of daily updates.
- SC-004: The existing test suite for current product types passes with no change to its expected values.
- SC-005: Boundary values (sell-in 1, 0, -1; quality 0, 48, 49, 50) give the results the rules predict.
- SC-006: An inventory containing several Cellar-Aged Mead entries and other products gives each item the result it would get if updated alone.

## Acceptance criteria

- AC-001: When the daily update runs on a Cellar-Aged Mead with quality 20 and sell-in 5, the system shall set quality to 21 and sell-in to 4 (checks FR-002, FR-005).
- AC-002: When the daily update runs on a Cellar-Aged Mead with quality 20 and sell-in 0, the system shall set quality to 22 and sell-in to -1 (checks FR-003).
- AC-003: When the daily update runs on a Cellar-Aged Mead with quality 30 and sell-in -5, the system shall set quality to 32 and sell-in to -6 (checks FR-003, FR-006).
- AC-004: When the daily update runs on a Cellar-Aged Mead with quality 49 and sell-in 5, the system shall set quality to 50 (checks FR-004).
- AC-005: When the daily update runs on a Cellar-Aged Mead with quality 50 and sell-in 0, the system shall keep quality at 50 and set sell-in to -1 (checks FR-004, FR-005).
- AC-006: When the daily update runs on a Cellar-Aged Mead with quality 20 and sell-in 1, the system shall set quality to 21 and sell-in to 0 (checks FR-002, FR-005).
- AC-007: When the daily update runs on an inventory of existing product types, the system shall give each item the same quality and sell-in it gave before this change (checks FR-007).
- AC-008: When the daily update runs on a Cellar-Aged Mead with quality 49 and sell-in 0, the system shall set quality to 50 (checks FR-004).
- AC-009: When the daily update runs on several Cellar-Aged Mead entries with different sell-in values alongside other products, the system shall update each entry according to its own sell-in and leave every other item unaffected (checks FR-008).
- AC-010: When the daily update runs repeatedly on a Cellar-Aged Mead, the system shall apply +1 per day while sell-in is above 0, +2 per day once it is 0 or lower, cap at 50, and give the same results on every run with the same starting values (checks FR-009).
- AC-011: When the daily update runs on a Cellar-Aged Mead with quality 0 or 48, the system shall give 1 or 49 with sell-in above 0, and 2 or 50 with sell-in 0 or lower (checks FR-010, FR-002, FR-003).
- AC-012: When an item is named "Cellar-Aged Mead", the system shall treat it as a maturing product, and when an item's name only contains part of that name, the system shall not treat it as Cellar-Aged Mead (checks FR-001).
- AC-013: When the daily update runs on a Cellar-Aged Mead with a negative sell-in, the system shall not decrease its quality (checks FR-006).

## Out of scope

- Changing the rules for normal products, Aged Brie, Backstage passes, Sulfuras or Conjured items.
- Changing the `Item` class.
- Any user interface, pricing, packaging or ordering features (for example the "bundle of 6 bottles" marketing text in the issue's product definition).
- Fuzzy or partial name matching for Cellar-Aged Mead.
- Adding other new product types.

## Open questions

- [NEEDS CLARIFICATION: Should the name match be exact and case-sensitive, like the existing product names? The code matches the other names exactly.]
- [NEEDS CLARIFICATION: Must the legacy `OriginalCode/UpdateQualityService` also handle Cellar-Aged Mead, or only the `ProposedCode` maturation service (`ItemMaturationService`)? The repository has both update paths.]
29 changes: 29 additions & 0 deletions specs/KAN-5-introduce-cellar-aged-mead-as-a-new/tasks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Tasks: Introduce Cellar-Aged Mead as a new maturing product (KAN-5)

- [ ] T001 [P] Add a sealed record `CellarAgedMead : ItemKind` and the exact, case-sensitive arm `"Cellar-Aged Mead" => new CellarAgedMead()` in `ItemKind.FromName`, before the `_` default. Use no `Contains` or substring matching, and keep the copyright header and `this.` conventions. Change `src/Core/ProposedCode/Domain/ItemKind.cs` (FR-001, AC-012)
- [ ] T002 [P] Add the mead mapping tests after T001. Add `[InlineData("Cellar-Aged Mead", typeof(ItemKind.CellarAgedMead))]` to the existing theory. Add a `Standard` theory for "Cellar-Aged", "Mead", "Cellar-Aged Mead Reserve", "cellar-aged mead" and "Cellar-Aged Mead " (trailing space). Change `tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs` (FR-001, AC-012)
- [ ] T003 [P] Add the maturation rule after T001. Add the arm `ItemKind.CellarAgedMead => this.MatureCellarAgedMead(state)` before `_` in `ItemMaturingPolicy.Mature`. Add a private `MatureCellarAgedMead` that follows the `MatureAgedBrie` pattern:
- `newQuality = state.Quality.Increase()`, `newSellIn = state.SellIn.NextDay()`.
- If `newSellIn.IsExpired`, then `newQuality = newQuality.Increase()`.
- Return `new(newQuality, newSellIn)`.
- Do not reuse or edit `MatureAgedBrie`, and do not touch the other arms.

Change `src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs` (FR-002, FR-003, FR-004, FR-005, FR-006, FR-007, FR-009, FR-010, AC-001, AC-002, AC-003, AC-004, AC-005, AC-006, AC-008, AC-013)
- [ ] T004 [P] Add the policy tests after T003. Add the theory `Mature_WhenKindIsCellarAgedMead_FollowsExpectedRules` with `(quality, sellIn, expectedQuality, expectedSellIn)` rows:
- Rows: (20,5,21,4), (20,1,21,0), (20,0,22,-1), (30,-5,32,-6), (20,-1,22,-2), (49,5,50,4), (49,0,50,-1), (48,0,50,-1), (48,5,49,4), (50,5,50,4), (50,0,50,-1), (0,5,1,4), (0,0,2,-1).
- Add a test for repeated updates: (20, 2) over 3 days gives (24, -1).
- Add tests that updates from quality 0 and from quality 48 are capped at 50.
- Add a determinism test: two runs from the same state give equal results.
- Add a long-loop test that quality stays within 0–50.
- Add a test that a negative sell-in never lowers quality.

Change `tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs` (FR-002, FR-003, FR-004, FR-005, FR-006, FR-009, FR-010, AC-001, AC-002, AC-003, AC-004, AC-005, AC-006, AC-008, AC-010, AC-011, AC-013)
- [ ] T005 [P] Add the service-level and regression tests after T003.
- Add mead rows to the existing `Mature_UsesExpectedRulesForResolvedItemKinds` theory: ("Cellar-Aged Mead", 20, 5, 21, 4) and ("Cellar-Aged Mead", 20, 0, 22, -1).
- Add a mixed-inventory test with several mead entries at different sell-in and quality values plus Standard, Aged Brie, Backstage, Conjured and Sulfuras items. Assert each result equals the result of updating that item alone, and assert the input items are not mutated.
- Add a regression test over all existing kinds that asserts the known pre-change outputs.
- Add a test that a partial or variant name such as "Cellar-Aged Mead Reserve" is treated as a standard item that loses quality.

Change `tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs` (FR-001, FR-007, FR-008, FR-009, AC-007, AC-009, AC-012)
- [ ] T006 [P] Add a short note under the kata description that "Cellar-Aged Mead" increases in quality by 1 per day, by 2 once sell-in is 0 or lower, up to a maximum of 50. Change `README.md` (FR-002, FR-003, FR-004)
- [ ] T007 Run `dotnet build` and `dotnet test` from the repo root after T002, T004 and T005. Confirm the new tests pass, the existing expected values are unchanged, and that `Item.cs` and `UpdateQualityService.cs` are not modified. This changes no files. (FR-007, AC-007)
Loading
Loading