diff --git a/specs/add-a-new-product-to-gilded-rose/plan.md b/specs/add-a-new-product-to-gilded-rose/plan.md new file mode 100644 index 0000000..67db5ca --- /dev/null +++ b/specs/add-a-new-product-to-gilded-rose/plan.md @@ -0,0 +1,38 @@ +# Implementation plan: Add Mature Cheese to Gilded Rose + +## Technical context + +The proposed implementation is C# targeting .NET 8, built with the .NET SDK. The feature belongs to `src/Core/ProposedCode`, which classifies items with `ItemKind`, applies daily rules through `ItemMaturingPolicy`, and represents bounded quality with `Quality`. Tests use xUnit and FluentAssertions; CI runs `dotnet restore`, `dotnet build`, and `dotnet test`. + +## Approach + +Add a `MatureCheese` item kind for the exact name `Mature Cheese`, leaving similar names classified as standard. Add an isolated maturation rule that determines the quality increase from the item’s `sellIn` **before** decrementing it, then advances `sellIn` by one day. Reuse `Quality`’s existing clamping and increase behavior to preserve the 0–50 bounds and the established handling of imported out-of-range values. + +Extend the proposed-code unit tests for name matching, daily policy boundaries, quality limits, repeated updates, multiple items, and mixed inventories. Keep `OriginalCode` unchanged. The existing model has no separate create/read/display/search behavior; confirm the maturation service does not mutate its input item, and do not add unrelated APIs. + +## Files + +- `src/Core/ProposedCode/Domain/ItemKind.cs` — recognize the exact Mature Cheese name. +- `src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs` — implement the isolated daily rule and dispatch for the new kind. +- `tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs` — cover exact and similar-name classification. +- `tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs` — cover sell-by boundaries, quality bounds, and preservation of existing product rules. +- `tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs` — cover end-to-end updates, repeated updates, mixed and multiple items, and input immutability. + +## Risks + +- The spec marks the exact product name, quality increases, and maximum quality as needing confirmation; this plan uses the proposed values: exact name `Mature Cheese`, +1 before sell-by, +2 on and after sell-by, and a maximum of 50. +- `Quality` already clamps values on construction and increase, so imported invalid values should follow that existing policy without changing shared quality logic. +- `ItemKind` and `ItemMaturingPolicy` are shared by existing proposed products; narrowly scoped dispatch and regression tests reduce the risk of changing their behavior. +- `OriginalCode` is explicitly out of scope and must remain unchanged. + +## Test plan + +Run `dotnet test tests/Core.Tests/GildedRoseKata.Core.Tests.csproj`. + +- **FR-001–FR-003:** Policy tests for `sellIn` values 5, 1, 0, and -1 verify daily decrement and the correct increase based on the pre-update value. +- **FR-004–FR-005:** Policy tests for qualities 0, 1, 49, 50, and out-of-range inputs verify results remain within 0–50. +- **FR-006:** Classification and service tests verify exact-name matching and that similar names retain standard behavior. +- **FR-007:** Regression tests for Aged Brie, Backstage passes, Sulfuras, Conjured, and standard items verify existing behavior; confirm no `OriginalCode` files change. +- **FR-008:** Service test verifies an update does not mutate the input item; no separate read, display, or search APIs are present in this code. +- **FR-009:** Service tests cover multiple Mature Cheese items, consecutive updates, and mixed inventories. +- **FR-010:** Deterministic expected-result tests for the same starting state verify repeatable behavior. \ No newline at end of file diff --git a/specs/add-a-new-product-to-gilded-rose/spec.md b/specs/add-a-new-product-to-gilded-rose/spec.md new file mode 100644 index 0000000..27be30b --- /dev/null +++ b/specs/add-a-new-product-to-gilded-rose/spec.md @@ -0,0 +1,65 @@ +# Feature specification: Add Mature Cheese to Gilded Rose +gilded_rose_new_product_business_rules.md +UI work: no + +## User scenarios + +**Daily inventory update.** As an inventory operator, I want Mature Cheese quality and sell-by values updated each day so that its aging behavior is applied consistently. + +- Given Mature Cheese with `sellIn` 5 or 1 and quality 10, when the daily inventory update runs, then `sellIn` decreases by 1 and quality increases by 1. +- Given Mature Cheese with `sellIn` 0 or -1 and quality 10, when the daily inventory update runs, then `sellIn` decreases by 1 and quality increases by 2. +- Given Mature Cheese at a quality boundary, when the daily inventory update runs, then quality stays within 0–50. + +**Mixed inventory.** As an inventory operator, I want the new product’s rules to apply only to Mature Cheese so other products keep their established behavior. + +- Given an inventory containing Mature Cheese and existing products, when the daily inventory update runs, then each item follows its own product rules. +- Given a product with a name similar to but not exactly `Mature Cheese`, when the daily inventory update runs, then it does not receive Mature Cheese behavior. + +**Read and inspect inventory.** As an inventory user, I want to create, read, display, and search for Mature Cheese without changing its values so that only a daily update changes inventory state. + +- Given a Mature Cheese item, when it is created, read, displayed, or searched for, then its quality and `sellIn` remain unchanged. + +## Requirements + +- **FR-001** The system shall decrease Mature Cheese `sellIn` by 1 during each daily inventory update. +- **FR-002** When Mature Cheese `sellIn` is greater than 0 before an update, the system shall increase its quality by 1 for that update. [NEEDS CLARIFICATION: Confirm that the proposed increase is 1 before the sell-by date.] +- **FR-003** When Mature Cheese `sellIn` is 0 or less before an update, the system shall increase its quality by 2 for that update. [NEEDS CLARIFICATION: Confirm that the proposed increase is 2 on and after the sell-by date.] +- **FR-004** The system shall cap Mature Cheese quality at 50, including when quality is 49 or already 50 before the update. [NEEDS CLARIFICATION: Confirm that 50 is the maximum quality.] +- **FR-005** The system shall keep Mature Cheese quality between 0 and 50, including when existing inventory data has an out-of-range quality; invalid values shall follow the Proposed Code’s established data-validation policy. +- **FR-006** The system shall apply Mature Cheese behavior only to the exact product name `Mature Cheese`; similar names shall follow their own existing rules. [NEEDS CLARIFICATION: Confirm that `Mature Cheese` is the official exact product name.] +- **FR-007** The system shall leave existing product behavior unchanged, including Aged Brie, Backstage passes, Sulfuras, and Conjured items, and shall make this change only in Proposed Code, not Original Code. +- **FR-008** The system shall not change a Mature Cheese item’s quality or `sellIn` when it is created, read, displayed, or searched; those values change only during the daily inventory update. +- **FR-009** The system shall apply the specified behavior consistently across multiple Mature Cheese items, consecutive daily updates, and mixed inventories. +- **FR-010** The system shall produce the same result for the same starting item and daily update, and the Mature Cheese rules shall be verifiable with automated unit tests. + +## Success criteria + +- **SC-001** A daily update produces the specified quality and `sellIn` values for `sellIn` 5, 1, 0, and -1 with quality 10. +- **SC-002** Daily updates keep quality within 0–50, including starting qualities 0, 1, 49, 50, and out-of-range imported values. +- **SC-003** Automated unit tests cover the Mature Cheese rules, multiple updates and items, mixed inventories, similar names, and existing product behavior. +- **SC-004** Original Code remains unchanged. + +## Acceptance criteria + +- **AC-001 (FR-001):** When a Mature Cheese item is updated once, the system shall decrease `sellIn` by 1, including on each of multiple consecutive updates. +- **AC-002 (FR-002):** When Mature Cheese starts an update with `sellIn` 5 or 1 and quality 10, the system shall produce respectively `sellIn` 4 and 0, with quality 11. [NEEDS CLARIFICATION: Confirm that the proposed increase is 1 before the sell-by date.] +- **AC-003 (FR-003):** When Mature Cheese starts an update with `sellIn` 0 or -1 and quality 10, the system shall produce respectively `sellIn` -1 and -2, with quality 12. [NEEDS CLARIFICATION: Confirm that the proposed increase is 2 on and after the sell-by date.] +- **AC-004 (FR-004):** When Mature Cheese starts an update at quality 49 or 50, the system shall produce quality 50 and never exceed it. [NEEDS CLARIFICATION: Confirm that 50 is the maximum quality.] +- **AC-005 (FR-005):** When Mature Cheese starts an update with quality below 0 or above 50, the system shall follow the established validation policy and produce a quality within 0–50. +- **AC-006 (FR-006):** When an item named exactly `Mature Cheese` is updated, the system shall apply the specified Mature Cheese rules; when a similarly named item is updated, the system shall not apply those rules. [NEEDS CLARIFICATION: Confirm that `Mature Cheese` is the official exact product name.] +- **AC-007 (FR-007):** When a mixed inventory containing Mature Cheese and existing products is updated, the system shall apply the new rules to Mature Cheese while preserving the existing products’ behavior; Original Code shall remain unchanged. +- **AC-008 (FR-008):** When a Mature Cheese item is created, read, displayed, or searched for, the system shall leave its quality and `sellIn` unchanged. +- **AC-009 (FR-009):** When an inventory contains multiple Mature Cheese items and is updated over consecutive days, the system shall apply the specified daily rules to each item. +- **AC-010 (FR-010):** When the same starting item is subjected to the same daily update, the system shall produce the same result, and automated unit tests shall verify the specified Mature Cheese behavior. + +## Out of scope + +- Changing Original Code or refactoring unrelated product behavior. +- Adding behavior beyond the stated daily quality and sell-by rules. + +## Open questions + +- [NEEDS CLARIFICATION: Confirm that `Mature Cheese` is the official exact product name.] +- [NEEDS CLARIFICATION: Confirm that the proposed quality increase is 1 before the sell-by date and 2 on and after it.] +- [NEEDS CLARIFICATION: Confirm that 50 is the maximum quality.] +- [NEEDS CLARIFICATION: Should Mature Cheese have additional behavior after a maximum age, such as becoming unsafe or losing value?] \ No newline at end of file diff --git a/specs/add-a-new-product-to-gilded-rose/tasks.md b/specs/add-a-new-product-to-gilded-rose/tasks.md new file mode 100644 index 0000000..87a372e --- /dev/null +++ b/specs/add-a-new-product-to-gilded-rose/tasks.md @@ -0,0 +1,3 @@ +# Tasks: Add Mature Cheese to Gilded Rose +- [ ] T001 [P] Recognize the exact `Mature Cheese` name as its own item kind in `src/Core/ProposedCode/Domain/ItemKind.cs` (FR-006, AC-006) +- [ ] T002 Implement Mature Cheese’s daily quality and sell-by rules using the pre-update `sellIn`, existing quality bounds, and isolated dispatch in `src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs` after T001 (FR-001–FR-005, FR-007–FR-010, AC-001–AC-005, AC-007–AC-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..cb7f8a6 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 MatureCheese : ItemKind; + public static ItemKind FromName(string itemName) { return itemName switch @@ -29,7 +31,8 @@ public static ItemKind FromName(string itemName) "Conjured Mana Cake" => new Conjured(), "Conjured" => new Conjured(), "Sulfuras, Hand of Ragnaros" => new Sulfuras(), + "Mature Cheese" => new MatureCheese(), _ => new Standard(), }; } -} \ No newline at end of file +} diff --git a/src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs b/src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs index 0221621..c92d90f 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.MatureCheese => this.MatureCheese(state), _ => this.MatureStandard(state), }; } @@ -77,4 +78,13 @@ private ItemState MatureConjured(ItemState state) return new(newQuality, newSellIn); } -} \ No newline at end of file + + private ItemState MatureCheese(ItemState state) + { + var increase = state.SellIn.Value > 0 ? 1 : 2; + var newQuality = state.Quality.Increase(increase); + var newSellIn = state.SellIn.NextDay(); + + return new(newQuality, newSellIn); + } +} diff --git a/tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs b/tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs index 040e12d..9fe5837 100644 --- a/tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs +++ b/tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs @@ -25,4 +25,4 @@ public void FromName_MapsNamesToExpectedKinds(string itemName, Type expectedType result.Should().BeOfType(expectedType); } -} \ No newline at end of file +} diff --git a/tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs b/tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs index 4690ec6..26dab05 100644 --- a/tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs +++ b/tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs @@ -35,4 +35,147 @@ public void Mature_UsesExpectedRulesForResolvedItemKinds(string name, int qualit result.Quality.Should().Be(expectedQuality); result.SellIn.Should().Be(expectedSellIn); } -} \ No newline at end of file + + [Fact] + public void AC001_SellInDecreasesOnEachConsecutiveUpdate() + { + var service = new ItemMaturationService(); + var item = CreateItem("Mature Cheese", 10, 5); + + item = service.Mature(item); + item = service.Mature(item); + + item.SellIn.Should().Be(3); + item.Quality.Should().Be(12); + } + + [Theory] + [InlineData(5, 4)] + [InlineData(1, 0)] + public void AC002_QualityIncreasesByOneBeforeSellBy(int sellIn, int expectedSellIn) + { + var result = new ItemMaturationService().Mature(CreateItem("Mature Cheese", 10, sellIn)); + + result.SellIn.Should().Be(expectedSellIn); + result.Quality.Should().Be(11); + } + + [Theory] + [InlineData(0, -1)] + [InlineData(-1, -2)] + public void AC003_QualityIncreasesByTwoOnOrAfterSellBy(int sellIn, int expectedSellIn) + { + var result = new ItemMaturationService().Mature(CreateItem("Mature Cheese", 10, sellIn)); + + result.SellIn.Should().Be(expectedSellIn); + result.Quality.Should().Be(12); + } + + [Theory] + [InlineData(49)] + [InlineData(50)] + public void AC004_QualityNeverExceedsFifty(int quality) + { + var result = new ItemMaturationService().Mature(CreateItem("Mature Cheese", quality, 5)); + + result.Quality.Should().Be(50); + } + + [Theory] + [InlineData(-1, 1)] + [InlineData(51, 50)] + public void AC005_OutOfRangeQualityUsesEstablishedClamping(int quality, int expectedQuality) + { + var result = new ItemMaturationService().Mature(CreateItem("Mature Cheese", quality, 5)); + + result.Quality.Should().Be(expectedQuality); + result.Quality.Should().BeInRange(0, 50); + } + + [Fact] + public void AC006_OnlyExactMatureCheeseNameGetsItsRules() + { + var service = new ItemMaturationService(); + var exact = service.Mature(CreateItem("Mature Cheese", 10, 5)); + var similar = service.Mature(CreateItem("Mature Cheeses", 10, 5)); + + (exact.Quality, exact.SellIn).Should().Be((11, 4)); + (similar.Quality, similar.SellIn).Should().Be((9, 4)); + } + + [Fact] + public void AC007_MixedInventoryPreservesExistingProductRules() + { + var service = new ItemMaturationService(); + var results = new[] + { + service.Mature(CreateItem("Mature Cheese", 10, 5)), + service.Mature(CreateItem("Aged Brie", 10, 2)), + service.Mature(CreateItem("Backstage passes to a TAFKAL80ETC concert", 20, 10)), + service.Mature(CreateItem("Sulfuras, Hand of Ragnaros", 80, 0)), + service.Mature(CreateItem("Conjured Mana Cake", 10, 5)), + }; + + results.Select(item => (item.Quality, item.SellIn)).Should().Equal( + (11, 4), + (11, 1), + (22, 9), + (80, 0), + (8, 4)); + } + + [Fact] + public void AC008_DailyUpdateReturnsUpdatedItemWithoutMutatingInput() + { + var service = new ItemMaturationService(); + var original = CreateItem("Mature Cheese", 10, 5); + + var result = service.Mature(original); + + original.Quality.Should().Be(10); + original.SellIn.Should().Be(5); + result.Quality.Should().Be(11); + result.SellIn.Should().Be(4); + } + + [Fact] + public void AC009_MultipleItemsMatureCorrectlyOverConsecutiveDays() + { + var service = new ItemMaturationService(); + var items = new[] + { + CreateItem("Mature Cheese", 10, 1), + CreateItem("Mature Cheese", 49, 0), + }; + + for (var day = 0; day < 2; day++) + { + items = items.Select(service.Mature).ToArray(); + } + + items.Select(item => (item.Quality, item.SellIn)).Should().Equal( + (13, -1), + (50, -2)); + } + + [Fact] + public void AC010_SameStartingItemProducesTheSpecifiedDeterministicResult() + { + var service = new ItemMaturationService(); + var first = service.Mature(CreateItem("Mature Cheese", 10, 5)); + var second = service.Mature(CreateItem("Mature Cheese", 10, 5)); + + (first.Quality, first.SellIn).Should().Be((11, 4)); + (second.Quality, second.SellIn).Should().Be((11, 4)); + } + + private static Item CreateItem(string name, int quality, int sellIn) + { + return new Item + { + Name = name, + Quality = quality, + SellIn = sellIn, + }; + } +}