Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions specs/KAN-1-introduce-a-new-maturing-product/plan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Implementation plan: Introduce a new maturing product (Vintage Wine)

Issue: KAN-1. Spec: `spec.md`.

## Technical context

- **Language and runtime:** C# with `LangVersion` latest, nullable and implicit usings enabled. `Directory.Build.props` targets `net8.0`. CI (`dotnet.yml` calling `build.yml`) installs .NET SDK 9.0.x.
- **Solution layout:** `GildedRoseKata.sln` holds `src/Core` (`GildedRoseKata.Core.csproj`) and `tests/Core.Tests` (`GildedRoseKata.Core.Tests.csproj`).
- **Test tools:** xUnit 2.9.3, FluentAssertions 8.10.0 and coverlet. Tests are `[Theory]`/`[InlineData]` methods named `Method_WhenX_DoesY`.
- **Static analysis:** Roslynator and SonarAnalyzer run as analyzers. `TreatWarningsAsErrors` is false, but SC-003 requires no new warnings.
- **CI:** `dotnet restore`, then `dotnet build --no-restore`, then `dotnet test --no-build`.
- **Code style:** every file starts with the copyright header block. Types use file-scoped namespaces and `this.` member access.
- **Scope:** only the `ProposedCode` maturing flow changes. The flow is `ItemMaturationService.Mature`, which calls `ItemKind.FromName`, then `ItemState.Create`, then `IMaturingPolicy.Mature`. `OriginalCode` stays untouched.

## Approach

Follow the pattern the existing kinds use: a nested sealed record in `ItemKind`, a name mapping in `FromName`, and a private `MatureX` method selected by the policy's `switch`.

1. **`ItemKind`**
- Add `public sealed record VintageWine : ItemKind;`.
- Add the mappings `"Vintage Wine"` and `"Vintage"` to `VintageWine` in `FromName`. Matching stays exact and case-sensitive, as the spec requires (FR-001).
2. **`ItemMaturingPolicy`**
- Add the arm `ItemKind.VintageWine => this.MatureVintageWine(state)` before the default arm.
- Add `MatureVintageWine`, which reads SellIn at the start of the day, as `MatureBackstage` does:
- `state.SellIn.IsBelow(11)` gives `state.Quality.Increase(2)` (SellIn ≤ 10, including 0 and negative values, FR-004).
- Otherwise it gives `state.Quality.Increase()` (SellIn ≥ 11, FR-003).
- SellIn becomes `state.SellIn.NextDay()` (FR-002).
- No expiry penalty is applied, so Quality never drops (FR-005).
- `Quality.Increase` already clamps to 50 (FR-006).
3. **`ItemMaturationService`**
- No change. It already preserves `item.Name` (FR-008) and routes through `ItemKind.FromName` and the policy.
4. **`Quality`, `SellIn`, `ItemState`, `IMaturingPolicy`**
- No change. `Quality.FromItemKind` only special-cases Sulfuras, so Vintage Wine is clamped to 0–50 on creation.
5. **Existing kinds**
- Their code paths are not edited, so they behave as before (FR-007).

## Files

| File | Change | Why |
|---|---|---|
| `src/Core/ProposedCode/Domain/ItemKind.cs` | Change | Add the `VintageWine` record and the two name mappings (FR-001). |
| `src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs` | Change | Add the switch arm and `MatureVintageWine` (FR-002 to FR-006). |
| `tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs` | Change | Add `InlineData` rows for "Vintage Wine" and "Vintage" (AC-001). |
| `tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs` | Change | Add a `Mature_WhenKindIsVintageWine_FollowsExpectedRules` theory (AC-002 to AC-006). |
| `tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs` | Change | Add Vintage Wine rows to the existing theory. Add one test that the name is preserved (AC-008, SC-004). |

No new files, packages, project-file edits or CI edits are needed.

## Risks

