DebugAcquireCultivationTests.cs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using IvfTl.ControlHost.Debug;
  3. using IvfTl.ControlHost.Tests.Fakes;
  4. using Xunit;
  5. namespace IvfTl.ControlHost.Tests
  6. {
  7. /// <summary>
  8. /// Task7-t3:借用(Acquire)成功时回带培养态(cultivating + embryoCount)。
  9. /// 纯逻辑、注入式——cultivationOf 委托提供假培养态,不接真实数据源(下个任务才装配)。
  10. /// </summary>
  11. public class DebugAcquireCultivationTests
  12. {
  13. private static readonly DateTime Now = new DateTime(2026, 6, 24, 0, 0, 0, DateTimeKind.Utc);
  14. private DebugSessionManager NewMgr(FakeGate gate, Func<int, (bool, int)> cultivationOf)
  15. => new DebugSessionManager(_ => gate, () => Now, ttlMs: 10000, log: null, cultivationOf: cultivationOf);
  16. [Fact]
  17. public void Acquire_CultivatingHouse_ReturnsCultivationState()
  18. {
  19. var gate = new FakeGate(6, new FakeSerial());
  20. var mgr = NewMgr(gate, sn => (true, 3));
  21. var r = mgr.Acquire(6);
  22. Assert.True(r.Ok);
  23. Assert.True(r.Cultivating);
  24. Assert.Equal(3, r.EmbryoCount);
  25. // Result 仍是 sid 字符串(operate 端靠 r.Result 取 sessionId,不能破坏)
  26. Assert.IsType<string>(r.Result);
  27. Assert.False(string.IsNullOrEmpty((string)r.Result));
  28. }
  29. [Fact]
  30. public void Acquire_EmptyHouse_ReturnsNotCultivating()
  31. {
  32. var gate = new FakeGate(6, new FakeSerial());
  33. var mgr = NewMgr(gate, sn => (false, 0));
  34. var r = mgr.Acquire(6);
  35. Assert.True(r.Ok);
  36. Assert.False(r.Cultivating);
  37. Assert.Equal(0, r.EmbryoCount);
  38. Assert.IsType<string>(r.Result);
  39. Assert.False(string.IsNullOrEmpty((string)r.Result));
  40. }
  41. [Fact]
  42. public void Acquire_NoCultivationProvider_DefaultsFalse()
  43. {
  44. var gate = new FakeGate(6, new FakeSerial());
  45. // cultivationOf 传 null(不提供)——Acquire 不应崩,默认 false/0
  46. var mgr = NewMgr(gate, null);
  47. var r = mgr.Acquire(6);
  48. Assert.True(r.Ok);
  49. Assert.False(r.Cultivating);
  50. Assert.Equal(0, r.EmbryoCount);
  51. Assert.IsType<string>(r.Result);
  52. Assert.False(string.IsNullOrEmpty((string)r.Result));
  53. }
  54. [Fact]
  55. public void Acquire_CultivationProviderThrows_DoesNotBlockAcquire()
  56. {
  57. var gate = new FakeGate(6, new FakeSerial());
  58. // 业务红线:取培养态炸了也绝不能阻断借用本身(锁死 Acquire 里的 try-catch 兜底语义)
  59. var mgr = NewMgr(gate, sn => throw new Exception("模拟取培养态失败"));
  60. var r = mgr.Acquire(6);
  61. Assert.True(r.Ok); // 借用仍成功
  62. Assert.False(r.Cultivating); // 兜底为默认 false
  63. Assert.Equal(0, r.EmbryoCount); // 兜底为默认 0
  64. Assert.IsType<string>(r.Result);
  65. Assert.False(string.IsNullOrEmpty((string)r.Result));
  66. }
  67. }
  68. }