subscription.test.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import { describe, expect, test, setSystemTime, afterEach } from "bun:test"
  2. import { Subscription } from "../src/subscription"
  3. import { centsToMicroCents } from "../src/util/price"
  4. afterEach(() => {
  5. setSystemTime()
  6. })
  7. describe("Subscription.analyzeMonthlyUsage", () => {
  8. const subscribed = new Date("2026-01-15T08:00:00Z")
  9. test("returns ok with 0% when usage was last updated before current period", () => {
  10. setSystemTime(new Date("2026-03-20T10:00:00Z"))
  11. const result = Subscription.analyzeMonthlyUsage({
  12. limit: 10,
  13. usage: centsToMicroCents(500),
  14. timeUpdated: new Date("2026-02-10T00:00:00Z"),
  15. timeSubscribed: subscribed,
  16. })
  17. expect(result.status).toBe("ok")
  18. expect(result.usagePercent).toBe(0)
  19. // reset should be seconds until 2026-04-15T08:00:00Z
  20. const expected = Math.ceil(
  21. (new Date("2026-04-15T08:00:00Z").getTime() - new Date("2026-03-20T10:00:00Z").getTime()) / 1000,
  22. )
  23. expect(result.resetInSec).toBe(expected)
  24. })
  25. test("returns ok with usage percent when under limit", () => {
  26. setSystemTime(new Date("2026-03-20T10:00:00Z"))
  27. const limit = 10 // $10
  28. const half = centsToMicroCents(10 * 100) / 2
  29. const result = Subscription.analyzeMonthlyUsage({
  30. limit,
  31. usage: half,
  32. timeUpdated: new Date("2026-03-18T00:00:00Z"),
  33. timeSubscribed: subscribed,
  34. })
  35. expect(result.status).toBe("ok")
  36. expect(result.usagePercent).toBe(50)
  37. })
  38. test("returns rate-limited when at or over limit", () => {
  39. setSystemTime(new Date("2026-03-20T10:00:00Z"))
  40. const limit = 10
  41. const result = Subscription.analyzeMonthlyUsage({
  42. limit,
  43. usage: centsToMicroCents(limit * 100),
  44. timeUpdated: new Date("2026-03-18T00:00:00Z"),
  45. timeSubscribed: subscribed,
  46. })
  47. expect(result.status).toBe("rate-limited")
  48. expect(result.usagePercent).toBe(100)
  49. })
  50. test("resets usage when crossing monthly boundary", () => {
  51. // subscribed on 15th, now is April 16th — period is Apr 15 to May 15
  52. // timeUpdated is March 20 (previous period)
  53. setSystemTime(new Date("2026-04-16T10:00:00Z"))
  54. const result = Subscription.analyzeMonthlyUsage({
  55. limit: 10,
  56. usage: centsToMicroCents(10 * 100),
  57. timeUpdated: new Date("2026-03-20T00:00:00Z"),
  58. timeSubscribed: subscribed,
  59. })
  60. expect(result.status).toBe("ok")
  61. expect(result.usagePercent).toBe(0)
  62. })
  63. test("caps usage percent at 100", () => {
  64. setSystemTime(new Date("2026-03-20T10:00:00Z"))
  65. const limit = 10
  66. const result = Subscription.analyzeMonthlyUsage({
  67. limit,
  68. usage: centsToMicroCents(limit * 100) - 1,
  69. timeUpdated: new Date("2026-03-18T00:00:00Z"),
  70. timeSubscribed: subscribed,
  71. })
  72. expect(result.status).toBe("ok")
  73. expect(result.usagePercent).toBeLessThanOrEqual(100)
  74. })
  75. test("handles subscription day 31 in short month", () => {
  76. const sub31 = new Date("2026-01-31T12:00:00Z")
  77. // now is March 1 — period should be Feb 28 to Mar 31
  78. setSystemTime(new Date("2026-03-01T10:00:00Z"))
  79. const result = Subscription.analyzeMonthlyUsage({
  80. limit: 10,
  81. usage: 0,
  82. timeUpdated: new Date("2026-03-01T09:00:00Z"),
  83. timeSubscribed: sub31,
  84. })
  85. expect(result.status).toBe("ok")
  86. expect(result.usagePercent).toBe(0)
  87. const expected = Math.ceil(
  88. (new Date("2026-03-31T12:00:00Z").getTime() - new Date("2026-03-01T10:00:00Z").getTime()) / 1000,
  89. )
  90. expect(result.resetInSec).toBe(expected)
  91. })
  92. })