- **Shared switch in `ItemMaturingPolicy`:** the default arm is `MatureStandard`. The new arm must sit before `_`, otherwise Vintage Wine would be treated as standard. Existing arms are not edited.
- **`ItemKind.FromName`:** the new strings must not collide with existing ones. They don't.
- **Public interface:** `IMaturingPolicy` is unchanged. Adding a nested record to `ItemKind` is additive. Custom `IMaturingPolicy` implementations that don't handle `VintageWine` would treat it as their default, which is outside this scope.
- **Threshold off-by-one:** the boundary uses start-of-day SellIn with `IsBelow(11)`. The tests cover SellIn 11, 10, 1, 0 and -5 to pin it.
- **Analyzer warnings:** follow the existing method shape, with no new magic-number patterns beyond what `MatureBackstage` already uses, to avoid new Sonar or Roslynator warnings.
- **No migrations or data changes.**

## Test plan

Run all tests with `dotnet test` from the repository root. CI runs `dotnet restore`, `dotnet build --no-restore`, then `dotnet test --no-build`. To run only the affected tests, use `dotnet test --filter "FullyQualifiedName~ProposedCode.Tests"`.

| Requirement | Proving test |
|---|---|
| FR-001 / AC-001 | `ItemKindTests.FromName_MapsNamesToExpectedKinds`: "Vintage Wine" and "Vintage" give `ItemKind.VintageWine`. |
| FR-002 / AC-002 | `ItemMaturingPolicyTests` Vintage Wine theory: expected SellIn is the input minus 1 for SellIn 20, 11, 10, 5, 1, 0 and -5. |
| FR-003 / AC-003 | Same theory: (Q10, SellIn 20) gives Q11 and (Q10, SellIn 11) gives Q11. |
| FR-004 / AC-004 | Same theory: (Q10, SellIn 10, 1, 0 or -5) gives Q12. |
| FR-005 / AC-005 | Same theory: (Q10, SellIn -5) gives Q12, which is at least 10. |
| FR-006 / AC-006 | Same theory: (Q50, SellIn 20) gives Q50, (Q49, SellIn 5) gives Q50, and (Q49, SellIn 20) gives Q50. |
| FR-007 / AC-007 / SC-002 | The existing Standard, AgedBrie, Backstage, Conjured and Sulfuras tests in `ItemMaturingPolicyTests` and `ItemMaturationServiceTests` pass with no expectation changes. |
| FR-008 / AC-008 | `ItemMaturationServiceTests`: a new `[Fact]` asserts `result.Name == "Vintage Wine"` after `Mature`. |
| SC-001 | The policy theory above covers the boundaries at SellIn 11, 10 and 0, expired items, and the Quality cap of 50. |
| SC-004 | New rows in `ItemMaturationServiceTests.Mature_UsesExpectedRulesForResolvedItemKinds`: ("Vintage Wine", 10, 20 → 11, 19), (10, 11 → 11, 10), (10, 10 → 12, 9), (10, 1 → 12, 0), (10, 0 → 12, -1), (10, -5 → 12, -6), (50, 20 → 50, 19), (49, 5 → 50, 4). One row also uses the "Vintage" short name. |
| SC-003 | `dotnet build` produces no new warnings, and the CI workflow stays green. |
76 changes: 76 additions & 0 deletions specs/KAN-1-introduce-a-new-maturing-product/spec.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Feature specification: Introduce a new maturing product (Vintage Wine)

Issue: KAN-1

UI work: no

The issue asks for a new product that matures (changes in Quality as days pass) and leaves the business rules to the team. This spec picks the product and its rules, based on the current code. `ItemKind` classifies items by name, and `ItemMaturingPolicy` holds the per-kind rules. `ItemMaturationService` applies them to an `Item`. `Quality` always stays between 0 and 50, except for the legendary Sulfuras at 80. The new product is **"Vintage Wine"**. It gains value over time, accelerates as its sell-by date nears, and never loses value.

## User scenarios

### Scenario 1: Vintage Wine matures slowly while far from its sell-by date
The innkeeper stocks Vintage Wine and ends the day. The wine gains a little value each day.

Acceptance scenarios:
1. Given a "Vintage Wine" with SellIn 20 and Quality 10, when a day passes, then it has SellIn 19 and Quality 11.
2. Given a "Vintage Wine" with SellIn 11 and Quality 10, when a day passes, then it has SellIn 10 and Quality 11.

### Scenario 2: Vintage Wine matures faster as the sell-by date approaches
The wine gains value faster once 10 days or fewer remain.

