acdream/tests/AcDream.Runtime.Tests/Gameplay/VendorShopItemMaterializerTests.cs
Erik 97cf873870
Some checks are pending
Headless portability / portable-headless (ubuntu-latest) (push) Waiting to run
Headless portability / portable-headless (windows-latest) (push) Waiting to run
Headless portability / linux-graphical (push) Waiting to run
Headless portability / linux-vulkan (push) Waiting to run
feat(vendor): Slice 6 buy arc — shop items are real objects, vendor selection is THE selection, and Buy works (0x005F)
Three ordered pieces in one landing (the shared controller/composition
files carry all three; the internal order was 6.1 -> 6.2 -> 6.3):

6.1 VendorShopItemMaterializer diff-merges the shop list into the live
ClientObjectTable on VendorState transitions (so client-local close and
session teardown retire the entries too) and never claims a guid it did
not add — ACE's UniqueItemsForSale can re-list a guid a player once
held (AP-163 files the collision-skip; no retail counterpart traced).
Right-click examine on shop items now routes through the ordinary
appraisal path — the 5.4 F7c blocker dissolves with the table entries.

6.2 SelectionChangeSource.Vendor: row clicks, auto-select, and examine
all flow through the canonical SelectionState; the status bar and the
existing byte-faithful StackSplitQuantityState slider light up
unmodified. VendorSplitPolicy is the single 0xDC41CB0 mask owner; the
slider VALUE seeds to 1 for exempt items while maxSplitSize keeps the
stack (the splitSize/maxSplitSize distinction, research §B.3).
Selection clears at retail's actual site — VendorItemsUI::RemoveFromShop
(pc:202848), not a CloseVendor-level clear that does not exist.

6.3 BuildBuy (0x005F): vendorGuid, count, (i32 amount, u32 guid) pairs,
and the trailing alternateCurrencyId the REAL client sends
(CM_Vendor::Event_Buy pc:689288) though ACE's reader ignores it.
TryBuy rides the EXISTING J5.2 one-request-at-a-time reservation and
completes on UseDone; the Buy button disables while a request is in
flight. The reconciliation round-trip (money property update, inventory
CreateObject, ApproachVendor refresh -> panel rebuild) is proven by a
synthetic-inbound test against existing machinery — no new owner.

Register: AP-161 narrowed (selection + examine residuals close;
staging/Sell remain; double-click-to-buy confirmed ABSENT from retail
with negative evidence cited — we match retail). AP-162 files the
conscious no-client-side-affordability-precheck deferral.

Clean-room complete solution: 11,368 passed / 4 skipped / 0 failed.

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
2026-08-07 20:28:26 +02:00

191 lines
7.6 KiB
C#

