hostCapability.test.ts 988 B

12345678910111213141516171819202122232425
  1. import { describe, expect, it } from "vitest";
  2. import { createHostCapabilityStore } from "../src/server-core/hostCapability.js";
  3. describe("host capability store", () => {
  4. it("allows a single consumption within the TTL and rejects replay", () => {
  5. let now = 1_000;
  6. const store = createHostCapabilityStore({ ttlMs: 100, now: () => now });
  7. const issued = store.issue();
  8. expect(issued.expiresAt).toBe(1_100);
  9. expect(store.consume(issued.capability)).toBe(true);
  10. // 一次性 ticket:重放必须失败。
  11. expect(store.consume(issued.capability)).toBe(false);
  12. now += 1;
  13. expect(store.consume(issued.capability)).toBe(false);
  14. });
  15. it("rejects expired capabilities and purges them", () => {
  16. let now = 0;
  17. const store = createHostCapabilityStore({ ttlMs: 100, now: () => now });
  18. const issued = store.issue();
  19. now = 101;
  20. expect(store.consume(issued.capability)).toBe(false);
  21. expect(store.consume(undefined)).toBe(false);
  22. });
  23. });