Acceptance scenarios:
1. Given a "Vintage Wine" with SellIn 10 and Quality 10, when a day passes, then it has SellIn 9 and Quality 12.
2. Given a "Vintage Wine" with SellIn 1 and Quality 10, when a day passes, then it has SellIn 0 and Quality 12.

### Scenario 3: Vintage Wine never loses value after its sell-by date
The sell-by date passes, but the wine keeps maturing at the fast rate.

Acceptance scenarios:
1. Given a "Vintage Wine" with SellIn 0 and Quality 10, when a day passes, then it has SellIn -1 and Quality 12.
2. Given a "Vintage Wine" with SellIn -5 and Quality 10, when a day passes, then it has SellIn -6 and Quality 12.

### Scenario 4: Vintage Wine never exceeds the maximum quality
Acceptance scenarios:
1. Given a "Vintage Wine" with SellIn 20 and Quality 50, when a day passes, then its Quality is still 50.
2. Given a "Vintage Wine" with SellIn 5 and Quality 49, when a day passes, then its Quality is 50.

### Scenario 5: Existing products are unaffected
Acceptance scenarios:
1. Given an "Aged Brie", "Backstage passes to a TAFKAL80ETC concert", "Conjured Mana Cake", "Sulfuras, Hand of Ragnaros" or a standard item, when a day passes, then it matures exactly as it did before this change.

## Requirements

- FR-001: The system shall recognise an item named "Vintage Wine" (or the short name "Vintage") as a distinct product kind, not as a standard item.
- FR-002: When a day passes, the system shall decrease the SellIn of a Vintage Wine by 1, including after its sell-by date has passed.
- FR-003: When a day passes and a Vintage Wine has a SellIn of 11 or more at the start of the day, the system shall increase its Quality by 1.
- FR-004: When a day passes and a Vintage Wine has a SellIn of 10 or less at the start of the day (including a SellIn of 0 or below, so after its sell-by date), the system shall increase its Quality by 2.
- FR-005: The system shall never decrease the Quality of a Vintage Wine as it matures, including after its sell-by date.
- FR-006: The system shall never let the Quality of a Vintage Wine exceed 50.
- FR-007: The system shall mature the existing kinds (Standard, Aged Brie, Backstage passes, Conjured, Sulfuras) exactly as it does today.
- FR-008: The system shall keep the item's name unchanged when it matures a Vintage Wine.

## Success criteria

- SC-001: The automated tests for the Vintage Wine rules (the boundaries at SellIn 11, 10 and 0, expired items, and the Quality cap of 50) all pass.
- SC-002: The existing tests for Standard, Aged Brie, Backstage, Conjured and Sulfuras pass without any change to their expectations.
- SC-003: The project builds with no new warnings, and the CI workflow stays green.
- SC-004: Maturing a "Vintage Wine" item through `ItemMaturationService` gives the quality and sell-in values listed in the user scenarios above.

## Acceptance criteria

- AC-001: When the name "Vintage Wine" or "Vintage" is classified, the system shall return the Vintage Wine kind and not the Standard kind (FR-001).
- AC-002: When a day passes for a Vintage Wine with SellIn 5, 0 or -5, the system shall return an item whose SellIn is 1 lower (FR-002).
- AC-003: When a day passes for a Vintage Wine with SellIn 11 or 20 and Quality 10, the system shall return Quality 11 (FR-003).
- AC-004: When a day passes for a Vintage Wine with SellIn 10, 1, 0 or -5 and Quality 10, the system shall return Quality 12 (FR-004).
- AC-005: When a day passes for a Vintage Wine with SellIn -5 and Quality 10, the system shall return a Quality of at least 10 (FR-005).
- AC-006: When a day passes for a Vintage Wine with Quality 50, or Quality 49 and SellIn 5, the system shall return Quality 50 (FR-006).
- AC-007: When a day passes for each of Aged Brie, Backstage passes, Conjured Mana Cake, Sulfuras and a standard item, the system shall return the same SellIn and Quality as before this change (FR-007).
- AC-008: When a day passes for an item named "Vintage Wine", the system shall return an item whose name is "Vintage Wine" (FR-008).

## Out of scope

