diff --git a/specs/KAN-4-add-new-product-to-mature/plan.md b/specs/KAN-4-add-new-product-to-mature/plan.md new file mode 100644 index 0000000..8a04a4e --- /dev/null +++ b/specs/KAN-4-add-new-product-to-mature/plan.md @@ -0,0 +1,81 @@ +# Implementation plan: Add Bordeaux Wine as a new maturing product (KAN-4) + +## Technical context + +- **Language and runtime:** C# on .NET 9 (CI uses `dotnet-version: 9.0.x`). The solution is `GildedRoseKata.sln`, with `src/Core` for the code and `tests/Core.Tests` for the tests. +- **Test tools:** xUnit (`[Theory]`/`[InlineData]`/`[Fact]`) with FluentAssertions. +- **Build and test:** `dotnet restore`, `dotnet build --no-restore` and `dotnet test --no-build`. `.github/workflows/build.yml` runs these through `dotnet.yml`. +- **Style:** each file has a copyright header, and the project uses file-scoped namespaces. Namespaces are `GildedRoseKata.ProposedCode` and `GildedRoseKata.ProposedCode.Tests`. `.editorconfig` and `Directory.Build.props` apply, so follow the existing formatting and analyzer rules. +- **How the new service works today:** + - `ItemMaturationService.Mature(Item)` calls `ItemKind.FromName(name)`. + - It builds an `ItemState` with `Quality` (clamped to 0–50) and `SellIn`. + - It delegates to `IMaturingPolicy.Mature(kind, state)`, implemented by `ItemMaturingPolicy`. + - `ItemKind` is an abstract record with sealed nested records, one per kind. + - Any name not in `FromName` falls through to `Standard`. + - The service itself needs no change, because it only resolves a kind and delegates to the policy. + +## Approach + +Bordeaux Wine follows the same rule as Aged Brie, but it is a separate kind. That way it is recognised by its exact name and does not change Brie's behaviour (FR-008). + +1. Add a sealed record `ItemKind.BordeauxWine : ItemKind`. +2. In `ItemKind.FromName`, add one exact-match case `"Bordeaux Wine" => new BordeauxWine()`. Do not add aliases such as "BordeauxWine"; the spec puts aliases out of scope. Matching is case-sensitive, as for the other names. +3. In `ItemMaturingPolicy.Mature`, add the case `ItemKind.BordeauxWine => this.MatureBordeauxWine(state)`. +4. Add a private `MatureBordeauxWine(ItemState)`, following the existing `MatureAgedBrie` pattern: + - `newQuality = state.Quality.Increase()`. + - `newSellIn = state.SellIn.NextDay()`. + - If `newSellIn.IsExpired`, apply `newQuality.Increase()` again. + - Return `new(newQuality, newSellIn)`. + - `Quality` already clamps to 0–50 through `Math.Clamp`, which covers FR-004 and FR-005. It also covers the FR-006 sell-in decrement, since `NextDay()` does that. + + Check that `SellIn.IsExpired` means "value < 0", so that sell-in 1→0 gives +1 and sell-in 0→-1 gives +2. The Brie test cases `(20, 0, 22, -1)` show the same semantics. +5. Leave `Quality.FromItemKind` unchanged. Only Sulfuras is special-cased there, so Bordeaux Wine takes the normal clamped path. +6. Do not touch `OriginalCode/UpdateQualityService.cs` or `Item.cs` (FR-010). + +I considered reusing `MatureAgedBrie` for both kinds. I prefer a separate method so the two products can diverge later (for example, when KAN-1 adds Cellar-Aged Mead). The rule is small enough that a little duplication is acceptable. + +## Files + +| File | Change | Why | +|---|---|---| +| `src/Core/ProposedCode/Domain/ItemKind.cs` | Add the `BordeauxWine` record and the `"Bordeaux Wine"` case in `FromName` | FR-001, FR-007 | +| `src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs` | Add the switch case and `MatureBordeauxWine` | FR-002 to FR-006, FR-009 | +| `tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs` | Add `[InlineData("Bordeaux Wine", typeof(ItemKind.BordeauxWine))]`. Also add a test that near-variants (`"bordeaux wine"`, `"BordeauxWine"`, `" Bordeaux Wine"`) resolve to `Standard`. | FR-001, FR-007 and the no-aliases out-of-scope rule | +| `tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs` | Add `Mature_WhenKindIsBordeauxWine_FollowsExpectedRules` | FR-002 to FR-006, FR-011 | +| `tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs` | Add Bordeaux Wine `InlineData` rows to the existing theory | End-to-end through the service (FR-009) | + +No change to `ItemMaturationService.cs`, `IMaturingPolicy.cs`, `Quality.cs`, `SellIn.cs` or `ItemState.cs`. No change to `OriginalCode/`. + +## Risks + +- **Shared code:** `ItemKind.FromName` and `ItemMaturingPolicy.Mature` serve every product. The new switch arm must go before the `_` default arm. Adding a `FromName` arm cannot alter existing names, because the matches are exact strings. +- **Public interface:** adding a nested record to `ItemKind` is additive. If any code does an exhaustive match on kinds, the compiler or analyzers could warn. The search found only the policy's switch, which has a default arm. +- **Legacy code:** `OriginalCode/UpdateQualityService.cs` must remain byte-for-byte unchanged (SC-005). Verify with `git diff --stat -- src/Core/OriginalCode`. Bordeaux Wine in the legacy service will still behave as a normal item, which is intended. +- **Sell-in semantics:** the boundary relies on the `IsExpired` evaluation after `NextDay()`. The tests at sell-in 1 and 0 guard against an off-by-one error. +- **Migrations:** none. + +## Test plan + +Add these cases to the Bordeaux Wine policy theory as `(quality, sellIn, expectedQuality, expectedSellIn)`: + +| FR | Case | Where | +|---|---|---| +| FR-001 | `ItemKind.FromName("Bordeaux Wine")` is `BordeauxWine` | `ItemKindTests` | +| FR-007 | Case and whitespace variants resolve to `Standard`; service-level wine quality never decreases (rows below) | `ItemKindTests` and service theory | +| FR-002 | `(20, 10, 21, 9)` and `(20, 1, 21, 0)` | `ItemMaturingPolicyTests` | +| FR-003 | `(20, 0, 22, -1)` and `(20, -5, 22, -6)` | `ItemMaturingPolicyTests` | +| FR-004 | `(50, 10, 50, 9)`, `(50, -1, 50, -2)`, `(49, 10, 50, 9)`, `(49, -1, 50, -2)` and `(49, 0, 50, -1)` | `ItemMaturingPolicyTests` | +| FR-005 | `(0, 10, 1, 9)` and `(0, -1, 2, -2)`. The result is never negative. | `ItemMaturingPolicyTests` | +| FR-006 | The `expectedSellIn` column in all rows above | `ItemMaturingPolicyTests` | +| FR-008 | The existing theories for Standard, Brie, Backstage, Conjured and Sulfuras pass unmodified (SC-004) | existing tests | +| FR-009 | Service rows: `("Bordeaux Wine", 20, 10, 21, 9)`, `("Bordeaux Wine", 20, 0, 22, -1)` and `("Bordeaux Wine", 50, 0, 50, -1)` | `ItemMaturationServiceTests` | +| FR-010 | `git diff --stat -- src/Core/OriginalCode/UpdateQualityService.cs` shows no output (SC-005) | manual or CI check | +| FR-011 | The boundary and limit rows above | all new tests | + +Command to run everything: + +``` +dotnet restore && dotnet build --no-restore && dotnet test --no-build --verbosity normal +``` + +To run only the affected tests: `dotnet test --filter "FullyQualifiedName~ProposedCode"`. \ No newline at end of file diff --git a/specs/KAN-4-add-new-product-to-mature/spec.md b/specs/KAN-4-add-new-product-to-mature/spec.md new file mode 100644 index 0000000..a3846e2 --- /dev/null +++ b/specs/KAN-4-add-new-product-to-mature/spec.md @@ -0,0 +1,63 @@ +# Feature specification: Add Bordeaux Wine as a new maturing product + +Issue: KAN-4 + +Context: `ItemMaturationService` in `src/Core/ProposedCode` is the new service. It recognises products by exact name (`ItemKind.FromName`) and applies a maturing rule per product kind. Any name it does not know is treated as a normal item that loses quality. Aged Brie already gains quality as it ages, with +1 per day and +2 once sell-in is below 0. Quality is limited to 0–50. The legacy `OriginalCode/UpdateQualityService.cs` is a separate service. + +## User scenarios + +### Scenario 1: Bordeaux Wine gains quality while within its sell-by period +A shop keeper runs the daily update. Bordeaux Wine should become more valuable as it ages, not less. + +Acceptance scenarios: +- Given an item named `Bordeaux Wine` with sell-in 10 and quality 20, when a day is matured, then its quality is 21 and its sell-in is 9. +- Given an item named `Bordeaux Wine` with sell-in 1 and quality 20, when a day is matured, then its quality is 21 and its sell-in is 0. + +### Scenario 2: Bordeaux Wine matures faster after its sell-by date +Given a sell-by date that has passed, the wine gains value twice as fast. + +Acceptance scenarios: +- Given an item named `Bordeaux Wine` with sell-in 0 and quality 20, when a day is matured, then its quality is 22 and its sell-in is -1. +- Given an item named `Bordeaux Wine` with sell-in -5 and quality 20, when a day is matured, then its quality is 22 and its sell-in is -6. + +### Scenario 3: Quality stays within its limits +Acceptance scenarios: +- Given `Bordeaux Wine` with quality 50, when a day is matured, then its quality is still 50. +- Given `Bordeaux Wine` with sell-in 10 and quality 49 or sell-in below 0 and quality 49, when a day is matured, then its quality is 50. +- Given `Bordeaux Wine` with quality 0, when a day is matured, then its quality is never negative. It is 1 within the sell-by period and 2 after it. + +### Scenario 4: Other products and the legacy service are unaffected +Acceptance scenarios: +- Given any other item (Standard, Aged Brie, Backstage passes, Conjured, Sulfuras), when a day is matured, then the result is the same as before this change. +- Given the legacy file `OriginalCode/UpdateQualityService.cs`, when the change is delivered, then that file is unchanged. + +## Requirements + +- FR-001: The system shall recognise the product named exactly `Bordeaux Wine` as a new maturing product. +- FR-002: While the product is within its sell-by period, the system shall increase the quality of `Bordeaux Wine` by 1 point per day. +- FR-003: Once the sell-in value is below 0, the system shall increase the quality of `Bordeaux Wine` by 2 points per day. +- FR-004: The system shall never let the quality of `Bordeaux Wine` exceed 50. +- FR-005: The system shall never let the quality of `Bordeaux Wine` be negative. +- FR-006: The system shall decrease the sell-in of `Bordeaux Wine` by 1 each day, as for other products. +- FR-007: The system shall never treat `Bordeaux Wine` as a normal item that loses quality over time. +- FR-008: The system shall leave the maturation of all other products unchanged. +- FR-009: All Bordeaux Wine maturation rules shall be implemented in the new service (`ItemMaturationService`). +- FR-010: The legacy file `OriginalCode/UpdateQualityService.cs` shall remain completely unchanged, and shall not handle `Bordeaux Wine`. +- FR-011: Automated tests shall cover the Bordeaux Wine rules above, including the sell-by boundary (sell-in 0 becoming -1) and the quality limits of 0 and 50. + +## Success criteria + +- SC-001: Maturing `Bordeaux Wine` with sell-in ≥ 1 raises quality by exactly 1, up to a maximum of 50. +- SC-002: Maturing `Bordeaux Wine` with sell-in ≤ 0 raises quality by exactly 2, up to a maximum of 50. +- SC-003: No Bordeaux Wine quality value ever falls outside 0–50. +- SC-004: The existing tests for the other products still pass without modification. +- SC-005: `git diff` shows no change to `src/Core/OriginalCode/UpdateQualityService.cs`. +- SC-006: The new tests for Bordeaux Wine pass in the build. + +## Out of scope + +- Changing the legacy `OriginalCode/UpdateQualityService.cs` or making it support `Bordeaux Wine`. +- Adding `Cellar-Aged Mead` (KAN-1) or any other product. +- Changing the rules of existing products. +- Recognising name variants or aliases of `Bordeaux Wine` (only the exact name is in scope). +- Changing the 0–50 quality limits. \ No newline at end of file diff --git a/specs/KAN-4-add-new-product-to-mature/tasks.md b/specs/KAN-4-add-new-product-to-mature/tasks.md new file mode 100644 index 0000000..1899ed6 --- /dev/null +++ b/specs/KAN-4-add-new-product-to-mature/tasks.md @@ -0,0 +1,17 @@ +# Tasks: Add Bordeaux Wine as a new maturing product (KAN-4) + +## Tests first +- [ ] T001 [P] Add `[InlineData("Bordeaux Wine", typeof(ItemKind.BordeauxWine))]` to the existing `FromName` theory. Add a new test that `"bordeaux wine"`, `"BordeauxWine"` and `" Bordeaux Wine"` resolve to `ItemKind.Standard`, in tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs (FR-001, FR-007) +- [ ] T002 [P] Add `Mature_WhenKindIsBordeauxWine_FollowsExpectedRules` in tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs (FR-002, FR-003, FR-004, FR-005, FR-006, FR-011). + - Use the `(quality, sellIn, expectedQuality, expectedSellIn)` rows `(20, 10, 21, 9)`, `(20, 1, 21, 0)`, `(20, 0, 22, -1)` and `(20, -5, 22, -6)`. + - Add the limit rows `(50, 10, 50, 9)`, `(50, -1, 50, -2)`, `(49, 10, 50, 9)`, `(49, -1, 50, -2)` and `(49, 0, 50, -1)`. + - Add the lower-bound rows `(0, 10, 1, 9)` and `(0, -1, 2, -2)`. +- [ ] T003 [P] Add the rows `("Bordeaux Wine", 20, 10, 21, 9)`, `("Bordeaux Wine", 20, 0, 22, -1)` and `("Bordeaux Wine", 50, 0, 50, -1)` to the existing theory in tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs (FR-007, FR-009, FR-011) + +## Implementation +- [ ] T004 [P] Add `public sealed record BordeauxWine : ItemKind;` and the exact-match arm `"Bordeaux Wine" => new BordeauxWine()` before the `_` default in `FromName`. Do not add aliases. Both changes go in src/Core/ProposedCode/Domain/ItemKind.cs (FR-001, FR-007) +- [ ] T005 Add the arm `ItemKind.BordeauxWine => this.MatureBordeauxWine(state)` before the `_` default arm. Add a private `MatureBordeauxWine(ItemState)` that follows `MatureAgedBrie`. It calls `Quality.Increase()` and `SellIn.NextDay()`, and it increases quality again when `newSellIn.IsExpired`. Both changes go in src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs, after T004 because it needs the `ItemKind.BordeauxWine` type (FR-002, FR-003, FR-004, FR-005, FR-006, FR-009) + +## Verification +- [ ] T006 Run `dotnet restore && dotnet build --no-restore && dotnet test --no-build --verbosity normal`. Confirm that the new Bordeaux Wine tests pass and that the existing tests for the other products pass unmodified. The step changes no files; the files it checks are tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs, tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs and tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs. Run it after T001–T005 (FR-008, FR-011) +- [ ] T007 Run `git diff --stat -- src/Core/OriginalCode`. Confirm that it prints nothing, so src/Core/OriginalCode/UpdateQualityService.cs is unchanged. The step changes no files. Run it after T005 (FR-010) \ No newline at end of file diff --git a/src/Core/ProposedCode/Domain/ItemKind.cs b/src/Core/ProposedCode/Domain/ItemKind.cs index 199f351..5765b98 100644 --- a/src/Core/ProposedCode/Domain/ItemKind.cs +++ b/src/Core/ProposedCode/Domain/ItemKind.cs @@ -18,6 +18,8 @@ public sealed record Conjured : ItemKind; public sealed record Sulfuras : ItemKind; + public sealed record BordeauxWine : ItemKind; + public static ItemKind FromName(string itemName) { return itemName switch @@ -29,6 +31,7 @@ public static ItemKind FromName(string itemName) "Conjured Mana Cake" => new Conjured(), "Conjured" => new Conjured(), "Sulfuras, Hand of Ragnaros" => new Sulfuras(), + "Bordeaux Wine" => new BordeauxWine(), _ => new Standard(), }; } diff --git a/src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs b/src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs index 0221621..29aeac7 100644 --- a/src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs +++ b/src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs @@ -16,6 +16,7 @@ public ItemState Mature(ItemKind kind, ItemState state) ItemKind.Backstage => this.MatureBackstage(state), ItemKind.Conjured => this.MatureConjured(state), ItemKind.Sulfuras => state, + ItemKind.BordeauxWine => this.MatureBordeauxWine(state), _ => this.MatureStandard(state), }; } @@ -46,6 +47,19 @@ private ItemState MatureAgedBrie(ItemState state) return new(newQuality, newSellIn); } + private ItemState MatureBordeauxWine(ItemState state) + { + var newQuality = state.Quality.Increase(); + var newSellIn = state.SellIn.NextDay(); + + if (newSellIn.IsExpired) + { + newQuality = newQuality.Increase(); + } + + return new(newQuality, newSellIn); + } + private ItemState MatureBackstage(ItemState state) { var newQuality = state.SellIn switch diff --git a/tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs b/tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs index 040e12d..8b4f088 100644 --- a/tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs +++ b/tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs @@ -18,6 +18,7 @@ public class ItemKindTests [InlineData("Conjured Mana Cake", typeof(ItemKind.Conjured))] [InlineData("Conjured", typeof(ItemKind.Conjured))] [InlineData("Sulfuras, Hand of Ragnaros", typeof(ItemKind.Sulfuras))] + [InlineData("Bordeaux Wine", typeof(ItemKind.BordeauxWine))] [InlineData("Elixir of the Mongoose", typeof(ItemKind.Standard))] public void FromName_MapsNamesToExpectedKinds(string itemName, Type expectedType) { @@ -25,4 +26,15 @@ public void FromName_MapsNamesToExpectedKinds(string itemName, Type expectedType result.Should().BeOfType(expectedType); } -} \ No newline at end of file + + [Theory] + [InlineData("bordeaux wine")] + [InlineData("BordeauxWine")] + [InlineData(" Bordeaux Wine")] + public void FromName_WhenBordeauxWineNameIsNotExact_ResolvesToStandard(string itemName) + { + var result = ItemKind.FromName(itemName); + + result.Should().BeOfType(); + } +} diff --git a/tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs b/tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs index 55d645c..902ee98 100644 --- a/tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs +++ b/tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs @@ -43,6 +43,28 @@ public void Mature_WhenKindIsAgedBrie_FollowsExpectedRules(int quality, int sell result.SellIn.Value.Should().Be(expectedSellIn); } + [Theory] + [InlineData(20, 10, 21, 9)] + [InlineData(20, 1, 21, 0)] + [InlineData(20, 0, 22, -1)] + [InlineData(20, -5, 22, -6)] + [InlineData(50, 10, 50, 9)] + [InlineData(50, -1, 50, -2)] + [InlineData(49, 10, 50, 9)] + [InlineData(49, -1, 50, -2)] + [InlineData(49, 0, 50, -1)] + [InlineData(0, 10, 1, 9)] + [InlineData(0, -1, 2, -2)] + public void Mature_WhenKindIsBordeauxWine_FollowsExpectedRules(int quality, int sellIn, int expectedQuality, int expectedSellIn) + { + var state = new ItemState(new Quality(quality), new SellIn(sellIn)); + + var result = this.policy.Mature(new ItemKind.BordeauxWine(), state); + + result.Quality.Value.Should().Be(expectedQuality); + result.SellIn.Value.Should().Be(expectedSellIn); + } + [Theory] [InlineData(20, 15, 21, 14)] [InlineData(20, 11, 21, 10)] @@ -89,4 +111,4 @@ public void Mature_WhenKindIsSulfuras_PreservesProvidedState() result.Should().Be(state); } -} \ No newline at end of file +} diff --git a/tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs b/tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs index 4690ec6..40f9a43 100644 --- a/tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs +++ b/tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs @@ -20,6 +20,9 @@ public class ItemMaturationServiceTests [InlineData("Conjured Mana Cake", 10, 5, 8, 4)] [InlineData("Conjured", 10, 0, 6, -1)] [InlineData("Sulfuras, Hand of Ragnaros", 80, 0, 80, 0)] + [InlineData("Bordeaux Wine", 20, 10, 21, 9)] + [InlineData("Bordeaux Wine", 20, 0, 22, -1)] + [InlineData("Bordeaux Wine", 50, 0, 50, -1)] public void Mature_UsesExpectedRulesForResolvedItemKinds(string name, int quality, int sellIn, int expectedQuality, int expectedSellIn) { var service = new ItemMaturationService(); @@ -35,4 +38,4 @@ public void Mature_UsesExpectedRulesForResolvedItemKinds(string name, int qualit result.Quality.Should().Be(expectedQuality); result.SellIn.Should().Be(expectedSellIn); } -} \ No newline at end of file +}