using AcDream.Core.Items;
using AcDream.Runtime.Gameplay;
namespace AcDream.Runtime.Tests.Gameplay;
/// <summary>
/// Slice 6.1 — <see cref="VendorShopItemMaterializer"/> in isolation: the
/// diff/materialize/retire logic against a bare <see cref="VendorState"/> +
/// <see cref="ClientObjectTable"/> pair, independent of the wire parse
/// (<see cref="RuntimeVendorLifecycleTests"/> covers the end-to-end
/// ApproachVendor path).
/// </summary>
public sealed class VendorShopItemMaterializerTests
{
private const uint VendorGuid = 0x40001000u;
private const uint OtherVendorGuid = 0x40002000u;
private const uint ItemA = 0x50002000u;
private const uint ItemB = 0x50002001u;
private static VendorShopItem Item(uint guid, string name = "Item", int? descStackSize = null) =>
new(guid, StackSize: -1, WeenieClassId: 1u, Name: name, ItemType: (uint)ItemType.Misc,
IconId: 0x1234u, Value: 10, DescStackSize: descStackSize);
[Fact]
public void Apply_MaterializesEachShopItemWithVendorAsContainer()
{
var vendor = new VendorState();
var objects = new ClientObjectTable();
using var materializer = new VendorShopItemMaterializer(vendor, objects);
vendor.Apply(VendorGuid, default, new[] { Item(ItemA, "Iron Sword"), Item(ItemB, "Bread") });
ClientObject? a = objects.Get(ItemA);
ClientObject? b = objects.Get(ItemB);
Assert.NotNull(a);
Assert.NotNull(b);
Assert.Equal(VendorGuid, a!.ContainerId);
Assert.Equal(VendorGuid, b!.ContainerId);
Assert.Equal("Iron Sword", a.Name);
Assert.Equal(2, materializer.OwnedCount);
Assert.True(materializer.Owns(ItemA));
Assert.True(materializer.Owns(ItemB));
}
[Fact]
public void Close_RemovesEveryMaterializedItem()
{
var vendor = new VendorState();
var objects = new ClientObjectTable();
using var materializer = new VendorShopItemMaterializer(vendor, objects);
vendor.Apply(VendorGuid, default, new[] { Item(ItemA), Item(ItemB) });
vendor.Close();
Assert.Null(objects.Get(ItemA));
Assert.Null(objects.Get(ItemB));
Assert.Equal(0, materializer.OwnedCount);
}
[Fact]
public void Reset_RemovesEveryMaterializedItem()
{
var vendor = new VendorState();
var objects = new ClientObjectTable();
using var materializer = new VendorShopItemMaterializer(vendor, objects);
vendor.Apply(VendorGuid, default, new[] { Item(ItemA) });
vendor.Reset();
Assert.Null(objects.Get(ItemA));
Assert.Equal(0, materializer.OwnedCount);
}
[Fact]
public void DifferentVendorSupersedes_RemovesPriorVendorsItemsBeforeMaterializingTheNewOnes()
{
var vendor = new VendorState();
var objects = new ClientObjectTable();
using var materializer = new VendorShopItemMaterializer(vendor, objects);
vendor.Apply(VendorGuid, default, new[] { Item(ItemA, "First Vendor Item") });
Assert.NotNull(objects.Get(ItemA));
const uint NewItem = 0x50003000u;
vendor.Apply(OtherVendorGuid, default, new[] { Item(NewItem, "Second Vendor Item") });
Assert.Null(objects.Get(ItemA));
ClientObject? replacement = objects.Get(NewItem);
Assert.NotNull(replacement);
Assert.Equal(OtherVendorGuid, replacement!.ContainerId);
Assert.Equal(1, materializer.OwnedCount);
}
[Fact]
public void Refreshed_SameVendor_DoesNotFireObjectRemovedForStillListedItems()
{
// A same-vendor re-approach (post-buy refresh) must not remove+
// re-add a guid that's still in stock -- see the class doc's "diff,
// not blanket remove-then-reinsert" rationale. A UI panel holding
// the guid (an open appraisal window) would see a false "it's gone"
// notice if this regressed to blanket removal.
var vendor = new VendorState();
var objects = new ClientObjectTable();
using var materializer = new VendorShopItemMaterializer(vendor, objects);
vendor.Apply(VendorGuid, default, new[] { Item(ItemA, "Chainmail") });
var removed = new List<uint>();
objects.ObjectRemoved += o => removed.Add(o.ObjectId);
// Same vendor id re-approaches with the SAME item guid still listed
// (e.g. a post-buy refresh where this item wasn't the one bought)
// but with a refreshed field value.
vendor.Apply(VendorGuid, default, new[] { Item(ItemA, "Chainmail", descStackSize: 5) });
Assert.Empty(removed);
Assert.Equal(1, materializer.OwnedCount);
Assert.Equal(5, objects.Get(ItemA)!.StackSize);
}
[Fact]
public void Refreshed_ItemNoLongerListed_IsRemoved()
{
// A unique item sold out (bought up / delisted) between one
// ApproachVendor and the next same-vendor refresh.
var vendor = new VendorState();
var objects = new ClientObjectTable();
using var materializer = new VendorShopItemMaterializer(vendor, objects);
vendor.Apply(VendorGuid, default, new[] { Item(ItemA), Item(ItemB) });
vendor.Apply(VendorGuid, default, new[] { Item(ItemB) });
Assert.Null(objects.Get(ItemA));
Assert.NotNull(objects.Get(ItemB));
Assert.Equal(1, materializer.OwnedCount);
}
[Fact]
public void CollidingGuid_AlreadyOwnedBySomethingElse_IsNeverClobbered()
{
// The Slice 6.1 collision policy: ACE's UniqueItemsForSale can list
// the EXACT guid a player last held (e.g. a sold-off item, or --
// worst case -- any other collision). If that guid is already in
// ClientObjectTable for a reason this materializer did not itself
// create, it must be left completely untouched, not silently
// reparented into the vendor's container.
var vendor = new VendorState();
var objects = new ClientObjectTable();
// Simulate a pre-existing, non-vendor-owned object at this guid --
// e.g. a live entity, or an item still sitting in someone's
// inventory/equipment.
const uint LiveOwner = 0x60000001u;
objects.AddOrUpdate(new ClientObject
{
ObjectId = ItemA,
Name = "Definitely Not A Shop Item",
ContainerId = LiveOwner,
});
using var materializer = new VendorShopItemMaterializer(vendor, objects);
vendor.Apply(VendorGuid, default, new[] { Item(ItemA, "Shop Listing With A Colliding Guid") });
ClientObject? survivor = objects.Get(ItemA);
Assert.NotNull(survivor);
Assert.Equal("Definitely Not A Shop Item", survivor!.Name);
Assert.Equal(LiveOwner, survivor.ContainerId);
Assert.False(materializer.Owns(ItemA));
Assert.Equal(0, materializer.OwnedCount);
// The collision guid must also survive session close -- since this
// materializer never claimed it, it must never remove it either.
vendor.Close();
Assert.NotNull(objects.Get(ItemA));
}
[Fact]
public void Dispose_UnsubscribesFromVendorChanged()
{
var vendor = new VendorState();
var objects = new ClientObjectTable();
var materializer = new VendorShopItemMaterializer(vendor, objects);
vendor.Apply(VendorGuid, default, new[] { Item(ItemA) });
Assert.NotNull(objects.Get(ItemA));
materializer.Dispose();
// No further reaction once disposed -- a Close() after disposal
// must not throw and must not touch the table (nothing left
// subscribed to react).
vendor.Close();
Assert.NotNull(objects.Get(ItemA));
}
}