- Any user interface, console output or reporting.
- Changing the rules of existing products.
- Changing the legacy `OriginalCode` (`Item`, `UpdateQualityService`). Vintage Wine is added only to the `ProposedCode` maturing flow.
- Price, stock, or any item property other than Name, SellIn and Quality.
- Matching item names by case-insensitive or partial matching. The existing exact-name matching is kept.
24 changes: 24 additions & 0 deletions specs/KAN-1-introduce-a-new-maturing-product/tasks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Tasks: Introduce a new maturing product (Vintage Wine)

- [ ] T001 Add `public sealed record VintageWine : ItemKind;` and the exact-name mappings `"Vintage Wine"` and `"Vintage"` to `VintageWine` in `FromName`. Place the mappings before the `_` default arm, and leave the existing arms alone. Do this in src/Core/ProposedCode/Domain/ItemKind.cs (FR-001, AC-001)
- [ ] T002 [P] Add `InlineData` rows to the `FromName` mapping theory so that "Vintage Wine" and "Vintage" give `ItemKind.VintageWine`. Keep the existing rows unchanged. Do this in tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs, after T001 because the rows need the `VintageWine` type to compile (FR-001, AC-001)
- [ ] T003 Add the arm `ItemKind.VintageWine => this.MatureVintageWine(state)` before the `_` default arm. Add a private `MatureVintageWine` method. It uses the start-of-day SellIn: `state.SellIn.IsBelow(11)` gives `state.Quality.Increase(2)`, and otherwise `state.Quality.Increase()`. SellIn becomes `state.SellIn.NextDay()`. It applies no expiry penalty and no reset. Do this in src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs, after T001 (FR-002, FR-003, FR-004, FR-005, FR-006, AC-002, AC-003, AC-004, AC-005, AC-006)
- [ ] T004 [P] Add a `Mature_WhenKindIsVintageWine_FollowsExpectedRules` theory with `InlineData` rows. It uses the quality-and-SellIn pairs below, and the expected SellIn is always the input minus 1.
- Q10 with SellIn 20 and 11 gives Q11.
- Q10 with SellIn 10, 1, 0 and -5 gives Q12.
- Q50 with SellIn 20 gives Q50.
- Q49 with SellIn 5 gives Q50.
- Q49 with SellIn 20 gives Q50.
- Do this in tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs, after T001 and T003. Leave the existing tests unchanged (FR-002 to FR-007, AC-002 to AC-007, SC-001, SC-002)
- [ ] T005 [P] Add Vintage Wine rows to `Mature_UsesExpectedRulesForResolvedItemKinds`. Each row is (name, quality, sellIn → expected quality, expected sellIn); use "Vintage" as the name in one row.
- ("Vintage Wine", 10, 20 → 11, 19)
- (10, 11 → 11, 10)
- (10, 10 → 12, 9)
- (10, 1 → 12, 0)
- (10, 0 → 12, -1)
- (10, -5 → 12, -6)
- (50, 20 → 50, 19)
- (49, 5 → 50, 4)

Also add a `[Fact]` that asserts `result.Name == "Vintage Wine"` after `Mature`. Do this in tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs, after T001 and T003. Leave the existing rows unchanged (FR-007, FR-008, AC-007, AC-008, SC-002, SC-004)
- [ ] T006 Run `dotnet restore`, `dotnet build --no-restore` and `dotnet test --no-build` from the repository root, after T002, T004 and T005. Check that all tests pass, the existing Standard, Aged Brie, Backstage, Conjured and Sulfuras expectations are unchanged, and the build shows no new Roslynator or Sonar warnings. No file changes are expected (FR-007, SC-001, SC-002, SC-003)
6 changes: 5 additions & 1 deletion src/Core/ProposedCode/Domain/ItemKind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public sealed record Conjured : ItemKind;

public sealed record Sulfuras : ItemKind;

public sealed record VintageWine : ItemKind;

public static ItemKind FromName(string itemName)
{
return itemName switch
Expand All @@ -29,7 +31,9 @@ public static ItemKind FromName(string itemName)
"Conjured Mana Cake" => new Conjured(),
"Conjured" => new Conjured(),
"Sulfuras, Hand of Ragnaros" => new Sulfuras(),
"Vintage Wine" => new VintageWine(),
"Vintage" => new VintageWine(),
_ => new Standard(),
};
}
}
}
11 changes: 10 additions & 1 deletion src/Core/ProposedCode/Domain/ItemMaturingPolicy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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.VintageWine => this.MatureVintageWine(state),
_ => this.MatureStandard(state),
};
}
Expand Down Expand Up @@ -77,4 +78,12 @@ private ItemState MatureConjured(ItemState state)

return new(newQuality, newSellIn);
}
}

private ItemState MatureVintageWine(ItemState state)
{
var newQuality = state.SellIn.IsBelow(11) ? state.Quality.Increase(2) : state.Quality.Increase();
var newSellIn = state.SellIn.NextDay();

return new(newQuality, newSellIn);
}
}
4 changes: 3 additions & 1 deletion tests/Core.Tests/ProposedCode/Domain/ItemKindTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ public class ItemKindTests
[InlineData("Conjured Mana Cake", typeof(ItemKind.Conjured))]
[InlineData("Conjured", typeof(ItemKind.Conjured))]
[InlineData("Sulfuras, Hand of Ragnaros", typeof(ItemKind.Sulfuras))]
[InlineData("Vintage Wine", typeof(ItemKind.VintageWine))]
[InlineData("Vintage", typeof(ItemKind.VintageWine))]
[InlineData("Elixir of the Mongoose", typeof(ItemKind.Standard))]
public void FromName_MapsNamesToExpectedKinds(string itemName, Type expectedType)
{
var result = ItemKind.FromName(itemName);

result.Should().BeOfType(expectedType);
}
}
}
22 changes: 21 additions & 1 deletion tests/Core.Tests/ProposedCode/Domain/ItemMaturingPolicyTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@ public void Mature_WhenKindIsConjured_FollowsExpectedRules(int quality, int sell
result.SellIn.Value.Should().Be(expectedSellIn);
}

[Theory]
[InlineData(10, 20, 11, 19)]
[InlineData(10, 11, 11, 10)]
[InlineData(10, 10, 12, 9)]
[InlineData(10, 1, 12, 0)]
[InlineData(10, 0, 12, -1)]
[InlineData(10, -5, 12, -6)]
[InlineData(50, 20, 50, 19)]
[InlineData(49, 5, 50, 4)]
[InlineData(49, 20, 50, 19)]
public void Mature_WhenKindIsVintageWine_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.VintageWine(), state);

result.Quality.Value.Should().Be(expectedQuality);
result.SellIn.Value.Should().Be(expectedSellIn);
}

[Fact]
public void Mature_WhenKindIsSulfuras_PreservesProvidedState()
{
Expand All @@ -89,4 +109,4 @@ public void Mature_WhenKindIsSulfuras_PreservesProvidedState()

result.Should().Be(state);
}
}
}
26 changes: 25 additions & 1 deletion tests/Core.Tests/ProposedCode/ItemMaturationServiceTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ 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("Vintage Wine", 10, 20, 11, 19)]
[InlineData("Vintage Wine", 10, 11, 11, 10)]
[InlineData("Vintage Wine", 10, 10, 12, 9)]
[InlineData("Vintage Wine", 10, 1, 12, 0)]
[InlineData("Vintage Wine", 10, 0, 12, -1)]
[InlineData("Vintage", 10, -5, 12, -6)]
[InlineData("Vintage Wine", 50, 20, 50, 19)]
[InlineData("Vintage Wine", 49, 5, 50, 4)]
public void Mature_UsesExpectedRulesForResolvedItemKinds(string name, int quality, int sellIn, int expectedQuality, int expectedSellIn)
{
var service = new ItemMaturationService();
Expand All @@ -35,4 +43,20 @@ public void Mature_UsesExpectedRulesForResolvedItemKinds(string name, int qualit
result.Quality.Should().Be(expectedQuality);
result.SellIn.Should().Be(expectedSellIn);
}
}

[Fact]
public void Mature_WhenItemIsVintageWine_PreservesName()
{
var service = new ItemMaturationService();
var item = new Item
{
Name = "Vintage Wine",
Quality = 10,
SellIn = 20,
};

var result = service.Mature(item);

result.Name.Should().Be("Vintage Wine");
}
}
Loading